Skip to content

The DFT

Turn N samples into N bins, each one the signal multiplied by a turning probe and added up, then read bins as hertz, mirrors and resolution.

Before this12.3 · 13.1 · 5 more
Chapter 13 · Lesson 2 of 6

First, the picture

Eight samples of two cosines, each multiplied by a turning probe and added up. Watch a bar rise only where a probe matches a cosine.

Each bin: the signal times a probe, added up

N = 8. x[n] = cos(2πn/8) + 0.5 cos(2π·3n/8). Probe k turns k/8 of a turn per sample.

k = 0: no turning, so X[0] is the plain sum of the samples, 0. x has no average.

bin k
0
X[k]
0.00
0.00 / 19.50 s
Describe this picture

Three panels, for N=8N=8 samples of x[n]=cos⁡(2πn/8)+0.5cos⁡(2π⋅3n/8)x[n]=\cos(2\pi n/8)+0.5\cos(2\pi\cdot3n/8), where probe kk turns k/8k/8 of a turn per sample. The first is the signal, drawn as stems with dot heads, x[n]x[n] from −1.6 to 1.6 against the sample nn from 0 to 7. The second is the complex plane, real against imaginary. It places the eight arrows x[n]e−j2πkn/8x[n]e^{-j2\pi kn/8} nose to tail: the arrow being added in the accent colour, the earlier ones in the text colour, and the sum so far as a filled square at the tip, labelled X[k]X[k]. The third is the bins strip, X[k]X[k] from −1 to 5 against the bin kk from 0 to 7, one bar for each finished bin. The readouts are the bin kk and X[k]X[k].

The clip plays once, in 19.5 s, and holds on its last frame. Each bin builds its chain one arrow at a time over 1 s and holds, while the caption says what the probe does. Bin 0 reads 0.00, bin 1 reads 4.00, bin 2 reads 0.00, bin 3 reads 2.00 and bin 4 reads 0.00. For bin 1 the chain’s running sum passes through 1.51.5, 1.75−0.25j1.75-0.25j, 1.75−0.25j1.75-0.25j, 22, 3.53.5, 3.75−0.25j3.75-0.25j, 3.75−0.25j3.75-0.25j and ends at 4. In the last 4 s bins 5, 6 and 7 rise together as hatched bars, labelled “mirror of 3”, “mirror of 2” and “mirror of 1”, with readouts 2.00, 0.00 and 4.00. The last caption reads “Bins light up where the probe matches a frequency in x. Bins 5 to 7 mirror bins 3 to 1: for real x, X[8 − k] = X[k]*.” Once the clip has finished, tapping a bar, or the arrow keys, rebuilds that bin’s chain.

A signal of NN samples, NN numbers of spectrum

Sampling the spectrum (13.1) ended with a count. A computer keeps NN samples of a signal, and it can keep NN values of its spectrum, one at each Ωk=2πk/N\Omega_k=2\pi k/N. Those NN values are the discrete Fourier transform, or DFT.

For a signal x[n]x[n] of NN samples, n=0,…,N−1n=0,\dots,N-1, the DFT gives NN values, one for each kk from 0 to N−1N-1:

X[k]=∑n=0N−1x[n] e−j2πkn/N,X[k]=\sum_{n=0}^{N-1}x[n]\,e^{-j2\pi kn/N},

and the signal comes back with

x[n]=1N∑k=0N−1X[k] ej2πkn/N.x[n]=\frac1N\sum_{k=0}^{N-1}X[k]\,e^{j2\pi kn/N}.

There is no scale on the forward transform and a 1/N1/N on the way back, which is also what NumPy does. Many books write WNknW_N^{kn} for e−j2πkn/Ne^{-j2\pi kn/N}, with WN=e−j2π/NW_N=e^{-j2\pi/N}.

Read the forward sum as a recipe. The arrow e−j2πkn/Ne^{-j2\pi kn/N} is probe kk: it turns back k/Nk/N of a turn for every sample. Multiply each sample by the probe’s arrow for that sample, add the results, and you have bin kk, the number X[k]X[k]. It is the winding of Fourier series coefficients (7.2) and the chain of The DTFT (12.2), now with NN fixed probes. When xx has at most NN samples, bin kk is the DTFT sampled at Ωk\Omega_k.

Each bin: the signal times a probe, added up

I use one signal of N=8N=8 samples: x[n]=cos⁡(2πn/8)+0.5cos⁡(2π⋅3n/8)x[n]=\cos(2\pi n/8)+0.5\cos(2\pi\cdot3n/8). It is two cosines, one that turns 1 time in 8 samples and one, half as big, that turns 3 times. Its samples are 1.51.5, 0.3540.354, 00, −0.354-0.354, −1.5-1.5, −0.354-0.354, 00 and 0.3540.354.

The picture at the top of the page uses this signal. I will follow it bin by bin.

The probe for k=0k=0 does not turn, so X[0]X[0] only adds up the samples, and these eight cancel: xx has no average. The probe for k=1k=1 turns with the first cosine. That part of every arrow points the same way and adds up, and the rest closes into a loop, so X[1]=4X[1]=4, half of 8 times the cosine’s size 1.

No part of xx turns at the rate of probe 2, so its chain comes back to 0. Probe 3 matches the second cosine, and X[3]=2X[3]=2, half of 8 times 0.5. Bin 4 is 0. Bins 5, 6 and 7 come last, as mirrors of bins 3, 2 and 1.

When the clip has finished, tap a bar, or use the arrow keys, to rebuild that bin’s chain. Tap bin 5 and watch it: “k = 5 turns like k = −3: the mirror of bin 3. X[5] = 2.”

Why does a chain close into a loop? A probe and a cosine that do not turn at matching rates leave arrows pointing all round the circle, and equal steps around the circle add to 0 (the roots of unity of Complex numbers for signals, 3.3). Only a part that the probe turns to a constant direction survives. Think of pushing a swing: pushes in step with the swing add up, and pushes out of step cancel.

The maths behind it · inner products

Bin kk is the inner product ⟨x,ek⟩=∑nx[n]ek∗[n]\langle\mathbf{x},\mathbf{e}_k\rangle=\sum_nx[n]e_k^*[n] of the signal with the probe vector ek[n]=ej2πkn/Ne_k[n]=e^{j2\pi kn/N}. It measures how much of the probe the signal contains.

A bin is a complex number

Bins are complex in general. The instrument’s signal gave real bins, because its two cosines are even about n=0n=0. A signal that starts at n=0n=0 and runs on one side does not.

Take x=1,1,1,1,0,0,0,0x=1,1,1,1,0,0,0,0. Its first four samples are the only ones that count, so bin 1 is ∑n=03e−jπn/4=1+(0.707−0.707j)+(−j)+(−0.707−0.707j)\sum_{n=0}^{3}e^{-j\pi n/4}=1+(0.707-0.707j)+(-j)+(-0.707-0.707j), which is 1−2.414j1-2.414j. Its size is 1+2.4142=2.613\sqrt{1+2.414^2}=2.613 and its angle is −67.5°-67.5° (−0.375π-0.375\pi rad). The size says how much of probe 1 the signal has, and the angle says where in its turn.

All eight bins are 44, 1−2.414j1-2.414j, 00, 1−0.414j1-0.414j, 00, 1+0.414j1+0.414j, 00, 1+2.414j1+2.414j. The sizes are 44, 2.6132.613, 00, 1.0821.082, 00, 1.0821.082, 00, 2.6132.613, and the angles are 00, −67.5°-67.5°, −-, −22.5°-22.5°, −-, 22.5°22.5°, −-, 67.5°67.5°. A dash marks a bin of size 0, which has no angle.

To check the inverse at n=0n=0, add all eight bins and divide by 8. The imaginary parts cancel in pairs, the real parts add to 8, and x[0]=8/8=1x[0]=8/8=1.

Bins in hertz, and the mirror

Bin kk is the frequency at which probe kk turns: k/Nk/N of a turn per sample, which is Ωk=2πk/N\Omega_k=2\pi k/N rad/sample. Sampled at fsf_s, that is

fk=kfsN Hz.f_k=\frac{k f_s}{N}\ \text{Hz}.

For N=8N=8 and fs=8f_s=8 kHz the bins are 0, 1, 2, 3 and 4 kHz. Bin N/2N/2 is fs/2f_s/2, the Nyquist frequency of Sampling & aliasing (10.1).

Bins above N/2N/2 follow the rule of Frequency in discrete time (12.1). A probe that turns more than half a turn per sample is the same as one that turns backward by the rest, so bin kk with k>N/2k>N/2 is the frequency (k−N)fs/N(k-N)f_s/N. Bins 5, 6 and 7 are −3-3, −2-2 and −1-1 kHz.

negative frequencies0123456701000200030004000−3000−2000−1000bin k (top row)frequency f_k (Hz, bottom row)
Fig. Bin k is at k·f_s/N. Past N/2 the probe turns more than half a turn per sample, so bins 5, 6, 7 are −3, −2, −1 kHz. Bin 4 is f_s/2.

Bin 4 sits on the boundary. This page writes it as +fs/2+f_s/2, but NumPy’s fftfreq lists it as −fs/2-f_s/2, and the two are the same frequency.

The mirror in the last frame of the first instrument follows from the definition. Put N−kN-k in place of kk:

e−j2π(N−k)n/N=e−j2πn ej2πkn/N=ej2πkn/N,e^{-j2\pi(N-k)n/N}=e^{-j2\pi n}\,e^{j2\pi kn/N}=e^{j2\pi kn/N},

because e−j2πn=1e^{-j2\pi n}=1 for whole nn. This is the conjugate of probe kk. If x[n]x[n] is real, then X[N−k]=X∗[k]X[N-k]=X^*[k]: the sizes of the bins mirror about N/2N/2, and the angles flip sign. This is the DFT form of the real-signal symmetry of Properties of the DTFT (12.3), and it is why a real signal’s information sits in bins 0 to N/2N/2. For the 1, 1, 1, 1, 0, 0, 0, 0 signal above, bins 1 and 7 are 1−2.414j1-2.414j and 1+2.414j1+2.414j.

Two tones 100 Hz apart, more and more bins

Now the other question: how close can two frequencies be before the bins cannot tell them apart? Bins sit Δf=fs/N\Delta f=f_s/N apart, so a longer record gives more of them. With fs=8f_s=8 kHz and NN samples the record lasts N/fsN/f_s s, and Δf\Delta f is one over that time in seconds.

The next picture records two tones 100 Hz apart, at 1000 and 1100 Hz, and doubles NN three times. Watch when the two tones stop sharing a bar.

Two tones 100 Hz apart, more and more bins

f_s = 8 kHz, tones at 1000 and 1100 Hz. Sizes |X[k]| relative to N/2, bins from 0 to 2000 Hz.

N = 20: bins 400 Hz apart. Both tones fall into one lump: one peak, at 1200 Hz.

N
20
bin spacing f_s/N
400.0 Hz
0.00 / 14.00 s
Describe this picture

One panel at fs=8f_s=8 kHz: one thin bar for each bin, ∣X[k]∣\lvert X[k]\rvert divided by N/2N/2 from 0 to 2 against the frequency fkf_k from 0 to 2000 Hz. Dividing by N/2N/2 makes a cosine of size 1 read 1, whatever NN is. Two small upward triangles under the axis, at 1000 and 1100 Hz, are labelled “tones”. The readouts are NN and the bin spacing fs/Nf_s/N.

The clip plays once, in 14 s, and holds on its last frame; between holds the old bars fade out as the new ones fade in. At N=20N=20, bins 400.0 Hz apart, both tones fall into one lump: the tallest bar is at 1200 Hz, size 1.539, and a bar at 800 Hz, size 0.766, is the lump’s other side. At N=40N=40, 200 Hz apart, there is still one peak, 1.216 at 1000 Hz, with a shoulder of 0.658 at 1200 Hz. At N=80N=80, 100.0 Hz apart, two bars of size 1 stand at 1000 and 1100 Hz and every other bar is 0. At N=160N=160, 50.0 Hz apart, an empty bin sits between the tones, and the caption reads “Two tones need bin spacing no larger than their gap: N ≥ f_s/gap.” Once the clip has finished, dragging across the plot, or the arrow keys, sets NN from 16 to 320; at N=100N=100 the spacing is 80.0 Hz.

When the clip has finished, drag across the plot, or use the arrow keys, to set NN yourself, from 16 to 320.

Notice what the tones do at each stage. They share a bin at 400 Hz spacing and smear into a shoulder at 200 Hz. They take adjacent bins at 100 Hz, and at 50 Hz an empty bin separates them. The rule is Δf=fs/N\Delta f=f_s/N at or below the gap, which is N≥fs/gapN\ge f_s/\text{gap}. For a gap of 100 Hz at 8 kHz that is N≥80N\ge80, a record of 80/8000=0.0180/8000=0.01 s. Put another way, a record needs to be at least 1/gap1/\text{gap} seconds long. A ruler marked only every centimetre has the same limit: it cannot tell 3.2 cm from 3.3 cm.

Both tones here sit exactly on a bin at the chosen NN (they are multiples of 100 Hz), so the picture is clean. A tone between two bins spreads into all of them, which is called leakage, and Windowing & spectral leakage (15.1) takes it up.

The maths behind it · covariance and the periodogram

A bin is the signal’s covariance with a cosine and a sine at that frequency: the real and imaginary parts. Squared and scaled, bins form the periodogram of The periodogram (25.1).

Energy in time and in bins

The bins keep all the energy of the signal, with one factor. Parseval’s relation, which Properties of the DTFT (12.3) wrote as an integral, is a sum here:

∑n=0N−1∣x[n]∣2=1N∑k=0N−1∣X[k]∣2.\sum_{n=0}^{N-1}\lvert x[n]\rvert^2=\frac1N\sum_{k=0}^{N-1}\lvert X[k]\rvert^2 .

For the instrument’s signal the time side is 55, and the bin side is (16+4+4+16)/8=5(16+4+4+16)/8=5.

Worked example

  1. By hand. For x=1,1,1,1,0,0,0,0x=1,1,1,1,0,0,0,0, bin 1 is 1−2.414j1-2.414j, size 2.613 and angle −67.5°-67.5°. All the bins are above. Parseval: the time side is 44, and the sizes squared are 1616, 6.8286.828, 00, 1.1721.172, 00, 1.1721.172, 00, 6.8286.828, which add to 32, so the bin side is 32/8=432/8=4.
  2. The instrument’s signal. X=0X=0, 44, 00, 22, 00, 22, 00, 44, and 5=40/85=40/8.
  3. Which bin? A 1 kHz tone at fs=8f_s=8 kHz with N=64N=64 lands in bin k=Nf/fs=64×1000/8000=8k=Nf/f_s=64\times1000/8000=8.
  4. Bin spacing. At fs=48f_s=48 kHz and N=4096N=4096 the spacing is 48000/4096=11.7248000/4096=11.72 Hz.
  5. Resolution. To separate tones 100 Hz apart at 8 kHz you need N≥8000/100=80N\ge8000/100=80 samples, a record of 0.01 s.
  6. Hertz of bins. For N=8N=8 and fs=8f_s=8 kHz the bins are 00, 11, 22, 33, 44, −3-3, −2-2, −1-1 kHz.

Where you’ll meet this

Properties of the DFT (13.3) shows what a shift or a reversal does to the bins, once indices wrap round modulo NN. The DFT as a matrix (13.5) writes all NN probes as the rows of one matrix. The sum above costs N2N^2 multiplications, and The FFT (14.1) gets the same bins with far fewer. Windowing & spectral leakage (15.1) handles tones between bins, and Zero-padding and resolution (15.3) shows what padding with zeros does and does not give you.

Reference card

QuantityFormulaNotes
DFTX[k]=∑n=0N−1x[n]e−j2πkn/N=∑nx[n]WNknX[k]=\sum_{n=0}^{N-1}x[n]e^{-j2\pi kn/N}=\sum_nx[n]W_N^{kn}WN=e−j2π/NW_N=e^{-j2\pi/N}; no scale
Inversex[n]=1N∑k=0N−1X[k]ej2πkn/Nx[n]=\frac1N\sum_{k=0}^{N-1}X[k]e^{j2\pi kn/N}NumPy’s convention
A bincorrelation of xx with probe kkthe DTFT at Ωk=2πk/N\Omega_k=2\pi k/N
Bin in hertzfk=kfs/Nf_k=kf_s/N; k>N/2k>N/2 means (k−N)fs/N(k-N)f_s/Nbin N/2N/2 is fs/2f_s/2
Bin spacingΔf=fs/N=1/(record length in s)\Delta f=f_s/N=1/(\text{record length in s})
Resolutiontwo tones need Δf\Delta f no larger than their gapN≥fs/gapN\ge f_s/\text{gap}
Real xxX[N−k]=X∗[k]X[N-k]=X^*[k]sizes mirror about N/2N/2
Parseval∑n=0N−1∣x[n]∣2=1N∑k=0N−1∣X[k]∣2\sum_{n=0}^{N-1}\lvert x[n]\rvert^2=\frac1N\sum_{k=0}^{N-1}\lvert X[k]\rvert^2

End of lesson 13.2

Where to go next.

Phasorium
LibraryEvery lesson, in order

Parts

About Phasorium
Look