diff --git a/pyrobolearn/filters/utils.py b/pyrobolearn/filters/utils.py new file mode 100644 index 0000000..98ce013 --- /dev/null +++ b/pyrobolearn/filters/utils.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +"""Common filters used in signal processing + +These filters can be useful to smooth signal trajectories. +""" + +from scipy.signal import butter, lfilter + + +__author__ = "Brian Delhaisse" +__copyright__ = "Copyright 2018, PyRoboLearn" +__credits__ = ["Brian Delhaisse"] +__license__ = "GNU GPLv3" +__version__ = "1.0.0" +__maintainer__ = "Brian Delhaisse" +__email__ = "briandelhaisse@gmail.com" +__status__ = "Development" + + +# Butterworth Filter +# Example from http://scipy-cookbook.readthedocs.io/items/ButterworthBandpass.html +def butter_bandpass(lowcut, highcut, fs, order=5): + 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=5): + b, a = butter_bandpass(lowcut, highcut, fs, order=order) + y = lfilter(b, a, data) + return y