Signal Generators¶
Stateless utilities that create test signals without requiring audio
input. All generators return NumPy float64 arrays.
- pyminidsp.sine_wave(n, amplitude=1.0, freq=440.0, sample_rate=44100.0)[source]¶
Generate a sine wave.
- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
freq – Frequency in Hz.
sample_rate – Sampling rate in Hz.
- Returns:
numpy array of length n.
Generate a pure sine tone:
\[x[n] = A \sin(2\pi f \, n / f_s)\]The simplest test signal in DSP. Use it to verify filter responses, check FFT bin alignment, or provide a clean input for any processing chain.
- Parameters:
n – Number of samples.
amplitude – Peak amplitude (e.g. 1.0).
freq – Frequency in Hz.
sample_rate – Sampling rate in Hz.
signal = md.sine_wave(44100, amplitude=1.0, freq=440.0, sample_rate=44100.0)
- pyminidsp.white_noise(n, amplitude=1.0, seed=42)[source]¶
Generate Gaussian white noise.
- Parameters:
n – Number of samples.
amplitude – Standard deviation.
seed – Random seed for reproducibility.
- Returns:
numpy array of length n.
Generate Gaussian white noise via the Box-Muller transform. White noise has equal energy at all frequencies — its power spectral density is approximately flat.
Use white noise to test filters, measure impulse responses, or as an additive noise source for SNR experiments.
- Parameters:
n – Number of samples.
amplitude – Standard deviation of the noise.
seed – Random seed for reproducibility.
noise = md.white_noise(4096, amplitude=1.0, seed=42)
- pyminidsp.impulse(n, amplitude=1.0, position=0)[source]¶
Generate a discrete impulse (Kronecker delta).
- Parameters:
n – Number of samples.
amplitude – Spike amplitude.
position – Sample index of the spike.
- Returns:
numpy array of length n.
Generate a discrete impulse (Kronecker delta). The output is all zeros except at position, where the value is amplitude.
The unit impulse (amplitude 1.0 at position 0) is the identity element of convolution and has a perfectly flat magnitude spectrum.
Common uses:
Measure a system’s impulse response by feeding it through a filter.
Verify that
magnitude_spectrum()returns a flat spectrum.Create delayed spikes for testing convolution and delay estimation.
- Parameters:
n – Number of samples.
amplitude – Value of the spike (e.g. 1.0 for unit impulse).
position – Sample index of the spike (0-based, must be < n).
- pyminidsp.chirp_linear(n, amplitude=1.0, f_start=200.0, f_end=4000.0, sample_rate=16000.0)[source]¶
Generate a linear chirp (swept sine).
- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
f_start – Starting frequency in Hz.
f_end – Ending frequency in Hz.
sample_rate – Sampling rate in Hz.
- Returns:
numpy array of length n.
Generate a linear chirp (swept sine with linearly increasing frequency):
\[f(t) = f_\text{start} + (f_\text{end} - f_\text{start}) \cdot t / T\]A linear chirp is the standard test signal for spectrograms — its instantaneous frequency traces a straight diagonal line in the time-frequency plane.
- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
f_start – Starting frequency in Hz.
f_end – Ending frequency in Hz.
sample_rate – Sampling rate in Hz.
# 1-second linear chirp from 200 Hz to 4 kHz at 16 kHz chirp = md.chirp_linear(16000, f_start=200.0, f_end=4000.0, sample_rate=16000.0)
- pyminidsp.chirp_log(n, amplitude=1.0, f_start=20.0, f_end=20000.0, sample_rate=44100.0)[source]¶
Generate a logarithmic chirp.
- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
f_start – Starting frequency in Hz (must be > 0).
f_end – Ending frequency in Hz (must be > 0, != f_start).
sample_rate – Sampling rate in Hz.
- Returns:
numpy array of length n.
Generate a logarithmic chirp (exponentially increasing frequency):
\[f(t) = f_\text{start} \cdot (f_\text{end} / f_\text{start})^{t / T}\]Spends equal time per octave, making it ideal for measuring systems whose behaviour is best described on a log-frequency axis (e.g. audio equaliser response).
- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
f_start – Starting frequency in Hz (must be > 0).
f_end – Ending frequency in Hz (must be > 0, ≠ f_start).
sample_rate – Sampling rate in Hz.
- pyminidsp.square_wave(n, amplitude=1.0, freq=440.0, sample_rate=44100.0)[source]¶
Generate a square wave.
Generate a bipolar square wave that alternates between +amplitude and −amplitude.
Contains only odd harmonics (1f, 3f, 5f, …) whose amplitudes decay as 1/k — a classic demonstration of the Fourier series and the Gibbs phenomenon.
- pyminidsp.sawtooth_wave(n, amplitude=1.0, freq=440.0, sample_rate=44100.0)[source]¶
Generate a sawtooth wave.
Generate a sawtooth wave that ramps linearly from −amplitude to +amplitude over each period.
Contains all integer harmonics (1f, 2f, 3f, …) decaying as 1/k — richer harmonic content compared to the square wave.
- pyminidsp.shepard_tone(n, amplitude=0.8, base_freq=440.0, sample_rate=44100.0, rate_octaves_per_sec=0.5, num_octaves=8)[source]¶
Generate a Shepard tone (auditory illusion of endlessly rising/falling pitch).
- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
base_freq – Centre frequency of the Gaussian envelope in Hz.
sample_rate – Sampling rate in Hz.
rate_octaves_per_sec – Glissando rate (positive=rising, negative=falling).
num_octaves – Number of audible octave layers.
- Returns:
numpy array of length n.
Generate a Shepard tone — the auditory illusion of endlessly rising or falling pitch.
Superimposes several sine waves spaced one octave apart. Each tone glides continuously in pitch while a Gaussian spectral envelope — fixed in log-frequency space — fades tones in at one end and out at the other.
\[x[n] = A_\text{norm}\sum_k \exp\!\left(-\frac{d_k(t)^2}{2\sigma^2}\right) \sin(\varphi_k(n))\]- Parameters:
n – Number of samples.
amplitude – Peak amplitude.
base_freq – Centre frequency of the Gaussian envelope (typical: 200–600 Hz).
sample_rate – Sampling rate in Hz.
rate_octaves_per_sec – Glissando rate (positive = rising, negative = falling, 0 = static chord).
num_octaves – Number of audible octave layers (typical: 6–10).
# 5 seconds of endlessly rising Shepard tone sig = md.shepard_tone(5 * 44100, amplitude=0.8, base_freq=440.0, sample_rate=44100.0, rate_octaves_per_sec=0.5)