Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 36 ยท Applications

Filter Design Basics: FIR and IIR Filters

Design digital low-pass filters in Python with scipy.signal: FIR windows with firwin, Butterworth IIR with butter, frequency responses, group delay, and when to use each.

A filter is an LTI system built to keep some frequencies and reject others. Filters clean noisy sensors, separate bass from treble, remove mains hum and prepare signals for downsampling. Everything you learned about convolution, frequency response and the z-transform comes together here.

What you specify

A low-pass design is described by

  • a passband edge fp where gain stays near 1,
  • a stopband edge fs_ where gain must be small,
  • a transition band between them,
  • allowed ripple in the passband and minimum attenuation in the stopband.

A narrow transition band or a high attenuation costs filter order, meaning more computation. Sampling frequency enters in normalization: SciPy accepts cutoffs in hertz if you pass fs=.

FIR filters

A finite impulse response (FIR) filter computes y[n] = ฮฃ h[k] x[n - k] with a finite list of coefficients and no feedback. Its transfer function has only zeros (plus trivial poles at z = 0), so it is always stable. If h is symmetric, the filter has exactly linear phase: every frequency is delayed by the same amount, (L - 1)/2 samples for L taps, so waveforms keep their shape.

The window method designs an ideal low-pass impulse response (a sinc) and truncates it with a window, which scipy.signal.firwin does in one call.

Python A FIR low-pass with firwin

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 gain at the cutoff is about -6 dB for a windowed-sinc design (half amplitude), whereas Butterworth designs define the cutoff at -3 dB. Always check which definition the tool uses.

IIR filters

An infinite impulse response (IIR) filter uses feedback, which gives it poles. Feedback allows a sharp cutoff with a low order, often 5 to 20 times fewer coefficients than an FIR filter of comparable sharpness. The costs: stability must be checked, and the phase is nonlinear, so different frequencies are delayed by different amounts.

Classic IIR families, each a trade-off:

  • Butterworth: maximally flat passband, gentle roll-off.
  • Chebyshev I: ripple in the passband, steeper roll-off.
  • Chebyshev II: ripple in the stopband.
  • Elliptic: ripple in both, the steepest roll-off for a given order.

Designs usually start from an analog prototype and use the bilinear transform s = 2 fs (z - 1)/(z + 1), which maps the left half of the s-plane into the unit disk and so keeps stable designs stable.

Python Butterworth order versus roll-off

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.

Every Butterworth design is -3.01 dB at the cutoff, and each additional order adds 6 dB per octave of steepness. In practice prefer the second-order sections (output="sos") form: high-order polynomials in b and a lose numerical precision.

Filtering a noisy signal

Apply a filter with lfilter (causal, introduces delay) or sosfiltfilt (zero-phase: runs forward and backward, offline only). The following example recovers a 5 Hz sine from additive noise.

Python Removing noise with a Butterworth low-pass

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.

FIR or IIR?

Need Choose
Linear phase, waveform shape matters FIR
Guaranteed stability, adaptive filters FIR
Sharp cutoff on tiny hardware IIR
Mimic an analog filter IIR
Real-time with minimal latency IIR

For offline analysis, zero-phase filtering (sosfiltfilt) removes the phase issue of IIR filters but doubles the effective order and cannot run in real time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is an FIR filter always stable?

    An FIR output depends only on a finite number of inputs, so its transfer function has no poles outside the origin.
  2. 2. Which property comes from a symmetric FIR impulse response?

    Symmetric coefficients delay all frequencies equally, by (L - 1)/2 samples.
  3. 3. What gain does a Butterworth filter have at its cutoff frequency?

    By definition the Butterworth cutoff is where the gain falls to 1/โˆš2, about -3.01 dB.
  4. 4. When would you prefer an IIR filter?

    Feedback gives IIR filters a much steeper roll-off for a given number of coefficients.

Summary

  • Specify passband, stopband, ripple and attenuation; sharper filters need higher order.
  • FIR: stable, can be exactly linear phase, more coefficients. Design with firwin.
  • IIR: feedback, few coefficients, nonlinear phase. Design with butter, cheby1, ellip; use SOS form.
  • lfilter is causal and delays; sosfiltfilt is zero-phase but offline.

Next: Modulation and Demodulation