Watch the bars as a tone moves off its bin, a quarter bin and then half a bin: one bar becomes many.
A tone between bins leaks into all of them
N = 32 samples of cos(2πk₁n/32). Bars: the DFT, sizes ÷ (N/2). Dashed: the spectrum of the cut tone (12.3).
k₁ = 8: a whole number of cycles. The bins sit on the curve's peak and on its zeros: one bar of height 1, every other bar 0.
Describe this picture
Two panels for samples of . The first is the signal, sample from 0 to 31 against from −1.2 to 1.2, drawn as stems with dot heads. The second is the spectrum, frequency in bins from 0 to 16 against size ÷ from 0 to 1.1. A dashed curve, labelled “spectrum of the cut tone”, is the size of the DTFT of the 32 samples, divided by ; the bars are the DFT, labelled “DFT bins”, each with a filled square on top. A downward triangle at the top of the plot, labelled “tone”, marks . The readouts are the tone at and the share of the DFT’s energy that is outside the tallest bar and its mirror.
The clip plays once, in 13 s, and holds on its last frame. At (readouts 8.00 bins and 0.0 %) the caption reads “k₁ = 8: a whole number of cycles. The bins sit on the curve’s peak and on its zeros: one bar of height 1, every other bar 0.” While eases to 8.25 it reads “The tone moves and the curve moves with it; the bins stay where they are.” At 8.25 (8.25 bins and 17.5 %): “k₁ = 8.25: the curve has moved a quarter bin, and the bins now sample it off its zeros. Bar 8 drops to 0.92 and every other bar gets a little: 17.5 % of the energy is outside bar 8.” At 8.5 (8.50 bins and 59.0 %): “k₁ = 8.5, halfway: bars 8 and 9 share the peak at about 0.64 each, and even bar 16 holds 0.06. This is leakage: the bins sample the lump between its zeros.”
When the clip has finished, the triangle becomes a handle named “Tone position k₁”, with the hint “Drag the tone, or use the arrow keys, to move it between bins (4 to 12).” The arrow keys move 0.05, Page Up and Page Down move 0.5, and Home and End go to 4 and 12. At any other whole the caption reads “k₁ = 10: a whole number of cycles. One bar, no leakage.” At a value between, it reads like “k₁ = 9.30: 24.1 % of the energy is outside the peak bar.” With reduced motion, the clip rests on its last frame and a step control shows = 8, 8.25 and 8.5.
A tone between bins leaks into all of them
The DFT (13.2) put its tones exactly on bins, and the picture was clean: one bar for each tone, zero everywhere else. That was a lucky choice. A real tone has no reason to land on a bin. Here I move one off its bin and watch what happens.
I write a tone’s position in bins as , where is its frequency in hertz, the number of samples and the sampling rate. On a bin, is a whole number. The picture at the top of this page keeps samples of and lets move between whole values.
At , a whole number of cycles, there is one bar of height 1 and every other bar is 0. The curve is the reason for that picture. Properties of the DTFT (12.3) showed that cutting a tone to samples turns each of its two spectral arrows into a lump, the spectrum of the cut, and Sampling the spectrum (13.1) showed that the DFT reads that spectrum only at the bins. The lump has a tall peak and zeros one bin apart, as in The DTFT (12.2). With the peak sits on bin 8 and every other bin sits on a zero.
At the curve has moved a quarter bin, and the bins now sample it off its zeros. Bar 8 drops to 0.92 and every other bar gets a little: 17.5 % of the energy is outside bar 8. The bars at bins 7, 9 and 10 are 0.203, 0.278 and 0.107, bar 0 is 0.061, and bar 16 is 0.002.
At , halfway between two bins, bars 8 and 9 share the peak, and even bar 16 holds 0.06: the bins sample the lump between its zeros. That is leakage: energy of one tone showing up in bins that are not its own. At 8.5, bars 8 and 9 are 0.641 and 0.635, bar 7 and bar 10 are 0.218 and 0.212, and bars 0 and 16 are both 0.0625. Close to 59 % of the energy is outside the tallest bar.
When the clip has finished, drag the triangle along the spectrum to move the tone yourself. Try and then 9.0, and watch every bar except one fall to 0.
Think of a fence of posts in front of a hill. The posts are the bins, and the hill is the lump. Move the hill half a post-gap and no post stands at its top any more, but the posts still touch it.
The seam
The same fact has a second reading, in time. The DFT treats its samples as one period of a repetition (13.1). If the samples hold whole cycles, the copies join smoothly. Otherwise, the repetition jumps where one copy ends and the next begins.
A jump cannot be made from one frequency. It needs many, and the DFT has only its bins to put them in, so the energy lands in all of them. Both stories say the same thing: the lump is the frequency-domain view of the hard cut, and the seam is the time-domain view.
Taper the ends, and the leakage falls
The hard cut is a multiplication. Keeping samples means multiplying the signal by for to and by 0 outside, a window, in the sense of Operations on amplitude (2.2). This one is the rectangular window. Its lump has zeros one bin apart, and the tall middle part, between the first zeros on each side, is the main lobe, 2 bins wide. The small humps beyond it are the side lobes.
Nothing says the window has to be 1. A window that falls smoothly to 0 at both ends has no seam, since both copies end at 0. The Hann window is
which is SciPy’s get_window('hann', N). Its spectrum is three copies of the rectangular lump, one bin apart:
where is the rectangular window’s lump.
Far from the peak the three copies have nearly equal sizes and alternating signs, so the side lobes of the three nearly cancel. The price is the main lobe. The three copies sit one bin apart, so the lump is wider: 4 bins instead of 2.
Two numbers describe a window. The main-lobe width is the distance in bins from the first zero on one side to the first on the other. The highest side lobe is the tallest hump beyond the main lobe, in dB relative to the main lobe’s peak (How big is a signal (1.3) defined dB as of an amplitude ratio). From here on spectra are in dB, because a side lobe at dB is invisible on a linear axis.
The rectangular window has a main lobe of 2 bins and a highest side lobe of dB at ( dB for large ). The Hann window has 4 bins and dB at ( dB for large ).
The picture puts both windows on the tone at . Watch the far bars sink as the window bends from flat to the Hann shape.
Taper the ends, and the leakage falls
The same tone at k₁ = 8.5, N = 32, times a window w[n]. Sizes in dB relative to the tallest bar.
Rectangular window (w[n] = 1, the plain cut): main lobe 2 bins, side lobes from −13.2 dB, and the bars far from the tone still near −20 dB.
Describe this picture
The same tone at , , times a window , with sizes in dB relative to the tallest bar. Two panels. The first is the signal, sample from 0 to 31 against value from −1.2 to 1.2: a dashed line, labelled “window w[n]”, is the window, and stems with dot heads are the windowed tone . The second is the spectrum, frequency in bins from 0 to 16 against dB re tallest bar from −80 to 5: a solid curve, labelled “spectrum of the windowed tone”, is the DTFT of the windowed tone, and the bars, labelled “DFT bins”, each have a filled square on top. A bar below −80 dB is drawn at −80 as a short flat mark. The readouts are the main lobe and the highest side lobe, in dB re the main lobe’s peak.
The clip plays once, in 10 s. It opens on the rectangular window (readouts 2.00 bins and −13.2 dB), with the caption “Rectangular window (w[n] = 1, the plain cut): main lobe 2 bins, side lobes from −13.2 dB, and the bars far from the tone still near −20 dB.” Between 3 and 5.5 s the dashed line bends from flat to the Hann shape, the stems follow it, and the old spectrum fades out as the new one fades in, under the caption “Tapering the window towards 0 at both ends.” The clip holds on the Hann window (4.00 bins and −31.5 dB), with the caption “Hann window: the ends fall smoothly to 0, so there is no jump at the seam. The main lobe doubles to 4 bins, but the side lobes drop to −31.5 dB and keep falling: bar 2 is at −59 dB, where the rectangular window left −20 dB.” When the clip has finished, two buttons, “rectangular” and “Hann”, switch between the windows.
Bar 2 stands at dB under the rectangular window and dB under the Hann window, and bar 16 at dB and in the Hann case at essentially 0, below any floor. Bar 5 goes from to dB.
Two things changed. The two bars at the peak are closer in height, and every far bar sank: bar 2 by about 40 dB, and bar 5 by about 24 dB. The tallest bar also sits below the curve’s peak, by 3.9 dB for the rectangular window and 1.4 dB for the Hann window, which is why the axis reaches +5 dB. I come back to that in Window functions compared (15.2).
When the clip has finished, flip between the two windows and watch the bars: the ones near the peak barely move, and the far ones jump. This is fading a song in and out instead of cutting it. A cut makes a click, and a fade does not.
The maths behind it · diagonal matrices
Windowing multiplies the data vector by the diagonal matrix before the DFT matrix of The DFT as a matrix (13.5). In the frequency domain that diagonal matrix becomes a circulant one: each bin is mixed with its neighbours, which is leakage written as a matrix.
A weak tone beside a strong one
A window pays off when a weak tone sits near a strong one. The Blackman window is
one more cosine than Hann. It pushes the side lobes lower again, to dB, for a main lobe of 6 bins.
Here is the test. The picture below takes kHz and , so the bins are 31.25 Hz apart. The strong tone is at 1010 Hz with amplitude 1. The weak tone is at 1200 Hz and 50 dB weaker, with amplitude 0.0032. I moved the strong tone off 1000 Hz on purpose, because 1000 Hz is bin 32 exactly and would not leak. At 1010 Hz it is bin 32.32, and the weak tone is bin 38.4.
Each level in the picture is the bin’s size divided by half the sum of the window, so a tone on a bin reads 0 dB:
Watch the bars near 1200 Hz as the window changes from rectangular to Hann to Blackman: a peak stands up where the weak tone is.
A weak tone beside a strong one
f_s = 8 kHz, N = 256. A 1010 Hz tone of amplitude 1 and a 1200 Hz tone 50 dB weaker (amplitude 0.0032). Levels in dB re amplitude 1.
Rectangular: near 1200 Hz the strong tone's leakage is at −27 dB, 23 dB above the weak tone. The bars there just slope down; the weak tone is buried.
Describe this picture
One panel for kHz and : a 1010 Hz tone of amplitude 1 and a 1200 Hz tone 50 dB weaker (amplitude 0.0032), with frequency from 800 to 1400 Hz against dB re amplitude 1 from −100 to 5. The DFT bins of the sum are stems with dot heads, labelled “DFT bins”, 31.25 Hz apart. A dashed curve, labelled “strong tone alone”, is the spectrum of the windowed strong tone, and a dotted level at −50 dB is labelled “weak tone’s level”. An upward triangle standing on the axis at 1200 Hz is labelled “weak tone”. The readouts are the window and the leak at 1187.5 Hz: the strong tone alone at bin 38, the bin nearest 1200 Hz, in dB.
The clip plays once, in 14 s. It opens on the rectangular window (leak −26.7 dB), with the caption “Rectangular: near 1200 Hz the strong tone’s leakage is at −27 dB, 23 dB above the weak tone. The bars there just slope down; the weak tone is buried.” After a cross-fade, with a blank caption, it holds on the Hann window (−56.4 dB): “Hann: the leakage near 1200 Hz falls to −56 dB, below the weak tone. The bar at 1187.5 Hz now stands up as a peak, at −47 dB.” After a second cross-fade it holds on the Blackman window (−65.6 dB): “Blackman: the leakage is down at −66 dB and the weak tone stands clear, at −49 dB. The price is a wider main lobe around 1010 Hz.” When the clip has finished, three buttons, “rectangular”, “Hann” and “Blackman”, switch windows and show the caption for each.
Under the rectangular window the strong tone’s leakage near 1200 Hz is 23 dB above the weak tone, and the weak tone is buried. Under the Hann window the leakage falls below the weak tone. Under the Blackman window the weak tone stands clear, and the price is a wider main lobe around 1010 Hz.
Look at the bars at 1125, 1156.25, 1187.5, 1218.75 and 1250 Hz. Under the rectangular window they read , , , and dB: they slope down, and nothing marks the weak tone. Under the Hann window they read , , , and dB, and the bar at 1187.5 Hz is a peak. Under the Blackman window they read , , , and dB, and the peak is clearer still.
Think of a candle next to a floodlight. You see the candle only if the floodlight’s glare falls off fast enough with distance. The strong tone’s leakage is that glare, a skirt that falls away on both sides, and a window with lower side lobes lowers the skirt.
The strong tone’s tallest bar, at 1000 Hz, reads , and dB under the three windows. It is below 0 dB because the tone is not on a bin, and the windows differ only a little there.
The weak tone is 190 Hz from the strong one, which is 6.1 bins. What decides is the strong tone’s leakage at that distance, not its main lobe. Which window for which job is the subject of Window functions compared (15.2).
The maths behind it · kernel density estimation
A window’s spectrum blurs the true spectrum the way a kernel blurs a histogram in kernel density estimation. A box kernel gives sharp but spiky estimates. A smooth kernel trades a little sharpness at the peak for far less spill into the tails.
Worked example
- Which tones leak? At kHz and , a 1000 Hz tone is , a whole bin, so it does not leak. So is any frequency . A tone at 1010 Hz is and leaks.
- A tone halfway between bins. At and , bars 8 and 9 are 0.641 and 0.635, and 59.0 % of the energy is outside bar 8 and its mirror.
- The seam. For the last two samples are and , and the next copy starts at . The cosine itself would be at .
- Rectangular against Hann at . The main lobes are 2.00 and 4.00 bins, and the highest side lobes are and dB. The Hann window gives up one extra bin of main lobe on each side of the peak, in exchange for 18 dB lower side lobes.
- A weak tone. A 1200 Hz tone at dB beside a 1010 Hz tone is hidden by the rectangular window, whose leakage there is dB. It is uncovered by the Hann window ( dB of leakage) and by the Blackman window ( dB).
Where you’ll meet this
Window functions compared (15.2) lines up the whole catalogue of windows with their figures of merit, so you can choose between them. Zero-padding and resolution (15.3) shows that padding with zeros gives more samples of the same lump, and how to read a peak between bins. Reading a spectrum: scaling and units (15.4) explains the scaling of the bars. Spectrograms and the STFT (15.5) puts a window on every frame. Window-method FIR design (19.1) uses the same windows to shape filters.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Tone position in bins | whole: no leakage | |
| What the DFT sees | samples of at | 12.3 §2, 13.1 |
| Rectangular | main lobe 2 bins, highest side lobe −13.3 dB | |
| Hann | 4 bins, −31.5 dB | |
| Blackman | 6 bins, −58.1 dB | |
| Windowing | analyse , to | periodic windows (SciPy default) |
| Weak tone visible | its level above the strong tone’s leakage at its distance | lower side lobes help |