Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Filters: FIR

The Window Method for FIR Design

Design FIR low-pass, high-pass and band-pass filters with the window method: ideal sinc, truncation, window choice, filter length, and scipy.signal.firwin.

You know what a filter should do: keep the frequencies below a cutoff and remove the rest. The window method is the most direct way to turn that wish into a finite list of coefficients. You write the ideal answer, cut it to a finite length, and soften the cut with a window.

The ideal low-pass filter

A perfect "brick-wall" low-pass filter with cutoff wc (radians per sample) has frequency response 1 for |w| < wc and 0 above. Taking the inverse transform gives its impulse response:

h_ideal[n] = sin(wc * n) / (pi * n), with h_ideal[0] = wc / pi

This is a sinc function. It is symmetric, but it extends infinitely in both directions and is nonzero for negative n: it is neither finite nor causal. So the ideal filter cannot be built, but we can approximate it.

flowchart LR
    A["ideal response<br/>h_ideal[n] = sin(wc n)/(pi n)"] --> B["keep M+1 taps<br/>around n = 0"]
    B --> C["multiply by a window"]
    C --> D["delay by M/2<br/>(make it causal)"]
    D --> E["FIR coefficients b"]

Truncate, taper, delay. The delay by M/2 makes the filter causal and is the linear-phase delay.

Step by step in code

Choose an odd number of taps N = M + 1 and center the sinc at (N - 1)/2. Multiplying by a window tapers the ends smoothly.

Python A low-pass filter built by hand and checked against 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 hand-made filter matches signal.firwin, SciPy's window-method routine. Note that the taps are symmetric, so the filter has linear phase.

Why a window?

Cutting the sinc abruptly (a rectangular window) causes the same Gibbs overshoot you saw with Fourier series: ripples of about 9 % (roughly -21 dB) appear in the stopband and passband however long the filter is. A smooth window removes the jump and lowers the ripples, at the cost of a wider transition band.

Window Stopband attenuation Transition width (approx., in 2*pi/N units)
Rectangular about 21 dB 0.9
Hann about 44 dB 3.1
Hamming about 53 dB 3.3
Blackman about 74 dB 5.5
Kaiser adjustable by beta adjustable

The numbers are the standard textbook ones. Pick the cheapest window that reaches the attenuation you need, then choose N to meet the transition width.

Python Compare stopband attenuation of several windows

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 rectangular window is far worse than the tapered ones, and Blackman is the best. Hann and Hamming come out almost equal here: Hamming has the lower nearest sidelobe, while Hann's far sidelobes fall away faster, so which looks better depends on where you measure.

Choosing the length

A rule of thumb: the transition width delta_f (in Hz) and the number of taps are inversely related:

N ~ (window factor) * fs / delta_f, with a factor around 3.3 for Hamming and 5.5 for Blackman.

A narrow transition costs many taps, and more taps mean more computation and more delay. Kaiser windows turn this into a recipe: from the wanted attenuation A in dB and the transition width, scipy.signal.kaiserord(ripple_db, width) returns the number of taps and the shape parameter beta.

Python Design from a specification with the Kaiser window

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 specification is met with a modest number of taps. Halve the transition width and the number of taps roughly doubles.

High-pass, band-pass and band-stop

Any of these is built from low-pass ideals:

  • High-pass: the impulse d[n] (all frequencies) minus a low-pass: h_hp = delta - h_lp. This needs an odd length so the delta sits on a tap.
  • Band-pass: a low-pass with the upper edge minus a low-pass with the lower edge.
  • Band-stop: delta - band-pass.

firwin does this with pass_zero and a list of cutoffs.

Python High-pass and band-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.

Limits of the method

The window method is easy and reliable, but it is not optimal: the ripple of passband and stopband is tied together, and the corners are approximate. When you need the shortest possible filter for strict specifications, use the equiripple and frequency-sampling methods of the next lesson.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the impulse response of an ideal low-pass filter?

    The inverse transform of a rectangle in frequency is a sinc in time.
  2. 2. Why is the sinc multiplied by a window?

    Abrupt truncation leaves about 21 dB of stopband attenuation however long the filter is; tapering gives more.
  3. 3. To halve the transition width with the same window and attenuation, the number of taps must:

    The number of taps is inversely proportional to the transition width.
  4. 4. How is a high-pass FIR obtained from a low-pass one?

    delta minus low-pass leaves everything the low-pass removed. It requires an odd number of taps.

Summary

  • Start from the ideal sinc response, truncate it to N taps, taper it with a window and center it (delay (N-1)/2).
  • Windows trade stopband attenuation against transition width: rectangular is worst, Blackman best of the classics, Kaiser adjustable.
  • The number of taps grows as the transition band narrows.
  • signal.firwin designs low-pass, high-pass, band-pass and band-stop filters; kaiserord sizes a Kaiser design from specifications.
  • High-pass and band-stop use an odd length so a unit impulse can be subtracted.

Next: Equiripple and Frequency-Sampling Design