---
title: FIR filters
heading: Filters that compute each output as a weighted sum of recent inputs, the moving average and its comb of nulls, the impulse response equal to the taps, linear phase and its constant delay, designing a low-pass by windowing a sinc, and how many taps a sharp filter needs
eyebrow: D5 · Lesson 4 · FIR filters
xp: 40
---

::: hook
The simplest digital filter is an average: replace each sample with the mean of the last few. It smooths noise, and it has a remarkable property: frequencies whose period fits a whole number of times into the averaging window vanish completely. Generalize the average to a weighted sum and you have the **finite impulse response** filter, the workhorse of digital signal processing. FIR filters are always stable, can have perfectly linear phase so that every frequency is delayed equally, and can be designed to any shape, at the cost of many multiplications per sample.
:::

::: play
- The frequency response, in decibels, of an M-point moving average at a sampling rate of 1 kHz: $|H(f)| = \left|\frac{\sin(\pi fM/f_s)}{M\sin(\pi f/f_s)}\right|$.
- It's a crude low-pass: the main lobe ends in a null at $f_s/M$, with more nulls at every multiple of it.
- Choose M so that a null lands on a disturbance: M = 20 at $f_s$ = 1.2 kHz would remove 60 Hz hum and all its harmonics.
:::

```sim plot
view: graph
x: f = 1..500
y: H = -50..3
slider: M = 2..40 step 1 = 8
plot: 20*log(abs(sin(pi*f*M/1000)/(M*sin(pi*f/1000))) + 1e-9) | samples: 800 | label: moving-average response, dB (f in Hz)
marker: x = 1000/M | label: first null
readout: first null, Hz = 1000/M | digits: 4
readout: delay, ms = (M - 1)/2 | digits: 3
```

::: predict
## An 8-point moving average runs at 1 kHz. At what frequency does its response first fall to zero?

- [ ] 8 Hz
- [ ] 62.5 Hz
- [x] 125 Hz
- [ ] 500 Hz

::: reveal
A tone at 1000/8 = 125 Hz completes exactly one cycle in the 8 samples being averaged, so the average of its samples is zero whatever its phase. The same holds at 250 and 375 Hz. The −3 dB point is lower, at 55.7 Hz: a moving average is a poor low-pass but an excellent notch for anything periodic in M samples.
:::
:::

::: nameit
## The FIR filter

$$y_n = \sum_{k=0}^{N-1}h_kx_{n-k}$$

The **taps** $h_k$ are the filter's impulse response: an impulse in gives the taps out, and after N samples the response is over, hence "finite". There's no feedback, so an FIR filter is always stable. Its frequency response is the DTFT of the taps, $H(f) = \sum h_ke^{-j2\pi fk/f_s}$.

## Linear phase

If the taps are symmetric, $h_k = h_{N-1-k}$, every frequency is delayed by the same

$$\frac{N - 1}{2} \text{ samples}$$

so the waveform's shape is preserved, which matters for audio, data pulses and ECGs. Analog filters can't do this exactly.

## Designing a low-pass

An ideal low-pass has a sinc impulse response, infinitely long. Truncating it to N taps and multiplying by a window (lesson 3) gives a practical filter; the window sets the stopband attenuation, and the length sets the transition width. Kaiser's estimate for the taps needed for attenuation A dB over a transition Δf is

$$N \approx \frac{A - 8}{2.285 \times 2\pi\Delta f/f_s}$$

Each output costs N multiply-accumulates, so the work is $Nf_s$ per second.
:::

::: example
## An audio low-pass at 48 kHz needs 60 dB of stopband attenuation and a 1 kHz transition. How many taps, how much delay and how much work?

::: solution
1. **Kaiser's estimate.** With $A - 8 = 52$ and the 1 kHz transition in radians per sample:
   $$\begin{aligned} N &\approx \frac{52}{2.285 \times 2\pi \times \frac{1000}{48\,000}} \\ &= 174\ \text{taps} \end{aligned}$$
2. **Round to an odd length.** Choose 175, an odd number for a symmetric filter with a whole-sample delay.
3. **Delay.**
   $$\begin{aligned} D &= \frac{175 - 1}{2} \\ &= 87\ \text{samples} \\ &= 1.81\ \text{ms} \end{aligned}$$
4. **Work.** Every output takes 175 multiply-accumulates:
   $$\begin{aligned} &175 \times 48\,000 \\ &= 8.4 \times 10^6\ \text{per second} \end{aligned}$$

**Answer:** 175 taps, a delay of 87 samples (1.81 ms), and 8.4 million multiply-accumulates per second.
:::
:::

::: example
## How much does the 8-point, 1 kHz moving average attenuate a 60 Hz tone?

::: solution
1. **Gain at 60 Hz.** 60 Hz at 1 kHz is 0.06 cycles per sample, and $8 \times 0.06 = 0.48$:
   $$\begin{aligned} |H| &= \left|\frac{\sin(\pi \times 0.48)}{8\sin(\pi \times 0.06)}\right| \\ &= 0.666 \end{aligned}$$
   or −3.5 dB.
2. **Removing 60 Hz entirely.** Choose the average so that 60 Hz completes whole cycles in it: M = 20 at 1.2 kHz, since
   $$\frac{1200\ \text{Hz}}{20} = 60\ \text{Hz}$$

**Answer:** the average passes 0.666 of the 60 Hz tone, an attenuation of 3.5 dB; a 20-point average at 1.2 kHz would remove it.
:::
:::

::: example
## A linear-phase FIR filter has 101 taps at 48 kHz. What delay does every frequency see?

::: solution
1. **Delay in samples.**
   $$\frac{101 - 1}{2} = 50\ \text{samples}$$
2. **Delay in time.**
   $$\frac{50}{48\,000\ \text{Hz}} = 1.04\ \text{ms}$$
   the same for all frequencies, so a pulse keeps its shape.
3. **Effect on music.** Musicians notice delays of about 10 ms, so this one is harmless; a 1001-tap filter's 10 ms would not be.

**Answer:** every frequency is delayed 50 samples, 1.04 ms.
:::
:::

::: check
given: s = 1 | 8 | 48; M = 4 | 10 | 16
answer: s*1000/M
unit: Hz
tolerance: 0.5%

## An {M}-point moving average runs at {s} kHz. Where is its first null?

::: right
Right. $f_s/M$ = {answer} Hz.
:::

::: misses
- [s*1000/(2*M)] One full cycle fits in M samples at $f_s/M$.
- [any] Not yet. The tone that completes one cycle in M samples.
:::
:::

::: check
given: N = 31 | 65 | 255; s = 8 | 48
answer: (N - 1)/2/(s*1000)*1000
unit: ms
tolerance: 0.5%

## A symmetric {N}-tap FIR filter runs at {s} kHz. How long does it delay the signal?

::: right
Right. $(N - 1)/2$ samples = {answer} ms.
:::

::: misses
- [N/(s*1000)*1000] That's the whole filter's span; a symmetric filter delays by half of N − 1.
- [any] Not yet. Half the length, in samples, then in time.
:::
:::

::: check
given: A = 40 | 60 | 80; d = 0.5 | 1 | 2; s = 8 | 48
answer: (A - 8)/(2.285*2*pi*d/s)
tolerance: 0.5%

## About how many taps does a windowed FIR filter at {s} kHz need for {A} dB of attenuation with a {d} kHz transition band?

::: right
Right. Kaiser's estimate gives {answer}.
:::

::: misses
- [(A - 8)/(2.285*d/s)] The transition goes in radians per sample: multiply by 2π.
- [any] Not yet. $(A - 8)/(2.285\,\Delta\omega)$.
:::
:::

::: check
given: N = 31 | 101 | 255; s = 8 | 48 | 192
answer: N*s/1000
tolerance: 0.5%

## How many million multiply-accumulates per second does a {N}-tap FIR filter at {s} kHz need?

::: right
Right. $Nf_s$ = {answer} million per second.
:::

::: misses
- [any] Not yet. Taps per output times outputs per second.
:::
:::

::: check
kind: parts
given: A = 40 | 60 | 80; fs = 8 | 48; d = 0.5 | 1 | 2

## A Kaiser-window FIR at {fs} kHz needs {A} dB of attenuation and a {d} kHz transition; Kaiser's estimate is $N \approx (A - 8)/(2.285\,\Delta\omega)$.

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

### About how many taps?

::: misses
- [any] Δω = 2π × transition ÷ $f_s$; taps = order + 1.
:::
:::

::: part
answer: (ceil((A - 8)/(2.285*2*pi*d/fs)))/2/fs
unit: ms
tolerance: 0.5%

### What delay does it add?

::: misses
- [any] Half the order, in samples.
:::
:::
:::

::: check
kind: parts
given: N = 4 | 8 | 16; f = 50 | 60 | 120; fs = 1 | 2

## An {N}-point moving average runs at {fs} kHz.

::: part
answer: abs(sin(pi*f*N/(fs*1000))/(N*sin(pi*f/(fs*1000))))
tolerance: 0.5%

### What is its gain at {f} Hz?

::: misses
- [any] $|\sin(N\omega/2)/(N\sin(\omega/2))|$.
:::
:::

::: part
answer: fs*1000/N
unit: Hz
tolerance: 0.5%

### Where is its first null?

::: misses
- [any] $f_s/N$.
:::
:::
:::

::: explain
## Why does an FIR filter with symmetric taps delay every frequency by the same amount?

::: model
With symmetric taps, the filter's response can be written as a pair of terms for each tap equally far from the center, and each pair combines into a cosine centered on the middle tap. So the whole response is a real-valued function of frequency multiplied by a single phase factor that corresponds to a delay of half the filter's length. A pure delay shifts every frequency by the same time, so all the frequency components of a waveform come out aligned as they went in, and its shape is preserved apart from the filtering of its spectrum. Filters with feedback, or asymmetric taps, delay different frequencies by different amounts and distort the waveform's shape.
:::
:::

::: practice

::: check
given: s = 0.1 | 1000; M = 2 | 100
answer: s*1000/M
unit: Hz
tolerance: 0.5%

## First null of an {M}-point average at {s} kHz?

::: misses
- [any] $f_s/M$.
:::
:::

::: check
given: N = 11 | 1001; s = 1 | 200
answer: (N - 1)/2/(s*1000)*1000
unit: ms
tolerance: 0.5%

## Delay of a symmetric {N}-tap filter at {s} kHz?

::: misses
- [any] $(N - 1)/(2f_s)$.
:::
:::
:::
