---
title: DSP in hardware
heading: FIR filter structures, folding, CIC filters, polyphase decimation and CORDIC
eyebrow: S3 · Lesson 31 · DSP in hardware
xp: 50
---

::: hook
A software filter runs one multiply after another; a hardware filter can do all of them at once, every clock. That's why radios, radar, video and network chips do their signal processing in dedicated logic. Designing it means matching the arithmetic to the sample rate: a 64-tap filter at 48 kHz needs only three million multiply-accumulates a second, which one multiplier handles easily, while the same filter on a 500 MHz radio stream needs all 64 multipliers running in parallel. Clever structures cut the cost further: symmetric coefficients halve the multipliers, a CIC filter decimates with no multipliers at all, and CORDIC computes sines and rotations with nothing but shifts and adds.
:::

::: play
- The magnitude response of a moving-average filter of N taps (all coefficients 1/N), in dB, against frequency in cycles per sample. M stages in cascade make a CIC filter, which hardware builds from adders and registers alone.
- Nulls fall at multiples of 1/N cycles per sample, exactly where the aliases land when the output is decimated by N.
- Add stages: each one deepens the stopband by the same number of dB, at the cost of more droop in the passband.
:::
:::

```sim plot
view: graph
x: f = 0.001..0.5 cycles/sample
y: G = -100..5 dB
slider: N = 2..64 step 1 = 8
slider: M = 1..5 step 1 = 1
plot: M*20*log(abs(sin(pi*N*f)/(N*sin(pi*f))) + 1e-9) | samples: 1500 | label: response, dB
marker: x = 1/N | label: first null
readout: first null, cycles/sample = 1/N
readout: gain at 0.25/N, dB = M*20*log(abs(sin(pi*0.25)/(N*sin(pi*0.25/N))))
```

::: predict
## A 64-tap FIR filter processes audio at 48 kHz in an FPGA clocked at 100 MHz. How many hardware multipliers does it need?

- [x] One
- [ ] 32
- [ ] 64
- [ ] 128

::: reveal
One. The filter needs 64 × 48,000 = 3.07 million multiply-accumulates a second, and one multiplier at 100 MHz can do 100 million. There are 2,083 clock cycles between samples, so a single multiplier stepping through the taps uses only 64 of them. This is **folding**: hardware is time-shared when the clock is much faster than the sample rate, and replicated when it isn't.
:::
:::

::: nameit
## FIR structures

An N-tap FIR filter computes $y[n] = \sum_{k=0}^{N-1} h_kx[n - k]$: N multiplies and N − 1 additions per output.

- **Direct form:** a delay line of samples, a multiplier per tap and an adder tree. The adder tree sets the critical path.
- **Transposed form:** each input is multiplied by every coefficient at once, and the products ripple through a chain of registered adders. The critical path is one multiply and one add, whatever N is.
- **Linear-phase symmetry:** with $h_k = h_{N-1-k}$, pairs of samples are added before multiplying, so only $\lceil N/2 \rceil$ multipliers are needed. Linear phase means a delay of $(N - 1)/2$ samples at every frequency.

## Folding

The work per second is $N f_s$ multiply-accumulates. With a clock $f_\text{clk}$, the number of multipliers needed is $\left\lceil \frac{Nf_s}{f_\text{clk}} \right\rceil$. Each output's accumulator needs $B_x + B_h + \lceil \log_2 N \rceil$ bits to never overflow.

## Decimation

- **Polyphase:** when only every Dth output is kept, compute only those, splitting the filter into D phases: $N/D$ multiplies per input sample, and the arithmetic runs at the low rate.
- **CIC (cascaded integrator-comb):** M integrators at the high rate, decimation by R, then M combs at the low rate. Its response is the moving average's to the Mth power, with no multipliers. The registers must grow by $M\log_2(RD)$ bits (D the comb's differential delay, usually 1), and wraparound in two's complement keeps it exact.

## CORDIC

Rotates a vector by a sequence of fixed angles $\arctan 2^{-i}$, each step using only shifts and adds. It gains about one bit of accuracy per iteration and computes sines, cosines, magnitudes and phases. The vector grows by $K = \prod_i \sqrt{1 + 2^{-2i}}$ ≈ 1.6468, a constant corrected once.
:::

::: example
## A 64-tap symmetric FIR with 16-bit data and 16-bit coefficients runs at 48 kHz on a 100 MHz FPGA. How many multipliers, how wide an accumulator, and what delay?

::: solution
1. **Work.**
   $$64 \times 48\ \text{kHz} = 3.07\ \text{M MAC/s}$$
   so one multiplier is ample (with symmetry, 32 multiplies per output, after pre-adding pairs).
2. **Accumulator.** Data, coefficient and $\log_2 64$ guard bits:
   $$16 + 16 + 6 = 38\ \text{bits}$$
   With symmetry the pre-adder adds one bit to the data, but there are only 32 products to sum, so $17 + 16 + 5 = 38$ bits as well.
3. **Delay.**
   $$\frac{64 - 1}{2} = 31.5\ \text{samples}$$
   which is 656 µs at 48 kHz, the same at every frequency.

**Answer:** one multiplier, a 38-bit accumulator, and a delay of 31.5 samples (656 µs).
:::
:::

::: example
## A CIC decimator with 4 stages, R = 64 and D = 1 takes 12-bit samples. How wide must its registers be, and where does its response first null?

::: solution
1. **Register width.** The growth is $4\log_2 64 = 24$ bits:
   $$12 + 24 = 36\ \text{bits}$$
   for the integrators and combs.
2. **Nulls.** The response is a 64-tap moving average to the 4th power, with nulls at multiples of 1/64 of the input rate: the frequencies that alias onto DC after decimating by 64.
3. **Sidelobes.** The first sidelobes are about $4 \times 13.26 \approx 53$ dB down.
4. **Droop.** A short FIR at the low rate usually follows to flatten the passband droop.

**Answer:** 36-bit registers, and the first null is at 1/64 of the input rate.
:::
:::

::: example
## A 128-tap FIR runs at 10 MS/s on a 250 MHz clock. How many multipliers does it need, and how many if its coefficients are symmetric?

::: solution
1. **Work.**
   $$128 \times 10\ \text{MS/s} = 1.28 \times 10^9$$
   multiplies a second.
2. **Multipliers.** Each does 250 million a second:
   $$\lceil 5.12 \rceil = 6$$
   multipliers, each time-shared over about 21 taps.
3. **With symmetric coefficients.** Pairs of samples are added first, leaving 64 multiplies per output:
   $$\lceil 2.56 \rceil = 3\ \text{multipliers}$$

**Answer:** 6 multipliers, or 3 with symmetric coefficients.
:::
:::

::: check
given: N = 31 | 64 | 255; f = 48 | 192 | 1000
answer: N*f*1e3/1e6
tolerance: 0.5%

## An {N}-tap FIR runs at {f} kHz. How many million multiply-accumulates per second does it need?

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

::: misses
- [f*1e3/1e6] Every output needs N multiplies.
- [any] Not yet. Taps times sample rate.
:::
:::

::: check
given: N = 48 | 96 | 200; f = 2 | 10 | 40; c = 150 | 400
answer: ceil(N*f/c - 0.0001)
tolerance: 0

## An {N}-tap FIR filters a {f} MHz stream on a {c} MHz clock. How many multipliers are needed if they're time-shared?

::: right
Right. $\lceil Nf_s/f_\text{clk} \rceil$ = {answer}.
:::

::: misses
- [N] Time-share them: each multiplier does several taps per sample.
- [any] Not yet. Work per second over what one multiplier can do.
:::
:::

::: check
given: N = 31 | 64 | 101
answer: ceil(N/2 - 0.0001)
tolerance: 0

## A linear-phase FIR has {N} symmetric taps. How many multiplies does each output need?

::: right
Right. $\lceil N/2 \rceil$ = {answer}: pairs of samples share a coefficient.
:::

::: misses
- [N] Symmetric taps can share a multiplier after a pre-add.
- [any] Not yet. Pairs with equal coefficients.
:::
:::

::: check
given: x = 12 | 16; h = 14 | 18; N = 40 | 128 | 500
answer: x + h + ceil(log(N - 0.5)/log(2))
tolerance: 0

## An {N}-tap FIR has {x}-bit data and {h}-bit coefficients. How wide must its accumulator be to never overflow?

::: right
Right. $B_x + B_h + \lceil \log_2 N \rceil$ = {answer} bits.
:::

::: misses
- [x + h] Summing N products adds bits.
- [any] Not yet. Product width plus growth from the sum.
:::
:::

::: check
given: M = 3 | 4 | 5; R = 8 | 32 | 100; b = 10 | 14
answer: b + ceil(M*log(R)/log(2) - 0.0001)
tolerance: 0

## A CIC decimator has {M} stages, decimation {R} and differential delay 1, with {b}-bit input. How wide must its registers be?

::: right
Right. $B_\text{in} + \lceil M\log_2R \rceil$ = {answer} bits.
:::

::: misses
- [b + ceil(log(R)/log(2) - 0.0001)] Each of the M stages contributes the growth.
- [any] Not yet. Hogenauer's growth: $M\log_2(RD)$.
:::
:::

::: check
given: N = 8 | 20 | 50; f = 48 | 1000
answer: f/N
unit: kHz
tolerance: 0.5%

## An {N}-tap moving average runs at {f} kHz. Where is its first null?

::: right
Right. $f_s/N$ = {answer} kHz.
:::

::: misses
- [f/(2*N)] The first null is where exactly one cycle fits in the N samples.
- [any] Not yet. One full cycle across the window averages to zero.
:::
:::

::: check
given: N = 31 | 63 | 127; f = 8 | 48
answer: (N - 1)/2/(f*1e3)*1e6
unit: µs
tolerance: 0.5%

## A linear-phase FIR of {N} taps runs at {f} kHz. What's its delay?

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

::: misses
- [N/(f*1e3)*1e6] The delay is to the center tap, half the length.
- [any] Not yet. Half the filter's length, in time.
:::
:::

::: check
given: n = 8 | 12 | 16 | 20
answer: atan(2^(1 - n))*180/pi
unit: °
tolerance: 0.5%

## A CORDIC runs {n} iterations, i = 0 to {n} − 1, with micro-rotations of $\arctan 2^{-i}$. What's the smallest rotation angle, in degrees?

::: right
Right. $\arctan 2^{-(n-1)}$ = {answer}°, which bounds the angle resolution.
:::

::: misses
- [atan(2^(-n))*180/pi] The last iteration is i = n − 1.
- [any] Not yet. The last micro-rotation.
:::
:::

::: check
kind: parts
given: N = 31 | 64 | 127; s = 1 | 10; c = 100 | 300; B = 12 | 16

## An {N}-tap FIR takes {B}-bit samples and {B}-bit coefficients at {s} MS/s, on a {c} MHz clock.

::: part
answer: N*s
unit: M/s
tolerance: 0.5%

### How many multiply-accumulates a second does it need, in millions?

::: misses
- [any] Taps × sample rate.
:::
:::

::: part
answer: ceil(N*s/c)
tolerance: 0

### How many multipliers does it need?

::: misses
- [any] $\lceil Nf_s/f_\text{clk}\rceil$.
:::
:::

::: part
answer: 2*B + ceil(log(N)/log(2) - 1e-9)
unit: bits
tolerance: 0

### How wide must the accumulator be to never overflow?

::: misses
- [2*B] Add $\lceil\log_2N\rceil$ guard bits.
- [any] $B_x + B_h + \lceil\log_2N\rceil$.
:::
:::
:::

::: check
kind: derivation
vars: N = 8..1024, D = 2..64, M = 1..6, R = 2..1024

## An N-tap filter decimates by D; separately, a CIC filter has M stages and decimates by R (differential delay 1).

::: step
answer: N/D

### How many multiplies per input sample does the polyphase form need?
:::

::: step
answer: M*log(R)/log(2)

### How many bits must the CIC's registers grow?
:::

::: right
Right. Both forms do their arithmetic at the low rate, or without multipliers at all.
:::
:::

::: explain
## Why does a transposed-form FIR filter run at a higher clock than a direct-form one with the same coefficients?

::: model
In the direct form, every product for one output is formed at once and then summed through an adder tree, so the path from a register through a multiplier and the whole tree to the output register grows with the number of taps. The transposed form moves the registers into the adder chain: each new input is multiplied by every coefficient at once, and each product is added to a running partial sum that's registered before passing to the next adder. The longest path between registers is one multiplier and one adder no matter how many taps there are, so the clock can be as fast as a single multiply-add allows, while the filter computes the same outputs.
:::
:::

::: practice

::: check
given: N = 48 | 128; f = 4 | 25; c = 200 | 500
answer: ceil(N*f/c - 0.0001)
tolerance: 0

## Multipliers for {N} taps at {f} MHz on a {c} MHz clock?

::: misses
- [any] $\lceil Nf_s/f_\text{clk} \rceil$.
:::
:::

::: check
given: x = 10 | 18; h = 12 | 16; N = 60 | 300
answer: x + h + ceil(log(N - 0.5)/log(2))
tolerance: 0

## Accumulator for {N} taps, {x}-bit data, {h}-bit coefficients?

::: misses
- [any] $B_x + B_h + \lceil \log_2N \rceil$.
:::
:::

::: check
given: M = 2 | 5; R = 16 | 50; b = 8 | 16
answer: b + ceil(M*log(R)/log(2) - 0.0001)
tolerance: 0

## CIC width: {M} stages, R = {R}, {b}-bit input?

::: misses
- [any] $B + \lceil M\log_2R \rceil$.
:::
:::
:::
