Real time
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.
Numbers as integers
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.
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.
One filter tick
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.
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.
What rounding does
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.
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.
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.
Code and budget
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.
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.
The working
# step 1/10 · realtime 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
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.