D5 · Lesson 12 · Fixed-point arithmetic and real-time DSP
Representing fractions as integers in Q formats, the resolution and range of Q15, what happens to products and long sums, guard bits and saturation, how rounding a coefficient can move a pole onto the unit circle, and budgeting a processor's cycles per sample
The filters in this course look exact on paper, but the chip in a hearing aid or a motor drive has no floating-point unit and a few thousand clock cycles between samples. It stores every number as an integer with an implied binary point, and every multiplication doubles the number of bits, every sum risks overflow, and every coefficient is rounded. Most of the time the effects are a little extra noise. Sometimes rounding a single coefficient turns a smoothing filter into one that never forgets, or into an oscillator.
- A one-pole smoother meant to have a = 0.999, a time constant of 999.5 samples, with its coefficient rounded to B fractional bits: .
- The stems show the time constant the rounded coefficient actually gives. With few bits it's far off, and at 8 bits a rounds to exactly 1: the filter becomes an integrator that never decays.
- Coefficients close to 1, which is where poles of sharp or slow filters live, need the most bits.
Predict
A smoothing filter uses a = 0.999, stored in 8 fractional bits. What does the coefficient become?
0.999 × 256 = 255.74, which rounds to 256, so = 256/256 = 1. The filter no longer decays at all, and since (1 − a) also rounds to 0, it ignores its input entirely. With 16 bits, = 65,470/65,536 = 0.99899 and the time constant is 992 samples, close to the intended 999.5.
Name it
Q formats
A Qm.n number is an integer with n fractional bits: its value is the integer divided by . Q15 (Q1.15) uses 16-bit signed integers for values from −1 to , with resolution . To store x, use round().
Products and sums
Multiplying two Q15 numbers gives a Q30 product in 32 bits; shifting right by 15 (with rounding) brings it back to Q15. Adding N products can grow the result by a factor of N, so the accumulator needs guard bits above the product's width: DSP chips have 40-bit accumulators for that reason. When a result doesn't fit, saturating to the largest value is far better than wrapping around, which turns a large positive number into a large negative one.
Coefficient rounding
Rounding moves a filter's poles and zeros. Poles near the unit circle, as in slow smoothers and sharp IIR filters, move the most relative to their distance from the circle, which is why high-order IIR filters are built from biquads (lesson 5), and why sensitive filters are checked with their rounded coefficients before they're built.
Real-time budgets
A processor at clock f_clk handling one sample every has cycles per sample for everything: the filters, the control logic and the input and output. Work that doesn't fit has to be simplified, spread across samples or moved to faster hardware.
Worked example
A 64-tap Q15 FIR filter runs on a 200 MHz DSP at 48 kHz. How many guard bits does its accumulator need, and how much of the cycle budget does it use at one tap per cycle?
Show the solution
- Guard bits.
- Accumulator width. A 32-bit product sum then needs 38 bits, so a 40-bit accumulator fits.
- Cycle budget. per sample.
- Share of the budget. At one tap per cycle the filter takes 64 cycles:
Answer 6 guard bits (a 40-bit accumulator fits), and the filter uses 1.5% of the cycle budget.
Worked example
Store 0.3 and −0.75 in Q15. What does 0.3 × −0.75 come to?
Show the solution
- Store 0.3.
- Store −0.75.
- Multiply. The product is in Q30:
- Shift back to Q15. Shifted right by 15 it's −7372.5, which rounds to −7373 (or −7372, depending on the rounding rule).
- Read the result. against the exact −0.225.
Answer the product comes out as −7373 in Q15, which is −0.22501, against the exact −0.225.
Worked example
Multiply −1 by −1 in Q15. What goes wrong, and what should the processor do?
Show the solution
- −1 in Q15. It's 0x8000, which is −32768.
- The product. It's , which shifted right 15 bits is 32768: one more than the largest Q15 value, 32767.
- What goes wrong. Without care it wraps to −32768, so −1 × −1 = −1.
- What the processor should do. DSPs saturate instead, giving 32767 (0.99997), the closest representable answer.
Answer the result overflows and wraps to −1; the processor should saturate it to 32767, which is 0.99997.
Check
What's the resolution of a fixed-point format with 12 fractional bits?
Check
What integer stores 0.3 in Q15?
Check
How many guard bits does an accumulator need to add 100 full-scale products without overflow?
Check
A 200 MHz processor handles an 8 kHz sample stream. How many cycles does it have per sample?
Check
A 64-tap Q15 FIR accumulates products in a wider register.
(a)
How many guard bits keep the worst-case sum from overflowing?
(b)
How wide must the accumulator be (Q30 products plus guard bits plus sign)?
Check
−0.5 and −0.75 are multiplied in Q15.
(a)
What integer stores −0.5?
(b)
What Q15 integer does the product give after shifting right 15 (rounding down)?
Explain why
Why do coefficients close to 1 need more bits than coefficients near 0.5?
Rounding a coefficient to n fractional bits moves it by up to half of one step, an absolute error that's the same wherever the coefficient lies. What matters for a filter is often the coefficient's distance from 1, because a pole at a, for example, decays at a rate set by 1 − a: a coefficient of 0.999 has only 0.001 of distance from 1, so an error of a few ten-thousandths changes its time constant by a large fraction, and a big enough error makes it exactly 1, where the filter stops decaying. A coefficient near 0.5 is far from that edge, so the same absolute error changes the filter's behavior by only a small fraction.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.