TExcelTAD
Home › Signals, Systems & Digital Filters for Engineers › CIC Filters and Sample-Rate Conversion Chains

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 64 kSa/s64\ \text{kSa/s} 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 D=RMD = RM samples, y[n]=∑k=0D−1x[n−k]y[n] = \sum_{k=0}^{D-1} x[n-k], has the transfer function

H1(z)=∑k=0D−1z−k=1−z−D1−z−1H_1(z) = \sum_{k=0}^{D-1} z^{-k} = \frac{1 - z^{-D}}{1 - z^{-1}}

the product of an integrator 1/(1−z−1)1/(1 - z^{-1}) (a running sum, y[n]=y[n−1]+x[n]y[n] = y[n-1] + x[n]) and a comb 1−z−D1 - z^{-D} (a difference over DD samples). Cascade NN of them for more attenuation, then decimate by RR. By the noble identity, the comb 1−z−RM1 - z^{-RM} before the downsampler equals 1−z−M1 - z^{-M} after it, so the combs run at the low rate with a delay of only MM samples (MM, the differential delay, is 1 or 2):

x I 1/(1-z^-1) I 1/(1-z^-1) I 1/(1-z^-1) I 1/(1-z^-1) down R C 1 - z^-M C 1 - z^-M C 1 - z^-M C 1 - z^-M y integrators at fs_in (registers of Bin + N log2(RM) bits) combs at fs_in / R H(z) = [ (1 - z^-RM) / (1 - z^-1) ]^N : only adders, subtractors and delays
H(z)=(1−z−RM1−z−1)N,∣H(f)∣=∣sin⁡(πRMf/fs,in)sin⁡(πf/fs,in)∣NH(z) = \left(\frac{1 - z^{-RM}}{1 - z^{-1}}\right)^N, \qquad \lvert H(f) \rvert = \left\lvert \frac{\sin(\pi R M f/f_{s,\text{in}})}{\sin(\pi f/f_{s,\text{in}})} \right\rvert^N

The DC gain is (RM)N(RM)^N, so the registers grow by Nlog⁡2(RM)N\log_2(RM) bits.

The surprising part: overflow is allowed

The integrators have a pole at z=1z = 1: 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 Bout=Bin+⌈Nlog⁡2(RM)⌉B_\text{out} = B_\text{in} + \lceil N\log_2(RM) \rceil 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: R=16R = 16, M=1M = 1, N=4N = 4; 12-bit signed input; fs,in=1.024 MSa/sf_{s,\text{in}} = 1.024\ \text{MSa/s}, so fs,out=64 kSa/sf_{s,\text{out}} = 64\ \text{kSa/s}; band of interest 00 to 8 kHz8\ \text{kHz}.

Find: the gain, the register width, a bit-exact check of the wrap-around claim; the droop at 8 kHz8\ \text{kHz} and the rejection of the bands that alias onto 00 to 8 kHz8\ \text{kHz}; a compensator.

Solve: gain (16)4=65 536(16)^4 = 65\,536, growth 4log⁡216=164 \log_2 16 = 16 bits, registers 12+16=2812 + 16 = 28 bits.

python · try it yourself
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 kfs,out±8 kHzk f_{s,\text{out}} \pm 8\ \text{kHz} fold onto the band of interest; the CIC's nulls sit exactly at kfs,outk f_{s,\text{out}}, which is why it rejects them well:

python · try it yourself
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: ∣H(8 kHz)∣=∣sin⁡(π⋅16⋅8/1024)/(16sin⁡(π⋅8/1024))∣4\lvert H(8\ \text{kHz}) \rvert = \lvert \sin(\pi \cdot 16 \cdot 8/1024)/(16 \sin(\pi \cdot 8/1024)) \rvert^4: sin⁡(0.3927)/(16×0.024541)=0.38268/0.39266=0.97459\sin(0.3927)/(16 \times 0.024541) = 0.38268/0.39266 = 0.97459, to the fourth power 0.90220.9022, i.e. −0.89 dB-0.89\ \text{dB}. The first alias band (5656 to 72 kHz72\ \text{kHz}) is suppressed by at least 68.3 dB68.3\ \text{dB}, the next ones by more. The 31-tap compensator, designed with firwin2 as the inverse of the droop up to 10 kHz10\ \text{kHz} and rolling off above, makes the chain flat to within 0.03 dB0.03\ \text{dB} over 00 to 8 kHz8\ \text{kHz}. It runs at 64 kSa/s64\ \text{kSa/s}, where 31 multiplications per sample are cheap, and it can also decimate by 2 to 32 kSa/s32\ \text{kSa/s}, provided its stopband removes the content above 24 kHz24\ \text{kHz}, which would otherwise fold onto 00 to 8 kHz8\ \text{kHz}.

Design rules

  • Order NN sets the alias rejection (about NN times the rejection of a single stage); 3 to 5 is typical.
  • Ratio RR can be large (8 to several hundred) and even programmable; the register width must be sized for the largest RR.
  • Droop grows with the band of interest relative to fs,outf_{s,\text{out}}: 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 1.024 MHz1.024\ \text{MHz} with R=16R = 16, N=4N = 4 gives 64 kSa/s64\ \text{kSa/s} with −0.89 dB-0.89\ \text{dB} of droop at 8 kHz8\ \text{kHz} and more than 68 dB68\ \text{dB} of rejection for noise folding into the band.

Simulation. In the second program, try N=3N = 3 and N=5N = 5: the droop and the alias rejection both change by about the factor N/4N/4 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 22, 44 and 8 kHz8\ \text{kHz} with and without the compensator. Expect the uncompensated 8 kHz8\ \text{kHz} reading about 0.9 dB0.9\ \text{dB} below the 2 kHz2\ \text{kHz} one, and the compensated readings level within about ±0.5 dB\pm 0.5\ \text{dB}; 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 (RM)N(RM)^N gain, so the output scale is off by that factor (2162^{16} in the example).
  • Putting the band of interest too close to fs,out/2f_{s,\text{out}}/2: 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

  1. A CIC filter has R=32R = 32, M=1M = 1, N=5N = 5 and 16-bit input. How wide must its registers be?
  2. Where are the nulls of a CIC decimator with R=16R = 16, M=1M = 1 at fs,in=1.024 MHzf_{s,\text{in}} = 1.024\ \text{MHz}?
  3. Why can the combs run at the low rate with delay MM instead of RMRM?
Answer
  1. Growth 5log⁡232=255 \log_2 32 = 25 bits: 16+25=4116 + 25 = 41 bits.
  2. At multiples of fs,in/(RM)=64 kHzf_{s,\text{in}}/(RM) = 64\ \text{kHz} (the output rate), exactly where the alias bands are centred.
  3. Noble identity: 1−z−RM1 - z^{-RM} followed by ↓R\downarrow R equals ↓R\downarrow R followed by 1−z−M1 - z^{-M}.

Test yourself

A CIC filter is built from

  1. multipliers only
  2. integrators, a rate change and combs, with no multipliers
  3. biquads
  4. a polyphase FIR

With R=16R = 16, M=1M = 1, N=4N = 4, the DC gain and bit growth are

  1. 16 and 4 bits
  2. 64 and 6 bits
  3. 65536 and 16 bits
  4. 256 and 8 bits

The integrators of a CIC filter overflow. The output is still correct if

  1. the arithmetic saturates
  2. the arithmetic wraps around (two's complement) and the registers have Bin+Nlog⁡2(RM)B_\text{in} + N\log_2(RM) bits
  3. the input is small
  4. never

Why does a compensation FIR usually follow a CIC decimator?

  1. to add gain
  2. to correct the passband droop and sharpen the cutoff at the low rate
  3. to remove DC
  4. to make it stable