loading...
Aliasing map: the frequency you would see against the true input frequency (dashed line = Nyquist)
Fourier amplitudes of the chosen waveform (dashed line = last harmonic below Nyquist)
running...
How to use
Pick a preset or build your own: waveform, frequency, sampling rate, bit depth. The upper screen is the scope (yellow = true input, cyan = samples, magenta = reconstruction from the samples); the lower screen is the spectrum analyser. Raise the frequency past half the sampling rate and watch the reconstructed wave and the spectral peak jump to the alias. Switch on the anti-alias filter to remove it. Lower the bit depth to see quantization steps and the noise floor rise. Drag to orbit, wheel to zoom. Space pauses, R resets.
The bench, scope, analyser, function generator and breadboard were modelled in Blender (baked PBR textures); the stems, ribbon and columns above the bench are drawn from the same model as the 2D lab.
The science
Sampling. An analogue-to-digital converter looks at a signal s(t) only at the instants t_n = n / fs. Two different sinusoids give identical samples if their frequencies differ by a multiple of fs, because sin(2 pi (f + m fs) n / fs) = sin(2 pi f n / fs). After sampling, a tone of frequency f is therefore indistinguishable from one at
f_alias = | f - fs round(f / fs) |
which always lies between 0 and the Nyquist frequency fs / 2. The Nyquist-Shannon theorem says a signal containing no energy above fs / 2 can be rebuilt exactly from its samples by x(t) = sum_n x_n sinc(fs t - n). An anti-alias filter has to remove everything above fs / 2 before the sampler, because no later processing can tell the difference.
Non-sinusoidal waveforms. A periodic wave of frequency f is a sum of harmonics at k f. For the ideal square, triangle, sawtooth and pulse waveforms:
square: (4/pi) sum_(k odd) sin(2 pi k f t)/k, triangle: (8/pi^2) sum_(k odd) (-1)^((k-1)/2) sin(2 pi k f t)/k^2, saw: (2/pi) sum_k (-1)^(k+1) sin(2 pi k f t)/k
pulse (duty D): (2D - 1) + (4/pi) sum_k sin(pi k D)/k cos(2 pi k f t)
Their harmonics extend to infinity, so a sampled square wave always has components above Nyquist, and they fold back into the band as spurious tones: the spectrum shows lines that are not harmonics of f. Cutting the series at N harmonics gives a band-limited wave with the Gibbs overshoot: about 8.95% of the jump (peak 1.179 for a +-1 square), however large N is.
Quantization. An N-bit converter rounds every sample to a multiple of q = 2 / 2^N (full scale +-1). The rounding error is nearly uniform on (-q/2, q/2), with power q^2 / 12; for a full-scale sine of power 1/2:
SQNR = 10 log10 ( (1/2) / (q^2/12) ) = 6.02 N + 1.76 dB
Each extra bit buys about 6 dB. The formula holds when the signal is large and busy; for small or very slow signals the error is correlated with the signal and the rule fails. Noise added in the generator shows up as a floor in the spectrum of height 10 log10 (sigma^2 / (M/2)) per bin.
Spectrum. The analyser takes M = 2048 samples, multiplies them by a window, and computes the discrete Fourier transform. The amplitude is scaled by the window sum so that a full-scale sine reads 0 dBFS. A rectangular window leaks energy into neighbouring bins when the tone does not fit a whole number of cycles; Hann and Blackman windows trade a wider main lobe for much lower leakage. The tone frequency is refined by parabolic interpolation of the three bins around the peak.
Honest simplifications. (1) The sampler is ideal: no jitter, no aperture, no sample-and-hold droop, no nonlinearity or missing codes. (2) The anti-alias filter is a brick wall implemented by dropping harmonics above Nyquist, not a real filter with a roll-off and phase shift. (3) The sinc interpolator uses a finite Hann-windowed kernel of 20 taps, so it is accurate in the middle of the screen but not exact. (4) The scope draws the analogue signal as 900 straight segments. (5) Noise is a fixed pseudo-random sequence, so the picture is reproducible but not random.
Checks run in the page. Measured quantization SQNR of a full-scale sine at 8 and 12 bits against 6.02 N + 1.76 dB; the FFT peak of a 9 kHz sine sampled at 12 kHz against the alias at 3 kHz; the Gibbs peak of a 199-harmonic square wave against 1.1790; Parseval's identity between the time-domain power and the FFT power; and the FFT against a direct DFT at four bins.