Fixed point and real-time filtering
About this lesson
A filter you designed in floating point still has to run on real hardware, often a small processor with no floating-point unit and a hard deadline: one output every 125 µs. Follow the smoother y[n] = 0.9·y[n−1] + 0.1·x[n] as it becomes integer arithmetic in the Q7 format. You will round its coefficients, run one multiply-accumulate tick by hand, and see what finite words do to it: overflow that wraps around, a dead band, and a zero-input limit cycle. Then you will write the same integer filter in Python 3. It builds on the moving average and the IIR lessons of this module, and the Circuits lessons on filters and Bode plots.
A deadline every 125 µs
A filter runs in real time when each output is ready before the next input arrives. Here samples come 8000 times a second, one every 125 µs. A 16 MHz processor then has 2000 clock cycles per sample.
fs = 8000 samples per second Ts = 1 / fs = 125 µs clock 16 MHz: 16 000 000 × 0.000125 = 2000 cycles per sample read x[n] -> compute y[n] -> write y[n] all inside one Ts, every Ts, forever late once = a lost or repeated sample
Read the lesson step by step
A deadline every 125 µs
A filter runs in real time when each output is ready before the next input arrives. Here samples come 8000 times a second, one every 125 µs. A 16 MHz processor then has 2000 clock cycles per sample.
fs = 8000 samples per second Ts = 1 / fs = 125 µs clock 16 MHz: 16 000 000 × 0.000125 = 2000 cycles per sample read x[n] -> compute y[n] -> write y[n] all inside one Ts, every Ts, forever late once = a lost or repeated sampleQ7: a fraction stored as an integer
Many small processors have no floating point. Q7 stores a number as an 8-bit integer k and reads it as k/128. The top bit weighs −1, the others ½, ¼ … 1/128. So 01000000 = 64 means 0.5.
Q7 = signed 8-bit integer k, value k / 128 bit weights −1 ½ ¼ ⅛ 1/16 1/32 1/64 1/128 0.5 = 0 1 0 0 0 0 0 0 = 64 / 128 range −128 .. 127 -> −1 .. 0.9921875 step 1/128 = 0.0078125 (two's complement; Q15 = 16 bits, step 1/32768)Rounding the coefficients
Our smoother is y[n] = 0.9·y[n−1] + 0.1·x[n]. In Q7, 0.9 × 128 = 115.2 rounds to 115, which means 0.8984375; 0.1 becomes 13, which means 0.1015625. We run a slightly different filter from the one we designed.
a = 0.9 -> 0.9 × 128 = 115.2 -> 115 115 / 128 = 0.8984375 b = 0.1 -> 0.1 × 128 = 12.8 -> 13 13 / 128 = 0.1015625 115 = 01110011 (on the lamps) the pole moved from 0.9 to 0.8984375 115 + 13 = 128, so the gain at DC is still 1 (coefficient quantization)Multiply: the binary point moves
Take sample n = 1, with y[0] = 7 and x[1] = 64. Q7 times Q7 gives Q14: 7 + 7 = 14 fraction bits, up to 16 bits wide. 115 × 7 = 805 and 13 × 64 = 832. Nothing is lost yet.
y[0] = 7 (0.055) x[1] = 64 (0.5) Q7 × Q7 = Q14 (value = integer / 16384) A·y[0] = 115 × 7 = 805 B·x[1] = 13 × 64 = 832 exact: no bits dropped while the products sit in a wide accumulatorAdd, round, shift back
Add the products: 805 + 832 = 1637. Add 64 (half of 128), then shift right 7 bits, which divides by 128 and rounds: 1701 >> 7 = 13. So y[1] = 13, meaning 0.102. The ideal filter gives 0.095.
acc = 805 + 832 = 1637 (Q14) + 64 = 1701 (+ half a step) >> 7 = 13 (÷128, floor) 1637 / 128 = 12.79 -> rounds to 13 y[1] = 13 / 128 = 0.1016 ideal y[1] = 0.0950 the rounding is the only place error entersRunning it: close, but stuck
Repeat the tick every sample. The Q7 output (stairs) follows the ideal filter, then stops at 60 (0.469) instead of 64 (0.5). Near the end, each change 0.1·(64 − y) is under half a step, so it rounds away.
x[n] = 64 (0.5) for n = 0 .. 39 Q7 y[n]: 7, 13, 18, 23, 27, 31, 34, 37 ... 60 from n = 23 on ideal: -> 0.5 (64) stuck where the change per tick rounds to 0: |64 − y| ≤ 0.5 / (1 − 0.8984375) = 4.92 60 .. 68 all stay put (a dead band)Overflow wraps around
Eight bits hold only −128 … 127. 100 + 50 = 150 does not fit: two's complement wraps it to 150 − 256 = −106, so 0.781 + 0.391 gives −0.828. Saturation clamps to 127 (0.992) instead: wrong, but close.
100 + 50 = 150 > 127 wrap: 150 − 256 = −106 = 10010110 the sign bit flipped: 0.781 + 0.391 -> −0.828 saturate: 127 = 01111111 -> 0.992 this smoother cannot overflow: |y| ≤ |x| (gain 1); filters with gain > 1 must be scaled down or saturatedStuck above zero: a limit cycle
At n = 40 the input drops to 0. The ideal output decays toward 0, but Q7 stops at 4: 115 × 4 = 460, and (460 + 64) >> 7 = 4. With zero input, rounding alone keeps it alive: a zero-input limit cycle.
x[n] = 0 from n = 40 Q7: 60 -> 54 -> 49 -> 44 -> 40 -> 36 -> 32 ... -> 4, 4, 4 ideal: decays to 0.007 by n = 79 dead band: |y| ≤ 0.5 / (1 − |a|) steps = 0.5 / (1 − 0.8984375) = 4.92 -> any |y| ≤ 4 never moves Q15: same 4.92 steps, but steps of 1/32768Fixed point in Python
Python 3 integers never overflow, so the code must copy the hardware. >> on a negative integer floors, like an arithmetic shift. This loop gives every stair in the plot. Clip to −128 … 127 to model saturation.
A, B = 115, 13 # Q7 y = 0 for x in X: y = (A*y + B*x + 64) >> 7 # saturation, as the hardware does it: y = max(-128, min(127, y)) >>> -5 >> 1 -3 (floor, like -5 // 2) numpy int8 arrays wrap silently instead: >>> np.array([300]).astype(np.int8) array([44], dtype=int8) (plain Python 3; numpy only for the last line)Fitting in the budget
Each sample costs 2 multiplies, 2 adds and a shift, far below 2000 cycles on a processor with a hardware multiplier. A 64-tap FIR filter costs 64 multiply-accumulates. Wider words (Q15, Q31) shrink every rounding error.
per sample (this filter): 2 multiplies, 2 adds, 1 shift, 1 clamp per sample (N-tap FIR): N multiply-accumulates budget 2000 cycles at 16 MHz, 8000 samples/s word step dead band here Q7 1/128 4.92 × 1/128 Q15 1/32768 4.92 × 1/32768 Q31 1/2147483648
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Audio arrives at 48 000 samples per second and the processor runs at 96 MHz. How many clock cycles does it have per sample?
Cycles per sample = clock rate / sample rate = 96 000 000 / 48 000 = 2000. Every output must be finished inside that budget, every sample.
02In Q7 (a signed 8-bit integer k read as k/128), which integer stores the coefficient 0.75?
0.75 × 128 = 96, an exact integer, so 0.75 is stored with no rounding error. 192 would not fit: Q7 only holds −128 to 127.
03Two Q7 numbers are multiplied in a wide accumulator. How many fraction bits does the product have, and how do you get back to Q7?
Fraction bits add when you multiply: 7 + 7 = 14 (Q14). Adding half a step (64) and shifting right by 7 divides by 128 and rounds to the nearest Q7 value.
04An 8-bit two's-complement adder computes 100 + 50 without saturation. What does it store?
150 does not fit in −128 … 127, so it wraps around: 150 − 256 = −106. A saturating adder would store 127 instead, which is much closer to the truth.
05The filter y[n] = Q[0.875·y[n−1]] rounds every product to whole steps. With zero input, which outputs can stay stuck forever?
The dead band is |y| ≤ 0.5 / (1 − |a|) = 0.5 / 0.125 = 4 steps. There 0.875·y rounds back to y (for example 0.875 × 4 = 3.5, which rounds up to 4), so the output never moves: a zero-input limit cycle.
Glossary
- Real time
- Processing that keeps up with its input: each output is ready before the next sample arrives, every sample.
- Fixed point
- Storing a fractional number as an integer with an implied scale, for example k/128, so integer hardware can do the arithmetic.
- Q format (Qm.n)
- A name for a fixed-point layout: n fraction bits (and m integer bits) plus a sign bit. Q7 is 8 bits with 7 fraction bits; Q15 is 16 bits with 15.
- Coefficient quantization
- Rounding a designed coefficient (such as 0.9) to the nearest value the word can hold (115/128), which moves the filter's poles and zeros slightly.
- Multiply-accumulate (MAC)
- Multiplying two numbers and adding the product to a wide running sum, the core operation of FIR and IIR filters.
- Overflow, wrap-around and saturation
- A result too big for the word. Two's complement wraps it to the other end of the range; saturation clamps it to the largest value instead.
- Zero-input limit cycle
- An output that keeps a nonzero value or keeps oscillating after the input becomes zero, caused by rounding inside a feedback loop.
- Dead band
- The range of outputs a rounded recursive filter cannot leave on its own; for a first-order filter |y| ≤ 0.5/(1 − |a|) steps.
Notes and sources
- The sample rate 8000 per second and the 16 MHz clock are example values; the filter, the Q7 rounding rule and every output value are computed from this model. An 8-bit word is used so the rounding effects are big enough to see.
- The motion is slowed down a great deal: in the real system one sample passes every 125 µs.
- Rounding here is "add half, then shift right", which rounds halves upward, as in the Q-format and Oppenheim & Schafer examples. Truncation toward zero (magnitude truncation) instead removes zero-input limit cycles in a first-order filter, at the cost of a larger error.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., ch. 6 (fixed-point arithmetic, coefficient quantization, zero-input limit cycles), course text of MIT OCW 6.341
- M. Wickert, ECE 5650/4650 Modern DSP, chapter 6 notes: overflow and §6.9 zero-input limit cycles, dead band (University of Colorado Colorado Springs)
- Q (number format) (Wikipedia): signed Qm.n range, multiplication and rounding by adding half
- Fixed-point arithmetic (Wikipedia): implied scale, double-width products, overflow, processors without an FPU
- Saturation arithmetic (Wikipedia): saturation versus wrap-around in DSP
- Python documentation: Built-in Types (x >> n is floor division by 2**n; integers have unlimited precision)
- NumPy documentation: Data types (fixed-size integers overflow; 300 cast to int8 becomes 44)
- ARM CMSIS-DSP library documentation (q7, q15 and q31 fixed-point filter functions)
Quiz Unlocks after the last step.