Skip to content

Windowing & spectral leakage

See why a tone between bins spreads into every bin, then taper the record's ends so a weak tone beside a strong one stands out.

Before this13.2 · 14.4 · 5 more
Chapter 15 · Lesson 1 of 6

First, the picture

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.

tone at k₁
8.00 bins
outside the peak bar
0.0 %
0.00 / 13.00 s
Describe this picture

Two panels for N=32N = 32 samples of cos⁡(2πk1n/32)\cos(2\pi k_1n/32). The first is the signal, sample nn from 0 to 31 against x[n]x[n] 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 ÷ (N/2)(N/2) 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 N/2N/2; 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 k1k_1. The readouts are the tone at k1k_1 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 k1=8k_1=8 (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 k1k_1 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 k1k_1 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 k1k_1 = 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 k1=f1N/fsk_1=f_1N/f_s, where f1f_1 is its frequency in hertz, NN the number of samples and fsf_s the sampling rate. On a bin, k1k_1 is a whole number. The picture at the top of this page keeps N=32N=32 samples of cos⁡(2πk1n/32)\cos(2\pi k_1n/32) and lets k1k_1 move between whole values.

At k1=8k_1=8, 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 NN samples turns each of its two spectral arrows into a lump, the spectrum W(ejΩ)W(e^{j\Omega}) 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 k1=8k_1=8 the peak sits on bin 8 and every other bin sits on a zero.

At k1=8.25k_1=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. 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 k1=8.5k_1=8.5, 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 k1=9.3k_1=9.3 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 NN 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.

jumpk₁ = 8 (dots)k₁ = 8.5 (squares)-3203263sample n
Fig. The DFT treats its N samples as one period of a repetition (13.1). With 8 whole cycles the copies join smoothly. With 8.5 the cosine stops half a cycle short: after 0.98 and 0.10 the next copy starts at 1.00, where the tone itself would be at −1.00. A jump needs many frequencies, so the energy spreads into every bin.

A jump cannot be made from one frequency. It needs many, and the DFT has only its NN 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 NN samples means multiplying the signal by w[n]=1w[n]=1 for n=0n=0 to N−1N-1 and by 0 outside, a window, in the sense of Operations on amplitude (2.2). This one is the rectangular window. Its lump WW 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

w[n]=0.5−0.5cos⁡ ⁣(2πnN),n=0,…,N−1,w[n]=0.5-0.5\cos\!\left(\frac{2\pi n}{N}\right),\qquad n=0,\dots,N-1,

which is SciPy’s get_window('hann', N). Its spectrum is three copies of the rectangular lump, one bin apart:

0.5 W(ejΩ)−0.25 W(ej(Ω−2π/N))−0.25 W(ej(Ω+2π/N)),\begin{aligned} &0.5\,W(e^{j\Omega})\\ &-0.25\,W\big(e^{j(\Omega-2\pi/N)}\big)\\ &-0.25\,W\big(e^{j(\Omega+2\pi/N)}\big), \end{aligned}

where W(ejΩ)W(e^{j\Omega}) 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 20log⁡1020\log_{10} of an amplitude ratio). From here on spectra are in dB, because a side lobe at −31-31 dB is invisible on a linear axis.

The rectangular window has a main lobe of 2 bins and a highest side lobe of −13.2-13.2 dB at N=32N=32 (−13.26-13.26 dB for large NN). The Hann window has 4 bins and −31.5-31.5 dB at N=32N=32 (−31.47-31.47 dB for large NN).

The picture puts both windows on the tone at k1=8.5k_1=8.5. 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.

main lobe
2.00 bins
highest side lobe
−13.2 dB
0.00 / 10.00 s
Describe this picture

The same tone at k1=8.5k_1 = 8.5, N=32N = 32, times a window w[n]w[n], with sizes in dB relative to the tallest bar. Two panels. The first is the signal, sample nn 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 w[n]x[n]w[n]x[n]. 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 −19.6-19.6 dB under the rectangular window and −59.3-59.3 dB under the Hann window, and bar 16 at −20.2-20.2 dB and in the Hann case at essentially 0, below any floor. Bar 5 goes from −16.0-16.0 to −40.5-40.5 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 diag(w[0],…,w[N−1])\mathrm{diag}(w[0],\dots,w[N-1]) before the DFT matrix F\mathbf{F} 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

w[n]=0.42−0.5cos⁡ ⁣(2πnN)+0.08cos⁡ ⁣(4πnN),\begin{aligned} w[n]&=0.42-0.5\cos\!\left(\frac{2\pi n}{N}\right)\\ &\quad+0.08\cos\!\left(\frac{4\pi n}{N}\right), \end{aligned}

one more cosine than Hann. It pushes the side lobes lower again, to −58.1-58.1 dB, for a main lobe of 6 bins.

Here is the test. The picture below takes fs=8f_s=8 kHz and N=256N=256, 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:

∣X∣12∑nw[n].\frac{\lvert X\rvert}{\tfrac12\sum_{n}w[n]}.

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.

window
rectangular
leak at 1187.5 Hz
−26.7 dB
0.00 / 14.00 s
Describe this picture

One panel for fs=8f_s = 8 kHz and N=256N = 256: 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 −22.9-22.9, −25.1-25.1, −27.2-27.2, −27.8-27.8 and −29.3-29.3 dB: they slope down, and nothing marks the weak tone. Under the Hann window they read −44.8-44.8, −53.7-53.7, −47.3-47.3, −55.8-55.8 and −60.2-60.2 dB, and the bar at 1187.5 Hz is a peak. Under the Blackman window they read −58.6-58.6, −68.7-68.7, −49.3-49.3, −52.8-52.8 and −60.0-60.0 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 −1.5-1.5, −0.6-0.6 and −0.4-0.4 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

  1. Which tones leak? At fs=8f_s=8 kHz and N=256N=256, a 1000 Hz tone is k1=1000×256/8000=32k_1=1000\times256/8000=32, a whole bin, so it does not leak. So is any frequency kfs/Nkf_s/N. A tone at 1010 Hz is k1=32.32k_1=32.32 and leaks.
  2. A tone halfway between bins. At N=32N=32 and k1=8.5k_1=8.5, bars 8 and 9 are 0.641 and 0.635, and 59.0 % of the energy is outside bar 8 and its mirror.
  3. The seam. For k1=8.5k_1=8.5 the last two samples are x[30]=0.981x[30]=0.981 and x[31]=0.098x[31]=0.098, and the next copy starts at x[0]=1x[0]=1. The cosine itself would be at cos⁡17π=−1\cos 17\pi=-1.
  4. Rectangular against Hann at N=32N=32. The main lobes are 2.00 and 4.00 bins, and the highest side lobes are −13.2-13.2 and −31.5-31.5 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.
  5. A weak tone. A 1200 Hz tone at −50-50 dB beside a 1010 Hz tone is hidden by the rectangular window, whose leakage there is −26.7-26.7 dB. It is uncovered by the Hann window (−56.4-56.4 dB of leakage) and by the Blackman window (−65.6-65.6 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

QuantityFormulaNotes
Tone position in binsk1=f1N/fsk_1=f_1N/f_swhole: no leakage
What the DFT seessamples of 12W(ej(Ω−Ω1))+12W(ej(Ω+Ω1))\tfrac12W(e^{j(\Omega-\Omega_1)})+\tfrac12W(e^{j(\Omega+\Omega_1)}) at Ωk=2πk/N\Omega_k=2\pi k/N12.3 §2, 13.1
Rectangularw[n]=1w[n]=1main lobe 2 bins, highest side lobe −13.3 dB
Hannw[n]=0.5−0.5cos⁡(2πn/N)w[n]=0.5-0.5\cos(2\pi n/N)4 bins, −31.5 dB
Blackmanw[n]=0.42−0.5cos⁡(2πn/N)+0.08cos⁡(4πn/N)w[n]=0.42-0.5\cos(2\pi n/N)+0.08\cos(4\pi n/N)6 bins, −58.1 dB
Windowinganalyse w[n]x[n]w[n]x[n], n=0n=0 to N−1N-1periodic windows (SciPy default)
Weak tone visibleits level above the strong tone’s leakage at its distancelower side lobes help

End of lesson 15.1

Where to go next.

Phasorium
LibraryEvery lesson, in order

Parts

About Phasorium
Look