---
title: Fixed-point arithmetic and real-time DSP
heading: 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
eyebrow: D5 · Lesson 12 · Fixed-point arithmetic and real-time DSP
xp: 40
---

::: hook
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.
:::

::: play
- 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: $a_q = \mathrm{round}(a \cdot 2^B)/2^B$.
- 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.
:::

```sim plot
view: graph
x: B = 6..20
y: T = 0..2000
slider: a = 0.9..0.9995 step 0.0005 = 0.999
plot: -1/ln(a) + 0*B | dashed | color: ch3 | label: intended time constant, samples
plot: -1/ln(round(a*2^B)/2^B + 1e-15*step(round(a*2^B)/2^B - 1)) | stems | label: with the coefficient rounded to B bits
readout: intended time constant, samples = -1/ln(a) | digits: 4
readout: 16-bit coefficient = round(a*65536)/65536 | digits: 6
```

::: predict
## A smoothing filter uses a = 0.999, stored in 8 fractional bits. What does the coefficient become?

- [ ] 0.999
- [ ] 0.996
- [x] 1.000
- [ ] 0.992

::: reveal
0.999 × 256 = 255.74, which rounds to 256, so $a_q$ = 256/256 = 1. The filter $y_n = y_{n-1} + (1 - a)x_n$ no longer decays at all, and since (1 − a) also rounds to 0, it ignores its input entirely. With 16 bits, $a_q$ = 65,470/65,536 = 0.99899 and the time constant is 992 samples, close to the intended 999.5.
:::
:::

::: nameit
## Q formats

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

## 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 $\lceil\log_2N\rceil$ **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/f_s$ has $f_\text{clk}/f_s$ 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.
:::

::: 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?

::: solution
1. **Guard bits.**
   $$\lceil\log_2 64\rceil = 6\ \text{bits}$$
2. **Accumulator width.** A 32-bit product sum then needs 38 bits, so a 40-bit accumulator fits.
3. **Cycle budget.**
   $$\frac{200\ \text{MHz}}{48\ \text{kHz}} = 4167\ \text{cycles}$$
   per sample.
4. **Share of the budget.** At one tap per cycle the filter takes 64 cycles:
   $$\frac{64}{4167} = 1.5\%$$

**Answer:** 6 guard bits (a 40-bit accumulator fits), and the filter uses 1.5% of the cycle budget.
:::
:::

::: example
## Store 0.3 and −0.75 in Q15. What does 0.3 × −0.75 come to?

::: solution
1. **Store 0.3.**
   $$\text{round}(0.3 \times 32\,768) = 9830$$
2. **Store −0.75.**
   $$-0.75 \times 32\,768 = -24\,576$$
3. **Multiply.** The product is in Q30:
   $$\begin{aligned} &9830 \times (-24\,576) \\ &= -241\,582\,080 \end{aligned}$$
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.**
   $$\frac{-7373}{32\,768} = -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.
:::
:::

::: example
## Multiply −1 by −1 in Q15. What goes wrong, and what should the processor do?

::: solution
1. **−1 in Q15.** It's 0x8000, which is −32768.
2. **The product.** It's $2^{30}$, 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
given: n = 7 | 12 | 15 | 23 | 31
answer: 2^(-n)
tolerance: 0.5%

## What's the resolution of a fixed-point format with {n} fractional bits?

::: right
Right. $2^{-n}$ = {answer}.
:::

::: misses
- [2^(-n + 1)] With n fractional bits the step is $2^{-n}$.
- [any] Not yet. One unit in the last place.
:::
:::

::: check
given: x = 0.1 | 0.3 | 0.707 | -0.5
answer: round(x*32768)
tolerance: 0.4

## What integer stores {x} in Q15?

::: right
Right. round(x × 2¹⁵) = {answer}.
:::

::: misses
- [any] Not yet. Scale by 2¹⁵ and round.
:::
:::

::: check
given: N = 16 | 100 | 1000 | 4096
answer: ceil(log(N)/log(2))
tolerance: 0.4

## How many guard bits does an accumulator need to add {N} full-scale products without overflow?

::: right
Right. ⌈log₂ N⌉ = {answer}.
:::

::: misses
- [any] Not yet. The sum can grow by a factor of N.
:::
:::

::: check
given: c = 50 | 200 | 1000; s = 8 | 48 | 192
answer: c*1000/s
tolerance: 0.5%

## A {c} MHz processor handles a {s} kHz sample stream. How many cycles does it have per sample?

::: right
Right. $f_\text{clk}/f_s$ = {answer}.
:::

::: misses
- [any] Not yet. Clock rate over sample rate.
:::
:::

::: check
kind: parts
given: N = 16 | 64 | 256

## An {N}-tap Q15 FIR accumulates products in a wider register.

::: part
answer: log(N)/log(2)
unit: bits
tolerance: 0

### How many guard bits keep the worst-case sum from overflowing?

::: misses
- [any] $\log_2N$.
:::
:::

::: part
answer: 30 + log(N)/log(2) + 1
unit: bits
tolerance: 0

### How wide must the accumulator be (Q30 products plus guard bits plus sign)?

::: misses
- [any] 30 + guard + 1.
:::
:::
:::

::: check
kind: parts
given: x = 0.3 | -0.5 | 0.9; y = -0.75 | 0.6

## {x} and {y} are multiplied in Q15.

::: part
answer: round(x*32768)
tolerance: 0

### What integer stores {x}?

::: misses
- [any] $x \times 32768$, rounded.
:::
:::

::: part
answer: floor(round(x*32768)*round(y*32768)/32768)
tolerance: 0

### What Q15 integer does the product give after shifting right 15 (rounding down)?

::: misses
- [any] Multiply, then shift.
:::
:::
:::

::: explain
## Why do coefficients close to 1 need more bits than coefficients near 0.5?

::: model
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

::: check
given: x = 0.01 | 0.05 | 0.125 | 0.2 | 0.45 | 0.6 | 0.8 | 0.95
answer: round(x*32768)
tolerance: 0.4

## Q15 integer for {x}?

::: misses
- [any] round(x × 32,768).
:::
:::

::: check
given: c = 10 | 3000; s = 1 | 1000
answer: c*1000/s
tolerance: 0.5%

## Cycles per sample: {c} MHz, {s} kHz?

::: misses
- [any] $f_\text{clk}/f_s$.
:::
:::
:::
