Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Filters: FIR

FIR Filter Basics and the Moving Average

Learn what FIR filters are, why the moving average is a low-pass filter, how to read magnitude and phase responses, and why symmetric coefficients give linear phase.

A finite impulse response (FIR) filter computes each output as a weighted sum of the most recent input samples:

y[n] = b0 x[n] + b1 x[n-1] + ... + bM x[n-M]

The weights b are the filter coefficients (or taps), and they are also exactly the impulse response h: the filter is simply the convolution y = b * x. The filter has M + 1 taps and order M. Because there is no feedback, an FIR filter is always stable, and its design can give an exactly linear phase, which means every frequency is delayed by the same time. Those two properties make FIR filters the default choice when waveform shape matters (audio, medical signals, data communication).

flowchart LR
    X["x[n]"] --> D1["z^-1"] --> D2["z^-1"] --> D3["..."]
    X --> M0["b0"]
    D1 --> M1["b1"]
    D2 --> M2["b2"]
    M0 --> S["sum"]
    M1 --> S
    M2 --> S
    S --> Y["y[n]"]

A tapped delay line: delay blocks hold past inputs, each is scaled by a coefficient and all are added.

The moving average

The simplest FIR filter sets all L coefficients to 1/L. It replaces each sample by the mean of the last L samples. It smooths noise, but how much does it blur the signal? The answer is its frequency response.

For the frequency response H(e^(jw)) = sum of b[k] e^(-jwk) the moving average gives a smooth low-pass shape, with zeros at multiples of 2*pi/L where whole periods fit into the window.

Python Frequency response of a moving average

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 longer window cuts off lower (more smoothing, more blur). The first null sits at 2*pi/L: a sine that fits exactly one period into the window is averaged to zero.

Reading a frequency response

H(e^(jw)) is a complex function of frequency. We plot two things:

  • The magnitude |H| (often in dB) says how much each frequency is amplified or attenuated.
  • The phase angle(H) says how much each is shifted.

Terms you will meet when specifying filters: passband (frequencies kept), stopband (frequencies removed), transition band (the gap between them), ripple (wobble in the passband), stopband attenuation (how low the stopband falls, in dB) and cutoff frequency.

Python Magnitude and phase plots

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 magnitude has deep notches at the nulls, and the phase falls along a straight line (with jumps of pi where the gain changes sign). A straight line is what linear phase looks like.

Linear phase and delay

If the coefficients are symmetric (b[k] = b[M-k]) or antisymmetric (b[k] = -b[M-k]), the phase is a straight line in frequency, and every frequency component is delayed by the same amount: the group delay is constant at M/2 samples. The waveform comes out undistorted, only late. Four kinds (types I to IV) exist, depending on whether the length is odd or even and the symmetry is even or odd. Type I (odd length, symmetric) is the workhorse, and it is the one with an integer delay, which is easy to compensate.

Python Symmetric taps give a constant delay

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 symmetric filter holds exactly 2 samples of delay at every frequency, while the asymmetric one delays different frequencies by different amounts (phase distortion).

Applying an FIR filter

Use np.convolve or scipy.signal.lfilter(b, 1, x). The output of a causal filter begins with a start-up transient of M samples, and it lags by the group delay. To cancel the lag, shift the output left by M/2 samples, or use mode="same".

Python Smoothing a noisy pulse and compensating the delay

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.

Removing the 4-sample delay lowers the error substantially: the lag itself was part of the error.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

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

    Bounded inputs times a finite set of coefficients give a bounded output.
  2. 2. A 4-point moving average (b = [0.25, 0.25, 0.25, 0.25]) has a first null at which digital frequency?

    The first null is at 2pi/L = 2pi/4 = pi/2.
  3. 3. What delay does a symmetric linear-phase FIR filter with M + 1 = 21 taps introduce?

    Group delay is M/2 = 20/2 = 10 samples at every frequency.
  4. 4. What does a longer moving-average window do?

    The main lobe narrows as the window gets longer, so only lower frequencies pass.

Summary

  • An FIR filter is y = b * x: the coefficients are its impulse response, and it is always stable.
  • The moving average is a low-pass FIR with nulls at multiples of 2*pi/L.
  • Magnitude shows which frequencies pass; phase shows their delay.
  • Symmetric or antisymmetric taps give linear phase: a constant delay of M/2 samples.
  • Use lfilter or np.convolve, and compensate the delay if alignment matters.

Next: The Window Method for FIR Design