Skip to content

Commit 63f4e5b

Browse files
committed
use non-causal filter
1 parent e802346 commit 63f4e5b

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/explorepy/asr_processor.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
asr_process
1818
)
1919

20-
from explorepy.filters import ExGFilter
21-
20+
from explorepy.filters import bp_filter
2221

2322
logger = logging.getLogger(__name__)
2423

@@ -137,7 +136,6 @@ def __init__(self, stream_proc, in_topic):
137136
self.instantiate_buffers()
138137
self.is_initialized = True
139138
self.lifecycle_state = State.STABLE
140-
self.filter = None
141139

142140
@property
143141
def cutoff(self):
@@ -190,7 +188,7 @@ def on_calib_data_received(self, packet):
190188
return
191189
self.calibration_data_input = np.append(
192190
self.calibration_data_input,
193-
self.filter.apply(packet, in_place=False).get_data()[1],
191+
packet.get_data()[1],
194192
axis=1,
195193
)
196194

@@ -221,8 +219,10 @@ def clean_data(self):
221219
return
222220
if self.to_clean_ts[0][0] <= 1.0:
223221
return
222+
filt_sig = bp_filter(self.to_clean.copy(), 45, 55, btype='bandstop', fs=self.sr)
223+
filtered_data = bp_filter(filt_sig, .1, 40, fs=self.sr)
224224
try:
225-
ret = asr_pipeline(self.to_clean, self.sr, self.ch_count, self._state)
225+
ret = asr_pipeline(filtered_data, self.sr, self.ch_count, self._state)
226226
self.cleaned_data_available = True
227227
idx = np.searchsorted(self.to_clean_ts[0], self.last_cleaned_timestamp)
228228
self.cleaned_data = ret[:, idx:]
@@ -258,12 +258,6 @@ def start_calibration(self, calib_length: float = -1.0):
258258
f"[{self._min_calibration_length},{self._max_calibration_length}]")
259259
logger.info(f"Starting ASR calibration for {self.calibration_length}s...")
260260
self.calib_started_at = time.time()
261-
self.filter = ExGFilter(
262-
cutoff_freq=(1, 45),
263-
filter_type='bandpass',
264-
s_rate=self.sr,
265-
n_chan=self.ch_count,
266-
)
267261
self.stream_processor.subscribe(self.on_calib_data_received, topic=self.in_topic)
268262

269263
def stop_calibration(self):
@@ -276,7 +270,9 @@ def stop_calibration(self):
276270

277271
def set_state_from_calibration_data(self, calib_data):
278272
self.lifecycle_state = State.CLEANING
279-
cleaned, state = clean_calib_data(calib_data, self.sr)
273+
filt_sig = bp_filter(calib_data, 45, 55, btype='bandstop', fs=self.sr)
274+
filtered_data = bp_filter(filt_sig, .1, 40, self.sr)
275+
cleaned, state = clean_calib_data(filtered_data, self.sr)
280276
if cleaned is None:
281277
self.calibration_data_available = False
282278
self.lifecycle_state = state

src/explorepy/filters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55

66
import numpy as np
7+
from scipy import signal
78
from scipy.signal import (
89
butter,
910
iirfilter,
@@ -17,6 +18,13 @@
1718
logger = logging.getLogger(__name__)
1819

1920

21+
@staticmethod
22+
def bp_filter(exg, lf, hf, fs, btype='bandpass'):
23+
N = 5
24+
b, a = signal.butter(N, [lf, hf], btype=btype, fs=fs)
25+
return signal.filtfilt(b, a, exg, axis=1)
26+
27+
2028
class ExGFilter:
2129
"""Filter class for ExG signals"""
2230
def __init__(self, cutoff_freq, filter_type, s_rate, n_chan, order=5):

0 commit comments

Comments
 (0)