---
title: The discrete Fourier transform
heading: A spectrum a computer can calculate
eyebrow: C9 · Lesson 24 · The discrete Fourier transform
xp: 40
---

::: hook
The DTFT is a function of a continuous frequency, so no computer can store it. The **DFT** takes N samples and returns N numbers: the DTFT at N equally spaced frequencies. It's what an FFT computes, what a spectrum analyzer shows, and the engine of fast convolution. Because it only ever sees N samples, it quietly treats the signal as repeating every N, and that assumption explains its surprises: tones that smear across several bins, convolutions that wrap around, and zero padding that makes a spectrum smoother without making it sharper.
:::

::: play
- The dots are the N-point DFT magnitudes of a complex tone at "bin" $k_0$, that is at frequency $2\pi k_0/N$. The dashed curve is the tone's DTFT, which the DFT samples.
- With $k_0$ a whole number, the tone completes whole cycles in the window: one bin gets everything (height N) and the rest are exactly zero.
- Move $k_0$ between bins. The energy leaks into every bin, and the tallest drops to about 0.64 N halfway between: the **scalloping loss**.
:::
:::

```sim plot
view: graph
x: k = 0..31
y: X = 0..33
slider: N = 8..32 = 16 step 1
slider: k0 = 0..15 = 3 step 0.05
plot: abs(sin(pi*(k - k0 + 0.000001))/sin(pi*(k - k0 + 0.000001)/N))*step(N - 0.5 - k) | dashed | samples: 3000
plot: abs(sin(pi*(k - k0 + 0.000001))/sin(pi*(k - k0 + 0.000001)/N))*step(N - 0.5 - k) | stems | label: |X[k]|
readout: largest bin, as a fraction of N = abs(sin(pi*(k0 - round(k0) + 0.000001))/sin(pi*(k0 - round(k0) + 0.000001)/N))/N | digits: 3
```

::: predict
## A real cosine completes exactly 3 cycles in a 16-point window. How many of its 16 DFT values are nonzero?

- [ ] 1
- [x] 2
- [ ] 3
- [ ] 16

::: reveal
Two: bins 3 and 13. A cosine is two complex exponentials, at $+\omega_0$ and $-\omega_0$, and in the DFT the negative frequency $-2\pi \cdot 3/16$ shows up at bin 16 − 3 = 13. Each holds A·N/2.
:::
:::

::: nameit
## The DFT

$$X[k] = \sum_{n=0}^{N-1} x[n]\,e^{-j2\pi kn/N}, \qquad x[n] = \frac{1}{N}\sum_{k=0}^{N-1} X[k]\,e^{j2\pi kn/N}$$

for k = 0 to N − 1. X[k] is the DTFT of the N samples at $\omega_k = 2\pi k/N$, which at sample rate $f_s$ is $kf_s/N$ hertz: the bins are $f_s/N$ apart. X[0] is the sum of the samples, and for even N, X[N/2] is the alternating sum. Bins above N/2 are negative frequencies: for real x, $X[N - k] = X[k]^*$.

A cosine of amplitude A exactly on bin $k_0$ gives $\lvert X[k_0]\rvert = AN/2$ (and the same at $N - k_0$). Off a bin, its energy **leaks** into all bins, and halfway between two bins the peak falls to $2/\pi \approx 0.64$ of its on-bin height.

## It assumes the signal repeats

The inverse DFT produces a sequence that repeats every N. So shifts are **circular** (what falls off the end comes back at the start), and multiplying DFTs gives **circular convolution**:

$$y_c[n] = \sum_{k=0}^{N-1} x[k]\,h[(n - k) \bmod N]$$

which equals linear convolution with its tail wrapped onto its start. To get the linear convolution of L and M samples, zero-pad both to at least L + M − 1 points.

## Zero padding

Appending zeros before the DFT gives more bins, closer together: samples of the same DTFT, more finely spaced. It smooths the picture but doesn't separate two tones closer than about $f_s/N$, where N is the number of real samples.
:::

::: example
## A 1024-point DFT at 48 kHz. How far apart are its bins, and where does a 1 kHz tone fall?

::: solution
1. **Bin spacing.**
   $$48000/1024 = 46.875\ \text{Hz}$$
2. **Where 1 kHz falls.**
   $$1000 \times 1024/48000 = 21.33$$
   That's between bins 21 and 22, so it leaks, and its peak is somewhat below the on-bin height.

**Answer:** the bins are 46.875 Hz apart, and 1 kHz falls at 21.33, between bins 21 and 22.
:::
:::

::: example
## Circularly convolve [1, 2, 3, 4] with [1, 1, 0, 0], and compare with the linear convolution.

::: solution
1. **Linear convolution.** It has five samples: [1, 3, 5, 7, 4].
2. **Wrap it for N = 4.** The fifth sample, 4, wraps onto n = 0:
   $$\begin{aligned} &[1 + 4,\ 3,\ 5,\ 7] \\ &= [5,\ 3,\ 5,\ 7] \end{aligned}$$
3. **Making them agree.** Padding both to 5 points or more makes the two agree.

**Answer:** the circular convolution is [5, 3, 5, 7], against the linear [1, 3, 5, 7, 4]: the last sample wraps onto the first.
:::
:::

::: example
## A 256-sample record at 8 kHz is zero-padded to 1024 points. What changes, and what doesn't?

::: solution
1. **Bin spacing.** The bins get closer:
   $$8000/1024 = 7.8125\ \text{Hz}$$
   instead of 31.25 Hz, so peaks are drawn more smoothly and located more precisely.
2. **Resolution.** The resolution doesn't change: two tones closer than about
   $$8000/256 = 31\ \text{Hz}$$
   still merge, because the record is still 32 ms long.
3. **The rule.** Padding interpolates the spectrum; only a longer record separates close tones.

**Answer:** the bin spacing shrinks from 31.25 Hz to 7.8125 Hz, but the resolution stays about 31 Hz.
:::
:::

::: check
kind: parts
given: fs = 8000 | 48000 | 44100; N = 256 | 1024 | 4096; f = 1000 | 3000

## An {N}-point DFT is taken at {fs} Hz.

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

### How far apart are the bins?

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

::: part
answer: f*N/fs
tolerance: 0.5%

### At what (possibly fractional) bin number does {f} Hz fall?

::: misses
- [any] f divided by the bin spacing.
:::
:::
:::

::: check
given: A = 1 | 2 | 0.5; N = 64 | 256 | 1000
answer: A*N/2
tolerance: 0.5%

## A real cosine of amplitude {A} sits exactly on a bin of an {N}-point DFT. How big is that bin?

::: right
Right. AN/2 = {answer}; the other half is in the mirror bin.
:::

::: misses
- [A*N] A real cosine splits evenly between its positive and negative frequency bins.
- [A] The DFT sums N samples without dividing by N.
- [any] Not yet. AN/2.
:::
:::

::: check
given: N = 16 | 64 | 128; k = 3 | 5 | 10
answer: N - k
tolerance: 0

## A real signal has a strong component in bin {k} of an {N}-point DFT. Which other bin has the same magnitude?

::: right
Right. $X[N - k] = X[k]^*$: bin {answer}.
:::

::: misses
- [N + k] Bins only go up to N − 1; the mirror is at N − k.
- [any] Not yet. N − k.
:::
:::

::: check
kind: parts
given: L = 300 | 1000 | 700; M = 64 | 129

## A {L}-sample signal is to be linearly convolved with a {M}-tap filter using DFTs.

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

### What is the smallest DFT length that avoids wrap-around?

::: misses
- [L] The output is longer than the input: L + M − 1.
- [any] Not yet. L + M − 1.
:::
:::

::: part
answer: 2^ceil(log(L + M - 1)/log(2))
tolerance: 0

### What is the smallest power of two that works?

::: misses
- [any] Round L + M − 1 up to a power of 2.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 3 | -1; c = 2 | 4; d = 5 | 1

## x = [{a}, {b}, {c}, {d}] is circularly convolved (N = 4) with h = [1, 1, 0, 0].

::: part
answer: a + d
tolerance: 0

### What is $y_c[0]$?

::: misses
- [a] That's the linear convolution's first sample; circularly, x[3]·h[1] wraps around to n = 0.
- [any] Not yet. $x[0]h[0] + x[3]h[1]$.
:::
:::

::: part
answer: c + d
tolerance: 0

### What is $y_c[3]$?

::: misses
- [any] $x[3]h[0] + x[2]h[1]$.
:::
:::
:::

::: check
given: N = 16 | 64 | 512
answer: abs(1/sin(pi/(2*N)))/N
tolerance: 0.5%

## A complex tone sits exactly halfway between two bins of an {N}-point DFT. How tall is the nearest bin, as a fraction of the on-bin height N?

::: right
Right. $\lvert 1/\sin(\pi/2N)\rvert/N \approx 2/\pi$ = {answer}.
:::

::: misses
- [0.5] The energy doesn't split evenly between two bins; it leaks into all of them.
- [any] Not yet. Evaluate $\lvert\sin(\pi\delta)/\sin(\pi\delta/N)\rvert$ at δ = 1/2.
:::
:::

::: check
kind: choice

## A 256-sample recording is zero-padded to 4096 points before its DFT. What does that do?

- [ ] Separates two tones 2 Hz apart that the 256-point DFT couldn't
- [x] Gives 16 times as many bins, sampling the same DTFT more finely
- [ ] Removes leakage
- [ ] Raises the sample rate

::: misses
- [1] Resolution is set by the 256 real samples; padding adds no new information about the signal.
- [3] The leakage is still there, just drawn in finer detail.
- [4] The sample rate is unchanged; only the frequency grid gets finer.
:::
:::

::: check
kind: parts
given: N = 256 | 1024 | 4096; fs = 8000 | 44100 | 48000; f = 1000 | 2500

## An {N}-point DFT runs at {fs} Hz.

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

### How far apart are its bins?

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

::: part
answer: f*N/fs
tolerance: 0.5%

### At what (fractional) bin does a {f} Hz tone fall?

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

::: check
kind: parts
given: L1 = 50 | 100 | 300; L2 = 20 | 64 | 101

## Sequences of {L1} and {L2} samples are convolved by DFT.

::: part
answer: L1 + L2 - 1
tolerance: 0

### How long is their linear convolution?

::: misses
- [any] $L_1 + L_2 - 1$.
:::
:::

::: part
answer: 2^ceil(log(L1 + L2 - 1)/log(2))
tolerance: 0

### What is the smallest power-of-two DFT that avoids wraparound?

::: misses
- [any] The next power of two at or above the length.
:::
:::
:::

::: explain
## Why does multiplying two DFTs give a circular convolution instead of the ordinary one?

::: model
The DFT only knows N samples and represents them with N harmonics of the window length, which are all periodic with period N. So everything it computes treats the signal as one period of an endlessly repeating sequence. Multiplying DFTs convolves those periodic versions, and the part of the true output that runs past sample N − 1 lands in the next period, which overlaps the start of this one: it wraps around. Padding with enough zeros makes the period longer than the true output, so nothing overlaps and circular and linear convolution agree.
:::
:::

::: practice

::: check
given: fs = 1000 | 16000; N = 100 | 512
answer: fs/N
unit: Hz
tolerance: 0.5%

## Bin spacing of an {N}-point DFT at {fs} Hz?

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

::: check
given: L = 200 | 800; M = 33 | 65
answer: L + M - 1
tolerance: 0

## Minimum DFT size for linear convolution of {L} and {M} samples?

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

::: check
given: N = 32 | 1024; k = 4 | 100
answer: N - k
tolerance: 0

## Mirror bin of bin {k} in a real signal's {N}-point DFT?

::: misses
- [any] N − k.
:::
:::
:::
