Pico 2: mixing audio with the Cortex-M33's DSP instructions
A Raspberry Pi Pico 2 mixes two Q15 audio tracks and runs the mix through a 16-tap FIR filter twice, in plain C and with the Cortex-M33's SIMD and saturating…
Firmware · Advanced
In a real product: Digital audio mixer (Consumer electronics): A small speaker or intercom mixes and filters its sound on the microcontroller, a block of samples every few milliseconds.
Audio on a microcontroller is usually Q15: each sample an int16_t, -32768 to 32767 standing for -1.0 to just under +1.0. Mixing two tracks is an add that must saturate (stop at the limit) rather than wrap, and filtering is a long run of multiply-adds. The Cortex-M33 in the RP2350 has the Armv8-M DSP extension: SIMD instructions that work on two 16-bit samples packed in one 32-bit register, with saturation built in. This sketch mixes and filters 1024 samples (21 ms at 48 kHz) in plain C and again with those instructions, checks that both give the same samples, and prints the times. A knob on GP26 sets track B's volume; the LED on GP15 lights when the mix clips.
What you'll learn
- Q15 samples, and why a mix must saturate: a wrapped sum turns a loud passage into a loud click
QADD16(two saturating adds at once),SMLAD(two multiplies and both added to a sum) andSSAT, used from C througharm_acle.h- A 16-tap FIR low-pass filter, and how to pair its taps with the samples for
SMLAD - Checking an optimized path against a plain one, sample for sample
How it works
Track A is a 440 Hz tone at 60 % with a little 6 kHz hiss; track B is a 660 Hz tone at the knob's volume. mixPlain() adds each pair of samples in 32 bits and clamps the sum to the int16_t range. mixSimd() loads two samples of each track as one 32-bit word (pair()) and adds both pairs with one __qadd16(), which saturates each half on its own.
The filter's 16 taps are a Hann window scaled to sum to 1.0 (32768): a gentle low-pass that takes out the hiss. firPlain() does 16 multiply-adds a sample. firSimd() does 8 __smlad() calls, each multiplying two samples by two taps and adding both products to the sum. The taps are stored in reverse (reversed[]) so that each pair of samples lines up with its pair of taps. __ssat(acc >> 15, 16) turns the Q30 sum back into a saturated Q15 sample.
Every 250 ms the sketch builds a block, times the four functions with micros(), counts the clipped samples and compares the outputs. With the knob at 68 %:
volume 68 % clipped 145 mix 123 us -> 42 us (2.9x) FIR 1309 us -> 602 us (2.2x) same: yes
volume 68 % clipped 145 mix 123 us -> 41 us (3.0x) FIR 1309 us -> 602 us (2.2x) same: yes
The mix is three times faster: half as many loads, adds and stores, and no compares. The FIR gains a little over two times, as the loop around each SMLAD costs the same in both. The times are the simulator's; a real Pico 2 prints times close to them. same: yes matters most: the DSP path gives exactly the plain path's samples.
Try this
- Turn the knob below about 35 %: nothing clips, and the LED goes out.
- Replace
__qadd16with__sadd16, which wraps instead of saturating, and watchsame:turn toNOonce the mix clips. - Make the filter 32 taps long. The plain loop's time doubles; how much does the SIMD one's?
sketch.ino
// Raspberry Pi Pico 2 (Arduino core): two audio tracks mixed and filtered in Q15, once in plain C and once with the
// Cortex-M33's DSP instructions (QADD16, SMLAD, SSAT through arm_acle.h), timed and checked against each other. A
// knob on GP26 sets track B's volume; the LED on GP15 lights when the mix clips.
#include <arm_acle.h>
#include <string.h>
const int KNOB = 26, CLIP_LED = 15;
const int N = 1024; // samples a block (at 48 kHz: 21 ms of sound)
const int TAPS = 16;
int16_t trackA[N], trackB[N]; // the two tracks, Q15: -32768 to 32767 is -1.0 to just under +1.0
int16_t mixC[N], mixDsp[N]; // A + B, saturated
int16_t lowC[N], lowDsp[N]; // the mix through the FIR low-pass
int16_t taps[TAPS]; // its coefficients, Q15, summing to 1.0
// One sample pair (two int16_t) as a 32-bit word: SIMD instructions work on both halves at once.
static inline int32_t pair(const int16_t *p) { int32_t v; memcpy(&v, p, 4); return v; }
// Plain C: add, then clamp to the int16_t range.
void mixPlain() {
for (int i = 0; i < N; i++) {
int32_t s = trackA[i] + trackB[i];
mixC[i] = s > 32767 ? 32767 : s < -32768 ? -32768 : s;
}
}
// QADD16: two saturating 16-bit adds in one instruction.
void mixSimd() {
for (int i = 0; i < N; i += 2) {
int32_t s = __qadd16(pair(&trackA[i]), pair(&trackB[i]));
memcpy(&mixDsp[i], &s, 4);
}
}
// out[n] = sum of mix[n - k] * taps[k], Q15 times Q15 is Q30: shifted back by 15 and saturated.
void firPlain() {
for (int n = TAPS - 1; n < N; n++) {
int32_t acc = 0;
for (int k = 0; k < TAPS; k++) acc += mixC[n - k] * taps[k];
acc >>= 15;
lowC[n] = acc > 32767 ? 32767 : acc < -32768 ? -32768 : acc;
}
}
// SMLAD: two multiplies and both products added to the sum, in one instruction. The taps are kept in reverse so a pair
// of samples meets the pair of taps that goes with it; SSAT saturates to 16 bits.
int16_t reversed[TAPS];
void firSimd() {
for (int n = TAPS - 1; n < N; n++) {
const int16_t *x = &mixDsp[n - (TAPS - 1)];
int32_t acc = 0;
for (int k = 0; k < TAPS; k += 2) acc = __smlad(pair(&x[k]), pair(&reversed[k]), acc);
lowDsp[n] = __ssat(acc >> 15, 16);
}
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 3000) delay(10); // the USB port open (up to 3 s), so the first lines are seen
analogReadResolution(12);
pinMode(CLIP_LED, OUTPUT);
// A low-pass FIR: a Hann window's 16 points, scaled to sum to 32768 (1.0).
float sum = 0, w[TAPS];
for (int k = 0; k < TAPS; k++) { w[k] = 0.5f - 0.5f * cosf(2 * PI * (k + 1) / (TAPS + 1)); sum += w[k]; }
for (int k = 0; k < TAPS; k++) { taps[k] = (int16_t)(w[k] / sum * 32767); reversed[TAPS - 1 - k] = taps[k]; }
// Track A: a 440 Hz tone at 60 %, with a little 6 kHz hiss the filter takes out.
for (int i = 0; i < N; i++) trackA[i] = (int16_t)(19660 * sinf(2 * PI * 440 * i / 48000) + 3000 * sinf(2 * PI * 6000 * i / 48000));
}
void loop() {
// Track B: a 660 Hz tone at the knob's volume (0 to 100 %).
float volume = analogRead(KNOB) / 4095.0f;
for (int i = 0; i < N; i++) trackB[i] = (int16_t)(32767 * volume * sinf(2 * PI * 660 * i / 48000));
uint32_t t0 = micros(); mixPlain(); uint32_t tMixC = micros() - t0;
t0 = micros(); mixSimd(); uint32_t tMixDsp = micros() - t0;
t0 = micros(); firPlain(); uint32_t tFirC = micros() - t0;
t0 = micros(); firSimd(); uint32_t tFirDsp = micros() - t0;
int clipped = 0;
for (int i = 0; i < N; i++) if (mixC[i] == 32767 || mixC[i] == -32768) clipped++;
digitalWrite(CLIP_LED, clipped ? HIGH : LOW);
bool same = memcmp(mixC, mixDsp, sizeof mixC) == 0 && memcmp(lowC + TAPS - 1, lowDsp + TAPS - 1, (N - TAPS + 1) * 2) == 0;
Serial.printf("volume %3d %% clipped %4d mix %4lu us -> %3lu us (%.1fx) FIR %5lu us -> %4lu us (%.1fx) same: %s\n",
(int)(volume * 100), clipped, tMixC, tMixDsp, (float)tMixC / tMixDsp, tFirC, tFirDsp, (float)tFirC / tFirDsp, same ? "yes" : "NO");
delay(250);
}