---
title: CIC and halfband filters
heading: Multiplier-free cascaded integrator–comb decimators, their droop and register growth, and halfband stages
eyebrow: S7 · Lesson 9 · CIC and halfband filters
xp: 50
---

::: hook
A delta-sigma converter or a software radio's front end must decimate by factors of 64 to thousands at rates of tens or hundreds of megahertz, where every multiplier counts. Two filter families do most of that work. The cascaded integrator–comb (CIC) filter uses no multipliers at all, just adders and registers, and handles the first, largest rate reduction. Halfband filters, with nearly half their coefficients exactly zero, then take each remaining factor of two at a fraction of an ordinary filter's cost.
:::

::: play
- The curve is a CIC decimator's response, relative to DC, against frequency in units of the output rate: $N \cdot 20\log_{10}\left|\frac{\sin(\pi\nu)}{R\sin(\pi\nu/R)}\right|$ for R = 16 and N stages.
- Nulls fall at every multiple of the output rate, exactly where the bands that would alias onto DC lie.
- Each added stage deepens the rejection near the nulls, but also the droop across the wanted band.
:::

```sim plot
view: graph
x: v = 0.01..3
y: H = -120..0 dB
slider: N = 1..6 = 4 step 1
plot: N*20*log(abs(sin(pi*v)/(16*sin(pi*v/16)))) | label: CIC, R = 16
```

::: predict
## A four-stage CIC decimates by 16 and must pass up to 10% of its output rate. How much does it droop at that edge, and how well does it reject the band that would alias onto it?

- [ ] Droop 4 dB, rejection 20 dB
- [x] Droop 0.57 dB, rejection about 77 dB
- [ ] No droop, rejection 120 dB
- [ ] Droop 0.1 dB, rejection 10 dB

::: reveal
At ν = 0.1: $4 \times 20\log_{10}\frac{\sin(0.1\pi)}{16\sin(0.1\pi/16)} = -0.57$ dB. The band that aliases onto the edge sits at ν = 0.9, near the first null: $4 \times 20\log_{10}\frac{\sin(0.9\pi)}{16\sin(0.9\pi/16)} = -76.7$ dB. A short compensating FIR at the low rate then flattens the droop.
:::
:::

::: nameit
## The CIC decimator

N integrators $1/(1 - z^{-1})$ run at the input rate, a downsampler by R, then N combs $1 - z^{-M}$ at the output rate (M = 1 or 2, the differential delay). By the noble identities this equals
$$H(z) = \left[\frac{1 - z^{-RM}}{1 - z^{-1}}\right]^N$$
N cascaded moving sums of RM samples, built from adders only.

- **DC gain**: $(RM)^N$.
- **Register growth**: the registers need $N\log_2(RM)$ bits more than the input. The integrators overflow, but in two's-complement arithmetic with enough bits the combs undo the wrap-around exactly.
- **Response**, in units ν of the output rate: $\left|\frac{\sin(\pi M\nu)}{RM\sin(\pi\nu/R)}\right|^N$ relative to DC: droop across the passband, nulls at multiples of 1/M.
- A short FIR compensator at the output rate, roughly an inverse sinc, flattens the passband.

## Halfband filters

A lowpass with cutoff π/2 and equal ripple δ in both bands can be designed so that $h[n] = 0$ for every even offset from the center except the center itself, which is ½. Then:
- $H(e^{j\omega}) + H(e^{j(\pi - \omega)}) = 1$: the response is symmetric about π/2, where it is exactly ½ (−6 dB);
- an N-tap halfband (N = 4K − 1) has only 2K + 1 nonzero taps, and with symmetry K distinct multiplications plus a shift for the center;
- decimating by 2 in polyphase form, one branch is a pure delay, so it costs K multiplications per output sample.

A decimate-by-$2^k$ chain is often a CIC followed by halfband stages and a final sharper FIR.
:::

::: example
## A three-stage CIC decimates 8-bit data by 8. How wide must its registers be, and what is its DC gain?

::: solution
1. **Bit growth.**
   $$\begin{aligned} \text{growth} &= 3\log_2 8 \\ &= 9\ \text{bits} \end{aligned}$$
   so the registers need 8 + 9 = 17 bits.
2. **DC gain.**
   $$\begin{aligned} G &= 8^3 \\ &= 512 \end{aligned}$$
   which shows as the 9 extra bits.
3. **Wraparound.** The integrators will wrap around constantly, and that's fine: with 17-bit two's-complement arithmetic, the combs subtract the wrapped values and the output matches the direct moving-sum filter exactly.

**Answer:** the registers must be 17 bits wide, and the DC gain is 512.
:::
:::

::: example
## A 23-tap halfband filter decimates by 2. How many of its taps are nonzero, and how many multiplications does it need per output sample?

::: solution
1. **Find K.** 23 = 4 × 6 − 1, so K = 6.
2. **Nonzero taps.** That gives 2K + 1 = 13 nonzero taps: the center ½ and 12 others in symmetric pairs.
3. **Multiplications.** With symmetry that's 6 distinct multiplications, and in polyphase form the center tap's branch is a delay and a shift: 6 multiplications per output sample, against 12 for an ordinary symmetric 23-tap decimator.

**Answer:** 13 taps are nonzero, and it needs 6 multiplications per output sample.
:::
:::

::: example
## A 3.072 MHz delta-sigma bitstream must become 48 kHz audio. Plan a decimation chain.

::: solution
1. **Total factor.**
   $$\frac{3.072\ \text{MHz}}{48\ \text{kHz}} = 64$$
2. **CIC first.** A CIC decimating by 16 brings it to 192 kHz using only adders.
3. **Two halfbands.** Two halfbands by 2 take it to 96 and then 48 kHz, the last one sharp enough to keep the 20 kHz band and reject everything above 24 kHz.
4. **Cost and droop.** Each halfband runs at a quarter or less of the input rate, and the CIC's droop at 20 kHz is corrected in the last stage.

**Answer:** a CIC by 16 to 192 kHz, then two halfband filters by 2 to 96 and 48 kHz, with the CIC's droop corrected in the last stage.
:::
:::

::: check
kind: parts
given: N = 3 | 4 | 5; R = 8 | 16 | 64; b = 1 | 12

## A CIC decimator has N = {N} stages, R = {R}, M = 1, and {b}-bit input.

::: part
answer: R^N
tolerance: 0.5%

### What is its DC gain?

::: misses
- [N*R] The gain multiplies: $(RM)^N$.
- [any] $(RM)^N$.
:::
:::

::: part
answer: b + round(N*log(R)/log(2))
unit: bits
tolerance: 0

### How wide must its registers be?

::: misses
- [N*log(R)/log(2)] Add the input's own width.
- [any] $b + N\log_2(RM)$.
:::
:::
:::

::: check
kind: parts
given: N = 3 | 4 | 5; R = 8 | 16 | 32; p = 0.05 | 0.1 | 0.2

## A CIC decimator (N = {N}, R = {R}, M = 1) must pass up to {p} of its output rate.

::: part
answer: -N*20*log(abs(sin(pi*p)/(R*sin(pi*p/R))))
unit: dB
tolerance: 0.5%

### How much does it droop at that edge?

::: misses
- [any] $-N \cdot 20\log_{10}\left|\frac{\sin\pi\nu}{R\sin(\pi\nu/R)}\right|$.
:::
:::

::: part
answer: -N*20*log(abs(sin(pi*(1 - p))/(R*sin(pi*(1 - p)/R))))
unit: dB
tolerance: 0.5%

### How much does it attenuate the band that aliases onto that edge?

::: misses
- [any] The same formula at ν = 1 − ν_p.
:::
:::
:::

::: check
kind: parts
given: K = 4 | 8 | 16

## A halfband filter has N = 4K − 1 taps with K = {K}.

::: part
answer: 4*K - 1
unit: taps
tolerance: 0

### How long is it?

::: misses
- [any] 4K − 1.
:::
:::

::: part
answer: 2*K + 1
unit: taps
tolerance: 0

### How many of its taps are nonzero?

::: misses
- [any] 2K plus the center.
:::
:::

::: part
answer: K
unit: multiplications
tolerance: 0

### How many multiplications per output does it need, decimating by 2?

::: misses
- [any] One per symmetric pair; the center is a shift.
:::
:::
:::

::: check
kind: parts
given: f = 3.072 | 6.144 | 12.288; o = 48 | 96; c = 4 | 8 | 16

## A {f} MHz bitstream is decimated to {o} kHz: a CIC by {c}, then halfband stages.

::: part
answer: f*1000/o
tolerance: 0.5%

### What is the total decimation factor?

::: misses
- [any] Input over output rate.
:::
:::

::: part
answer: log(f*1000/o/c)/log(2)
unit: stages
tolerance: 0.5%

### How many halfband stages follow the CIC?

::: misses
- [any] $\log_2$ of the remaining factor.
:::
:::
:::

::: check
kind: parts
given: v = 0.2 | 0.3 | 0.45

## A halfband filter's response is {v} at ω = π/2 − x.

::: part
answer: 1 - v
tolerance: 0.5%

### What is it at π/2 + x?

::: misses
- [v] The response is antisymmetric about ½: $H(\omega) + H(\pi - \omega) = 1$.
- [any] $1 - H(\pi/2 - x)$.
:::
:::
:::

::: check
kind: choice

## Why can a CIC filter's integrators overflow constantly and still give correct outputs?

- [ ] The overflows average out over time
- [x] In two's-complement (modular) arithmetic, the combs subtract register values whose true difference fits in the register width; wrap-around in the integrators cancels exactly in those differences, as long as the registers have the full $N\log_2(RM)$ bits of growth
- [ ] The downsampler discards the overflowed samples
- [ ] CIC filters use floating point

::: misses
- [1] Nothing averages; the cancellation is exact.
- [3] The downsampler keeps every Rth value, overflowed or not.
- [4] They use plain integer adders; that's their appeal.
:::
:::

::: check
kind: derivation
vars: N = 1..6, R = 2..64, v = 0.01..0.49

## A CIC decimator has N stages, decimation R and M = 1; ν is frequency in units of the output rate.

::: step
answer: R^N

### What is its DC gain?
:::

::: step
answer: N*20*log(abs(sin(pi*v)/(R*sin(pi*v/R))))

### What is its response at ν relative to DC, in dB?
:::

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

### How many bits does it add?
:::

::: right
Right. Gain, growth and droop all scale with N, the price of deeper alias rejection.
:::
:::

::: check
kind: python

## Write `cic(x, R, N, bits)`: an N-stage CIC decimator by R (M = 1) on integer input x, with integrators and combs computed modulo $2^\text{bits}$ and the output read as a signed number; return the list of outputs (one per R inputs, taken at n = R − 1, 2R − 1, …).

```python starter
def cic(x, R, N, bits):
    return []
```

```python solution
def cic(x, R, N, bits):
    mod = 1 << bits
    ints = [0] * N
    combs = [0] * N
    out = []
    for n, v in enumerate(x):
        a = v
        for i in range(N):
            ints[i] = (ints[i] + a) % mod
            a = ints[i]
        if n % R == R - 1:
            c = a
            for i in range(N):
                c, combs[i] = (c - combs[i]) % mod, c
            out.append(c - mod if c >= mod // 2 else c)
    return out
```

```python tests
import random
r = random.Random(2)
x = [r.randint(-128, 127) for _ in range(400)]
R, N = 8, 3
h = [1]
for _ in range(N):
    h = [sum(h[j] for j in range(max(0, i - R + 1), min(i, len(h) - 1) + 1)) for i in range(len(h) + R - 1)]
direct = [sum(h[k] * x[n - k] for k in range(len(h)) if n - k >= 0) for n in range(R - 1, len(x), R)]
assert cic(x, R, N, 17) == direct, "With 8 + 9 = 17 bits the wrapped arithmetic matches the direct filter exactly."
assert cic(x, R, N, 14) != direct, "Too few bits and the outputs are wrong."
assert cic([1] * 64, 8, 3, 17)[-1] == 512, "DC gain 8³ = 512."
```

::: right
Right. The integrators' values are meaningless on their own; only the combs' differences matter, and those fit.
:::
:::

::: explain
## Why is a CIC filter used for the first stage of high-ratio decimation but not for the last?

::: model
A CIC filter needs only adders and registers, no multipliers, and its integrators run at the full input rate while its combs run at the low output rate, so it is the cheapest possible way to reduce a very high sample rate by a large factor. Its nulls fall exactly on the frequency bands that would alias onto DC, so it rejects aliasing well near the band of interest. But its passband droops and its transition is very gradual, so it can't provide a sharp band edge. The last stages, at a much lower rate where multipliers are affordable, use halfband and FIR filters to sharpen the edge and correct the droop.
:::
:::

::: practice
::: check
given: N = 2 | 5; R = 10 | 32
answer: N*log(R)/log(2)
unit: bits
tolerance: 0.5%

## Register growth for N = {N}, R = {R}?

::: misses
- [any] $N\log_2R$.
:::
:::

::: check
given: N = 3 | 4; R = 8 | 20; p = 0.08 | 0.15
answer: -N*20*log(abs(sin(pi*p)/(R*sin(pi*p/R))))
unit: dB
tolerance: 0.5%

## CIC droop at {p} of the output rate, N = {N}, R = {R}?

::: misses
- [any] $-N \cdot 20\log_{10}|\sin\pi\nu/(R\sin(\pi\nu/R))|$.
:::
:::

::: check
given: K = 3 | 5 | 8 | 10 | 12 | 16
answer: 2*K + 1
unit: taps
tolerance: 0

## Nonzero taps of a (4K − 1)-tap halfband, K = {K}?

::: misses
- [any] 2K + 1.
:::
:::
:::
