Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal #284

Merged
merged 3 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions pynapple/process/signal_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Signal processing module
"""

import numpy as np
from .. import core as nap
from scipy.signal import butter, lfilter, filtfilt


def _butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a

def _butter_bandpass_filter(data, lowcut, highcut, fs, order=4):
b, a = _butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y

def compute_bandpass_filter(data, freq_band, sampling_frequency=None, order=4):
"""
Bandpass filtering the LFP.

Parameters
----------
data : Tsd/TsdFrame
Description
lowcut : TYPE
Description
highcut : TYPE
Description
fs : TYPE
Description
order : int, optional
Description

Raises
------
RuntimeError
Description
"""
time_support = data.time_support
time_index = data.as_units('s').index.values
if type(data) is nap.TsdFrame:
tmp = np.zeros(data.shape)
for i in np.arange(data.shape[1]):
tmp[:,i] = bandpass_filter(data[:,i], lowcut, highcut, fs, order)

return nap.TsdFrame(
t = time_index,
d = tmp,
time_support = time_support,
time_units = 's',
columns = data.columns)

elif type(data) is nap.Tsd:
flfp = _butter_bandpass_filter(data.values, lowcut, highcut, fs, order)
return nap.Tsd(
t=time_index,
d=flfp,
time_support=time_support,
time_units='s')

else:
raise RuntimeError("Unknow format. Should be Tsd/TsdFrame")