Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def convolve(tl, kernel, kdt, start=None, stop=None, dt=None):
    """Convolve a multi-trial point process with a kernel.

    R(x) = SUM   W(s-x)
          s in S

    tl:        a list of event time arrays
    kernel:    the convolution kernel
    kdt:       the temporal resolution of the kernel
    dt:        the resolution of the output. Defaults to kdt
    start:     the first time point in the output. This is *exclusive*
    stop:      the last time point in the output. Also exclusive.

    Returns:
       result of convolution (array, time x trial);
       time grid (array, time)

    The kernel function can be any 1D sequence of values. It's assumed to start
    at tau=0 on an evenly spaced grid (kdt). To ensure that the integral of the
    output is equal to the spike count, sum(kernel)*kdt should be equal to
    1.0. See signal.kernel() for help in constructing kernels with a fixed
    bandwidth.

    For acausal kernels (i.e., with support at t<0), shift the output.

    """
    from toelis import range
    from numpy import arange, column_stack
    from dlab._convolve import discreteconv
    if dt is None:
        dt = kdt

    t1, t2 = range(tl)
    onset = start if start is not None else t1
    offset = stop if stop is not None else t2
    grid = arange(onset, offset, dt)
    rate = [discreteconv(x, kernel, kdt, onset, offset, dt) for x in tl]
    return column_stack(rate), grid
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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