19  Signal processing

19.1 Simulated multi-frequency signal

import pynapple as nap
import numpy as np
fs = 1000
t = np.linspace(0, 2, fs * 2)
f2 = np.cos(t*2*np.pi*2)
f10 = np.cos(t*2*np.pi*10)
f50 = np.cos(t*2*np.pi*50)

tsd = nap.Tsd(t=t,d=f2+f10+f50 + np.random.normal(0, 0.5, len(t)))
tsd
Time (s)
---------------------  -------
0.0                    2.64245
0.0010005002501250625  3.57662
0.002001000500250125   2.65813
0.003001500750375187   2.39727
0.00400200100050025    2.13886
0.0050025012506253125  2.03328
0.006003001500750374   1.77981
...
1.9939969984992494     1.57808
1.9949974987493746     1.53595
1.9959979989994996     1.76478
1.9969984992496246     2.70225
1.9979989994997498     2.85359
1.9989994997498748     2.61803
2.0                    2.77927
dtype: float64, shape: (2000,)

19.2 Bandpass filter

filtered = nap.apply_bandpass_filter(
    data=tsd, 
    cutoff=(8, 12), 
    fs=fs, 
    mode='butter'
)
filtered
Time (s)
---------------------  ----------
0.0                    0.408717
0.0010005002501250625  0.39122
0.002001000500250125   0.372175
0.003001500750375187   0.351626
0.00400200100050025    0.329622
0.0050025012506253125  0.306216
0.006003001500750374   0.28147
...
1.9939969984992494     0.00309179
1.9949974987493746     0.0027922
1.9959979989994996     0.00250474
1.9969984992496246     0.0022313
1.9979989994997498     0.0019734
1.9989994997498748     0.00173219
2.0                    0.00150849
dtype: float64, shape: (2000,)

19.3 Power-spectral density

psd = nap.compute_power_spectral_density(
    tsd, 
    fs=fs
)
psd
0
0.0 0.000005
0.5 0.000274
1.0 0.000373
1.5 0.000156
2.0 1.011827
... ...
497.5 0.000191
498.0 0.000046
498.5 0.000079
499.0 0.000026
499.5 0.001498

1000 rows × 1 columns