ENع
←Fixed point and real time
Lesson 45 · Digital filter design

Fixed point and real-time filtering

16 min10 steps5 questionsAdvanced
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 45 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Q7: 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)
  3. 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)
  4. 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 accumulator
  5. Add, 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 enters
  6. Running 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)
  7. 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 saturated
  8. Stuck 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/32768
  9. Fixed 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)
  10. 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

Quiz Unlocks after the last step.