def test_precalc_psd(): nfft = 256 E,V = libtfr.dpss(200, 3, 5) D = libtfr.mfft_precalc(nfft, E, V) assert_array_equal(E, D.tapers) Z = D.mtpsd(sig) assert_tuple_equal(Z.shape, (nfft//2 + 1,)) assert_equal(Z.dtype, libtfr.DTYPE)
def test_precalc_psd(): nfft = 256 E, V = libtfr.dpss(200, 3, 5) D = libtfr.mfft_precalc(nfft, E, V) assert_array_equal(E, D.tapers) Z = D.mtpsd(sig) assert_tuple_equal(Z.shape, (nfft // 2 + 1, )) assert_equal(Z.dtype, libtfr.DTYPE)
def test_hanning_mtstft(): from numpy import hanning nfft = 256 shift = 10 window = hanning(nfft - 50) nframes = (sig.size - window.size) // shift + 1 D = libtfr.mfft_precalc(nfft, window) Z = D.mtstft(sig, shift) assert_tuple_equal(Z.shape, (nfft//2 + 1, nframes, 1)) assert_equal(Z.dtype, libtfr.CTYPE)
def test_hanning_mtstft(): from numpy import hanning nfft = 256 shift = 10 window = hanning(nfft - 50) nframes = (sig.size - window.size) // shift + 1 D = libtfr.mfft_precalc(nfft, window) Z = D.mtstft(sig, shift) assert_tuple_equal(Z.shape, (nfft // 2 + 1, nframes, 1)) assert_equal(Z.dtype, libtfr.CTYPE)
def load_stimulus(path, window, step, f_min=0.5, f_max=8.0, f_count=30, compress=1, gammatone=False, **kwargs): """Load sound stimulus and calculate spectrotemporal representation. Parameters: path: location of wave file window: duration of window (in ms) step: window step (in ms) f_min: minimum frequency (in kHz) f_max: maximum frequency (in kHz) f_count: number of frequency bins gammatone: if True, use gammatone filterbank Returns spectrogram, duration (ms) """ import ewave fp = ewave.open(path, "r") Fs = fp.sampling_rate / 1000. osc = ewave.rescale(fp.read(), 'h') if gammatone: import gammatone.gtgram as gg Pxx = gg.gtgram(osc, Fs * 1000, window / 1000, step / 1000, f_count, f_min * 1000) else: import libtfr # nfft based on desired number of channels btw f_min and f_max nfft = int(f_count / (f_max - f_min) * Fs) npoints = int(Fs * window) if nfft < npoints: raise ValueError( "window size {} ({} points) too large for desired freq resolution {}. " "Decrease to {} ms or increase f_count.".format( window, f_count, npoints, nfft / Fs)) nstep = int(Fs * step) taper = np.hanning(npoints) mfft = libtfr.mfft_precalc(nfft, taper) Pxx = mfft.mtspec(osc, nstep) freqs, ind = libtfr.fgrid(Fs, nfft, [f_min, f_max]) Pxx = Pxx[ind, :] if compress is not None: Pxx = np.log10(Pxx + compress) - np.log10(compress) return Pxx, Pxx.shape[1] * step