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

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: aq=round(a⋅2B)/2B.
  • 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?

Name it

Q formats

A Qm.n number is an integer with n fractional bits: its value is the integer divided by 2n. Q15 (Q1.15) uses 16-bit signed integers for values from −1 to 1−2−15, with resolution 2−15=3.05×10−5. To store x, use round(x⋅215).

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 ⌈log2⁡N⌉ 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 1/fs has fclk/fs 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
  1. Guard bits. ⌈log2⁡64⌉=6bits
  2. Accumulator width. A 32-bit product sum then needs 38 bits, so a 40-bit accumulator fits.
  3. Cycle budget. 200MHz48kHz=4167cycles per sample.
  4. Share of the budget. At one tap per cycle the filter takes 64 cycles: 644167=1.5%

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
  1. Store 0.3. round(0.3×32768)=9830
  2. Store −0.75. −0.75×32768=−24576
  3. Multiply. The product is in Q30: 9830×(−24576)=−241582080
  4. Shift back to Q15. Shifted right by 15 it's −7372.5, which rounds to −7373 (or −7372, depending on the rounding rule).
  5. Read the result. −737332768=−0.22501 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. −1 in Q15. It's 0x8000, which is −32768.
  2. The product. It's 230, which shifted right 15 bits is 32768: one more than the largest Q15 value, 32767.
  3. What goes wrong. Without care it wraps to −32768, so −1 × −1 = −1.
  4. 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?

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.