One block, one loop
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.
The DFT treats the block as one period of a signal that repeats forever. 750 Hz fits exactly 3 whole cycles in 4 ms, so the end of the block flows smoothly into its start.
750 Hz is bin 3 and 2500 Hz is bin 10. The FFT shows the loud tone at 0 dB and the quiet one at −40 dB (100 times smaller). Every other bin is zero, so both tones are easy to see.
Between bins: leakage
Retune the loud tone to 875 Hz, bin 3.5, halfway between two bins. Now 3.5 cycles fit in the block. Where the loop joins end to start, the wave jumps from −1 back to +1: a seam.
A jump needs many frequencies to build it, so the tone's energy leaks into every bin. Its biggest bin reads only −3.4 dB, and bins far away still hold −24.1 dB. This spreading is spectral leakage.
Bin 10 should read −40 dB, the quiet tone. But leakage from the loud tone alone already puts −21.9 dB there, about 8 times more. Bin 10 reads −21.1 dB, and the quiet tone is buried.
Windowing
Cutting out a block already used a rectangle window: all ones. Instead, multiply each sample by the Hann window w[n] = 0.5 − 0.5·cos(2πn/N): near 0 at both ends, 1 in the middle. The seam is gone.
Away from the peak, leakage now drops fast. The Hann side lobes (small humps beside the peak) start 31.5 dB down and fall 18 dB per octave (each doubling of distance from the peak). Bin 10 reads −39.9 dB: the quiet tone is back.
Nothing is free. The Hann main lobe (the central peak) is 4 bins wide at its base, twice the rectangle's 2 bins, so tones close together blur sooner. Pick the window for the job: sharp peaks or low leakage.
In code
With numpy, windowing the block is one line and the spectrum another. Dividing by w.sum() = 16, not 32, makes up for the window's halved gain, so a tone on a bin still reads 0 dB.
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.