Last active
March 16, 2022 13:50
-
-
Save marcocamma/6080e08ef401a3e80b3015f67aaee49e to your computer and use it in GitHub Desktop.
Simple functions around scipy.signal.butter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def digital_filter(x,y,f=1000,order=10,kind="lowpass"): | |
dt = x[1]-x[0] | |
fs = 1/dt | |
sos = signal.butter(order, f, kind, fs=fs, output='sos') | |
return signal.sosfiltfilt(sos,y) | |
def lowpass(x,y,f=1000,order=10): | |
return digital_filter(x,y,f=f,order=order,kind="lowpass") | |
def highpass(x,y,f=1000,order=10,add_average=True): | |
temp = digital_filter(x,y,f=f,order=order,kind="highpass") | |
const = y.mean() if add_average else 0 | |
return temp+const | |
def bandpass(x,y,freq=[99,101],order=10,add_average=True): | |
""" freq can be a tuple (fmin,fmax) or a list of tuples if multiple bands """ | |
freqs = np.atleast_2d(freq) | |
temp = np.zeros_like(x) | |
for band in freqs: | |
temp += digital_filter(x,y,f=band,order=order,kind="bandpass") | |
const = y.mean() if add_average else 0 | |
return temp+const | |
def bandstop(x,y,freq=[99,101],order=10): | |
""" freq can be a tuple (fmin,fmax) or a list of tuples if multiple bands """ | |
freqs = np.atleast_2d(freq) | |
temp = np.zeros_like(x) | |
for band in freqs: | |
y = digital_filter(x,y,f=band,order=order,kind="bandstop") | |
return y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment