A Q15 FIR filter tap by tap
One output of a Q15 FIR filter: multiply-accumulate, shift back and saturate. A medium embedded C problem (DSP instructions and fixed point on the…
medium · DSP instructions and fixed point on the Cortex-M33 · fir · q15 · smlad
A Pico 2 filters audio with an FIR: each output is the sum of the last taps samples times the filter's coefficients, y = x[0]·h[0] + x[1]·h[1] + ..., where x[0] is the newest sample. Samples and coefficients are Q15, so each product is Q30.
Return the output in Q15: add the products in 32 bits, shift the sum right by 15 and saturate it to the int16_t range.
Starter code
int16_t fir_q15(const int16_t *x, const int16_t *h, int taps) {
int32_t acc = 0;
for (int k = 0; k < taps; k++) acc += x[k] * h[k];
// No saturation: a sum above 1.0 wraps.
return (int16_t)(acc >> 15);
}Example tests
A moving average of four
int16_t x[4] = { 4000, 8000, -2000, 2000 };
int16_t h[4] = { 8192, 8192, 8192, 8192 };
printf("%d\n", fir_q15(x, h, 4));Expected output:
3000A gain above one saturates
int16_t x[2] = { 30000, 30000 };
int16_t h[2] = { 32767, 32767 };
printf("%d\n", fir_q15(x, h, 2));Expected output:
32767Learn it first
- DSP instructions on the Cortex-M33 · SIMD and saturating instructions, Q15 mixing and FIR filters from C.
More DSP instructions and fixed point on the Cortex-M33 problems
- What QADD16 does · Add two pairs of packed 16-bit samples, each half saturating on its own.