Signal Filtering in Python
Over a decade ago I posted code demonstrating how to filter data in Python, but there have been many improvements since then. My original posts (1, 2, 3, 4) required creating discrete filtering functions, but modern approaches can leverage Numpy and Scipy to do this more easily and efficiently. In this article we will use scipy.signal.filtfilt
to apply lowpass, highpass, and bandpass filters to reduce noise in an ECG signal (stored in ecg.wav (created as part of my Sound Card ECG project).
Movingwindow filtering methods often result in a filtered signal that lags behind the original data (a phase shift). By filtering the signal twice in opposite directions filtfilt
cancelsout this phase shift to produce a filtered signal which is nicely aligned with the input data.
import scipy.io.wavfile
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
# read ECG data from the WAV file
sampleRate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sampleRate
# apply a 3pole lowpass filter at 0.1x Nyquist frequency
b, a = scipy.signal.butter(3, 0.1)
filtered = scipy.signal.filtfilt(b, a, data)
# plot the original data next to the filtered data
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.plot(times, data)
plt.title("ECG Signal with Noise")
plt.margins(0, .05)
plt.subplot(122)
plt.plot(times, filtered)
plt.title("Filtered ECG Signal")
plt.margins(0, .05)
plt.tight_layout()
plt.show()
Cutoff Frequency
The second argument passed into the butter
method customizes the cutoff frequency of the Butterworth filter. This value (Wn) is a number between 0 and 1 representing the fraction of the Nyquist frequency to use for the filter. Note that Nyquist frequency is half of the sample rate. As this fraction increases, the cutoff frequency increases. You can get fancy and express this value as 2 * Hz / sample rate.
plt.plot(data, '.', alpha=.5, label="data")
for cutoff in [.03, .05, .1]:
b, a = scipy.signal.butter(3, cutoff)
filtered = scipy.signal.filtfilt(b, a, data)
label = f"{int(cutoff*100):d}%"
plt.plot(filtered, label=label)
plt.legend()
plt.axis([350, 500, None, None])
plt.title("Effect of Different Cutoff Values")
plt.show()
Improve Edges with Gustafsson’s Method
Something weird happens at the edges. There's not enough data "off the page" to know how to smooth those points, so what should be done?
Padding is the default behavior, where edges are padded with with duplicates of the edge data points and smooth the trace as if those data points existed. The drawback of this is that one stray data point at the edge will greatly affect the shape of your smoothed data.
Gustafsson’s Method may be superior to padding. The advantage of this method is that stray points at the edges do not greatly influence the smoothed curve at the edges. This technique is described in a 1994 paper by Fredrik Gustafsson. "Initial conditions are chosen for the forward and backward passes so that the forwardbackward filter gives the same result as the backwardforward filter." Interestingly this paper demonstrates the method by filtering noise out of an EKG recording.
# A small portion of data will be inspected for demonstration
segment = data[350:400]
filtered = scipy.signal.filtfilt(b, a, segment)
filteredGust = scipy.signal.filtfilt(b, a, segment, method="gust")
plt.plot(segment, '.', alpha=.5, label="data")
plt.plot(filtered, 'k', label="padded")
plt.plot(filteredGust, 'k', label="Gustafsson")
plt.legend()
plt.title("Padded Data vs. Gustafsson’s Method")
plt.show()
BandPass Filter
Lowpass and highpass filters can be selected simply by customizing the third argument passed into the filter. The second argument indicates frequency (as fraction of Nyquist frequency, half the sample rate). Passing a list of two values in for the second argument allows for bandpass filtering of a signal.
b, a = scipy.signal.butter(3, 0.05, 'lowpass')
filteredLowPass = scipy.signal.filtfilt(b, a, data)
b, a = scipy.signal.butter(3, 0.05, 'highpass')
filteredHighPass = scipy.signal.filtfilt(b, a, data)
b, a = scipy.signal.butter(3, [.01, .05], 'band')
filteredBandPass = scipy.signal.lfilter(b, a, data)
Filter using Convolution
Another way to lowpass a signal is to use convolution. In this method you create a window (typically a bellshaped curve) and convolve the window with the signal. The wider the window is the smoother the output signal will be. Also, the window must be normalized so its sum is 1 to preserve the amplitude of the input signal.
There are different ways to handle what happens to data points at the edges (see numpy.convolve
for details), but setting mode
to valid
delete these points to produce an output signal slightly smaller than the input signal.
# create a normalized Hanning window
windowSize = 40
window = np.hanning(windowSize)
window = window / window.sum()
# filter the data using convolution
filtered = np.convolve(window, data, mode='valid')
plt.subplot(131)
plt.plot(kernel)
plt.title("Window")
plt.subplot(132)
plt.plot(data)
plt.title("Data")
plt.subplot(133)
plt.plot(filtered)
plt.title("Filtered")
Different window functions filter the signal in different ways. Hanning windows are typically preferred because they have a mostly Gaussian shape but touch zero at the edges. For a discussion of the pros and cons of different window functions for spectral analysis using the FFT, see my notes on FftSharp.
Resources

Sample data: ecg.wav

Jupyter notebook for this page: signalfiltering.ipynb

SciPy Cookbook: Filtfilt, Buterworth Bandpass Filter

SciPy Documentation: scipy.signal.filtfilt, scipy.signal.butter

Numpy Documentation: numpy.convolve

Savitzky Golay Filtering  The Savitzky Golay filter is a particular type of lowpass filter, well adapted for data smoothing.