I am trying to filter EEG signals using butterworth filter and filtfilt. I have gone through a lot of documentation and these 2 commands seem sufficient for filtering. However, the results are bizarre.
from scipy.signal import butter, filtfilt
import sys, pickle
from numpy import *
import matplotlib.pyplot as plt
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=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = filtfilt(b, a, data)
return y
with open(sys.argv[1]) as eeg:
eeg_data = pickle.load(eeg)
eeg_data = eeg_data[:,3]
fs = 128
lowcut = 1
highcut = 40.0
y = butter_bandpass_filter(eeg_data, lowcut, highcut, fs, order = 9)
plt.plot(y,'r')
plt.show()
This is the eeg data
Also, isn't the output in time domain? It seems incorrect irrespective of the domain. The order is too high i.e. 1e28 for the first 1000 points. I checked for the other points and the order is 1e17, even for points after 4000. Is this what I should expect?
order=order
is fine in python, the first is the argument name, and the second is a variable. @user2497484 My guess would be that 9th order is too high and you're running into numerical problems. Can you try lower orders first? $\endgroup$ – endolith Jul 7 '14 at 14:04eeg_data = randn(10000)*20+2350
and it worked ok. Does this work for you also? Maybe something is wrong with the data itself? $\endgroup$ – endolith Jul 7 '14 at 14:36