Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Signal Processing

Filter Design and Filtering Signals

Design and apply digital filters with scipy.signal: Butterworth IIR filters, sosfilt and filtfilt, FIR filters with firwin, freqz responses, convolution and median filters.

A filter lets some frequencies through and blocks others. A low-pass filter keeps slow changes and removes fast jitter, a high-pass filter removes slow drift, a band-pass filter keeps one range (a heartbeat, an engine note) and a band-stop or notch filter cuts one narrow band, such as 50 Hz mains hum. In the last lesson, The Fast Fourier Transform, you learned to see frequencies. Here you learn to remove them.

scipy.signal can design and apply filters in a few lines. There are two families. IIR filters (such as Butterworth) are cheap and sharp but have a frequency-dependent delay. FIR filters need more coefficients but can be exactly linear-phase, which means no distortion of the waveform's shape. This lesson designs both, inspects them with freqz, shows the difference between sosfilt and sosfiltfilt and finishes with convolution and the median filter, which handles spikes that linear filters cannot.

flowchart LR
    X["noisy input x[n]"] --> F["filter<br/>(coefficients b, a<br/>or sos sections)"]
    F --> Y["output y[n]"]
    D["design:<br/>butter, firwin, iirnotch"] --> F
    D -.->|"freqz / sosfreqz"| R["gain vs frequency<br/>(check before use)"]

Design first, check the frequency response, then apply. Applying a filter you never inspected is how bugs hide.

Designing a Butterworth filter

signal.butter(N, Wn, btype, fs=fs, output="sos") designs an order-N Butterworth filter, whose gain is as flat as possible in the pass band. Give it the cutoff Wn in Hz and the sampling rate via fs. For band-pass and band-stop pass a pair [low, high]. The cutoff is the frequency where the gain has dropped to about 70.7% (-3 dB).

Always request output="sos" (second-order sections) and apply the result with sosfilt. The older (b, a) polynomial form is numerically fragile for higher orders; sos splits the filter into stable little pieces.

Python Low-pass filter a noisy signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The noise is mostly gone, but the plain sosfilt error is held up by a different problem: the filtered curve follows the true signal but lags slightly to the right, and that delay counts as error. The delay is a property of causal IIR filters. The zero-phase sosfiltfilt (explained below) removes it, and its error is much smaller.

Looking at the frequency response

Before trusting a filter, ask what it does at each frequency. signal.sosfreqz(sos, worN, fs=fs) returns the frequencies and the complex response h; abs(h) is the gain. Decibels (20 * log10(gain)) are the usual unit: -3 dB is about 70%, -20 dB is 10% and -40 dB is 1%. A higher order makes the transition from pass band to stop band steeper.

Python Comparing filter orders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All three filters pass through -3 dB at 20 Hz, whatever the order. What changes is how fast they fall after that point: order 8 is about 77 dB lower than order 2 at 80 Hz. A higher order costs more delay and more risk of ringing, so choose the lowest order that meets your need.

Zero-phase filtering with sosfiltfilt

sosfilt runs through the data once, forward in time. Its output is delayed, and the delay depends on frequency, which distorts the shape of a waveform. When you process recorded data offline, you can do better: sosfiltfilt filters forward, then runs the filter again backward over the result. The delays cancel exactly, so the output lines up with the input. The cost is that the effective order doubles, and you cannot use it for real-time streams because it needs the future samples.

Python Delay of sosfilt versus sosfiltfilt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The forward-only filter shifts the peak later by tens of milliseconds, while sosfiltfilt keeps it at 1.0 s. If the exact timing of a feature matters (an ECG spike, a step response), use the zero-phase version for offline analysis.

FIR filters with firwin

A finite impulse response (FIR) filter is just a weighted moving average with numtaps coefficients, designed so the weights produce the frequency response you want. signal.firwin(numtaps, cutoff, fs=fs) designs the common types (add pass_zero="bandpass" or "highpass" and so on, and a window). The coefficients are symmetric, which makes the filter linear-phase: every frequency is delayed by the same amount, exactly (numtaps - 1) / 2 samples, so the waveform keeps its shape.

FIR filters are always stable and easy to reason about; the price is length. A sharp transition needs many taps. Apply them with signal.lfilter(taps, 1.0, x) or, for zero delay offline, signal.filtfilt(taps, 1.0, x).

Python A band-pass FIR filter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 5 Hz drift and the 300 Hz noise are suppressed to almost nothing, while 60 Hz passes with a gain very close to 1. Rolling the output by the constant delay realigns it with the input (the first and last few samples contain start-up effects and should be ignored).

Convolution and moving averages

Filtering by a set of weights is convolution: each output sample is a weighted sum of neighbouring input samples. signal.convolve(x, kernel, mode="same") returns an output as long as the input; for long kernels signal.fftconvolve computes the same result via the FFT, much faster. A moving average is the simplest kernel: n equal weights summing to 1. It is a crude low-pass filter, and it blurs sharp edges.

Python A moving average by convolution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Modes matter at the edges: "full" gives all len(x) + len(kernel) - 1 outputs, "same" trims to the input length, and "valid" keeps only the positions where the kernel fully overlaps the data.

Spikes need a median filter

A linear filter spreads a spike into its neighbours: averaging a single glitch of 50 smears it across several samples. The median filter instead replaces each value by the median of its neighbourhood, so isolated outliers vanish completely while genuine steps keep their sharp edge. signal.medfilt(x, kernel_size=5) requires an odd window.

Python Removing sensor glitches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The median result is far closer to the truth and keeps the step crisp, while the average still carries every spike as a bump.

A noise-removal pipeline

Real signals have several problems at once. Here a slow 2 Hz wave is buried in 50 Hz mains hum, a DC drift and white noise. The recipe is to notch out the hum (iirnotch), then low-pass the rest, always inspecting the result against a known answer.

Python Hum, drift and noise removed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Warning: Filters have start-up transients at the edges of the data, so ignore (or trim) the first and last stretch of the output when you measure errors. Also choose cutoffs below the Nyquist frequency fs / 2; butter raises an error otherwise.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why prefer output="sos" when designing an IIR filter?

    High-order (b, a) polynomials can lose precision or become unstable. Splitting the filter into second-order sections avoids that.
  2. 2. What does sosfiltfilt do that sosfilt does not?

    Filtering forward and then backward cancels the delay, at the cost of needing the whole recording and doubling the effective order.
  3. 3. A FIR filter with 101 taps is linear-phase. How many samples does it delay the signal?

    A symmetric FIR filter delays everything by (numtaps - 1) / 2 samples, here 50.
  4. 4. Which filter is best for removing isolated sensor spikes while keeping a sharp step?

    A median ignores outliers within its window and preserves edges. Linear filters smear each spike into its neighbours.
  5. 5. A Butterworth low-pass has a 20 Hz cutoff. What is its gain at 20 Hz?

    The cutoff frequency of a Butterworth design is the half-power point, where the gain is about -3 dB (70.7%), whatever the order.

Summary

  • signal.butter(N, Wn, btype, fs=fs, output="sos") designs IIR filters; apply them with sosfilt (causal) or sosfiltfilt (zero phase, offline).
  • sosfreqz and freqz show the gain at every frequency; check the response before applying a filter and prefer the lowest order that works.
  • firwin designs linear-phase FIR filters; their delay is (numtaps - 1) / 2 samples.
  • Filtering is convolution: convolve for short kernels, fftconvolve for long ones; mind the mode argument.
  • medfilt removes spikes and keeps edges; iirnotch removes a single narrow frequency such as mains hum.
  • Ignore the edge transients when judging filter results.

Next up: look at how frequencies change over time in Spectrograms, Peaks and Resampling.