Skip to content

pychronux

dpsschk(tapers, N, Fs)

Check and generate DPSS tapers.

Parameters:

Name Type Description Default
tapers Union[ndarray, Tuple[float, int]]

Input can be either an array representing [NW, K] or a tuple with the number of tapers and the maximum number of tapers.

required
N int

Number of points for FFT.

required
Fs float

Sampling frequency.

required

Returns:

Name Type Description
tapers ndarray

Tapers matrix, shape [tapers, eigenvalues].

Notes

The function computes DPSS (Discrete Prolate Spheroidal Sequences) tapers and scales them by the square root of the sampling frequency.

Source code in neuro_py/process/pychronux.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def dpsschk(
    tapers: Union[np.ndarray, Tuple[float, int]], N: int, Fs: float
) -> np.ndarray:
    """
    Check and generate DPSS tapers.

    Parameters
    ----------
    tapers : Union[np.ndarray, Tuple[float, int]]
        Input can be either an array representing [NW, K] or a tuple with
        the number of tapers and the maximum number of tapers.
    N : int
        Number of points for FFT.
    Fs : float
        Sampling frequency.

    Returns
    -------
    tapers : np.ndarray
        Tapers matrix, shape [tapers, eigenvalues].

    Notes
    -----
    The function computes DPSS (Discrete Prolate Spheroidal Sequences) tapers
    and scales them by the square root of the sampling frequency.
    """
    tapers, eigs = dpss(N, NW=tapers[0], Kmax=tapers[1], sym=False, return_ratios=True)
    tapers = tapers * np.sqrt(Fs)
    tapers = tapers.T
    return tapers

get_tapers(N, bandwidth, *, fs=1.0, min_lambda=0.95, n_tapers=None)

Compute tapers and associated energy concentrations for the Thomson multitaper method.

Parameters:

Name Type Description Default
N int

Length of taper.

required
bandwidth float

Bandwidth of taper, in Hz.

required
fs float

Sampling rate, in Hz. Default is 1 Hz.

1.0
min_lambda float

Minimum energy concentration that each taper must satisfy. Default is 0.95.

0.95
n_tapers Optional[int]

Number of tapers to compute. Default is to use all tapers that satisfy 'min_lambda'.

None

Returns:

Name Type Description
tapers ndarray

Array of tapers with shape (n_tapers, N).

lambdas ndarray

Energy concentrations for each taper with shape (n_tapers,).

Raises:

Type Description
ValueError

If not enough tapers are available or if none of the tapers satisfy the minimum energy concentration criteria.

Source code in neuro_py/process/pychronux.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def get_tapers(
    N: int,
    bandwidth: float,
    *,
    fs: float = 1.0,
    min_lambda: float = 0.95,
    n_tapers: Optional[int] = None,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Compute tapers and associated energy concentrations for the Thomson
    multitaper method.

    Parameters
    ----------
    N : int
        Length of taper.
    bandwidth : float
        Bandwidth of taper, in Hz.
    fs : float, optional
        Sampling rate, in Hz. Default is 1 Hz.
    min_lambda : float, optional
        Minimum energy concentration that each taper must satisfy. Default is 0.95.
    n_tapers : Optional[int], optional
        Number of tapers to compute. Default is to use all tapers that satisfy 'min_lambda'.

    Returns
    -------
    tapers : np.ndarray
        Array of tapers with shape (n_tapers, N).
    lambdas : np.ndarray
        Energy concentrations for each taper with shape (n_tapers,).

    Raises
    ------
    ValueError
        If not enough tapers are available or if none of the tapers satisfy the
        minimum energy concentration criteria.
    """

    NW = bandwidth * N / fs
    K = int(np.ceil(2 * NW)) - 1
    if n_tapers is not None:
        K = min(K, n_tapers)
    if K < 1:
        raise ValueError(
            f"Not enough tapers, with 'NW' of {NW}. Increase the bandwidth or "
            "use more data points"
        )

    tapers, lambdas = dpss(N, NW=NW, Kmax=K, sym=False, norm=2, return_ratios=True)
    mask = lambdas > min_lambda
    if not np.sum(mask) > 0:
        raise ValueError(
            "None of the tapers satisfied the minimum energy concentration"
            f" criteria of {min_lambda}"
        )
    tapers = tapers[mask]
    lambdas = lambdas[mask]

    if n_tapers is not None:
        if n_tapers > tapers.shape[0]:
            raise ValueError(
                f"'n_tapers' of {n_tapers} is greater than the {tapers.shape[0]}"
                f" that satisfied the minimum energy concentration criteria of {min_lambda}"
            )
        tapers = tapers[:n_tapers]
        lambdas = lambdas[:n_tapers]

    return tapers, lambdas

getfgrid(Fs, nfft, fpass)

Get frequency grid for evaluation.

Parameters:

Name Type Description Default
Fs int

Sampling frequency.

required
nfft int

Number of points for FFT.

required
fpass List[float]

Frequency range to evaluate (as [fmin, fmax]).

required

Returns:

Name Type Description
f ndarray

Frequency vector within the specified range.

findx ndarray

Boolean array indicating the indices of the frequency vector that fall within the specified range.

Notes

The frequency vector is computed based on the sampling frequency and the number of FFT points. Only frequencies within the range defined by fpass are returned.

Source code in neuro_py/process/pychronux.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def getfgrid(Fs: int, nfft: int, fpass: List[float]) -> Tuple[np.ndarray, np.ndarray]:
    """
    Get frequency grid for evaluation.

    Parameters
    ----------
    Fs : int
        Sampling frequency.
    nfft : int
        Number of points for FFT.
    fpass : List[float]
        Frequency range to evaluate (as [fmin, fmax]).

    Returns
    -------
    f : np.ndarray
        Frequency vector within the specified range.
    findx : np.ndarray
        Boolean array indicating the indices of the frequency vector that fall within the specified range.

    Notes
    -----
    The frequency vector is computed based on the sampling frequency and the number of FFT points.
    Only frequencies within the range defined by `fpass` are returned.
    """
    df = Fs / nfft
    f = np.arange(0, Fs + df, df)
    f = f[0:nfft]
    findx = (f >= fpass[0]) & (f <= fpass[-1])
    f = f[findx]
    return f, findx

mtcoherencept(data1, data2, Fs, fpass, NW=2.5, n_tapers=4, time_support=None, tapers=None, tapers_ts=None, nfft=None)

Multitaper coherence for point processes.

Parameters:

Name Type Description Default
data1 ndarray

Array of spike times for the first signal (in seconds).

required
data2 ndarray

Array of spike times for the second signal (in seconds).

required
Fs int

Sampling frequency.

required
fpass list

Frequency range to evaluate as [min_freq, max_freq].

required
NW Union[int, float]

Time-bandwidth product, by default 2.5.

2.5
n_tapers int

Number of tapers, by default 4.

4
time_support Union[list, None]

Time range to evaluate, by default None.

None
tapers Union[ndarray, None]

Precomputed tapers, given as [NW, K] or [tapers, eigenvalues], by default None.

None
tapers_ts Union[ndarray, None]

Taper time series, by default None.

None
nfft Optional[int]

Number of points for FFT, by default None.

None

Returns:

Type Description
DataFrame

Coherence between the two point processes.

Source code in neuro_py/process/pychronux.py
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
def mtcoherencept(
    data1: np.ndarray,
    data2: np.ndarray,
    Fs: int,
    fpass: list,
    NW: Union[int, float] = 2.5,
    n_tapers: int = 4,
    time_support: Union[list, None] = None,
    tapers: Union[np.ndarray, None] = None,
    tapers_ts: Union[np.ndarray, None] = None,
    nfft: Optional[int] = None,
) -> pd.DataFrame:
    """
    Multitaper coherence for point processes.

    Parameters
    ----------
    data1 : np.ndarray
        Array of spike times for the first signal (in seconds).
    data2 : np.ndarray
        Array of spike times for the second signal (in seconds).
    Fs : int
        Sampling frequency.
    fpass : list
        Frequency range to evaluate as [min_freq, max_freq].
    NW : Union[int, float], optional
        Time-bandwidth product, by default 2.5.
    n_tapers : int, optional
        Number of tapers, by default 4.
    time_support : Union[list, None], optional
        Time range to evaluate, by default None.
    tapers : Union[np.ndarray, None], optional
        Precomputed tapers, given as [NW, K] or [tapers, eigenvalues], by default None.
    tapers_ts : Union[np.ndarray, None], optional
        Taper time series, by default None.
    nfft : Optional[int], optional
        Number of points for FFT, by default None.

    Returns
    -------
    pd.DataFrame
        Coherence between the two point processes.
    """
    # Check if data is a single unit and put in array
    if isinstance(data1, np.ndarray):
        data1 = np.array([data1])
    if isinstance(data2, np.ndarray):
        data2 = np.array([data2])

    # Compute power spectral densities (PSD) for both spike trains
    psd1 = mtspectrumpt(
        data1, Fs, fpass, NW, n_tapers, time_support, tapers, tapers_ts, nfft
    )
    psd2 = mtspectrumpt(
        data2, Fs, fpass, NW, n_tapers, time_support, tapers, tapers_ts, nfft
    )

    # Compute cross-spectral density (CSD) between the two spike trains
    csd = mtcsdpt(
        data1, data2, Fs, fpass, NW, n_tapers, time_support, tapers, tapers_ts, nfft
    )

    # Calculate coherence: |Sxy(f)|^2 / (Sxx(f) * Syy(f))
    coherence = np.abs(csd["CSD"].values) ** 2 / (psd1.values * psd2.values).flatten()

    # Return coherence as a pandas DataFrame
    coherence_df = pd.DataFrame(index=csd.index, data=coherence, columns=["Coherence"])
    return coherence_df

mtcsdpt(data1, data2, Fs, fpass, NW=2.5, n_tapers=4, time_support=None, tapers=None, tapers_ts=None, nfft=None)

Multitaper cross-spectral density (CSD) for point processes.

Parameters:

Name Type Description Default
data1 ndarray

Array of spike times for the first signal (in seconds).

required
data2 ndarray

Array of spike times for the second signal (in seconds).

required
Fs int

Sampling frequency.

required
fpass list

Frequency range to evaluate as [min_freq, max_freq].

required
NW Union[int, float]

Time-bandwidth product, by default 2.5.

2.5
n_tapers int

Number of tapers, by default 4.

4
time_support Union[list, None]

Time range to evaluate, by default None.

None
tapers Union[ndarray, None]

Precomputed tapers, given as [NW, K] or [tapers, eigenvalues], by default None.

None
tapers_ts Union[ndarray, None]

Taper time series, by default None.

None
nfft Optional[int]

Number of points for FFT, by default None.

None

Returns:

Type Description
DataFrame

Cross-spectral density between the two point processes.

Source code in neuro_py/process/pychronux.py
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
def mtcsdpt(
    data1: np.ndarray,
    data2: np.ndarray,
    Fs: int,
    fpass: list,
    NW: Union[int, float] = 2.5,
    n_tapers: int = 4,
    time_support: Union[list, None] = None,
    tapers: Union[np.ndarray, None] = None,
    tapers_ts: Union[np.ndarray, None] = None,
    nfft: Optional[int] = None,
) -> pd.DataFrame:
    """
    Multitaper cross-spectral density (CSD) for point processes.

    Parameters
    ----------
    data1 : np.ndarray
        Array of spike times for the first signal (in seconds).
    data2 : np.ndarray
        Array of spike times for the second signal (in seconds).
    Fs : int
        Sampling frequency.
    fpass : list
        Frequency range to evaluate as [min_freq, max_freq].
    NW : Union[int, float], optional
        Time-bandwidth product, by default 2.5.
    n_tapers : int, optional
        Number of tapers, by default 4.
    time_support : Union[list, None], optional
        Time range to evaluate, by default None.
    tapers : Union[np.ndarray, None], optional
        Precomputed tapers, given as [NW, K] or [tapers, eigenvalues], by default None.
    tapers_ts : Union[np.ndarray, None], optional
        Taper time series, by default None.
    nfft : Optional[int], optional
        Number of points for FFT, by default None.

    Returns
    -------
    pd.DataFrame
        Cross-spectral density between the two point processes.
    """
    if time_support is not None:
        mintime, maxtime = time_support
    else:
        mintime = min(np.min(data1), np.min(data2))
        maxtime = max(np.max(data1), np.max(data2))
    dt = 1 / Fs

    # Create tapers if not provided
    if tapers is None:
        tapers_ts = np.arange(mintime - dt, maxtime + dt, dt)
        N = len(tapers_ts)
        tapers, eigens = dpss(N, NW, n_tapers, return_ratios=True)

    tapers = tapers.T
    N = len(tapers_ts)

    # Number of points in FFT
    if nfft is None:
        nfft = np.max([int(2 ** np.ceil(np.log2(N))), N])
    f, findx = getfgrid(Fs, nfft, fpass)

    # Compute the multitaper Fourier transforms of both spike trains
    J1, Msp1, Nsp1 = mtfftpt(data1, tapers, nfft, tapers_ts, f, findx)
    J2, Msp2, Nsp2 = mtfftpt(data2, tapers, nfft, tapers_ts, f, findx)

    # Cross-spectral density: Sxy = mean(conjugate(J1) * J2)
    csd = np.real(np.mean(np.conj(J1) * J2, axis=1))

    csd_df = pd.DataFrame(index=f, data=csd, columns=["CSD"])
    return csd_df

mtfftc(data, tapers, nfft, Fs)

Multi-taper Fourier Transform - Continuous Data (Single Signal)

Parameters:

Name Type Description Default
data ndarray

1D array of data (samples).

required
tapers ndarray

Precomputed DPSS tapers with shape (samples, tapers).

required
nfft int

Length of padded data for FFT.

required
Fs int

Sampling frequency.

required

Returns:

Name Type Description
J ndarray

FFT in the form (nfft, K), where K is the number of tapers.

Source code in neuro_py/process/pychronux.py
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
def mtfftc(data: np.ndarray, tapers: np.ndarray, nfft: int, Fs: int) -> np.ndarray:
    """
    Multi-taper Fourier Transform - Continuous Data (Single Signal)

    Parameters
    ----------
    data : np.ndarray
        1D array of data (samples).
    tapers : np.ndarray
        Precomputed DPSS tapers with shape (samples, tapers).
    nfft : int
        Length of padded data for FFT.
    Fs : int
        Sampling frequency.

    Returns
    -------
    J : np.ndarray
        FFT in the form (nfft, K), where K is the number of tapers.
    """
    # Ensure data is 1D
    if data.ndim != 1:
        raise ValueError("Input data must be a 1D array.")

    NC = data.shape[0]  # Number of samples in data
    NK, K = tapers.shape  # Number of samples and tapers

    if NK != NC:
        raise ValueError("Length of tapers is incompatible with length of data.")

    # Project data onto tapers
    data_proj = data[:, np.newaxis] * tapers  # Shape: (samples, tapers)

    # Compute FFT for each taper
    J = np.fft.fft(data_proj, n=nfft, axis=0) / Fs  # Shape: (nfft, K)

    return J

mtfftpt(data, tapers, nfft, t, f, findx)

Multitaper FFT for point process times.

Parameters:

Name Type Description Default
data ndarray

1D array of spike times (in seconds).

required
tapers ndarray

Tapers from the DPSS method.

required
nfft int

Number of points for FFT.

required
t ndarray

Time vector.

required
f ndarray

Frequency vector.

required
findx list of bool

Frequency index.

required

Returns:

Name Type Description
J ndarray

FFT of the data.

Msp float

Mean spikes per time.

Nsp float

Total number of spikes in data.

Notes

The function computes the multitaper FFT of spike times using the specified tapers and returns the FFT result, mean spikes, and total spike count.

Source code in neuro_py/process/pychronux.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def mtfftpt(
    data: np.ndarray,
    tapers: np.ndarray,
    nfft: int,
    t: np.ndarray,
    f: np.ndarray,
    findx: List[bool],
) -> Tuple[np.ndarray, float, float]:
    """
    Multitaper FFT for point process times.

    Parameters
    ----------
    data : np.ndarray
        1D array of spike times (in seconds).
    tapers : np.ndarray
        Tapers from the DPSS method.
    nfft : int
        Number of points for FFT.
    t : np.ndarray
        Time vector.
    f : np.ndarray
        Frequency vector.
    findx : list of bool
        Frequency index.

    Returns
    -------
    J : np.ndarray
        FFT of the data.
    Msp : float
        Mean spikes per time.
    Nsp : float
        Total number of spikes in data.

    Notes
    -----
    The function computes the multitaper FFT of spike times using
    the specified tapers and returns the FFT result, mean spikes,
    and total spike count.
    """
    K = tapers.shape[1]
    nfreq = len(f)

    # get the FFT of the tapers
    H = np.zeros((nfft, K), dtype=np.complex128)
    for i in np.arange(K):
        H[:, i] = np.fft.fft(tapers[:, i], nfft, axis=0)

    H = H[findx, :]
    w = 2 * np.pi * f
    dtmp = data
    indx = np.logical_and(dtmp >= np.min(t), dtmp <= np.max(t))
    if len(indx):
        dtmp = dtmp[indx]
    Nsp = len(dtmp)

    # get the mean spike rate
    Msp = Nsp / len(t)

    if Msp != 0:
        # Interpolate spike times for each taper
        data_proj = np.empty((len(dtmp), K))
        for i in range(K):
            data_proj[:, i] = np.interp(dtmp, t, tapers[:, i])

        def compute_J(k):
            J_k = np.zeros(nfreq, dtype=np.complex128)
            for i, freq in enumerate(w):
                phase = -1j * freq * (dtmp - t[0])
                J_k[i] = np.sum(np.exp(phase) * data_proj[:, k])
            return J_k

        J = np.array(Parallel(n_jobs=-1)(delayed(compute_J)(k) for k in range(K))).T

        J -= H * Msp
    else:
        # No spikes: return zeros
        J = np.zeros((nfreq, K), dtype=np.complex128)

    return J, Msp, Nsp

mtspectrumc(data, Fs, fpass, tapers)

Compute the multitaper power spectrum for continuous data.

Parameters:

Name Type Description Default
data ndarray

1D array of continuous data (e.g., LFP).

required
Fs int

Sampling frequency in Hz.

required
fpass list

Frequency range to evaluate as [min_freq, max_freq].

required
tapers ndarray

Tapers array with shape [NW, K] or [tapers, eigenvalues].

required

Returns:

Name Type Description
S Series

Power spectrum with frequencies as the index.

Notes

This function utilizes the multitaper method for spectral estimation and returns the power spectrum as a pandas Series.

Source code in neuro_py/process/pychronux.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
def mtspectrumc(
    data: np.ndarray, Fs: int, fpass: list, tapers: np.ndarray
) -> pd.Series:
    """
    Compute the multitaper power spectrum for continuous data.

    Parameters
    ----------
    data : np.ndarray
        1D array of continuous data (e.g., LFP).
    Fs : int
        Sampling frequency in Hz.
    fpass : list
        Frequency range to evaluate as [min_freq, max_freq].
    tapers : np.ndarray
        Tapers array with shape [NW, K] or [tapers, eigenvalues].

    Returns
    -------
    S : pd.Series
        Power spectrum with frequencies as the index.

    Notes
    -----
    This function utilizes the multitaper method for spectral estimation
    and returns the power spectrum as a pandas Series.
    """
    N = len(data)
    nfft = np.max(
        [int(2 ** np.ceil(np.log2(N))), N]
    )  # number of points in fft of prolates
    # get the frequency grid
    f, findx = getfgrid(Fs, nfft, fpass)
    # get the fft of the tapers
    tapers = dpsschk(tapers, N, Fs)
    # get the fft of the data
    J = mtfftc(data, tapers, nfft, Fs)
    # restrict fft of tapers to required frequencies
    J = J[findx, :]
    # get the power spectrum
    S = np.real(np.mean(np.conj(J) * J, 1))
    # return the power spectrum
    return pd.Series(index=f, data=S)

mtspectrumpt(data, Fs, fpass, NW=2.5, n_tapers=4, time_support=None, tapers=None, tapers_ts=None, nfft=None)

Multitaper power spectrum estimation for point process data.

Parameters:

Name Type Description Default
data ndarray

Array of spike times (in seconds).

required
Fs int

Sampling frequency.

required
fpass list of float

Frequency range to evaluate.

required
NW Union[int, float]

Time-bandwidth product (default is 2.5).

2.5
n_tapers int

Number of tapers (default is 4).

4
time_support Union[list, None]

Time range to evaluate (default is None).

None
tapers Union[ndarray, None]

Precomputed tapers, given as [NW, K] or [tapers, eigenvalues] (default is None).

None
tapers_ts Union[ndarray, None]

Taper time series (default is None).

None
nfft Optional[int]

Number of points for FFT (default is None).

None

Returns:

Type Description
DataFrame

DataFrame containing the power spectrum.

Examples:

>>> spec = pychronux.mtspectrumpt(
>>>    st.data,
>>>    100,
>>>    [1, 20],
>>>    NW=3,
>>>    n_tapers=5,
>>>    time_support=[st.support.start, st.support.stop],
>>>    nfft=500,
>>> )
Source code in neuro_py/process/pychronux.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
def mtspectrumpt(
    data: np.ndarray,
    Fs: int,
    fpass: list,
    NW: Union[int, float] = 2.5,
    n_tapers: int = 4,
    time_support: Union[list, None] = None,
    tapers: Union[np.ndarray, None] = None,
    tapers_ts: Union[np.ndarray, None] = None,
    nfft: Optional[int] = None,
) -> pd.DataFrame:
    """
    Multitaper power spectrum estimation for point process data.

    Parameters
    ----------
    data : np.ndarray
        Array of spike times (in seconds).
    Fs : int
        Sampling frequency.
    fpass : list of float
        Frequency range to evaluate.
    NW : Union[int, float], optional
        Time-bandwidth product (default is 2.5).
    n_tapers : int, optional
        Number of tapers (default is 4).
    time_support : Union[list, None], optional
        Time range to evaluate (default is None).
    tapers : Union[np.ndarray, None], optional
        Precomputed tapers, given as [NW, K] or [tapers, eigenvalues] (default is None).
    tapers_ts : Union[np.ndarray, None], optional
        Taper time series (default is None).
    nfft : Optional[int], optional
        Number of points for FFT (default is None).

    Returns
    -------
    pd.DataFrame
        DataFrame containing the power spectrum.


    Examples
    -------
    >>> spec = pychronux.mtspectrumpt(
    >>>    st.data,
    >>>    100,
    >>>    [1, 20],
    >>>    NW=3,
    >>>    n_tapers=5,
    >>>    time_support=[st.support.start, st.support.stop],
    >>>    nfft=500,
    >>> )
    """

    # check data
    if len(data) == 0:
        return pd.DataFrame()

    # check frequency range
    if fpass[0] > fpass[1]:
        raise ValueError(
            "Invalid frequency range: fpass[0] should be less than fpass[1]."
        )

    if time_support is not None:
        mintime, maxtime = time_support
    else:
        if data.dtype == np.object_:
            mintime = np.min(np.concatenate(data))
            maxtime = np.max(np.concatenate(data))
        else:
            mintime = np.min(data)
            maxtime = np.max(data)

    dt = 1 / Fs

    if tapers is None:
        tapers_ts = np.arange(mintime - dt, maxtime + dt, dt)
        N = len(tapers_ts)
        tapers, eigens = dpss(N, NW, n_tapers, return_ratios=True)
        tapers = tapers.T

    if tapers_ts is None:
        tapers_ts = np.arange(mintime - dt, maxtime + dt, dt)

    N = len(tapers_ts)
    # number of points in fft of prolates
    if nfft is None:
        nfft = np.max([int(2 ** np.ceil(np.log2(N))), N])
    f, findx = getfgrid(Fs, nfft, fpass)

    spec = np.zeros((len(f), len(data)))
    for i, d in enumerate(data):
        J, _, _ = mtfftpt(d, tapers, nfft, tapers_ts, f, findx)
        spec[:, i] = np.real(np.mean(np.conj(J) * J, 1))

    spectrum_df = pd.DataFrame(index=f, columns=np.arange(len(data)), dtype=np.float64)
    spectrum_df[:] = spec
    return spectrum_df

point_spectra(times, Fs=1250, freq_range=[1, 20], tapers0=[3, 5], pad=0, nfft=None)

Compute multitaper power spectrum for point processes.

Parameters:

Name Type Description Default
times ndarray

Array of spike times (in seconds).

required
Fs int

Sampling frequency (default is 1250 Hz).

1250
freq_range List[float]

Frequency range to evaluate (default is [1, 20] Hz).

[1, 20]
tapers0 List[int]

Time-bandwidth product and number of tapers (default is [3, 5]). The time-bandwidth product is used to compute the tapers.

[3, 5]
pad int

Padding for the FFT (default is 0).

0
nfft Optional[int]

Number of points for FFT (default is None).

None

Returns:

Name Type Description
spectra ndarray

Power spectrum.

f ndarray

Frequency vector.

Notes

Alternative function to mtspectrumpt for computing the power spectrum

Source code in neuro_py/process/pychronux.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
def point_spectra(
    times: np.ndarray,
    Fs: int = 1250,
    freq_range: List[float] = [1, 20],
    tapers0: List[int] = [3, 5],
    pad: int = 0,
    nfft: Optional[int] = None,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Compute multitaper power spectrum for point processes.

    Parameters
    ----------
    times : np.ndarray
        Array of spike times (in seconds).
    Fs : int, optional
        Sampling frequency (default is 1250 Hz).
    freq_range : List[float], optional
        Frequency range to evaluate (default is [1, 20] Hz).
    tapers0 : List[int], optional
        Time-bandwidth product and number of tapers (default is [3, 5]).
        The time-bandwidth product is used to compute the tapers.
    pad : int, optional
        Padding for the FFT (default is 0).
    nfft : Optional[int], optional
        Number of points for FFT (default is None).

    Returns
    -------
    spectra : np.ndarray
        Power spectrum.
    f : np.ndarray
        Frequency vector.

    Notes
    -----
    Alternative function to `mtspectrumpt` for computing the power spectrum
    """

    # generate frequency grid
    timesRange = [min(times), max(times)]
    window = np.floor(np.diff(timesRange))
    nSamplesPerWindow = int(np.round(Fs * window[0]))
    if nfft is None:
        nfft = np.max(
            [(int(2 ** np.ceil(np.log2(nSamplesPerWindow))) + pad), nSamplesPerWindow]
        )
    fAll = np.linspace(0, Fs, int(nfft))
    frequency_ind = (fAll >= freq_range[0]) & (fAll <= freq_range[1])

    # Generate tapers
    tapers, _ = dpss(nSamplesPerWindow, tapers0[0], tapers0[1], return_ratios=True)
    tapers = tapers * np.sqrt(Fs)

    # Compute FFT of tapers and restrict to required frequencies
    H = np.fft.fft(tapers, n=nfft, axis=1)  # Shape: (K, nfft)
    H = H[:, frequency_ind]  # Shape: (K, Nf)

    # Angular frequencies
    f = fAll[frequency_ind]
    w = 2 * np.pi * f

    # Time grid
    timegrid = np.linspace(timesRange[0], timesRange[1], nSamplesPerWindow)

    # Ensure times are within range
    data = times[(times >= timegrid[0]) & (times <= timegrid[-1])]

    # Project spike times onto tapers
    data_proj = [np.interp(data, timegrid, taper) for taper in tapers]
    data_proj = np.vstack(data_proj)  # Shape: (K, len(data))

    # Compute multitaper spectrum
    exponential = np.exp(
        np.outer(-1j * w, (data - timegrid[0]))
    )  # Shape: (Nf, len(data))
    J = exponential @ data_proj.T - H.T * len(data) / len(timegrid)  # Shape: (Nf, K)
    spectra = np.squeeze(np.mean(np.real(np.conj(J) * J), axis=1))  # Mean across tapers

    return spectra, f