---
title: Designing FIR filters
heading: From an ideal response to taps you can run
eyebrow: C9 · Lesson 41 · Designing FIR filters
xp: 40
---

::: hook
The ideal low-pass filter's impulse response is a sinc that runs forever in both directions. A real FIR filter keeps only a few dozen of its samples, delays them so the filter is causal, and tapers the ends with a window. That recipe gives a filter with exactly linear phase, guaranteed stability and a response you can predict before computing a single tap. What it costs is length, and the formulas say how much.
:::

::: play
- The curve is the response, in dB, of a 21-tap low-pass built by the window method: the ideal sinc for cutoff $\omega_c$, cut to 21 samples.
- With the rectangular window (h = 0) the stopband ripples sit only about 21 dB down, and the passband ripples near the edge (the Gibbs effect).
- Switch to the Hamming window (h = 1): the stopband drops to around −53 dB, and the transition from pass to stop gets wider. Move the cutoff: the shape slides along without changing.
:::

```sim plot
view: graph
x: w = 0..3.1416
y: G = -90..5 dB
slider: wc = 0.3..2.6 step 0.05 = 0.8
slider: h = 0..1 step 1 = 1
plot: 20*log(abs(wc/pi + 2*(1 - h + h*(0.54 + 0.46*cos(pi*1/10)))*sin(wc*1)/(pi*1)*cos(1*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*2/10)))*sin(wc*2)/(pi*2)*cos(2*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*3/10)))*sin(wc*3)/(pi*3)*cos(3*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*4/10)))*sin(wc*4)/(pi*4)*cos(4*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*5/10)))*sin(wc*5)/(pi*5)*cos(5*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*6/10)))*sin(wc*6)/(pi*6)*cos(6*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*7/10)))*sin(wc*7)/(pi*7)*cos(7*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*8/10)))*sin(wc*8)/(pi*8)*cos(8*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*9/10)))*sin(wc*9)/(pi*9)*cos(9*w) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*10/10)))*sin(wc*10)/(pi*10)*cos(10*w)) + 0.00001) | samples: 600 | label: 21-tap response
readout: gain at DC, dB = 20*log(abs(wc/pi + 2*(1 - h + h*(0.54 + 0.46*cos(pi*1/10)))*sin(wc*1)/(pi*1)*cos(1*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*2/10)))*sin(wc*2)/(pi*2)*cos(2*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*3/10)))*sin(wc*3)/(pi*3)*cos(3*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*4/10)))*sin(wc*4)/(pi*4)*cos(4*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*5/10)))*sin(wc*5)/(pi*5)*cos(5*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*6/10)))*sin(wc*6)/(pi*6)*cos(6*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*7/10)))*sin(wc*7)/(pi*7)*cos(7*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*8/10)))*sin(wc*8)/(pi*8)*cos(8*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*9/10)))*sin(wc*9)/(pi*9)*cos(9*0) + 2*(1 - h + h*(0.54 + 0.46*cos(pi*10/10)))*sin(wc*10)/(pi*10)*cos(10*0))) | digits: 3
```

::: predict
## A Hamming-window design meets its stopband with 53 dB to spare in a 31-tap filter. The transition band must be made half as wide. What happens to the stopband attenuation if the filter is lengthened to 61 taps?

- [ ] It doubles to about 106 dB
- [x] It stays at about 53 dB
- [ ] It halves
- [ ] It improves by 6 dB

::: reveal
It stays at about 53 dB. The window's shape sets the side-lobe level, and so the attenuation; the length only sets how wide the transition is. Twice the taps, half the transition, the same −53 dB. To get more attenuation, change the window.
:::
:::

::: nameit
## The window method

1. Pick the cutoff $\omega_c$ in the middle of the transition band.
2. Take the ideal low-pass response, delayed by M samples so it's centered: $h_d[n] = \dfrac{\sin(\omega_c(n - M))}{\pi(n - M)}$ (and $\omega_c/\pi$ at n = M).
3. Keep N = 2M + 1 samples, n = 0 to N − 1, and multiply by a window w[n].

The filter's response is the ideal one convolved with the window's spectrum. The window's main lobe sets the **transition width**, and its side lobes set the **stopband attenuation**.

| Window | Stopband attenuation | Transition width Δf, cycles per sample |
|---|---|---|
| rectangular | 21 dB | 0.9/N |
| Hann | 44 dB | 3.1/N |
| Hamming | 53 dB | 3.3/N |
| Blackman | 74 dB | 5.5/N |

So $N \approx C/\Delta f$, where Δf is the transition width divided by the sample rate.

## The Kaiser window

The Kaiser window has a parameter β that trades attenuation against width continuously. For a stopband attenuation of A dB and a transition of Δω radians per sample:

$$N \approx \frac{A - 8}{2.285\,\Delta\omega} + 1, \qquad \beta = \begin{cases} 0.1102(A - 8.7) & A > 50 \\ 0.5842(A - 21)^{0.4} + 0.07886(A - 21) & 21 \le A \le 50 \end{cases}$$

## Linear phase and cost

Symmetric taps give exactly linear phase: every frequency is delayed by M = (N − 1)/2 samples, so the waveform's shape is kept. An N-tap filter costs N multiply-adds per output sample, or about N/2 multiplies if the symmetric pairs are added first. High-pass, band-pass and band-stop filters come from the same recipe, using differences of ideal low-passes.
:::

::: example
## Design a low-pass for an 8 kHz system: pass up to 1 kHz, stop from 1.5 kHz down by at least 50 dB.

::: solution
1. **Transition width.** The transition is 500 Hz:
   $$\begin{aligned} \Delta f &= 500/8000 \\ &= 0.0625 \end{aligned}$$
2. **Length.** Hamming gives 53 dB, so
   $$\begin{aligned} N &\approx 3.3/0.0625 \\ &= 52.8 \end{aligned}$$
   which makes 53 taps.
3. **Cutoff.** The cutoff goes in the middle, 1.25 kHz:
   $$\begin{aligned} \omega_c &= 2\pi \times 1250/8000 \\ &= 0.982\ \text{rad} \end{aligned}$$
4. **Kaiser alternative.** A Kaiser design for exactly 50 dB needs
   $$\begin{aligned} N &= \frac{42}{2.285 \times 0.393} + 1 \\ &= 47.8 \end{aligned}$$
   so 48 taps, with β = 4.53: a few taps saved by not overshooting the attenuation.

**Answer:** a 53-tap Hamming design with $\omega_c$ = 0.982 rad (1.25 kHz), or a 48-tap Kaiser design with β = 4.53.
:::
:::

::: example
## How long does that 53-tap filter delay a signal, and what does it cost to run?

::: solution
1. **Delay.**
   $$(53 - 1)/2 = 26\ \text{samples}$$
   which is 3.25 ms at 8 kHz, the same at every frequency.
2. **Cost.** It needs 53 multiply-adds per sample, 424,000 a second, or 27 multiplies per sample if the symmetric pairs are summed first.
3. **In context.** Easy for any processor at 8 kHz, but at 100 MHz sample rates it's why FIR filters are built in hardware.

**Answer:** a delay of 26 samples (3.25 ms), and 53 multiply-adds per sample, 424,000 a second.
:::
:::

::: example
## Find the center tap and the taps 1 and 4 samples from it for an ideal low-pass with $\omega_c = \pi/4$, before windowing.

::: solution
1. **Center.**
   $$\omega_c/\pi = 0.25$$
2. **One away.**
   $$\sin(\pi/4)/\pi = 0.225$$
3. **Four away.**
   $$\sin(\pi)/(4\pi) = 0$$
4. **The pattern.** The taps are symmetric about the center, and every fourth one is zero. With the Hamming window, the taps 4 away are still 0 and the others are scaled toward the ends.

**Answer:** the center tap is 0.25, the taps 1 away are 0.225, and the taps 4 away are 0.
:::
:::

::: check
kind: parts
given: fs = 8000 | 48000; fp = 1000 | 3000; t = 300 | 600 | 1000

## A low-pass at {fs} Hz passes up to {fp} Hz and stops from {fp} + {t} Hz, using a Hamming window.

::: part
answer: ceil(3.3*fs/t)
tolerance: 0

### About how many taps does it need?

::: misses
- [any] $N \approx 3.3/\Delta f$ with $\Delta f = t/f_s$, rounded up.
:::
:::

::: part
answer: fp + t/2
unit: Hz
tolerance: 0.5%

### Where should the cutoff go?

::: misses
- [any] The middle of the transition.
:::
:::
:::

::: check
kind: parts
given: A = 40 | 60 | 80; d = 0.02 | 0.05 | 0.1

## A Kaiser-window design needs {A} dB of attenuation over a transition of Δf = {d} cycles per sample.

::: part
answer: ceil((A - 8)/(2.285*2*pi*d) + 1)
tolerance: 0

### About how many taps does it need?

::: misses
- [ceil((A - 8)/(2.285*d) + 1)] The formula wants Δω in radians: 2π times Δf.
- [any] $N \approx (A - 8)/(2.285\,\Delta\omega) + 1$, rounded up.
:::
:::

::: part
answer: 0.5842*(A - 21)^0.4*step(50.5 - A) + 0.07886*(A - 21)*step(50.5 - A) + 0.1102*(A - 8.7)*step(A - 50.5)
tolerance: 0.5%

### What β does it use?

::: misses
- [any] Pick the formula for {A} dB.
:::
:::
:::

::: check
kind: parts
given: N = 21 | 53 | 101; fs = 8 | 44.1 | 96

## A linear-phase FIR filter has {N} taps at {fs} kHz.

::: part
answer: (N - 1)/2/fs
unit: ms
tolerance: 0.5%

### How long does it delay every frequency?

::: misses
- [N/fs] The delay is to the center tap, (N − 1)/2 samples.
- [any] (N − 1)/2 samples.
:::
:::

::: part
answer: N*fs*1000
tolerance: 0.5%

### How many multiply-adds a second does it need, without using the symmetry?

::: misses
- [any] N per sample.
:::
:::
:::

::: check
kind: parts
given: c = 0.2 | 0.25 | 0.4; k = 1 | 2 | 3

## An ideal low-pass has $\omega_c$ = {c}π.

::: part
answer: c
tolerance: 0.5%

### What is its center tap?

::: misses
- [any] $\omega_c/\pi$.
:::
:::

::: part
answer: sin(c*pi*k)/(pi*k)
tolerance: 0.5%

### What is the tap {k} samples from the center?

::: misses
- [any] $\sin(\omega_c k)/(\pi k)$.
:::
:::
:::

::: check
kind: choice

## A specification needs 65 dB of stopband attenuation. Which window can the window method use?

- [ ] Rectangular, with enough taps
- [ ] Hamming, with enough taps
- [x] Blackman, or a Kaiser window with β set for 65 dB
- [ ] Any window, if N is large enough

::: misses
- [1] Its attenuation is about 21 dB however long the filter is.
- [2] Hamming stops at about 53 dB; more taps only narrow the transition.
- [4] The window's shape sets the attenuation; N doesn't change it.
:::
:::

::: check
kind: derivation
vars: C = 0.9..6, f = 1000..200000, t = 10..10000

## A window whose transition width is C/N cycles per sample is used at sample rate f for a transition of t hertz.

::: step
answer: t/f

### What is the transition in cycles per sample?
:::

::: step
answer: C*f/t

### About how many taps are needed?
:::

::: right
Right. The length grows with the sample rate and shrinks as the transition widens.
:::
:::

::: explain
## Why does a longer FIR filter give a sharper transition but not a deeper stopband?

::: model
The window method's response is the ideal brick-wall response convolved with the window's spectrum. Convolving smears the brick wall's edge over the window's main lobe, so the transition is as wide as that lobe, which shrinks in proportion to 1/N: a longer filter gives a sharper edge. The stopband attenuation comes from the window's side lobes, which leak the passband into the stopband, and their level relative to the main lobe is fixed by the window's shape, not its length: a longer Hamming window has narrower side lobes at the same −53 dB. So length buys sharpness, and the choice of window buys attenuation.
:::
:::

::: practice
::: check
given: fs = 16000 | 44100; t = 500 | 2000
answer: ceil(3.3*fs/t)
tolerance: 0

## Hamming design at {fs} Hz with a {t} Hz transition. Taps?

::: misses
- [any] $3.3 f_s / t$, rounded up.
:::
:::

::: check
given: N = 31 | 63; fs = 8 | 48
answer: (N - 1)/2/fs
unit: ms
tolerance: 0.5%

## Delay of a {N}-tap linear-phase filter at {fs} kHz?

::: misses
- [any] (N − 1)/2 samples.
:::
:::

::: check
given: c = 0.3 | 0.5; k = 1 | 2
answer: sin(c*pi*k)/(pi*k)
tolerance: 0.5%

## Ideal low-pass, $\omega_c$ = {c}π: the tap {k} from the center?

::: misses
- [any] $\sin(\omega_c k)/(\pi k)$.
:::
:::
:::
