toneADCx[0]1.01x[1]0.83x[30]0.38x[31]0.83× w[n]FFT1n = np.arange(32)2x = np.cos(2*np.pi*875*n/8000)3x += 0.01*np.cos(2*np.pi*2500*n/8000)4w = 0.5 - 0.5*np.cos(2*np.pi*n/32)5A = 2*abs(np.fft.rfft(x*w))/w.sum()x
081624324048-101n (sample number)x[n]THE BLOCK, AS THE DFT SEES ITrepeat3 whole cycles: smooth join3.5 cycles: a jump at the seamx[n]·w[n]: ends taper to 001000200030004000-80-60-40-200f (Hz)level (dB)SPECTRUM, LEVEL IN dBrectangleHannbin 3: 0 dB · bin 10: −40 dBpeak −3.4 dB · far bins −24.1 dBbin 10: −21.1 dB, tone buriedbin 10: −39.9 dB, tone backrect 2 binsHann 4 binsmain lobe at its base
Coloured parts: what the current step is about; everything else rests in whiteBlocks: tone generator, converter (ADC), buffer, window, FFT chip and a Python code cardPlots: the block as the DFT sees it (looped) and its spectrum in dBThe token hops along the chain; the plots draw in from the leftA 32-sample block at 8000 samples a second (bins every 250 Hz). A 750 Hz tone lands on bin 3; a 875 Hz tone falls between bins and leaks into all of them, burying a quiet 2500 Hz tone at −40 dB, until a Hann window tapers the block. For spectral analysis the window is the periodic Hann, cos(2πn/N); numpy.hanning(N) is the symmetric one, cos(2πn/(N−1)), meant for filter design. With N = 32 the two differ only slightly.

One block, one loop

  1. A loud 750 Hz tone plus a quiet 2500 Hz tone, 100 times smaller, are sampled 8000 times a second. We keep one block of N = 32 samples (4 ms). Its DFT has a bin every fs/N = 250 Hz.

Between bins: leakage

Windowing

In code

The working

# step 1/10 · block

fs = 8000 samples per second
N  = 32 samples  ->  32 / 8000 s = 4 ms

x[n] = cos(2π·750·n/fs) + 0.01·cos(2π·2500·n/fs)

bin spacing  fs / N = 8000 / 32 = 250 Hz
bins 0 .. 16: 0 Hz .. 4000 Hz (fs/2)

buffer: x[0] x[1] ... x[30] x[31]

The two tones, 8000 samples a second and the 32-sample block are a chosen example; every level in the figure is computed from this model with the DFT sum, and the Python card was checked with numpy. Levels are 2·|X[k]|/Σw in dB, so a sine of amplitude 1 on a bin reads 0 dB with either window. The window figures (rectangle: first side lobe ≈13 dB down (13.3 dB in Harris), 6 dB per octave; Hann: −31.5 dB, 18 dB per octave, main lobe twice as wide) are from J. O. Smith III, Spectral Audio Signal Processing (CCRMA), and F. J. Harris, Proc. IEEE 1978.