Setting Parameters for a Butterworth Filter

1.7k Views Asked by At

I'm working with brain wave data and am trying to use a Butterworth filter for the first time.

There plenty of helpful resources on stack exchange here (i) Butterworth filter in python and here (ii) How to implement band-pass Butterworth filter with Scipy.signal.butter

Taking advantage of these resources, I've implemented the following code:

    from scipy.signal import butter, sosfilt, sosfreqz

    def butter_bandpass(lowcut, highcut, fs, order=5):
        nyq = 0.5 * fs
        low = lowcut / nyq
        high = highcut / nyq
        sos = butter(order, [low, high], analog=False, btype='band', output='sos')
        return sos

    def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
        sos = butter_bandpass(lowcut, highcut, fs, order=order)
        y = sosfilt(sos, data)
        return y

The data I'm working with is on the left-hand graph, my attempt to filter it is on the right: neural activity during 700 ms delay period

neural activity during 700 ms delay period

I believe that the issue I'm running into is with the parameters.

Order: set to 5. The frequency response looked ok for order 5

Low-cut: .5

High-cut: 60

FS/Sample Rate/Waves: we were able to collect 500 data points per second, so I set this to 500

N: 350. We are dealing with data over a 700 ms period, but only sampling every other millisecond

Looking at my data, it appears we have roughly 2 sinusoidal waves for a 700 ms period combined with 11 higher frequency waves...should I be able to look at this and set the low cut to 2 and the high cut to some value greater than 11? I've tried iterating over dozens of values at this point...

Thank you to anyone who attempts to help. I've been trying to figure this out for the past two days and have hit a wall.

UPDATE Thank you Bob and Tim! That was absolutely the issue--the signal has to be centered at zero. Serious duh on my part. Here are the updated charts:

centered neural activity during 700 ms delay period

I will not say these are the most comely sinusoidal curves I've ever seen, but it's live data so my expectations were already tempered.

1

There are 1 best solutions below

4
Bob On

Think about what would happen if you pad your signal with zeros.

You would have a sudden jump to ~821000.

Also notice that a bandpass filter has gain 0 at frequency 0, this means that DC component will be filtered, you probably will get better results if you subtract the average from the signal before filtering.