KNIFE-020 – micro:bit V2 whistle detection pipeline
📅 2026-11-17 · stav: rozpracované (PC pipeline overená, nasadenie na zariadenie čaká) · priorita: vysoká · ✍️ Aymeric
The question: the audio hardware moved from the ESP32-S3 + INMP441 to the BBC micro:bit V2 (KNIFE-019). Can the whistle detector be adapted to the new microphone and squeezed into 128 KiB of RAM without losing accuracy?
Short answer:
- Yes, on the PC so far. The model is trained, the audio frontend is validated, and the network was redesigned to fit the micro:bit resource budget.
- V4 DS-CNN: 46.9 k parameters, 2.33 M MACs, about 54 KiB as an INT8 model and about 38 KiB of peak activations. It reaches 0.99 clean / 1.00 simulated micro:bit balanced validation accuracy.
- Real-time detection works from a PC microphone at a threshold of 0.97.
- What remains is embedded deployment: INT8 quantization, CMSIS-DSP / CMSIS-NN and measurements on the real board. The model design itself is done.
Current pipeline:
microphone audio
→ 16 kHz mono signal
→ 2 s active window
→ STFT
→ 64-band log-Mel representation
→ compact DS-CNN
→ P(Whistle)
→ decision threshold
Remaining work:
PyTorch V4
→ INT8 quantization
→ CMSIS-DSP frontend
→ CMSIS-NN inference
→ micro:bit microphone acquisition
→ speaker response
→ on-device validation
Why V4 was necessary
The previous V3 classifier was developed for the ESP32-S3 + INMP441 architecture. It classified very well, but the hardware later changed to a BBC micro:bit V2 to reduce:
- component count;
- wiring;
- integration uncertainty;
- mechanical failure points;
- overall cost.
The micro:bit V2 provides:
| Feature | Value |
|---|---|
| SoC | Nordic nRF52833 |
| CPU | 64 MHz Cortex-M4F |
| Flash | 512 kB |
| RAM | 128 kB |
| Microphone | integrated analog MEMS (Knowles SPU0410LR5H-QB-7) |
| Speaker | integrated |
Manufacturer specifications of the microphone used in the acquisition model:
| Parameter | Value |
|---|---|
| Nominal sensitivity | −38 dBV/Pa |
| Sensitivity tolerance | ±3 dB |
| Typical SNR | 63 dB(A) |
| Acoustic overload point | 118 dB SPL |
| Output impedance | ≤ 400 Ω |
The hardware change required two separate adaptations:
- replace the INMP441 acquisition model with a micro:bit V2 model;
- redesign the DS-CNN so that its intermediate activations fit safely inside the micro:bit RAM budget.
Dataset
V4 keeps the same MLEnd Hums & Whistles subset as the previous experiments: 1,064 recordings from 37 interpreters, in two classes, Hum and Whistle.
The split is speaker-independent: each person belongs to only one of training, validation or test. This is essential. A random file split could put recordings of the same person in both training and test data and give an unrealistically optimistic estimate of generalisation.
| Split | Samples | Interpreters | Hum | Whistle |
|---|---|---|---|---|
| Training | 711 | 25 | 521 | 190 |
| Validation | 144 | 5 | 106 | 38 |
| Test | 209 | 7 | 153 | 56 |
| Total | 1,064 | 37 | 780 | 284 |
The split manifests are frozen as:
data/splits/train.csv
data/splits/validation.csv
data/splits/test.csv
data/splits/split_summary.json
Each split also gets a SHA-256 hash, so the exact partition can be reproduced later.
Audio preparation
Every recording is first converted to a deterministic input format:
audio file
→ mono
→ resample to 16 kHz
→ select 2 s active region
→ exactly 32,000 samples
- Short recordings are zero-padded at the end.
- Longer recordings: candidate 2 s windows are evaluated every 250 ms.
- In validation and testing, the highest-energy window is chosen deterministically.
- In training, one window is sampled from the five highest-energy candidates.
This gives some temporal variation in training while keeping evaluation reproducible.
Log-Mel frontend
The CNN does not process raw audio. Each 2 s waveform is turned into a time-frequency representation:
16 kHz mono audio
→ 32,000 samples
→ Hann window
→ STFT
→ power spectrum
→ 64 Mel filters
→ logarithmic dB scale
→ normalization
→ 1 × 64 × 201 tensor
The frozen configuration:
| Parameter | Value |
|---|---|
| Sample rate | 16,000 Hz |
| Clip length | 2.0 s |
| Samples | 32,000 |
| FFT size | 512 |
| Hop length | 160 |
| FFT interval | 10 ms |
| Mel bands | 64 |
| Minimum frequency | 100 Hz |
| Maximum frequency | 8,000 Hz |
| Dynamic range | 80 dB |
| Output | 1 × 64 × 201 |
The steps:
- FFT: a 512-point real FFT gives 257 non-redundant frequency bins.
- Power: the power spectrum is |X[k]|².
- Mel: the filter bank reduces the 257 bins to 64 perceptually spaced bands.
- dB: power is converted to dB relative to the strongest point of the spectrogram. The maximum energy becomes 0 dB, and the minimum retained value is −80 dB.
- Normalisation: values are mapped to [−1, +1] with
normalized = dB / 40 + 1:
| dB | Normalised |
|---|---|
| −80 dB | −1 |
| −40 dB | 0 |
| 0 dB | +1 |
The classifier input is therefore 1 × 64 × 201: 1 input channel, 64 Mel frequency bands and 201 time frames.
Frontend reproducibility
For V4, the frontend was rewritten explicitly instead of relying on a single high-level Librosa call. The implementation exposes each of these steps:
- the Hann window;
- the Mel filter bank;
- the STFT framing;
- the power calculation;
- the dB conversion;
- the normalisation.
This matters because these exact constants will later be exported to the micro:bit firmware.
The explicit implementation was compared numerically with the original Librosa reference:
| Metric | Difference |
|---|---|
| Maximum absolute error | ≈ 1.6 × 10⁻⁵ |
| Mean absolute error | ≈ 8.5 × 10⁻⁷ |
The two are numerically equivalent for this application. This also prepares the ground for golden-vector tests between Python and C/C++.
micro:bit V2 acquisition model
Training directly on clean MLEnd recordings would assume that the future microphone behaves exactly like the devices the dataset was recorded on. Instead, V4 adds a lightweight simulation of the micro:bit V2 acquisition chain in front of the Mel frontend.
The model deliberately keeps three kinds of parameters apart:
- manufacturer specifications;
- derived circuit parameters;
- training-only robustness augmentation.
Manufacturer-derived microphone response
The microphone is modelled with the approximate free-field response of the SPU0410LR5H-QB-7. It is almost flat through most of the useful whistle range:
- a small attenuation near 100 Hz;
- approximately flat around 1–3 kHz;
- a slight rise toward the upper frequencies.
These values approximate the manufacturer's curve. They are not calibration measurements of the actual NightJar enclosure.
Analog high-pass approximation
The micro:bit schematic uses 33 kΩ, 1 kΩ and a 1 µF coupling capacitor. The equivalent bias resistance:
Rbias = 33 kΩ || 1 kΩ ≈ 970.6 Ω
With the microphone output impedance between about 0 Ω and its specified maximum of 400 Ω, the first-order AC-coupling approximation fc = 1 / (2πRC) gives:
fc ≈ 116.12 – 163.98 Hz
This cutoff is derived from the circuit. It is not an arbitrary fixed value. A real-board measurement will still be needed after firmware integration.
Training-domain randomisation
During training, the simulated channel varies the microphone sensitivity, the frequency-response perturbation, relative noise, clipping, quantization and the analog high-pass cutoff.
| Parameter | Nominal training | Stress test |
|---|---|---|
| Sensitivity |