1

I have a problem with my butterworth bandpass filter. I have an 1D array (eeg signal) recorded with 250Hz. The cutoff frequencies are 6 and 11 Hz. What I have now is this, which doesn´t work:

import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import scipy.io
import scipy.signal
import scipy.fftpack
from scipy.signal import butter, lfilter
def butter_bandpass(lowcut, highcut, fs, order=6):
    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=6):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y


if __name__ == "__main__":
    fs = 250.0
    lowcut = 6.0
    highcut = 11.0
    t = range(len(eeg))
    x = eeg[t]
    y = butter_bandpass_filter(x, lowcut, highcut, fs, order=6)
    plt.plot(t, y)
    plt.show() 

What´s wrong?

Thanks Mike

men in black
  • 193
  • 1
  • 1
  • 9
  • 3
    What doesn't work? What's happening now and what did you expect would happen? – Patrick Kostjens Aug 11 '13 at 15:16
  • if I plot both (eeg and y) its the same plot with different y-scales. I´ve expected a kind of sin-wave... – men in black Aug 11 '13 at 15:45
  • Have you tried using `scipy.signal.freqz` to plot the frequency response of your filter? Is it as you expect? – mtrw Aug 11 '13 at 19:57
  • Yes I have and it is not as I´ve expected. I expected a few straight lines through the origin with positiv and negative slopes. What I see is the opposite. There are only downward lines which are positiv and look like a right-angled triangle where x- and y-axis the legs are. – men in black Aug 11 '13 at 20:59
  • Does anybody know if its easier to filter my data with an other filter? – men in black Aug 11 '13 at 21:02
  • That code looks familiar. :) (http://stackoverflow.com/questions/12093594/how-to-implement-band-pass-butterworth-filter-with-scipy-signal-butter/12233959#12233959) – Warren Weckesser Aug 11 '13 at 23:22
  • You could use an FIR filter; see http://stackoverflow.com/questions/16301569/bandpass-filter-in-python/16306891#16306891 – Warren Weckesser Aug 11 '13 at 23:36
  • Thanks for your help!! I have found the mistake (use the right axis for the lfilter and everything is good) :) – men in black Aug 12 '13 at 20:40

0 Answers0