Spectrum Analyser
The N6Audio library's FFT and display-ready spectrum, and the AudioSpectrum and AudioWaterfall examples.
The N6Audio library provides a real FFT and a spectrum structure that folds FFT bins into display bands. The AudioSpectrum example draws a 64-band analyser on the panel from the microphone; AudioWaterfall draws the same analysis as a scrolling spectrogram.
Processing chain
#include <Microphone.h>
#include <n6_fft.h>
#include <n6_spectrum.h>
Microphone mic;
n6_frame_t frame; // 4096-sample sliding window
n6_spectrum_t spec;
static float mag[N6_FFT_BINS];
static int16_t chunk[N6_FFT_HOP];
void loop() {
uint32_t got = mic.read(chunk, N6_FFT_HOP); // 1024 new samples
n6_frame_push(&frame, chunk, got); // slide into the window
n6_fft_magnitude(n6_frame_data(&frame), mag); // 2048 bins, full scale 1.0
n6_spectrum_update(&spec, mag); // bars[] and peaks[], 0 to 1
}Window and hop
The analysis window is 4096 samples and advances by 1024. Frequency resolution comes from the window (7.9 Hz per bin at 32552 Hz) while the display refreshes every 1024 samples. With a 1024-sample window the bins are 31.8 Hz wide, and log-spaced display bands below about 200 Hz become narrower than a bin: with 64 bands from 50 Hz, the lowest six bands all read bin 2 and move together, and 13 of 64 bands duplicate a neighbour. A 4096-sample window reduces this to one duplicate.
Spectrum structure
Three properties of n6_spectrum_t distinguish the output from a plain plot:
- Bands are log-spaced. Linear bins place most of the display between 8 and 16 kHz.
- Each band takes the loudest bin rather than the mean, so a pure tone is not averaged with quiet neighbours.
- Ballistics are asymmetric, with fast attack and slow release.
n6_fft_magnitude() subtracts the frame mean before windowing. The PDM microphone has a DC offset that, even with a Hann window and bin 0 excluded, leaks into bins 1 and 2 and holds the lowest band of a log display permanently lit. Measured on hardware, the loudest band moved from 52 Hz at 0.36 before the DC block to 221 Hz at 0.29 after it.
Waterfall
The AudioWaterfall example plots frequency vertically, time horizontally and level as colour, showing about six seconds. Two properties of its implementation apply to any scrolling display on this platform:
- The scroll is not a framebuffer shift. Shifting the 800x480 16-bit overlay would move 768 KB through PSRAM per frame. A 12 KB ring of intensity columns is kept and the image redrawn from it, measured at 18 ms per frame at 33 fps.
- Ballistics are disabled (
attackandreleaseset to 1.0), because smoothing would smear energy along the time axis.
Dependencies
N6Audio is self-contained. The core ships the CMSIS-DSP headers but no sources or library, so arm_rfft_fast_f32 does not link. A 1024-point float FFT runs well under a millisecond on the Cortex-M55.