---
title: The FFT and fast convolution
heading: Computing the DFT in N log N steps, and filtering with it
eyebrow: C9 · Lesson 27 · The FFT and fast convolution
xp: 40
---

::: hook
Computing a 1024-point DFT straight from its definition takes about a million complex multiplications. The fast Fourier transform gets the identical answer with about five thousand, by noticing that a DFT of N points can be split into two DFTs of N/2 points, and those into four, and so on. That factor of 200, growing with N, is why spectrum analyzers, Wi-Fi, MP3 and MRI scanners exist. It also turns convolution around: for long filters, transforming, multiplying and transforming back beats doing the sum directly.
:::

::: play
- The two curves count complex multiplications: $N^2$ for the DFT done directly, and $\frac{N}{2}\log_2 N$ for the radix-2 FFT, on log scales.
- The gap widens as N grows. Move the marker to compare at a particular size.
- At N = 8 the FFT saves a factor of about 5; at a million points, about 100,000.
:::
:::

```sim plot
view: graph
x: N = 8..1048576 log
y: ops = 10..1e12 log
slider: n = 3..20 = 10 step 1
plot: N^2 | label: direct DFT
plot: N/2*log(N)/log(2) | label: FFT
marker: x = 2^n | label: N
readout: N = 2^n | digits: 7
readout: speedup = 2^(n + 1)/n | digits: 4
```

::: predict
## How many times faster is a radix-2 FFT than the direct DFT at N = 1024, counting complex multiplications?

- [ ] About 10
- [ ] About 50
- [x] About 200
- [ ] About 1000

::: reveal
About 200: $1024^2 = 1{,}048{,}576$ against $512 \times 10 = 5120$. The ratio is $2N/\log_2 N$, which keeps growing with N.
:::
:::

::: nameit
## Splitting the DFT

Write the N-point DFT as a sum over even-numbered and odd-numbered samples:

$$X[k] = E[k] + W_N^k\,O[k], \qquad X[k + N/2] = E[k] - W_N^k\,O[k]$$

where E and O are the N/2-point DFTs of the even and odd samples and $W_N = e^{-j2\pi/N}$. Each pair of outputs costs one multiplication by $W_N^k$ (a **twiddle factor**) and two additions, a **butterfly**. Splitting again and again, for $N = 2^m$, gives $\log_2 N$ stages of N/2 butterflies:

| Method | Complex multiplications |
|---|---|
| direct DFT | $N^2$ |
| radix-2 FFT | $\frac{N}{2}\log_2 N$ |

a speedup of $2N/\log_2 N$. Other sizes work too (radix 3, 5, mixed radix), but powers of two are simplest.

## Fast convolution

Convolving by DFT: pad x and h to N ≥ L + M − 1, FFT both, multiply bin by bin, inverse FFT. With h's FFT computed once, each block costs two FFTs and N multiplications, against M multiplications per output sample directly.

## Overlap-add

A long signal is cut into blocks of L samples. Each block is convolved with the M-tap filter by FFTs of size $N \ge L + M - 1$, giving L + M − 1 output samples, and the blocks' outputs overlap by M − 1 samples and are added. With $N = 2^m$ fixed, $L = N - M + 1$. For filters longer than a few dozen taps, this beats direct convolution.
:::

::: example
## Compare the direct DFT and the FFT at N = 1024.

::: solution
1. **Direct DFT.**
   $$1024^2 = 1{,}048{,}576$$
   multiplications.
2. **FFT.** 10 stages of 512 butterflies:
   $$10 \times 512 = 5120$$
   multiplications.
3. **Compare.** That's 205 times fewer.

**Answer:** the FFT needs 5120 multiplications against 1,048,576 for the direct DFT, 205 times fewer.
:::
:::

::: example
## A 101-tap FIR filter is applied by overlap-add with 1024-point FFTs. How does its cost compare with direct convolution?

::: solution
1. **Block length.** Each block holds
   $$\begin{aligned} L &= 1024 - 101 + 1 \\ &= 924 \end{aligned}$$
   new samples.
2. **Cost per block.** A forward FFT (5120), 1024 multiplications by the filter's spectrum, and an inverse FFT (5120):
   $$5120 + 1024 + 5120 = 11{,}264$$
   complex multiplications for 924 outputs.
3. **Cost per output.**
   $$11{,}264/924 = 12.2$$
   against 101 real multiplications each directly.
4. **Compare.** Even counting a complex multiply as four real ones, it's about twice as fast, and the advantage grows with the filter length.

**Answer:** about 12.2 complex multiplications per output against 101 real ones directly, roughly twice as fast even counting a complex multiply as four real ones.
:::
:::

::: example
## A processor does a complex multiply in 1 µs. How long does a 4096-point DFT take directly, and by FFT?

::: solution
1. **Directly.**
   $$4096^2 = 16.8\ \text{million}$$
   multiplies, which take 16.8 s.
2. **By FFT.** $\log_2 4096 = 12$ stages of 2048 butterflies:
   $$12 \times 2048 = 24{,}576$$
   multiplies, which take 24.6 ms.
3. **Compare.** That's 683 times faster, and the gap grows with N.

**Answer:** 16.8 s directly and 24.6 ms by FFT.
:::
:::

::: check
kind: parts
given: m = 6 | 10 | 14

## A DFT has $N = 2^{\given{m}}$ points.

::: part
answer: 4^m
tolerance: 0.5%

### How many complex multiplications does the direct DFT take?

::: misses
- [any] $N^2$.
:::
:::

::: part
answer: 2^(m - 1)*m
tolerance: 0.5%

### How many does the radix-2 FFT take?

::: misses
- [2^m*m] Each stage has N/2 butterflies, one multiplication each.
- [any] Not yet. $\frac N2\log_2 N$.
:::
:::
:::

::: check
kind: parts
given: m = 8 | 12 | 16

## A radix-2 FFT has $N = 2^{\given{m}}$ points.

::: part
answer: m
tolerance: 0

### How many stages?

::: misses
- [any] $\log_2 N$.
:::
:::

::: part
answer: 2^(m - 1)
tolerance: 0

### How many butterflies per stage?

::: misses
- [2^m] Each butterfly makes two outputs.
- [any] Not yet. N/2.
:::
:::
:::

::: check
given: m = 5 | 11 | 20
answer: 2^(m + 1)/m
tolerance: 0.5%

## How many times fewer multiplications does the FFT need than the direct DFT at $N = 2^{\given{m}}$?

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

::: misses
- [2^m/m] $N^2$ over $\frac N2\log_2 N$ is $2N/\log_2 N$.
- [any] Not yet. Divide the two counts.
:::
:::

::: check
kind: parts
given: N = 512 | 2048 | 8192; M = 65 | 257; S = 100000 | 1000000

## Overlap-add filters {S} samples with a {M}-tap filter using {N}-point FFTs.

::: part
answer: N - M + 1
tolerance: 0

### How many new input samples does each block take?

::: misses
- [N] Leave room for the filter's tail: L + M − 1 ≤ N.
- [any] Not yet. N − M + 1.
:::
:::

::: part
answer: ceil(S/(N - M + 1))
tolerance: 0

### How many blocks does the whole signal need?

::: misses
- [any] Signal length over block length, rounded up.
:::
:::
:::

::: check
given: N = 1024 | 4096; M = 128 | 512
answer: (N*log(N)/log(2) + N)/(N - M + 1)
tolerance: 0.5%

## With the filter's FFT precomputed, overlap-add with {N}-point FFTs and a {M}-tap filter costs two FFTs and N products per block. How many complex multiplications is that per output sample?

::: right
Right. $(2 \cdot \frac N2\log_2N + N)/(N - M + 1)$ = {answer}.
:::

::: misses
- [M] That's direct convolution's cost.
- [any] Not yet. Cost per block over new samples per block.
:::
:::

::: check
given: m = 10 | 16; r = 100 | 1000
answer: 2^(m - 1)*m/r
unit: µs
tolerance: 0.5%

## A processor does {r} million complex multiplications a second. How long does a $2^{\given{m}}$-point FFT's multiplying take?

::: right
Right. $\frac N2\log_2N$ multiplications at {r} per µs: {answer} µs.
:::

::: misses
- [4^m/r] That's the direct DFT.
- [any] Not yet. Count, then divide by the rate.
:::
:::

::: check
kind: choice

## The FFT splits an N-point DFT into two DFTs of half the size. Why does that save work?

- [ ] Half-size DFTs need no multiplications
- [x] Two DFTs of N/2 points cost about half as much as one of N points, and repeating the split down to size 1 leaves only log₂N stages of cheap butterflies
- [ ] It skips the odd-numbered outputs
- [ ] It computes only the bins with signal in them

::: misses
- [1] They still need multiplications, just far fewer in total.
- [3] All N outputs are computed, from E and O with the butterfly.
- [4] Every bin is computed; the savings come from sharing work between bins.
:::
:::

::: check
kind: parts
given: m = 8 | 10 | 14 | 16

## An FFT of N = $2^{\given{m}}$ points.

::: part
answer: 2^m/2*m
tolerance: 0

### How many complex multiplications does it take, as $\tfrac N2\log_2 N$?

::: misses
- [any] N/2 butterflies in each of $\log_2 N$ stages.
:::
:::

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

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

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

::: check
kind: parts
given: m = 9 | 10 | 11; M = 33 | 101 | 257

## Overlap-add uses $2^{\given{m}}$-point FFTs for a {M}-tap filter.

::: part
answer: 2^m - M + 1
tolerance: 0

### How many new samples does each block hold?

::: misses
- [any] $N - M + 1$.
:::
:::

::: part
answer: (2*2^m/2*m + 2^m)/(2^m - M + 1)
tolerance: 0.5%

### How many complex multiplications per output sample (two FFTs plus N products per block)?

::: misses
- [any] $(N\log_2 N + N)/(N - M + 1)$.
:::
:::
:::

::: explain
## Where does the FFT's saving come from, in terms of repeated work?

::: model
Computed directly, each of the N output bins multiplies all N samples by its own set of complex exponentials, and many of those products are the same numbers computed again for different bins. The even/odd split shows that the bins in the bottom half and the top half share the same two half-size DFTs, differing only in the sign of the twiddle factor. Applying the split again at every level reuses each intermediate result many times, so the work drops from N² to about N log₂N.
:::
:::

::: practice

::: check
given: m = 4..16 int
answer: 2^(m - 1)*m
tolerance: 0.5%

## Complex multiplications in a $2^{\given{m}}$-point radix-2 FFT?

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

::: check
given: N = 256 | 1024; M = 31 | 100
answer: N - M + 1
tolerance: 0

## New samples per overlap-add block, N = {N}, M = {M}?

::: misses
- [any] N − M + 1.
:::
:::

::: check
given: m = 3..20 int
answer: m
tolerance: 0

## Stages in a $2^{\given{m}}$-point radix-2 FFT?

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