S335 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 2−(n−1): one more bit per iteration.
  • The dashed line is the error of a Q1.15 number's rounding, 2−16: 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?

Name it

The blocks

  • Q1.15 FIR: quantize coefficients with rounding, ⌊215h+0.5⌋; multiply-accumulate 16-bit samples into a wide accumulator exactly; round back to Q1.15 by adding 214 and shifting right 15. The error against exact arithmetic is under about one LSB.
  • CIC: M integrators y+=x at the input rate, keep every Rth value, then M combs y=x−xprev; registers of Bin+Mlog2⁡R bits, all arithmetic modulo 2B. 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 ±arctan⁡2−i toward z = 0 using x−=dy2−i and y+=dx2−i; the vector grows by K=∏1+2−2i≈1.64676, 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 214, 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?