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 goodness(signal, freq_range=None, D=None): """Compute the goodness of pitch of a signal.""" if D is None: D = libtfr.dpss(len(signal), 1.5, 1)[0] signal = signal * D[0, :] if freq_range is None: freq_range = FREQ_RANGE if np.all(signal == 0): return 0 else: return np.max(cepstrum(signal)[25:freq_range])
def mtfft(tl, **kwargs): """ Multitaper fourier transform from point process times [J,Nsp,f] = mtfftpt(tl, **kwargs) Input: tl ragged array of event times Optional keyword arguments: NW time-bandwidth parameter for DPSS tapers (default 3) k number of DPSS tapers to use (default nw*2-1) time_range the range of times to analyze (default is to use range of tl) Fs the frequency resolution (default 1) fpass frequency band to be used in the calculation in the form (fmin, fmax). Default (0, Fs/2) nfft number of frequency bins for transform Output: J complex spectrum with dimensions (freq x chan x tapers) Nsp total spike count in each trial, with dimension (chan,) f frequency grid, with dimension (freq,) Note on units: if the units of the data are T, the corresponding frequency units are in 1/T (sec -> Hz; msec -> kHz) """ from libtfr import fgrid, dpss from numpy import arange, sqrt import toelis NW = kwargs.get('NW', 3) K = kwargs.get('k', NW*2-1) Fs = kwargs.get('Fs', 1) fpass = kwargs.get('fpass', (0, Fs/2.)) dt = 1./Fs try: mintime, maxtime = kwargs['time_range'] t = arange(mintime, maxtime, dt) except KeyError: mintime, maxtime = toelis.range(tl) # pad around spike times to avoid edge effects t = arange(mintime-dt, maxtime+2*dt, dt) N = len(t) nfft = kwargs.get('nfft', N) f,findx = fgrid(Fs, nfft, fpass) tapers = dpss(N, NW, K)[0].T * sqrt(Fs) J, Nsp = _mtfft(tl, tapers, nfft, t, f, findx) return J, Nsp, f
def mtfft(tl, **kwargs): """ Multitaper fourier transform from point process times [J,Nsp,f] = mtfftpt(tl, **kwargs) Input: tl ragged array of event times Optional keyword arguments: NW time-bandwidth parameter for DPSS tapers (default 3) k number of DPSS tapers to use (default nw*2-1) time_range the range of times to analyze (default is to use range of tl) Fs the frequency resolution (default 1) fpass frequency band to be used in the calculation in the form (fmin, fmax). Default (0, Fs/2) nfft number of frequency bins for transform Output: J complex spectrum with dimensions (freq x chan x tapers) Nsp total spike count in each trial, with dimension (chan,) f frequency grid, with dimension (freq,) Note on units: if the units of the data are T, the corresponding frequency units are in 1/T (sec -> Hz; msec -> kHz) """ from libtfr import fgrid, dpss from numpy import arange, sqrt import toelis NW = kwargs.get('NW', 3) K = kwargs.get('k', NW * 2 - 1) Fs = kwargs.get('Fs', 1) fpass = kwargs.get('fpass', (0, Fs / 2.)) dt = 1. / Fs try: mintime, maxtime = kwargs['time_range'] t = arange(mintime, maxtime, dt) except KeyError: mintime, maxtime = toelis.range(tl) # pad around spike times to avoid edge effects t = arange(mintime - dt, maxtime + 2 * dt, dt) N = len(t) nfft = kwargs.get('nfft', N) f, findx = fgrid(Fs, nfft, fpass) tapers = dpss(N, NW, K)[0].T * sqrt(Fs) J, Nsp = _mtfft(tl, tapers, nfft, t, f, findx) return J, Nsp, f
def test_dpss_bad_args(): E,V = libtfr.dpss(128, -5, 3)
def run_dpss(args, concentrations, means): from numpy import ones_like E,V = libtfr.dpss(*args) assert_array_almost_equal(V, concentrations) assert_array_almost_equal(E.mean(1), means) assert_array_almost_equal((E**2).sum(1), ones_like(V))
def test_dpss_bad_args(): E, V = libtfr.dpss(128, -5, 3)
def run_dpss(args, concentrations, means): from numpy import ones_like E, V = libtfr.dpss(*args) assert_array_almost_equal(V, concentrations) assert_array_almost_equal(E.mean(1), means) assert_array_almost_equal((E**2).sum(1), ones_like(V))
# -*- coding: utf-8 -*- # -*- mode: python -*- #import pstats, cProfile from numpy.random import randn import libtfr sig = randn(17590) D = libtfr.dpss(256, 3, 5) #cProfile.runctx("D.mtspec(sig, 10)", globals(), locals(), "Profile.prof") #s = pstats.Stats("Profile.prof") #s.strip_dirs().sort_stats("time").print_stats()
def mtstft(tl, window, step, **kwargs): """ Multitaper short time fourier transform from point process times [J,Nsp,f,t] = mtstft(tl, window, step**kwargs) Input: tl ragged array of event times window duration of short time analysis window step step size between windows Optional keyword arguments: NW time-bandwidth parameter for DPSS tapers (default 3) k number of DPSS tapers to use (default nw*2-1) time_range the range of times to analyze (default is to use range of tl) Fs the frequency resolution (default 1) fpass frequency band to be used in the calculation in the form (fmin, fmax). Default (0, Fs/2) nfft number of frequency bins for transform Output: J complex spectrum with dimensions (freq x time x tapers x chan) Nsp total spike count in each trial, with dimension (time, chan) f frequency grid, with dimension (freq,) t time grid, with dimension (time,) Note on units: if the units of the data are T, the corresponding frequency units are in 1/T (sec -> Hz; msec -> kHz) """ from libtfr import fgrid, dpss from numpy import arange, sqrt, zeros import toelis NW = kwargs.get('NW', 3) K = kwargs.get('k', NW*2-1) Fs = kwargs.get('Fs', 1) dt = 1./ Fs fpass = kwargs.get('fpass', (0, Fs/2.)) C = len(tl) twin = arange(0, window, dt) N = len(twin) nfft = kwargs.get('nfft', N) f,findx = fgrid(Fs, nfft, fpass) tapers = dpss(N, NW, K)[0].T * sqrt(Fs) try: mintime, maxtime = kwargs['time_range'] except KeyError: mintime, maxtime = toelis.range(tl) # pad around spikes mintime -= dt maxtime += 2*dt tgrid = arange(mintime, maxtime, step) J = zeros((f.size, tgrid.size, K, C), dtype='D') Nsp = zeros((tgrid.size, C), dtype='i') for i, offset in enumerate(tgrid): j,n = _mtfft(tl, tapers, nfft, twin + offset, f, findx) J[:,i,:,:] = j Nsp[i,:] = n return J, Nsp, f, tgrid
def mtstft(tl, window, step, **kwargs): """ Multitaper short time fourier transform from point process times [J,Nsp,f,t] = mtstft(tl, window, step**kwargs) Input: tl ragged array of event times window duration of short time analysis window step step size between windows Optional keyword arguments: NW time-bandwidth parameter for DPSS tapers (default 3) k number of DPSS tapers to use (default nw*2-1) time_range the range of times to analyze (default is to use range of tl) Fs the frequency resolution (default 1) fpass frequency band to be used in the calculation in the form (fmin, fmax). Default (0, Fs/2) nfft number of frequency bins for transform Output: J complex spectrum with dimensions (freq x time x tapers x chan) Nsp total spike count in each trial, with dimension (time, chan) f frequency grid, with dimension (freq,) t time grid, with dimension (time,) Note on units: if the units of the data are T, the corresponding frequency units are in 1/T (sec -> Hz; msec -> kHz) """ from libtfr import fgrid, dpss from numpy import arange, sqrt, zeros import toelis NW = kwargs.get('NW', 3) K = int(kwargs.get('k', NW * 2 - 1)) Fs = kwargs.get('Fs', 1) dt = 1. / Fs fpass = kwargs.get('fpass', (0, Fs / 2.)) C = len(tl) twin = arange(0, window, dt) N = len(twin) nfft = kwargs.get('nfft', N) f, findx = fgrid(Fs, nfft, fpass) tapers = dpss(N, NW, K)[0].T * sqrt(Fs) try: mintime, maxtime = kwargs['time_range'] except KeyError: mintime, maxtime = toelis.range(tl) # pad around spikes mintime -= dt maxtime += 2 * dt tgrid = arange(mintime, maxtime, step) J = zeros((f.size, tgrid.size, K, C), dtype='D') Nsp = zeros((tgrid.size, C), dtype='i') for i, offset in enumerate(tgrid): j, n = _mtfft(tl, tapers, nfft, twin + offset, f, findx) J[:, i, :, :] = j Nsp[i, :] = n return J, Nsp, f, tgrid