//PROBLEM
I'm trying to filter EEG trials from an array in which each trail has dimensions [8x125] = [number of channels x samples@250Hz], but the resulting signal looks static and wrong. Why is this happening? How can I improve my filtered signal quality? See below how the original signal and its PSD are and how the signal comes out of the filtering process:
//PROCEDURE
I'm first applying an IIR 60Hz notch filter, padding the signal (50 mirrored samples on each side), applying a bandpass FIR filter from 5 to 30 Hz, and trimming the signal back down, considering the introduced delay from the FIR filter:
from scipy.signal import lfilter, kaiserord, firwin, iirnotch, freqz
##::FILTERS DESIGN
#Bandpass freq
low = 5 / NYQ
high = 30 / NYQ
#Notch
b1, a1 = iirnotch(60, 9, SPS)
#Kaiser window method
width = 2/NYQ #transition width
ripple_db = 11 #attenuation
N, beta = kaiserord(ripple_db, width)
b = firwin(N, [low, high], pass_zero = 'bandpass', window=('kaiser', beta))
print('>>FIR Order: ', N)
delay = int(0.5 * (N-1)) # / SPS
print('>>Delay: ', delay)
##::FILTERING DATA
win = 50
for x in range(data.shape[0]):
#Notch filter
filtsamples = lfilter(b1, a1, data[x, :, :], axis = 1)
prefilt = np.hstack(( np.flip(filtsamples[:, 0:win], axis = 1), filtsamples[:, :], np.flip(filtsamples[:, -win:], axis = 1) ))
#FIR
posfilt = lfilter(b, [1.0], prefilt, axis = 1)
flt_data[x,:,:] = posfilt[:, win+delay:-win]
Here are my frequency and phase responses for each filter:
//TESTS & UPDATES
- When I plot the trials individually and plot the data before and after the IIR notch, I can see an instability is created by the filter on the first half (or more). Also, that creates a huge 60Hz spike (which is exactly what I'm trying to avoid LOL). My trials are pretty short, if I were to chop it off, almost no data would remain. Is there a way to avoid this?
This is how the Notch filtered data looks by itself.
iirnotch
. $\endgroup$filtsamples
. I'm wondering if theprefilt
data is being transposed, so you're filtering across data sets rather than along them? $\endgroup$0
onlfilter
, it gives me this result. Both have a prominent spike at 60Hz... but it feels thataxis = 0
is not destructing the signal as much. What do you think? $\endgroup$