---
title: How the FFT works
heading: The discrete Fourier transform as N correlations of the signal with sampled sinusoids, what each bin means in hertz, the frequency resolution set by record length, how splitting even and odd samples turns N² work into N log N, the butterfly, and why spectrum analyzers and OFDM radios depend on it
eyebrow: D5 · Lesson 2 · How the FFT works
xp: 40
---

::: hook
The discrete Fourier transform answers a simple question: how much of each frequency does this block of samples contain? Computed directly it takes N² multiplications, which for a million samples is a trillion, far too slow for real time. In 1965 James Cooley and John Tukey published (and others had found before them, Gauss among them) an algorithm that does the same job in about N log₂ N steps: twenty million for a million samples. The fast Fourier transform made digital spectrum analysis, audio compression, medical imaging and OFDM radio practical.
:::

::: play
- Complex multiplications needed for an N-point DFT computed directly ($N^2$) and by a radix-2 FFT ($\frac{N}{2}\log_2N$), on logarithmic axes.
- The gap widens with N: for 16 points the FFT is 8 times faster, for a million about 100,000 times.
- The readouts give the counts and the speedup for the chosen size.
:::

```sim plot
view: graph
x: N = 2..1e7 log
y: M = 1..1e14 log
slider: p = 2..22 step 1 = 10
plot: N^2 | samples: 300 | color: ch3 | label: direct DFT
plot: N/2*log(N)/log(2) | samples: 300 | label: FFT
marker: x = 2^p | label: N = 2^p
readout: direct multiplications = (2^p)^2 | digits: 4
readout: FFT multiplications = 2^p/2*p | digits: 4
readout: speedup = 2*2^p/p | digits: 4
```

::: predict
## An audio analyzer takes 1024 samples at 48 kHz. How far apart are the frequencies of its DFT bins?

- [ ] 1 Hz
- [x] About 47 Hz
- [ ] 1024 Hz
- [ ] 48 kHz

::: reveal
The bins are spaced $f_s/N$ = 48,000/1024 = 46.9 Hz apart. That's the frequency resolution, and it's the reciprocal of the record's length, 1024/48,000 = 21.3 ms. To resolve tones 5 Hz apart you need at least 0.2 s of signal, whatever the algorithm.
:::
:::

::: nameit
## The DFT

$$X_k = \sum_{n=0}^{N-1}x_n e^{-j2\pi kn/N}, \qquad k = 0, 1, \dots, N - 1$$

Each $X_k$ is the correlation of the signal with a complex sinusoid that completes k cycles over the record, so it measures the content near frequency $kf_s/N$. Bins are spaced $f_s/N$ apart: the resolution is one over the record length. For a real signal, $X_{N-k} = X_k^*$, so only bins 0 to N/2 are independent; bins above N/2 stand for negative frequencies.

## The fast algorithm

Split the sum into even and odd samples:

$$X_k = E_k + e^{-j2\pi k/N}O_k$$

where $E_k$ and $O_k$ are the N/2-point DFTs of the even and odd samples. Since those repeat with period N/2, one pair of half-size transforms gives all N outputs, with $X_{k+N/2} = E_k - e^{-j2\pi k/N}O_k$. That pair of equations is the **butterfly**: one complex multiplication by the **twiddle factor**, an addition and a subtraction.

Splitting again and again, $\log_2N$ times, leaves N/2 butterflies per stage:

$$\frac{N}{2}\log_2N \text{ complex multiplications}$$

against $N^2$ directly: a speedup of $2N/\log_2N$.
:::

::: example
## How much work does a 1024-point FFT save?

::: solution
1. **Direct DFT.** Each of the 1024 outputs takes 1024 multiplications:
   $$1024^2 = 1\,048\,576$$
2. **FFT.** 10 stages of 512 butterflies:
   $$10 \times 512 = 5120$$
3. **Speedup.**
   $$\begin{aligned} \frac{1\,048\,576}{5120} &= \frac{2 \times 1024}{10} \\ &= 205 \end{aligned}$$

**Answer:** the FFT takes 5120 multiplications instead of 1,048,576, a speedup of about 205.
:::
:::

::: example
## With 4096 samples at 1 MHz, which bin holds a 123 kHz tone, and what's the resolution?

::: solution
1. **Bin spacing.**
   $$\begin{aligned} \Delta f &= \frac{10^6\ \text{Hz}}{4096} \\ &= 244.1\ \text{Hz} \end{aligned}$$
2. **Bin of the tone.**
   $$\begin{aligned} k &= \frac{123\,000\ \text{Hz}}{244.14\ \text{Hz}} \\ &= 503.8 \end{aligned}$$
3. **Between bins.** The tone falls between bins 503 and 504, mostly in 504. (Lesson 3 shows what happens to a tone that falls between bins.)

**Answer:** the resolution is 244.1 Hz, and the tone falls between bins 503 and 504, mostly in 504.
:::
:::

::: example
## 1000 samples at 10 kHz are zero-padded to a 4096-point FFT. What is the bin spacing, and what is the real frequency resolution?

::: solution
1. **Bin spacing.**
   $$\begin{aligned} \Delta f &= \frac{10\,000\ \text{Hz}}{4096} \\ &= 2.44\ \text{Hz} \end{aligned}$$
2. **Record length.** The record still lasts only 0.1 s.
3. **Real resolution.** Two tones closer than about one over the record length still merge:
   $$\frac{1}{0.1\ \text{s}} = 10\ \text{Hz}$$
4. **What padding does.** Zero padding interpolates the spectrum; only a longer record resolves finer detail.

**Answer:** the bins are 2.44 Hz apart, but the real resolution is still about 10 Hz.
:::
:::

::: check
given: s = 8 | 44.1 | 48 | 1000; N = 256 | 1024 | 8192
answer: s*1000/N
unit: Hz
tolerance: 0.5%

## A {N}-point DFT is taken of samples at {s} kHz. How far apart are its bins?

::: right
Right. $f_s/N$ = {answer} Hz.
:::

::: misses
- [N/(s*1000)] That's the record length in seconds; the spacing is its reciprocal.
- [any] Not yet. Sampling rate over the number of points.
:::
:::

::: check
given: s = 8 | 48; N = 512 | 2048; k = 10 | 100 | 200
answer: k*s*1000/N
unit: Hz
tolerance: 0.5%

## In a {N}-point DFT of samples at {s} kHz, what frequency does bin {k} stand for?

::: right
Right. $kf_s/N$ = {answer} Hz.
:::

::: misses
- [any] Not yet. Bin number times the spacing.
:::
:::

::: check
given: p = 8 | 10 | 12 | 16 | 20
answer: 2*2^p/p
tolerance: 0.5%

## How many times fewer multiplications does a radix-2 FFT of $2^{\given{p}}$ points need than a direct DFT?

::: right
Right. $N^2/(\frac{N}{2}\log_2N) = 2N/\log_2N$ = {answer}.
:::

::: misses
- [2^p/p] Each butterfly does one multiplication for two outputs: N/2 per stage.
- [any] Not yet. Compare $N^2$ with $\frac{N}{2}\log_2N$.
:::
:::

::: check
given: p = 6 | 10 | 14
answer: 2^p/2*p
tolerance: 0.5%

## How many butterflies does a radix-2 FFT of $2^{\given{p}}$ points compute in all?

::: right
Right. {p} stages of $2^{\given{p}}/2$: {answer}.
:::

::: misses
- [2^p*p] Each butterfly makes two outputs: N/2 per stage.
- [any] Not yet. Stages times butterflies per stage.
:::
:::

::: check
kind: parts
given: N = 256 | 1024 | 8192

## A transform has {N} points.

::: part
answer: N^2/(N/2*log(N)/log(2))
tolerance: 0.5%

### How many times fewer complex multiplies does the FFT ($\tfrac N2\log_2N$) need than the direct DFT ($N^2$)?

::: misses
- [any] $N^2/(\tfrac N2\log_2N)$.
:::
:::

::: part
answer: log(N)/log(2)
tolerance: 0.5%

### How many stages does a radix-2 FFT have?

::: misses
- [any] $\log_2N$.
:::
:::
:::

::: check
kind: parts
given: N = 1000 | 3000; fs = 10 | 48 | 1000; P = 4096 | 16384

## {N} samples at {fs} kHz are zero-padded to {P} points.

::: part
answer: fs*1000/P
unit: Hz
tolerance: 0.5%

### What is the bin spacing?

::: misses
- [any] $f_s/P$.
:::
:::

::: part
answer: fs*1000/N
unit: Hz
tolerance: 0.5%

### What frequency resolution does the record really have?

::: misses
- [fs*1000/P] Padding doesn't lengthen the record.
- [any] $1/T = f_s/N$.
:::
:::
:::

::: explain
## Why does splitting a DFT into even and odd samples save so much work?

::: model
The DFT of N points multiplies every sample by every frequency's sinusoid, which is N² products. Splitting the samples into even and odd halves turns the sum into two DFTs of N/2 points each, and because those half-size transforms repeat with period N/2, a single set of them serves both the lower and upper halves of the outputs, combined with one extra multiplication by a twiddle factor. Two half-size problems cost half as much as one full-size problem, and repeating the split until only single points remain takes log₂N levels, each costing about N/2 multiplications, so the total is N/2 times log₂N instead of N².
:::
:::

::: practice

::: check
given: s = 1 | 100000; N = 64 | 256 | 1024 | 4096 | 65536
answer: s*1000/N
unit: Hz
tolerance: 0.5%

## Bin spacing: {N} points at {s} kHz?

::: misses
- [any] $f_s/N$.
:::
:::

::: check
given: p = 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 20 | 24
answer: 2*2^p/p
tolerance: 0.5%

## FFT speedup for $2^{\given{p}}$ points?

::: misses
- [any] $2N/\log_2N$.
:::
:::
:::
