|
miniDSP
A small C library for audio DSP
|
Signal generators produce test signals from scratch — no audio file or microphone required. They are the "hello world" of DSP: a pure sine wave, an impulse, or a burst of noise gives you a known input that you can trace through any processing chain and verify at every step.
miniDSP ships generators as simple, stateless functions. They take an output buffer and a handful of parameters; no setup or teardown is needed.
A sine wave at frequency \(f\) Hz, amplitude \(A\), and sample rate \(f_s\):
\[ x[n] = A \sin\!\left(\frac{2\pi f n}{f_s}\right), \quad n = 0, 1, \ldots, N-1 \]
API:
Listen — 440 Hz, 2 seconds:
Quick example — generate one second of A4 (440 Hz):
Verifying via spectrum
The clearest way to confirm the generator is correct is to feed its output to MD_magnitude_spectrum() and check that the peak lands on the expected bin:
See examples/sine_wave.c for a full runnable program that generates the spectrum and writes an interactive HTML plot.
A discrete impulse (Kronecker delta) at position \(n_0\) with amplitude \(A\):
\[ x[n] = \begin{cases} A & \text{if } n = n_0 \\ 0 & \text{otherwise} \end{cases} \]
The unit impulse ( \(A = 1\), \(n_0 = 0\)) is the identity element of convolution and has a perfectly flat magnitude spectrum — every frequency bin equals 1.0.
API:
Listen — impulse train (four clicks at 0.5-second intervals), 2 seconds:
Quick example — generate a unit impulse at sample 0:
Verifying via spectrum
Feed the impulse to MD_magnitude_spectrum() and confirm every bin has the same magnitude:
See examples/impulse.c for a full runnable program that generates both time-domain and frequency-domain plots.
A chirp sweeps frequency over time — either linearly or logarithmically. Chirps are the standard test signal for spectrograms and for measuring filter magnitude response.
A linear chirp sweeps instantaneous frequency from \(f_0\) to \(f_1\) at a constant rate over duration \(T = (N-1)/f_s\):
\[ x[n] = A \sin\!\left(2\pi\!\left(f_0\,t + \frac{1}{2}\,\frac{f_1 - f_0}{T}\,t^2\right)\right), \quad t = n / f_s \]
API:
Listen — linear sweep from 20 Hz to 4000 Hz, 2 seconds:
A logarithmic chirp sweeps frequency exponentially, spending equal time per octave. This is ideal for audio systems whose response is best viewed on a log-frequency axis.
\[ x[n] = A \sin\!\left(\frac{2\pi f_0 T}{\ln r}\!\left(r^{t/T} - 1\right)\right), \quad r = f_1 / f_0,\quad t = n / f_s \]
API:
Requires \(f_0 > 0\), \(f_1 > 0\), and \(f_0 \ne f_1\).
Listen — logarithmic sweep from 20 Hz to 4000 Hz, 2 seconds:
Quick example — generate both chirp types and compare:
See examples/chirp_wave.c for a full runnable program that generates the magnitude spectra of both chirp types and writes an interactive HTML plot.
A square wave at frequency \(f\) Hz alternates between \(+A\) and \(-A\):
\[ x[n] = \begin{cases} +A & 0 < \phi < \pi \\ -A & \pi < \phi < 2\pi \\ 0 & \phi = 0 \text{ or } \phi = \pi \end{cases} \]
where \(\phi = 2\pi f n / f_s \pmod{2\pi}\).
Its Fourier series contains only odd harmonics (1f, 3f, 5f, …) with amplitudes decaying as \(1/k\) — a textbook demonstration of the Gibbs phenomenon.
API:
Listen — 440 Hz, 2 seconds:
Quick example:
See examples/square_sawtooth.c for a full program that compares the square and sawtooth spectra side by side.
A sawtooth wave ramps linearly from \(-A\) to \(+A\) over each period:
\[ x[n] = A \left(\frac{\phi}{\pi} - 1\right) \]
where \(\phi = 2\pi f n / f_s \pmod{2\pi}\).
Unlike the square wave, the sawtooth contains all integer harmonics (1f, 2f, 3f, …), each decaying as \(1/k\). Comparing the two spectra shows how waveform shape determines harmonic content.
API:
Listen — 440 Hz, 2 seconds:
Quick example:
See examples/square_sawtooth.c for the full runnable program.
Gaussian white noise has equal power at all frequencies — its power spectral density (PSD) is approximately flat across the entire band. It is the standard broadband test signal for filter characterisation, impulse response measurement, and SNR experiments.
Each sample is drawn independently from a normal distribution with mean 0 and standard deviation \(\sigma\):
\[ x[n] \sim \mathrm{N}(0,\, \sigma^2), \quad n = 0, 1, \ldots, N-1 \]
Samples are generated with the Box-Muller transform seeded by seed, so the same seed always produces the same sequence — useful for reproducible tests.
API:
amplitude is the standard deviation \(\sigma\) of the distribution.
Listen — Gaussian white noise (sigma 0.25), 2 seconds:
Quick example — generate 4096 samples of unit-variance noise:
Verifying via PSD
Feed the noise to MD_power_spectral_density() and confirm the spectrum is approximately flat — no bin should dominate:
See examples/white_noise.c for a full runnable program that computes and plots the power spectral density.