From 428786b1269af643e1d556d50262297129537f05 Mon Sep 17 00:00:00 2001 From: Brian Delhaisse Date: Fri, 21 Jun 2019 20:18:14 +0200 Subject: [PATCH] add filters to smooth signals --- pyrobolearn/filters/utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 pyrobolearn/filters/utils.py 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