S3 · Lesson 35 · DSP hardware lab
A fixed-point FIR, a CIC decimator in wrapping arithmetic, and CORDIC, bit-exact in Python
DSP hardware is specified in floating point and built in fixed point, and the gap between them is where bugs live: coefficients that round to the wrong value, accumulators that overflow, rounding that biases the output. This lab writes bit-exact models of three classic blocks from lesson 31 and checks each against an exact reference: a Q1.15 FIR filter, a CIC decimator whose registers are allowed to wrap, and a CORDIC rotator that computes sines and cosines with shifts and adds.
- The line is CORDIC's worst error computing a cosine after n iterations, about : one more bit per iteration.
- The dashed line is the error of a Q1.15 number's rounding, : about 17 iterations match it.
- Each iteration costs two shift-and-adds and a small table lookup, so a 16-bit CORDIC is 16 adder delays or 16 pipeline stages.
Predict
A CIC decimator with 3 stages and R = 8 takes 12-bit samples. Its integrators overflow and wrap around constantly. Does the output come out right?
The integrators compute running sums modulo , and the combs take differences of those sums, which are exact modulo as well. As long as the true output fits in B bits, the wrapped arithmetic gives exactly the right answer. With B = 21 the output's range fits; with 19 bits it doesn't, and the output is wrong. The lab shows both.
Name it
The blocks
- Q1.15 FIR: quantize coefficients with rounding, ; multiply-accumulate 16-bit samples into a wide accumulator exactly; round back to Q1.15 by adding and shifting right 15. The error against exact arithmetic is under about one LSB.
- CIC: M integrators at the input rate, keep every Rth value, then M combs ; registers of bits, all arithmetic modulo . The result equals filtering by the moving average of length R applied M times, without dividing.
- CORDIC (rotation mode): starting from (1, 0) with angle z, each step i rotates by toward z = 0 using and ; the vector grows by , divided out at the end (or folded into the starting vector).
Check
Write q15(h): round coefficients to Q1.15 integers (clamped to −32768..32767), and fir_fixed(x, hq): filter Q1.15 integer samples with integer coefficients, accumulating exactly and rounding each output back to Q1.15 (add , shift right 15).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write cic(x, M, R, B): an M-stage CIC decimator by R on integer samples, with every register B bits wide and wrapping (two's complement), returning the output samples as signed integers (one per R inputs, taken after inputs R − 1, 2R − 1, …).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write cordic(theta, n): rotate (1, 0) toward angle theta (|theta| < 1.5) with n CORDIC iterations and return (cos, sin) after dividing out the gain, and the gain K.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why can a CIC filter's integrators overflow without corrupting its output?
Two's complement arithmetic with B bits is arithmetic modulo : sums and differences are exact except for multiples of . The integrators accumulate running sums that wrap around many times, and the combs then subtract one wrapped running sum from an earlier one. The difference of two values that are each correct modulo is also correct modulo , so the wraps cancel. If the true output is guaranteed to fit within the B-bit range, which sets , the value that remains modulo is exactly the true output.