Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Filters: FIR

FIR Design in Practice

A practical FIR workflow: write the specification, pick a method, verify with freqz, remove hum and drift from a signal, use zero-phase filtfilt and test coefficient rounding.

You now know the pieces: FIR structure, linear phase, three design methods and fast convolution. This lesson puts them into a repeatable workflow and runs it on a realistic problem: cleaning a slow biological-style signal that carries baseline drift, mains hum and broadband noise.

A design workflow

  1. Write the specification in physical units: sampling rate, passband edges, stopband edges, allowed passband ripple, required stopband attenuation, and the maximum delay you can afford.
  2. Estimate the number of taps. Transition width and attenuation fix it (Kaiser formula, or remez trial and error).
  3. Choose the method: firwin with a Kaiser window for routine work, remez when taps are expensive, firwin2 for odd shapes.
  4. Verify with freqz (magnitude, phase or group delay) and with a test signal. Never trust a design you did not measure.
  5. Decide how to handle the delay: compensate it, accept it, or use zero-phase filtering offline.
  6. Check the edges: the first M output samples are a transient.
flowchart LR
    S["specification<br/>(fs, edges, ripple, attenuation)"] --> T["estimate taps"]
    T --> D["design<br/>firwin / remez / firwin2"]
    D --> V["verify with freqz"]
    V -->|"misses spec"| T
    V -->|"meets spec"| A["apply and align delay"]

If the verification fails, adjust the taps or the specification and repeat.

The test problem

A sensor records at fs = 500 Hz. The wanted signal is a slow waveform with content between 0.5 Hz and 40 Hz. It is contaminated by baseline drift below 0.3 Hz, 50 Hz mains hum and broadband noise. We design a band-pass filter from 0.5 Hz to 40 Hz. The narrow low edge (0.5 Hz at a rate of 500 Hz) needs a long filter, which makes a good test of the delay and the edge transients.

Python Design and verify a band-pass FIR

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.

A 0.4 Hz transition at 500 Hz sampling needs a very long filter: about 3,660 taps and a delay of roughly 3.7 seconds. That is why narrow low-frequency edges are expensive for FIR filters, and why IIR designs (next chapter) are popular for them.

Apply it and measure the improvement

Python Clean a drifting, humming, 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 error falls sharply because the drift and the hum are removed, and a part of the noise too. The good slice excludes the start-up transient, which in a real recording is a reason to record extra data around the part you need.

Zero-phase filtering with filtfilt

When you process recorded data offline you can avoid delay altogether: filter forward, reverse the result, filter again, and reverse again (scipy.signal.filtfilt). The phase shifts cancel exactly, so the output is aligned with the input and nothing needs compensating. The price: the magnitude is squared (a 50 dB filter becomes 100 dB; the passband ripple doubles in dB), the method cannot run in real time because it needs the whole record, and the transient at the edges is larger. filtfilt pads the ends to soften this.

Python lfilter versus filtfilt

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.

Coefficient quantization

On a microcontroller or an FPGA the coefficients are stored with a limited number of bits (fixed point). Rounding them changes the response, mostly the stopband attenuation. Always re-check the response with the rounded coefficients. Symmetric structure helps: you can halve the multipliers because b[k] = b[M-k].

Python How many coefficient bits do we need?

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.

Each extra coefficient bit buys roughly 6 dB until the design itself becomes the limit. Pick enough bits to keep the stopband attenuation above your specification, with some margin.

Common mistakes

  • Even length for a high-pass: a type II filter always has a zero at w = pi, so it cannot pass high frequencies. Use an odd number of taps.
  • Forgetting the delay when comparing input and output.
  • Giving edges in Hz without passing fs. Without it the edges are read as fractions of the Nyquist frequency; always pass fs= when you think in hertz.
  • Judging with the magnitude only. Check the group delay and the time-domain behaviour as well.
  • Using too few taps for a narrow transition and then "fixing" it by changing windows. Count the taps first.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What should you do right after designing a filter?

    freqz shows whether the real design meets the ripple, attenuation and edge requirements.
  2. 2. What is the main drawback of filtfilt?

    Backward filtering requires future samples, and the magnitude is squared.
  3. 3. Why is a narrow low-frequency transition expensive for FIR filters?

    At fs = 500 Hz, a 0.4 Hz band is a tiny fraction of the spectrum, and a very long filter is needed to resolve it.
  4. 4. How does rounding coefficients to fewer bits usually affect an FIR filter?

    Rounding adds an error that fills the stopband. Symmetric rounding keeps the linear phase, and FIR filters stay stable.

Summary

  • Work from a specification: edges, ripple, attenuation and delay budget; estimate the taps, design, then verify with freqz.
  • Narrow transition bands, especially at low frequencies, need many taps and cause a long delay.
  • Compensate the (N-1)/2 delay, ignore the start-up transient, or use zero-phase filtfilt offline.
  • Check the effect of coefficient quantization: roughly 6 dB of attenuation per bit.
  • Use an odd length for high-pass and band-stop filters.

Next: IIR Filters and Recursion