---
title: The FFT as an algorithm
heading: Splitting a transform in half, again and again
eyebrow: A13 · Lesson 47 · The FFT as an algorithm
xp: 40
---

::: hook
C9 and A10 used the FFT as a black box that computes a DFT quickly. Here's what's inside. A DFT of N points is N sums of N terms: $N^2$ multiplications. The fast Fourier transform notices that an N-point DFT is two N/2-point DFTs (of the even and odd samples) glued together with N/2 extra multiplications, and applies that idea recursively. The cost drops to $\frac N2\log_2N$, a 200-fold saving at 1024 points and 100 000-fold at a million. The same trick makes fast convolution, fast filtering and spectral methods for differential equations possible.
:::

::: play
- The curves count complex multiplications for a direct DFT ($N^2$) and a radix-2 FFT ($\frac N2\log_2N$), on log scales.
- At small N they're close; by N = 1024 the FFT is 200 times faster, and the gap keeps widening in proportion to $N/\log N$.
- The readouts give both counts and the speedup at the marked N.
:::
:::

```sim plot
view: graph
x: N = 2..1M log
y: multiplications = 1..1T log
slider: n = 1..20 step 1 = 10
plot: N^2 | label: direct DFT, N²
plot: N/2*ln(N)/ln(2) | color: ch3 | label: FFT, (N/2) log₂ N
marker: N = 2^n | label: N
readout: N = 2^n
readout: direct = (2^n)^2
readout: FFT = 2^n/2*n
readout: speedup = 2*2^n/n
```

::: predict
## An FFT of $2^{20}$ (about a million) points is about how many times faster than a direct DFT?

- [ ] 20 times
- [ ] 1000 times
- [x] 100 000 times
- [ ] A million times

::: reveal
About $10^5$: $\frac{N^2}{(N/2)\log_2N} = \frac{2N}{\log_2N} = \frac{2 \times 2^{20}}{20} = 104\,858$. What would take a direct DFT about 20 minutes at a billion multiplications per second takes the FFT 10 ms.
:::
:::

::: nameit
## The DFT

$$X_k = \sum_{n=0}^{N-1}x_nW_N^{nk}, \qquad W_N = e^{-j2\pi/N}, \qquad k = 0, \ldots, N - 1$$

## Decimation in time (radix 2)

Split x into even and odd samples, with N/2-point DFTs $E_k$ and $O_k$. Then for $k = 0, \ldots, N/2 - 1$:

$$X_k = E_k + W_N^kO_k, \qquad X_{k+N/2} = E_k - W_N^kO_k$$

the **butterfly**: one multiplication by the **twiddle factor** $W_N^k$ makes two outputs. Recursing down to 1-point DFTs takes $\log_2N$ stages of N/2 butterflies:

$$\text{cost} = \frac N2\log_2N\text{ complex multiplications}, \quad N\log_2N\text{ additions}$$

Done in place, the input ends up in **bit-reversed order**: sample n is stored at the index whose binary digits are n's, reversed.

## Practical points

- **Any N:** mixed-radix FFTs handle products of small primes; Bluestein's algorithm turns a prime-length DFT into a convolution done by power-of-two FFTs. Libraries like FFTW choose a plan for each N.
- **Real signals:** an N-point real FFT costs about half as much, since the spectrum is conjugate-symmetric.
- **Accuracy:** the FFT is backward stable, with relative RMS error growing only as $O(\varepsilon\log N)$; a direct DFT's grows faster.

## Uses

- **Fast convolution:** zero-pad both sequences to at least $L + M - 1$ points (to avoid circular wrap-around), transform, multiply, transform back. For long signals, overlap-add (C9) does it in blocks.
- **Spectral differentiation:** for a periodic signal, multiply coefficient k by $jk$ (in radians per sample) and transform back. For smooth periodic functions this is accurate to nearly every digit, far beyond any finite difference: the basis of spectral PDE solvers.
:::

::: example
## Compare direct and FFT cost for N = 1024.

::: solution
1. **Direct.**
   $$1024^2 = 1\,048\,576$$
   multiplications.
2. **FFT.**
   $$512 \times 10 = 5120$$
   multiplications.
3. **Speedup.** The ratio is 205.

**Answer:** 1 048 576 multiplications directly against 5120 by FFT, a speedup of 205.
:::
:::

::: example
## Convolve a 1000-sample signal with a 200-tap filter, directly and by FFT.

::: solution
1. **Directly.**
   $$1000 \times 200 = 200\,000$$
   multiplications.
2. **Pad.** The result has 1199 samples, so pad to 2048.
3. **The transforms.** Two forward transforms and one inverse cost
   $$3 \times 1024 \times 11 = 33\,792$$
   multiplications, plus 2048 for the spectra: about 36 000.
4. **Compare.** That's under a fifth of the direct cost. (Transforming the filter once and reusing it saves a third more.)

**Answer:** 200 000 multiplications directly against about 36 000 by FFT, under a fifth.
:::
:::

::: example
## Two tones 10 Hz apart are sampled at 1 kHz. How many samples does an FFT need to separate them, and does zero padding help?

::: solution
1. **Bin spacing.** It is $f_s/N$, so separating tones 10 Hz apart needs at least
   $$\begin{aligned} N &= 1000/10 \\ &= 100 \end{aligned}$$
   samples.
2. **With a window.** A Hann window's main lobe is four bins wide, so about 200 to 400 samples are needed in practice to see two clear peaks.
3. **Zero padding.** Padding a 100-sample record to 1024 points draws the same spectrum on a finer grid of 0.98 Hz, which makes the peaks easier to locate, but the two tones stay as blurred together as before.
4. **Where resolution comes from.** It comes from the record length, $1/T$, and nothing else.

**Answer:** at least 100 samples, and 200 to 400 with a Hann window; zero padding doesn't help separate the tones.
:::
:::

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

## How many complex multiplications does a radix-2 FFT of $2^{\given{p}}$ points take?

::: right
Right. $\frac N2\log_2N$ = {answer}.
:::

::: misses
- [2^p*p] That counts every butterfly output; each butterfly needs only one multiplication for two outputs.
- [(2^p)^2] That's the direct DFT.
- [any] Not yet. $\log_2N$ stages of N/2 butterflies.
:::
:::

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

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

::: right
Right. $\frac{2N}{\log_2N}$ = {answer}.
:::

::: misses
- [2^p/p] Divide $N^2$ by $\frac N2\log_2N$: that's $2N/\log_2N$.
- [any] Not yet. $\frac{N^2}{(N/2)\log_2N}$.
:::
:::

::: check
given: L = 1000 | 2000 | 500; M = 64 | 200 | 1000
answer: 2^ceil(ln(L + M - 1)/ln(2))
tolerance: 0

## To convolve {L} samples with an {M}-tap filter by FFT, what's the smallest power-of-two length that avoids wrap-around?

::: right
Right. At least $L + M - 1$, rounded up: {answer}.
:::

::: misses
- [2^ceil(ln(L)/ln(2))] The full convolution has $L + M - 1$ samples; shorter wraps around.
- [any] Not yet. Round $L + M - 1$ up to a power of 2.
:::
:::

::: check
given: N = 8 | 16 | 12; k = 1 | 5
answer: cos(2*pi*k/N)
tolerance: 0.1%

## What's the real part of the twiddle factor $W_{\given{N}}^{\given{k}} = e^{-j2\pi k/N}$?

::: right
Right. $\cos(2\pi k/N)$ = {answer}.
:::

::: misses
- [-sin(2*pi*k/N)] That's the imaginary part.
- [any] Not yet. $\cos\frac{2\pi k}{N}$.
:::
:::

::: check
given: n = 1 | 3 | 6
answer: 4*(n - 2*floor(n/2)) + 2*(floor(n/2) - 2*floor(n/4)) + (floor(n/4) - 2*floor(n/8))
tolerance: 0

## In an in-place 8-point FFT, where does input sample {n} end up (its 3-bit index reversed)?

::: right
Right. Index {answer}.
:::

::: misses
- [any] Not yet. Write {n} in 3 binary digits, reverse them, and read the result.
:::
:::

::: check
kind: choice

## You need the derivative of a smooth periodic signal from 64 samples per period. Which gives the most accurate result?

- [ ] A central difference
- [ ] A forward difference
- [x] Spectral differentiation: FFT, multiply coefficient k by $jk$, inverse FFT
- [ ] A cubic spline's derivative

::: misses
- [1] Its error is $O(h^2)$: a few digits at 64 points.
- [2] Worse: $O(h)$.
- [4] Better than differences, but its error is still a power of h; the spectral method's falls exponentially for smooth periodic signals.
:::
:::

::: check
kind: parts
given: fs = 1000 | 48000; N = 256 | 1024

## A record of {N} samples at {fs} Hz is analyzed with an FFT.

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

### What is its bin spacing?

::: misses
- [fs/(2*N)] The bins span 0 to $f_s$ in N steps.
- [any] $f_s/N$.
:::
:::

::: part
answer: fs/(4*N)
unit: Hz
tolerance: 0.5%

### After zero padding to 4N points, what is the spacing of the plotted points?

::: misses
- [fs/N] The grid gets finer, even though the resolution doesn't change.
- [any] $f_s/(4N)$.
:::
:::
:::

::: check
kind: derivation
vars: N = 8..4096

## Count the work of a radix-2 FFT of length N.

::: step
answer: N/2*log(N)/log(2)

### How many complex multiplications (one per butterfly)?
:::

::: step
answer: 2*N*log(2)/log(N)

### How many times fewer than a direct DFT's $N^2$?
:::

::: right
Right. For N = 1024 that's about 205 times fewer.
:::
:::

::: explain
## How does splitting a DFT into even and odd samples save work?

::: model
An N-point DFT of the even-indexed samples and one of the odd-indexed samples each have only N/2 outputs, and their twiddle factors repeat with period N/2. The full transform's outputs can be built from them: output k is the even part plus a twiddle factor times the odd part, and output k + N/2 uses the same two numbers with a minus sign. So two half-size DFTs plus N/2 multiplications replace one full DFT. Applying the same split to each half, and again, until only single samples remain, gives $\log_2N$ levels with N/2 multiplications each, instead of the $N^2$ of the direct sum.
:::
:::

::: practice

::: check
given: p = 8..24 int
answer: 2^p/2*p
tolerance: 0.5%

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

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

::: check
given: L = 4000 | 250; M = 150 | 32
answer: 2^ceil(ln(L + M - 1)/ln(2))
tolerance: 0

## FFT length to convolve {L} and {M} samples?

::: misses
- [any] Next power of 2 at or above $L + M - 1$.
:::
:::

::: check
given: n = 1..6 int
answer: 4*(n - 2*floor(n/2)) + 2*(floor(n/2) - 2*floor(n/4)) + (floor(n/4) - 2*floor(n/8))
tolerance: 0

## Bit-reversed position of sample {n} in an 8-point FFT?

::: misses
- [any] Reverse its 3-bit binary index.
:::
:::
:::
