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

Adding support for lowpass filter #3665

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 35 additions & 2 deletions src/spikeinterface/preprocessing/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FilterRecording(BasePreprocessor):
band : float or list, default: [300.0, 6000.0]
If float, cutoff frequency in Hz for "highpass" filter type
If list. band (low, high) in Hz for "bandpass" filter type
btype : "bandpass" | "highpass", default: "bandpass"
btype : "bandpass" | "highpass" | "lowpass", default: "bandpass"
Type of the filter
margin_ms : float, default: 5.0
Margin in ms on border to avoid border effect
Expand Down Expand Up @@ -89,7 +89,7 @@ def __init__(
assert filter_mode in ("sos", "ba"), "'filter' mode must be 'sos' or 'ba'"
fs = recording.get_sampling_frequency()
if coeff is None:
assert btype in ("bandpass", "highpass"), "'bytpe' must be 'bandpass' or 'highpass'"
assert btype in ("bandpass", "highpass", "lowpass"), "'bytpe' must be 'bandpass' , 'highpass' or 'lowpass'"
# coefficient
# self.coeff is 'sos' or 'ab' style
filter_coeff = scipy.signal.iirfilter(
Expand Down Expand Up @@ -271,6 +271,37 @@ def __init__(self, recording, freq_min=300.0, margin_ms=5.0, dtype=None, **filte
self._kwargs.update(filter_kwargs)


class LowpassFilterRecording(FilterRecording):
"""
Lowpass filter of a recording

Parameters
----------
recording : Recording
The recording extractor to be re-referenced
freq_max : float
The lowpass cutoff frequency in Hz
margin_ms : float
Margin in ms on border to avoid border effect
dtype : dtype or None
The dtype of the returned traces. If None, the dtype of the parent recording is used
{}

Returns
-------
filter_recording : LowpassFilterRecording
The lowpass-filtered recording extractor object
"""

def __init__(self, recording, freq_max=300.0, margin_ms=5.0, dtype=None, **filter_kwargs):
FilterRecording.__init__(
self, recording, band=freq_max, margin_ms=margin_ms, dtype=dtype, btype="lowpass", **filter_kwargs
)
dtype = fix_dtype(recording, dtype)
self._kwargs = dict(recording=recording, freq_max=freq_max, margin_ms=margin_ms, dtype=dtype.str)
self._kwargs.update(filter_kwargs)


class NotchFilterRecording(BasePreprocessor):
"""
Parameters
Expand Down Expand Up @@ -326,6 +357,7 @@ def __init__(self, recording, freq=3000, q=30, margin_ms=5.0, dtype=None):
bandpass_filter = define_function_from_class(source_class=BandpassFilterRecording, name="bandpass_filter")
notch_filter = define_function_from_class(source_class=NotchFilterRecording, name="notch_filter")
highpass_filter = define_function_from_class(source_class=HighpassFilterRecording, name="highpass_filter")
lowpass_filter = define_function_from_class(source_class=LowpassFilterRecording, name="lowpass_filter")


def causal_filter(
Expand Down Expand Up @@ -396,6 +428,7 @@ def causal_filter(

bandpass_filter.__doc__ = bandpass_filter.__doc__.format(_common_filter_docs)
highpass_filter.__doc__ = highpass_filter.__doc__.format(_common_filter_docs)
lowpass_filter.__doc__ = lowpass_filter.__doc__.format(_common_filter_docs)


def fix_dtype(recording, dtype):
Expand Down