ENع
←The DFT
Lesson 28 · From the DTFT to the FFT

The DFT: a spectrum you can compute

15 min11 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 28 · Interactive
01 / 11

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)
Loading figure…
or ← →
Read the lesson step by step
  1. 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)
  2. 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 store
  3. A 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 Hz
  4. Multiply, 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.50
  5. Add 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 wave
  6. Why 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 from
  7. Every 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 multiplies
  8. The 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)
  9. 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)
  10. 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 them
  11. One 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)

Quiz Unlocks after the last step.