I am looking into designing a Bandpass Butterworth filter in python, but, I was not sure I am designing my filter correctly. What I have are the following:
- High cutoff frequency = 200Hz
- Low cutoff frequency = 10Hz
- Sampling frequency = 1000Hz
- for my data, I used Filter order = 6
My code definition are below:
# section of my imports:
from scipy.signal import find_peaks, find_peaks_cwt, argrelextrema, welch, lfilter, butter, savgol_filter, medfilt, freqz, filtfilt
from scipy.signal import argrelextrema, filtfilt, butter, lfilter
def butter_bandpass(lowcut, highcut, fs, order):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='bandpass', output='ba')
# sos = butter(order, [low, high], btype='bandpass', output='sos')
return b, a
# return sos
def butter_bandpass_filter(data, lowcut, highcut, fs, order):
# sos = butter_bandpass(lowcut, highcut, fs, order=order)
# y = signal.sosfilt(sos=sos, x=data)
# y = signal.sosfiltfilt(sos=sos, x=data)
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = filtfilt(b=b, a=a, x=data)
# y = lfilter(b, a, data)
return y
How can I get the passband and stopband attenuation, also, where can I find the required equations to use in order for me to get my Butterworth filter design equation |H(w)|? Similar to the following link: (Bandpass and Bandstop Filter Design). I calculated the digital frequencies in radians per second:
- wh = 400π rad/sec
- wl = 20π rad/sec
- w(ah) ≈ 21.93 rad/sec
- w(al) ≈ 1.096 rad/sec
- W ≈ 20.84 rad/sec
- w^2 ≈ 578.53
Last steps are the prototype transformation from lowpass-to-bandpass and transforming the equation into Bilinear Transformation Technique (BLT) to get the digital filter are missing. So, what equation do I need to get the digital filter?
scipy.signal
and wherever you're importingbutter
from). Somewhere inscipy.signal
there's a function that'll give you the frequency response of a filter -- you can use that to graph the response. $\endgroup$filtfilt
? Again, please explicitly add the imports you're using, if only to allow anyone to reproduce your results with simple copy and paste. $\endgroup$butter
uses? $\endgroup$