CIC Filters and Sample-Rate Conversion Chains
A PDM microphone or a sigma-delta ADC delivers a 1-bit or few-bit stream at a megahertz or more; the application wants 16-bit samples at or less. The first decimation stage runs at the full input rate, where every multiplication is expensive. The cascaded integrator-comb (CIC) filter, introduced by Hogenauer in 1981, does that stage with no multiplications at all: only adders, subtractors and registers. Its response is not flat, so a short compensation FIR at the low rate follows it. This lesson derives the CIC filter from the moving average, sizes its registers, computes its droop and alias rejection, and builds the compensator. The moving average itself is in Moving Average; decimation and polyphase filters are in Multirate.
From a moving sum to integrator and comb
A moving sum of samples, , has the transfer function
the product of an integrator (a running sum, ) and a comb (a difference over samples). Cascade of them for more attenuation, then decimate by . By the noble identity, the comb before the downsampler equals after it, so the combs run at the low rate with a delay of only samples (, the differential delay, is 1 or 2):
The DC gain is , so the registers grow by bits.
The surprising part: overflow is allowed
The integrators have a pole at : on their own they are unstable and their values grow without bound for any input with a DC component. Yet the CIC filter works, because the total filter is an FIR with a bounded output, and in two's-complement arithmetic with wrap-around the combs' differences undo the integrators' overflows exactly, as long as every register has at least bits. (Saturation arithmetic would break this, the opposite of the advice for IIR filters in Filter Structures.)
Worked example: a 4-stage CIC decimating by 16
Given: , , ; 12-bit signed input; , so ; band of interest to .
Find: the gain, the register width, a bit-exact check of the wrap-around claim; the droop at and the rejection of the bands that alias onto to ; a compensator.
Solve: gain , growth bits, registers bits.
import numpy as np
R, M, N = 16, 1, 4 # decimation ratio, differential delay, number of stages
B_in = 12 # input word: 12-bit signed ADC codes
B_out = B_in + int(np.ceil(N * np.log2(R * M)))
print(f"DC gain (RM)^N = {(R * M)**N}, bit growth {N * np.log2(R * M):.0f} bits -> registers of {B_out} bits")
def wrap(v, bits): # two's-complement wrap-around in a register of 'bits' bits
m = 1 << bits
return ((v + (m >> 1)) % m) - (m >> 1)
rng = np.random.default_rng(2)
x = rng.integers(-2048, 2048, size=R * 200) # full-scale 12-bit input
# integrators at the high rate (with wrap-around: they overflow all the time)
acc = [0] * N
integ = np.empty(len(x), dtype=np.int64)
for n, v in enumerate(x):
s = int(v)
for k in range(N):
acc[k] = wrap(acc[k] + s, B_out)
s = acc[k]
integ[n] = s
# downsample, then combs at the low rate: c[m] = d[m] - d[m - M]
d = integ[R - 1::R]
for k in range(N):
prev = np.concatenate((np.zeros(M, dtype=np.int64), d[:-M]))
d = np.array([wrap(int(a) - int(b), B_out) for a, b in zip(d, prev)])
# reference: N cascaded moving sums of length R*M, then keep every R-th sample
ref = x.astype(np.int64)
for k in range(N):
ref = np.convolve(ref, np.ones(R * M, dtype=np.int64))[:len(x)]
ref = ref[R - 1::R]
print("CIC with wrapping integrators equals the boxcar cascade:", bool(np.array_equal(d, ref)))
print(f"largest |output| = {np.abs(d).max()} < 2^{B_out - 1} = {2**(B_out - 1)}; multiplications used: 0")DC gain (RM)^N = 65536, bit growth 16 bits -> registers of 28 bits
CIC with wrapping integrators equals the boxcar cascade: True
largest |output| = 37891552 < 2^27 = 134217728; multiplications used: 0
The integrators wrap around constantly, yet the output equals the reference (four cascaded 16-sample moving sums followed by decimation) exactly, and it never needs more than 28 bits. No multiplier was used. In hardware the output is then shifted right by 16 bits to undo the gain (or kept wider if the extra resolution is wanted, as in sigma-delta converters).
Now the frequency response. After decimation, the bands fold onto the band of interest; the CIC's nulls sit exactly at , which is why it rejects them well:
import numpy as np
from scipy import signal
R, M, N = 16, 1, 4
fs_in = 1.024e6 # e.g. a sigma-delta modulator or a PDM microphone stream
fs_out = fs_in / R # 64 kSa/s
def cic_gain(f): # |H(f)| normalised to 1 at DC
x = np.pi * np.asarray(f, dtype=float) / fs_in
with np.errstate(invalid="ignore", divide="ignore"):
g = np.abs(np.sin(R * M * x) / (R * M * np.sin(x))) ** N
return np.where(x == 0, 1.0, g)
fp = 8000.0 # band of interest: 0-8 kHz
print(f"fs_out = {fs_out / 1e3:.0f} kSa/s; droop at {fp / 1e3:.0f} kHz: {20 * np.log10(cic_gain(fp)):.2f} dB")
for k in (1, 2, 3): # the bands that alias onto 0..fp after decimation
lo = k * fs_out - fp
print(f" worst gain in the band {lo / 1e3:5.0f} .. {(k * fs_out + fp) / 1e3:4.0f} kHz (aliases onto 0-8 kHz): "
f"{20 * np.log10(cic_gain(lo)):6.1f} dB")
# Compensation FIR at the low rate: inverse of the droop up to fp, then down to zero
fgrid = np.linspace(0, fs_out / 2, 65)
fe = 1.25 * fp # correct a little beyond the band edge,
ramp = np.clip((3 * fp - fgrid) / (3 * fp - fe), 0, 1) # then fall to zero at 24 kHz
target = np.where(fgrid <= fe, 1 / cic_gain(fgrid), ramp / cic_gain(fe))
h = signal.firwin2(31, fgrid, target, fs=fs_out)
for f in (2000, 4000, 6000, 8000):
_, Hc = signal.freqz(h, worN=[f], fs=fs_out)
print(f" {f / 1e3:.0f} kHz: CIC {20 * np.log10(cic_gain(f)):6.2f} dB, CIC + 31-tap compensator {round(20 * np.log10(cic_gain(f) * abs(Hc[0])), 2) + 0.0:6.2f} dB")fs_out = 64 kSa/s; droop at 8 kHz: -0.89 dB
worst gain in the band 56 .. 72 kHz (aliases onto 0-8 kHz): -68.3 dB
worst gain in the band 120 .. 136 kHz (aliases onto 0-8 kHz): -94.2 dB
worst gain in the band 184 .. 200 kHz (aliases onto 0-8 kHz): -108.0 dB
2 kHz: CIC -0.06 dB, CIC + 31-tap compensator 0.03 dB
4 kHz: CIC -0.22 dB, CIC + 31-tap compensator 0.03 dB
6 kHz: CIC -0.50 dB, CIC + 31-tap compensator 0.03 dB
8 kHz: CIC -0.89 dB, CIC + 31-tap compensator 0.00 dB
Check: : , to the fourth power , i.e. . The first alias band ( to ) is suppressed by at least , the next ones by more. The 31-tap compensator, designed with firwin2 as the inverse of the droop up to and rolling off above, makes the chain flat to within over to . It runs at , where 31 multiplications per sample are cheap, and it can also decimate by 2 to , provided its stopband removes the content above , which would otherwise fold onto to .
Design rules
- Order sets the alias rejection (about times the rejection of a single stage); 3 to 5 is typical.
- Ratio can be large (8 to several hundred) and even programmable; the register width must be sized for the largest .
- Droop grows with the band of interest relative to : keep the band to a small fraction (here 1/8) of the output rate, or compensate.
- Register pruning: the least significant bits of later stages can be discarded (Hogenauer's analysis gives how many) to save hardware.
- Interpolating CIC: the mirror image (combs at the low rate, upsample by zero insertion, integrators at the high rate) is used in DAC and transmitter chains.
Guided lab: a PDM microphone chain
Hand calculation. A PDM microphone clocked at with , gives with of droop at and more than of rejection for noise folding into the band.
Simulation. In the second program, try and : the droop and the alias rejection both change by about the factor in dB.
What you would measure. On a microcontroller or FPGA board with a PDM microphone input, implement the CIC and the compensator, play a sine sweep from a small loudspeaker at a moderate level, and compare the recorded level at , and with and without the compensator. Expect the uncompensated reading about below the one, and the compensated readings level within about ; the microphone's and loudspeaker's own responses add several dB, so measure them with a reference microphone first or compare the two filter versions on the same recording.
Hazards and safe setup. USB-powered board, a small loudspeaker driven at low level; no mains-powered amplifier is opened. Protect your hearing: keep sweep levels moderate.
Pitfalls
- Using saturation arithmetic or too-narrow registers in the integrators (the output is then wrong).
- Forgetting the gain, so the output scale is off by that factor ( in the example).
- Putting the band of interest too close to : the droop and aliasing grow fast.
- Skipping the compensator in measurement applications that need a flat response.
- Expecting a CIC filter to act as a sharp low-pass: it is a cascade of moving averages with nulls, good only for the first, coarse stage.
Practice
- A CIC filter has , , and 16-bit input. How wide must its registers be?
- Where are the nulls of a CIC decimator with , at ?
- Why can the combs run at the low rate with delay instead of ?
Answer
- Growth bits: bits.
- At multiples of (the output rate), exactly where the alias bands are centred.
- Noble identity: followed by equals followed by .
Test yourself
A CIC filter is built from
- multipliers only
- integrators, a rate change and combs, with no multipliers
- biquads
- a polyphase FIR
With , , , the DC gain and bit growth are
- 16 and 4 bits
- 64 and 6 bits
- 65536 and 16 bits
- 256 and 8 bits
The integrators of a CIC filter overflow. The output is still correct if
- the arithmetic saturates
- the arithmetic wraps around (two's complement) and the registers have bits
- the input is small
- never
Why does a compensation FIR usually follow a CIC decimator?
- to add gain
- to correct the passband droop and sharpen the cutoff at the low rate
- to remove DC
- to make it stable