Skip to content

Instantly share code, notes, and snippets.

@ahwillia
Created February 20, 2019 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahwillia/4dbba6196918420b37db13c543779ce4 to your computer and use it in GitHub Desktop.
Save ahwillia/4dbba6196918420b37db13c543779ce4 to your computer and use it in GitHub Desktop.
Bandpass filter in python... (I have no idea why scipy does not provide this)
def bandpass(x, lowcut, highcut, fs, order=5, axis=-1, kind='butter'):
"""
Parameters
----------
x : ndarray
1d time series data
lowcut : float
Defines lower frequency cutoff (e.g. in Hz)
highcut : float
Defines upper frequency cutoff (e.g. in Hz)
fs : float
Sampling frequency (e.g. in Hz)
order : int
Filter order parameter
kind : str
Specifies the kind of filter
axis : int
Axis along which to bandpass filter data
"""
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
if kind == "butter":
b, a = butter(order, [low, high], btype="band")
else:
raise ValueError("Filter kind not recognized.")
return filtfilt(b, a, x, axis=axis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment