def specgram(x, NFFT=256, shift=128, Fs=1.0, drange=60, ax=None, cmap=mplt.cm.gray_r, **kwargs): from numpy import hanning from libtfr import stft, fgrid, tgrid, dynamic_range w = hanning(NFFT) S = stft(x, w, shift) F,ind = fgrid(Fs,NFFT,(0,Fs/2)) T = tgrid(x.size, Fs, shift) S = nx.log10(dynamic_range(S, drange)) if ax is None: ax = mplt.gca() ax.imshow(S, extent = (T[0],T[-1],F[0]-0.01,F[-1]), cmap=cmap, **kwargs) return S
def load_signal(self, locator, dtype='d'): """ Loads the signal and computes the spectrogram. dtype: the data type to store the output in. Use single-precision floats if needed to reduce storage requirements. """ from ewave import wavfile from libtfr import fgrid, dynamic_range from chirp.common.signal import spectrogram from chirp.common.geom import elementlist, masker from numpy import linspace, log10 fp = wavfile(locator) signal = fp.read() Fs = fp.sampling_rate speccr = spectrogram(**self.options) # adjust window size to get correct number of frequency bands df = 1. * (self.options['freq_range'][1] - self.options['freq_range'][0]) / self.options['nfreq'] nfft = int(Fs / df) spec, extent = speccr.linspect(signal, Fs / 1000, nfft=nfft) F, ind = fgrid(Fs, nfft, self.options['freq_range']) # in Hz spec = spec[ind, :] T = linspace(extent[0], extent[1], spec.shape[1]) # in ms # first convert the spectrogram to its final scale if self.options['powscale'].startswith('log'): # TODO calculate dynamic range of the signal for non 16 bit PCM? spec = log10(dynamic_range(spec, 96)) # recenter spectrogram if self.options['subtract_mean']: spec -= spec.mean() if self.options['mask'] != 'none': eblfile = os.path.splitext(locator)[0] + elementlist.default_extension if os.path.exists(eblfile): mask = elementlist.read(eblfile) spec = masker(boxmask=self.options['mask'] == 'box').cut(spec, mask, T, F / 1000.) return spec.astype(dtype)
def dbspect(self, signal, Fs, dBrange=96, *args, **kwargs): from numpy import log10 from libtfr import dynamic_range S, extent = self.linspect(signal, Fs, *args, **kwargs) return log10(dynamic_range(S, dBrange)), extent