The DFT: a spectrum you can compute
About this lesson
The DTFT gives the spectrum of a sampled signal at every frequency, but a computer can only hold a finite list of samples and work out a finite list of answers. The discrete Fourier transform (DFT) does exactly that: N samples in, N complex numbers out, one per frequency bin k·fs/N. Follow it by hand on 8 samples taken at 8000 samples per second: build a test wave, multiply sample by sample, add, and watch the spectrum X[k] fill in. Then read the amplitudes back, see why the top half of the bins are negative frequencies, check that the DFT is the DTFT sampled every fs/N, and get the same answer from numpy in one line. It helps to know the lessons on complex exponentials and the DTFT in this course first.
Eight numbers in memory
A converter sampled a sound 8000 times a second and kept N = 8 samples, x[0] to x[7]: one millisecond. A computer only ever holds a finite list like this. Which frequencies are inside it?
fs = 8000 samples per second Ts = 1 / fs = 125 µs N = 8 samples -> 8 × 125 µs = 1 ms n 0 1 2 3 4 5 6 7 x[n] 1.50 1.71 0.50 −0.71 −0.50 0.29 0.50 0.71 (the recipe that made them stays hidden for now)
Read the lesson step by step
Eight numbers in memory
A converter sampled a sound 8000 times a second and kept N = 8 samples, x[0] to x[7]: one millisecond. A computer only ever holds a finite list like this. Which frequencies are inside it?
fs = 8000 samples per second Ts = 1 / fs = 125 µs N = 8 samples -> 8 × 125 µs = 1 ms n 0 1 2 3 4 5 6 7 x[n] 1.50 1.71 0.50 −0.71 −0.50 0.29 0.50 0.71 (the recipe that made them stays hidden for now)N samples in, N numbers out
The DFT (discrete Fourier transform) turns N samples into N numbers X[0] to X[N−1]. Each X[k], a bin, measures one test frequency, k·fs/N. Here fs/N = 8000/8 = 1000 Hz, so bins sit 1 kHz apart.
X[k] = Σ x[n] · e^(−j2πkn/N) n=0..N−1 k = 0, 1, ..., N−1 (one number per bin) bin k <-> f = k · fs / N fs / N = 8000 / 8 = 1000 Hz bins: 0, 1000, 2000, ... 7000 Hz N numbers: a spectrum a computer can storeA test wave for bin 1
To measure bin k = 1, make a test wave that does exactly one cycle across the 8 samples: w[n] = cos(2π·1·n/8). In time that is 1000 Hz. Its 8 values fill the test-wave row.
w[n] = cos(2π·k·n / N), k = 1 n 0 1 2 3 4 5 6 7 w[n] 1.00 0.71 0.00 −0.71 −1.00 −0.71 0.00 0.71 k = 1 cycle in N = 8 samples = 1 cycle per 1 ms = 1000 HzMultiply, sample by sample
Multiply each sample by the test wave at the same n: x[0]·w[0], x[1]·w[1], and so on. Where the signal and the test wave rise and fall together, the products come out positive.
p[n] = x[n] · w[n] n x[n] w[n] p[n] 0 1.50 1.00 1.50 1 1.71 0.71 1.21 2 0.50 0.00 0.00 3 −0.71 −0.71 0.50 4 −0.50 −1.00 0.50 5 0.29 −0.71 −0.21 6 0.50 0.00 0.00 7 0.71 0.71 0.50Add them up: X[1]
Add the eight products: 1.50 + 1.21 + 0 + 0.50 + 0.50 − 0.21 + 0 + 0.50 = 4.00. A big sum means a strong match. The same test with a sine wave gives 0.00, so X[1] = 4.
cosine sum Σ x[n]·cos(2π·1·n/8) = 4.00 sine sum Σ x[n]·sin(2π·1·n/8) = 0.00 X[1] = cosine sum − j · sine sum = 4.00 − j·0.00 = 4 N = 8 multiplies and adds per test waveWhy a sine too: X[2]
A cosine test misses a shifted wave, so every bin also tests a sine. For k = 2 the cosine sum is 0.00 but the sine sum is 2.00. X[2] = cos sum − j·sin sum = −2j, a complex number (j = √−1).
k = 2: test waves cos(2π·2·n/8), sin(2π·2·n/8) cosine sum = 0.00 sine sum = 2.00 X[2] = 0.00 − j·2.00 = −2j |X[2]| = 2 e^(−jθ) = cos θ − j·sin θ: that is where the two tests come fromEvery bin, one after another
Repeat for every k from 0 to 7, with a cosine and a sine test each time. The spectrum appears: X = 4, 4, −2j, 0, 0, 0, 2j, 4. Most bins are empty: the signal holds only a few frequencies.
k f (Hz) X[k] |X[k]| 0 0 4 4 1 1000 4 4 2 2000 −2j 2 3 3000 0 0 4 4000 0 0 5 5000 0 0 6 6000 2j 2 7 7000 4 4 8 bins × 8 samples = 64 complex multipliesThe top half is negative frequencies
Bins above N/2 = 4 are really negative frequencies: bin 7 is −1000 Hz, bin 6 is −2000 Hz. For a real signal they mirror the low bins: X[8−k] is the complex conjugate of X[k], so X[6] = 2j.
bin k 0 1 2 3 4 5 6 7 f (Hz) 0 1000 2000 3000 −4000 −3000 −2000 −1000 real x[n] -> X[N−k] = conj(X[k]) X[7] = conj(X[1]) = 4 X[6] = conj(X[2]) = 2j so bins 0 .. N/2 carry all the information (numpy calls this "standard" order)Read the recipe back
Turn bins into amplitudes. X[0]/N = 4/8 = 0.5 is the constant part. For 0 < k < N/2 the amplitude is 2·|X[k]|/N: 1 at 1000 Hz and 0.5 at 2000 Hz. That is exactly how x[n] was made.
constant X[0] / N = 4 / 8 = 0.5 1000 Hz 2·|X[1]| / N = 2·4 / 8 = 1 2000 Hz 2·|X[2]| / N = 2·2 / 8 = 0.5 X[2] = −2j: a sine, not a cosine x[n] = 0.5 + 1·cos(2π·1000·n·Ts) + 0.5·sin(2π·2000·n·Ts)Samples of the DTFT
The DTFT of these 8 samples is a smooth curve over every frequency. The DFT values are exact samples of it, one every fs/N = 1000 Hz. Between bins the curve is not zero; the DFT simply does not look there.
DTFT: X(e^jω) = Σ x[n]·e^(−jωn), any ω DFT: X[k] = X(e^jω) at ω = 2πk/N f (Hz) |DTFT| 0 4.00 500 2.17 1000 4.00 1500 4.56 2000 2.00 2500 1.20 half-way points such as 1500 Hz are not bins, so the DFT never reports themOne line in numpy
In Python, np.fft.fft(x) returns the same 8 complex numbers, and np.fft.fftfreq names each bin's frequency. Done by hand, the DFT costs N² = 64 complex multiplies; the FFT gets the same answer with far fewer.
import numpy as np n = np.arange(8) x = 0.5 + np.cos(np.pi*n/4) + 0.5*np.sin(np.pi*n/2) X = np.fft.fft(x) >>> np.abs(X).round(2) array([4., 4., 2., 0., 0., 0., 2., 4.]) >>> np.fft.fftfreq(8, 1/8000) array([ 0., 1000., 2000., 3000., -4000., -3000., -2000., -1000.]) (Python 3 with numpy)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01You take N = 1024 samples at fs = 48 000 samples per second. How far apart are the DFT bins?
Bins are fs/N apart: 48 000 / 1024 = 46.875 Hz. More samples (a longer recording) means closer bins.
02What does one DFT value X[k] measure?
X[k] = Σ x[n]·cos(2πkn/N) − j·Σ x[n]·sin(2πkn/N): the cosine and sine matches at bin k combined in one complex number.
03With N = 8 and fs = 8000 samples per second, which frequency does bin k = 6 stand for in a real signal?
Bins above N/2 = 4 are negative frequencies: (6 − 8)·1000 Hz = −2000 Hz. That is the order np.fft.fftfreq reports, and for a real signal X[6] is the conjugate of X[2].
04A real signal of N = 8 samples has |X[3]| = 6, and nothing at bin 5 except the mirror of bin 3. What is the amplitude of that sinusoid?
For 0 < k < N/2 the amplitude is 2·|X[k]|/N = 2·6/8 = 1.5. The factor 2 is there because the other half of the sinusoid sits in the mirror bin N − k.
05How is the DFT of a finite list of samples related to its DTFT?
Evaluate the DTFT Σ x[n]·e^(−jωn) at ω = 2πk/N and you get X[k] exactly. Between those points the DTFT keeps going; the DFT just does not report it.
Glossary
- Discrete Fourier transform (DFT)
- X[k] = Σ x[n]·e^(−j2πkn/N) for k = 0 .. N−1: turns N samples into N complex numbers, one per frequency bin.
- Bin
- One output X[k] of the DFT; it stands for the frequency k·fs/N. Bins are fs/N apart.
- Test wave
- The cosine cos(2πkn/N) and sine sin(2πkn/N) that the samples are multiplied by to measure bin k.
- j
- The imaginary unit, √−1 (engineers write j because i means current). X[k] = a − j·b holds the cosine and sine matches together.
- Negative frequency
- Bins above N/2 stand for frequencies (k − N)·fs/N. For a real signal they mirror the low bins: X[N−k] is the conjugate of X[k].
- DTFT
- The discrete-time Fourier transform Σ x[n]·e^(−jωn): the spectrum at every frequency ω. The DFT samples it at ω = 2πk/N.
- FFT
- Fast Fourier transform: a family of algorithms that compute exactly the DFT in about N·log N operations instead of N².
Notes and sources
- The signal is a chosen example: a constant 0.5, a 1000 Hz cosine of amplitude 1 and a 2000 Hz sine of amplitude 0.5, sampled 8000 times a second. Every value, sum and curve in the figure is computed from this model.
- The DFT here uses e^(−j2πkn/N) and no 1/N in front, the same convention as numpy.fft; some books put 1/N or 1/√N in front, which changes the scale of X[k] but not its shape.
- Both test frequencies were chosen to land exactly on bins. A tone between bins (say 1500 Hz) spreads into every bin: that leakage, and windows that tame it, get their own lesson in this module.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., Prentice Hall 1999 (required text of MIT OCW 6.341; ch. 8 the DFT, ch. 9 computing it)
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson 1997
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim): discrete Fourier transform, FFT algorithm
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011): Fourier representations
- J. O. Smith III, Mathematics of the Discrete Fourier Transform, 2nd ed., CCRMA, Stanford University
- J. O. Smith III, Mathematics of the DFT: DFT Definition (ω_k = 2πk·fs/N)
- J. O. Smith III, Mathematics of the DFT: Frequencies in the Cracks (tones between bins leak)
- NumPy documentation: Discrete Fourier Transform (numpy.fft): definition, standard order, Hermitian symmetry for real input
- NumPy documentation: numpy.fft.fft
- NumPy documentation: numpy.fft.fftfreq
- Discrete Fourier transform (Wikipedia): definition; the DFT as a sampling of the DTFT
- Fast Fourier transform (Wikipedia): direct evaluation O(n²), FFT O(n log n)
Quiz Unlocks after the last step.