---
title: FFT algorithms
heading: Decimation in time and in frequency, butterflies and bit reversal, operation counts, radix-4 and mixed-radix FFTs, the Goertzel algorithm and the chirp z-transform
eyebrow: S7 · Lesson 27 · FFT algorithms
xp: 50
---

::: hook
Computing an N-point DFT straight from its definition takes N² complex multiplications: a million for N = 1024. Cooley and Tukey's 1965 algorithm, which Gauss had found and set aside in 1805, does it in about five thousand by splitting the transform in half again and again. C9 used the FFT; this lesson takes it apart: where the savings come from, why the data comes out in bit-reversed order, how other radices and lengths are handled, and when computing just a few bins directly with the Goertzel algorithm beats the FFT altogether.
:::

::: play
- Real multiplications against transform length N (both axes logarithmic): a direct DFT of a real signal (about 2N²), a radix-2 FFT (2N log₂N, counting four real multiplications per complex one) and the Goertzel algorithm for K bins (about K(N + 2)).
- The FFT's advantage grows without limit: 205 times at N = 1024.
- When only a few bins are needed, Goertzel's line sits below the FFT's: for K bins it wins while K is less than about 2 log₂N.
:::
:::

```sim plot
view: graph
x: N = 16..65536 log
y: C = 100..1e10 log
slider: K = 1..64 = 8 log
plot: 2*N^2 | samples: 300 | label: direct DFT
plot: 2*N*log(N)/log(2) | samples: 300 | color: ch2 | label: radix-2 FFT
plot: K*(N + 2) | samples: 300 | dashed | color: ch3 | label: Goertzel, K bins
```

::: predict
## A touch-tone decoder needs the strength of exactly 8 frequencies in each 205-sample block. Which is cheaper, a 256-point FFT or 8 Goertzel filters?

- [ ] The FFT, always
- [x] The Goertzel filters: about 1,660 real multiplications against about 4,100
- [ ] They cost the same
- [ ] Neither; DTMF needs a 1024-point FFT

::: reveal
Eight Goertzel recursions over 205 samples cost about 8 × 207 = 1,656 real multiplications, and need no buffering or bit reversal. A 256-point FFT costs about 2 × 256 × 8 = 4,096 and computes 248 bins nobody wants. With K bins the Goertzel approach wins while K < 2 log₂N, here about 16.
:::
:::

::: nameit
## Decimation in time

Split x[n] into even and odd samples:

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

where G and H are N/2-point DFTs. Each pair of outputs is a **butterfly**: one complex multiplication by the **twiddle factor** $W_N^k$, one addition and one subtraction. Repeating the split gives $\log_2N$ stages of N/2 butterflies:

$$\frac{N}{2}\log_2N \text{ complex multiplications}, \quad N\log_2N \text{ complex additions}$$

against N² for the direct DFT, a speedup of $2N/\log_2N$. In place, the input must be read in **bit-reversed** order (index 3 = 011 sits at 110 = 6 for N = 8). **Decimation in frequency** splits the outputs instead and takes natural-order input, bit-reversed output.

## Other lengths

- **Radix-4** butterflies combine two radix-2 stages and save about 25% of the multiplications; **split-radix** saves a bit more.
- **Mixed-radix** FFTs handle N = N₁N₂…; the prime-factor algorithm avoids twiddle factors when the factors are coprime.
- The **chirp z-transform** (Bluestein) computes any N, or samples of the z-transform along a spiral, by turning the DFT into a convolution done with power-of-two FFTs.

## The Goertzel algorithm

One bin k of an N-point DFT, as a second-order recursion run over the block:

$$s[n] = x[n] + 2\cos\left(\frac{2\pi k}{N}\right)s[n - 1] - s[n - 2], \qquad |X[k]|^2 = s^2[N - 1] + s^2[N - 2] - 2\cos\left(\frac{2\pi k}{N}\right)s[N - 1]s[N - 2]$$

One real multiplication per input sample per bin, plus a few at the end. The bin for a tone f is k = round(fN/f_s).
:::

::: example
## Count complex multiplications for a 1024-point DFT done directly and by radix-2 FFT.

::: solution
1. **Direct DFT.**
   $$1024^2 = 1\,048\,576$$
2. **Radix-2 FFT.** There are 10 stages of 512 butterflies:
   $$512 \times 10 = 5120$$
3. **Ratio.**
   $$\frac{2N}{\log_2N} = 204.8$$
   times fewer.
4. **Trivial twiddles.** Some twiddles are ±1 or ±j and cost nothing, so real FFTs do a little better still.

**Answer:** 1,048,576 complex multiplications directly and 5,120 by radix-2 FFT, 204.8 times fewer.
:::
:::

::: example
## A DTMF decoder uses N = 205 at 8 kHz. Which bin and coefficient does the 697 Hz row tone use?

::: solution
1. **Bin.**
   $$\begin{aligned} k &= \text{round}(697 \times 205/8000) \\ &= \text{round}(17.86) \\ &= 18 \end{aligned}$$
2. **Coefficient.**
   $$\begin{aligned} c &= 2\cos(2\pi \times 18/205) \\ &= 1.7033 \end{aligned}$$
3. **The 1209 Hz column tone.** The same steps give k = 31 and 1.1631.
4. **Why 205.** N = 205 is chosen so that all eight DTMF frequencies fall close to integer bins.

**Answer:** bin 18, with coefficient 1.7033.
:::
:::

::: example
## Trace a 16-point radix-2 decimation-in-time FFT: where does input sample 3 sit in the bit-reversed order, how many stages and butterflies are there, and how many twiddle multiplications?

::: solution
1. **Bit reversal.** 3 is 0011 in four bits; reversed, it's 1100 = 12, so x[3] goes in position 12.
2. **Stages and butterflies.** There are $\log_216 = 4$ stages of 8 butterflies, 32 butterflies in all.
3. **Twiddle multiplications.** Each butterfly has one twiddle multiplication: 32 in all, against 256 for the direct DFT.
4. **Nontrivial ones.** Only 10 of the 32 are nontrivial, since $W^0 = 1$ and $W^4 = -j$ only swap or negate parts, which is why small FFTs are hand-optimized to skip the rest.

**Answer:** x[3] sits in position 12; there are 4 stages of 8 butterflies (32 in all) and 32 twiddle multiplications, only 10 of them nontrivial.
:::
:::

::: check
given: N = 64 | 512 | 4096 | 65536
answer: N/2*log(N)/log(2)
tolerance: 0.5%

## How many complex multiplications does a radix-2 FFT of length {N} need (counting every twiddle)?

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

::: misses
- [N*log(N)/log(2)] That's the count of complex additions; each butterfly has one multiplication.
- [N^2] That's the direct DFT.
- [any] Not yet. log₂N stages of N/2 butterflies.
:::
:::

::: check
given: N = 64 | 512 | 4096 | 65536
answer: 2*N/(log(N)/log(2))
tolerance: 0.5%

## By what factor does the radix-2 FFT reduce the multiplications of an {N}-point DFT?

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

::: misses
- [N/(log(N)/log(2))] The FFT needs N/2 multiplications per stage, not N.
- [any] Not yet. Divide N² by the FFT's count.
:::
:::

::: check
given: f = 697 | 852 | 1336 | 1633; N = 205 | 256; s = 8
answer: round(f*N/(s*1000))
tolerance: 0

## A Goertzel detector with N = {N} at {s} kHz looks for {f} Hz. Which bin k does it use?

::: right
Right. round(fN/f_s) = {answer}.
:::

::: misses
- [round(f*N/(s*1000)/2)] Bin k covers $kf_s/N$, not $kf_s/2N$.
- [any] Not yet. The nearest bin to $fN/f_s$.
:::
:::

::: check
given: k = 18 | 31 | 42; N = 205 | 256
answer: 2*cos(2*pi*k/N)
tolerance: 0.5%

## What coefficient does the Goertzel recursion use for bin {k} of an {N}-point DFT?

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

::: misses
- [cos(2*pi*k/N)] The recursion multiplies by twice the cosine.
- [any] Not yet. $s[n] = x[n] + 2\cos(2\pi k/N)s[n - 1] - s[n - 2]$.
:::
:::

::: check
given: N = 128 | 1024 | 8192
answer: 2*N*(log(N)/log(2))/(N + 2)
tolerance: 0.5%

## By the real-multiplication model, below how many bins K does Goertzel beat a radix-2 FFT of length {N}?

::: right
Right. $K(N + 2) < 2N\log_2N$ gives K < {answer}.
:::

::: misses
- [log(N)/log(2)] Count the FFT's four real multiplications per complex one: $2N\log_2N$ in all.
- [any] Not yet. Solve $K(N + 2) = 2N\log_2N$ for K.
:::
:::

::: check
given: N = 256 | 2048 | 16384
answer: log(N)/log(2)
tolerance: 0.001

## How many butterfly stages does a radix-2 FFT of length {N} have?

::: right
Right. log₂{N} = {answer}, each with N/2 butterflies.
:::

::: misses
- [log(N)/log(4)] That's for radix-4 butterflies.
- [any] Not yet. Each stage halves the transform size.
:::
:::

::: check
kind: parts
given: N = 205 | 256 | 400; s = 8 | 16; f = 697 | 1209 | 1633

## A Goertzel detector looks for {f} Hz with N = {N} at {s} kHz.

::: part
answer: round(f*N/(s*1000))
tolerance: 0

### Which bin does it use?

::: misses
- [any] k = round(fN/f_s).
:::
:::

::: part
answer: 2*cos(2*pi*round(f*N/(s*1000))/N)
tolerance: 0.5%

### What coefficient does its recursion use?

::: misses
- [cos(2*pi*round(f*N/(s*1000))/N)] The recursion multiplies by twice the cosine.
- [any] $2\cos(2\pi k/N)$.
:::
:::
:::

::: check
kind: derivation
vars: N = 64..65536, K = 1..50

## A radix-2 FFT of length N is compared with K Goertzel detectors on the same block.

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

### How many real multiplications does the FFT need (four per complex one)?
:::

::: step
answer: K*N

### How many does Goertzel need for K bins?
:::

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

### Below how many bins is Goertzel cheaper?
:::

::: right
Right. A DTMF decoder's eight bins are well under the break-even point of a 256-point FFT (16).
:::
:::

::: explain
## Where does the FFT's saving come from?

::: model
A direct DFT computes each of the N outputs as a separate sum of N products, never reusing work. But the DFT's twiddle factors are powers of one root of unity, so the sums share structure: splitting the input into even and odd samples turns one N-point transform into two N/2-point transforms, whose results combine into all N outputs with only N/2 extra multiplications, since each half-size result is used twice, once with a plus sign and once with a minus. Repeating the split until the pieces are single samples gives log₂N stages of N/2 multiplications each, so the work grows like N log N instead of N². The saving is the reuse of each intermediate result in two outputs at every stage.
:::
:::

::: practice

::: check
given: N = 64 | 256 | 1024 | 4096 | 8192 | 32768
answer: N/2*log(N)/log(2)
tolerance: 0.5%

## Radix-2 complex multiplications for N = {N}?

::: misses
- [any] $\frac{N}{2}\log_2N$.
:::
:::

::: check
given: f = 697 | 770 | 852 | 941 | 1209 | 1336 | 1477 | 1633; N = 205; s = 8
answer: round(f*N/(s*1000))
tolerance: 0

## Goertzel bin for {f} Hz, N = {N}, {s} kHz?

::: misses
- [any] round(fN/f_s).
:::
:::

::: check
given: k = 18 | 20 | 22 | 24 | 31 | 34 | 38 | 42; N = 205
answer: 2*cos(2*pi*k/N)
tolerance: 0.5%

## Goertzel coefficient for bin {k}, N = {N}?

::: misses
- [any] $2\cos(2\pi k/N)$.
:::
:::
:::
