---
title: The DFT and circular convolution
heading: The DFT as samples of the DTFT, time aliasing, circular shifts and circular convolution, and computing linear convolution with the DFT
eyebrow: S7 · Lesson 24 · The DFT and circular convolution
xp: 50
---

::: hook
The DFT is the only Fourier transform a computer can actually evaluate, and it isn't quite the transform it's often taken for. It treats every finite sequence as one period of a periodic one. Shifting a sequence wraps samples around from the end to the start; multiplying two DFTs convolves the sequences circularly, with the tail of the result folded back onto its beginning. Used carelessly, that wrap-around corrupts filtering done in the frequency domain. Used with the right amount of zero padding, the DFT computes ordinary linear convolution exactly, and with the FFT it does it fast.
:::

::: play
- Two rectangular pulses of length L convolved linearly (a triangle 2L − 1 long) and circularly with N-point DFTs.
- If N ≥ 2L − 1 the circular result equals the linear one. If N is smaller, the triangle's tail wraps around and adds onto its start.
- With L = 6 and N = 8 the circular result is [4, 4, 4, 4, 5, 6, 5, 4]: the first four samples are corrupted by the wrapped tail.
:::
:::

```sim plot
view: graph
x: n = 0..32
y: y = 0..18
slider: L = 2..16 = 6 step 1
slider: N = 4..32 = 8 step 1
plot: (L - abs(n - L + 1) + abs(L - abs(n - L + 1)))/2 | stems | label: linear convolution
plot: step(N - 1 - n)*((L - abs(n - L + 1) + abs(L - abs(n - L + 1)))/2 + (L - abs(n + N - L + 1) + abs(L - abs(n + N - L + 1)))/2) | stems | color: ch2 | label: N-point circular convolution
readout: N needed for no wrap = 2*L - 1
```

::: predict
## x = [1, 2, 3] and h = [1, 1] are convolved by multiplying their 3-point DFTs. What comes out?

- [ ] [1, 3, 5, 3]
- [x] [4, 3, 5]
- [ ] [1, 3, 5]
- [ ] [3, 5, 3]

::: reveal
The linear convolution is [1, 3, 5, 3], four samples long, but a 3-point DFT can only hold three. Multiplying DFTs gives circular convolution, so the fourth sample wraps onto the first: [1 + 3, 3, 5] = [4, 3, 5]. With 4-point (or longer) DFTs, after zero padding, the result would be the true [1, 3, 5, 3].
:::
:::

::: nameit
## The DFT

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

- X[k] are N samples of the DTFT at $\omega_k = 2\pi k/N$, spaced $f_s/N$ in hertz.
- **Sampling the DTFT aliases in time**: inverting N samples of X(e^{jω}) gives $\sum_rx[n + rN]$, which equals x[n] only if the sequence is no longer than N.
- For real x, $X[N - k] = X^*[k]$. A real cosine $A\cos(2\pi k_0n/N)$ with integer $k_0$ gives exactly two nonzero bins, $X[k_0] = X[N - k_0] = AN/2$.

## Circular operations

- **Circular shift**: $x[((n - m))_N] \leftrightarrow W_N^{km}X[k]$; samples leaving one end re-enter at the other.
- **Circular convolution**: $X_1[k]X_2[k] \leftrightarrow \sum_{m=0}^{N-1}x_1[m]x_2[((n - m))_N]$, the linear convolution with its samples beyond N − 1 folded back.

## Linear convolution with the DFT

For sequences of lengths L and P, the linear convolution is L + P − 1 long. Zero-padding both to N ≥ L + P − 1 points and multiplying their DFTs gives the linear convolution exactly. With the FFT, N is usually the next power of two.
:::

::: example
## Compute [1, 2, 3] ⊛ [1, 1] with 3-point and with 4-point DFTs.

::: solution
1. **Linear convolution.** The linear result is [1, 3, 5, 3].
2. **With N = 3.** The last sample folds onto the first: [4, 3, 5].
3. **With N = 4.** Since 4 ≥ 3 + 2 − 1, nothing wraps: [1, 3, 5, 3].
4. **In general.** The circular result is
   $$y_N[n] = \sum_ry_\text{lin}[n + rN]$$
   for 0 ≤ n < N.

**Answer:** [4, 3, 5] with 3-point DFTs, and [1, 3, 5, 3] with 4-point DFTs.
:::
:::

::: example
## A 16-point DFT of $x[n] = 2\cos(2\pi \cdot 3n/16)$. Which bins are nonzero, and what are they?

::: solution
1. **Which bins.** Bins 3 and 13 (= 16 − 3) are nonzero.
2. **Their value.**
   $$\begin{aligned} X[3] &= \frac{AN}{2} \\ &= \frac{2 \times 16}{2} \\ &= 16 \end{aligned}$$
   and the same for bin 13.
3. **The other bins.** All others are exactly zero because the cosine completes a whole number of cycles in the record.
4. **In hertz.** At $f_s$ = 8 kHz, bin 3 is at
   $$\begin{aligned} f &= 3 \times \frac{8000}{16}\ \text{Hz} \\ &= 1.5\ \text{kHz} \end{aligned}$$
5. **Between bins.** A frequency between bins, say 3.5 cycles per record, would spread into every bin, the leakage of lesson 29.

**Answer:** only bins 3 and 13 are nonzero, each equal to 16.
:::
:::

::: example
## Find the 4-point DFT of x = [1, 2, 3, 4], then the DFT of its circular shift [4, 1, 2, 3].

::: solution
1. **The DFT.**
   $$X[k] = \sum x[n](-j)^{kn}$$
   gives X = [10, −2 + 2j, −2, −2 − 2j].
2. **Shift theorem.** Shifting by one multiplies bin k by $W_4^k = (-j)^k$:
   $$\begin{aligned} X' &= [10,\ (-2 + 2j)(-j), \\ &\qquad (-2)(-1),\ (-2 - 2j)(j)] \\ &= [10,\ 2 + 2j,\ 2,\ 2 - 2j] \end{aligned}$$
3. **Check directly.**
   $$\begin{aligned} X'[1] &= 4 - j - 2 + 3j \\ &= 2 + 2j \end{aligned}$$
   as predicted.
4. **What changed.** The magnitudes are unchanged; only the phases rotate.

**Answer:** X = [10, −2 + 2j, −2, −2 − 2j], and the shifted sequence has X′ = [10, 2 + 2j, 2, 2 − 2j].
:::
:::

::: check
given: L = 50 | 100 | 400; P = 21 | 64 | 129
answer: L + P - 1
tolerance: 0

## Sequences of lengths {L} and {P} are convolved. How long is the result?

::: right
Right. L + P − 1 = {answer}.
:::

::: misses
- [L + P] One less: the last sample of each overlaps once.
- [any] Not yet. Count from the first overlap to the last.
:::
:::

::: check
given: L = 50 | 100 | 400; P = 21 | 64 | 129
answer: 2^ceil(log(L + P - 1)/log(2) - 0.0001)
tolerance: 0

## To compute that convolution with power-of-two FFTs, what's the smallest usable FFT size?

::: right
Right. The next power of two at or above L + P − 1: {answer}.
:::

::: misses
- [2^ceil(log(L + P - 1)/log(2) - 0.0001)/2] That's too short: the result would wrap around.
- [any] Not yet. N ≥ L + P − 1, rounded up to a power of 2.
:::
:::

::: check
given: L = 7 | 8 | 9; m = 1 | 2 | 3; n = 0 | 1 | 2; let N = L + m
answer: (L - abs(n - L + 1) + abs(L - abs(n - L + 1)))/2 + (L - abs(n + N - L + 1) + abs(L - abs(n + N - L + 1)))/2
tolerance: 0

## Two length-{L} pulses of ones are convolved circularly with {N}-point DFTs. What's output sample n = {n}?

::: right
Right. The linear triangle's value there plus its wrapped tail: {answer}.
:::

::: misses
- [(L - abs(n - L + 1) + abs(L - abs(n - L + 1)))/2] That's the linear convolution; add the part that wraps around from n + N.
- [any] Not yet. $y_c[n] = y_\text{lin}[n] + y_\text{lin}[n + N]$.
:::
:::

::: check
given: f = 1 | 2.5 | 6; s = 8 | 16 | 48; N = 64 | 256 | 1024
answer: f*N/s
tolerance: 0.5%

## A {f} kHz tone is sampled at {s} kHz and analyzed with a {N}-point DFT. At what bin index does it fall?

::: right
Right. $k = fN/f_s$ = {answer}.
:::

::: misses
- [f*N/s/2] Bins span 0 to $f_s$, not 0 to $f_s/2$.
- [any] Not yet. Bin spacing is $f_s/N$.
:::
:::

::: check
given: A = 1 | 0.5 | 3; N = 16 | 64 | 512
answer: A*N/2
tolerance: 0.5%

## $A\cos(2\pi k_0n/N)$ with A = {A}, N = {N} and integer $k_0$. What's $|X[k_0]|$?

::: right
Right. AN/2 = {answer}.
:::

::: misses
- [A*N] The cosine's energy splits between bins $k_0$ and $N - k_0$.
- [A/2] The DFT sums N samples without dividing by N.
- [any] Not yet. Half of A, times N.
:::
:::

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

## What's the bin spacing of an {N}-point DFT at {s} kHz?

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

::: misses
- [s*1000/(2*N)] The bins divide $f_s$, not $f_s/2$, into N steps.
- [any] Not yet. $f_s/N$.
:::
:::

::: check
kind: parts
given: L = 50 | 100 | 300; P = 20 | 64; N = 128 | 256

## Sequences of lengths {L} and {P} are convolved circularly with {N}-point DFTs.

::: part
answer: L + P - 1
unit: samples
tolerance: 0

### How long is their linear convolution?

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

::: part
answer: (L + P - 1 - N)*step(L + P - 1 - N)
unit: samples
tolerance: 0

### How many output samples are corrupted by wraparound?

::: misses
- [any] The overlap L + P − 1 − N, or none if that's negative.
:::
:::
:::

::: check
kind: derivation
vars: N = 4..1024, A = 0.1..10

## $x[n] = A\cos(2\pi k_0n/N)$ with an integer $0 < k_0 < N/2$.

::: step
answer: A*N/2

### What is $|X[k_0]|$?
:::

::: step
answer: A^2*N/2

### What is the signal's energy $\sum x^2[n]$?
:::

::: step
answer: 2*(A*N/2)^2/N

### What does Parseval's $\frac1N\sum|X[k]|^2$ give?
:::

::: right
Right. Two bins of AN/2 carry the whole energy, and Parseval agrees.
:::
:::

::: explain
## Why does multiplying two DFTs give circular rather than linear convolution?

::: model
The DFT treats its N samples as one period of a periodic sequence, because its basis functions, $e^{j2\pi kn/N}$, all repeat every N samples. Multiplying two DFTs therefore convolves two periodic sequences, and the convolution of periodic sequences is itself periodic with period N. Any part of the true linear convolution that extends beyond N samples has nowhere to go except into the next period, which overlaps the current one, so it adds onto the beginning. Padding both sequences with zeros to at least the full length of their linear convolution makes the periods long enough that nothing overlaps, and then the circular result is exactly the linear one.
:::
:::

::: practice

::: check
given: L = 80 | 300; P = 31 | 100
answer: 2^ceil(log(L + P - 1)/log(2) - 0.0001)
tolerance: 0

## Smallest power-of-two FFT for lengths {L} and {P}?

::: misses
- [any] Next power of two ≥ L + P − 1.
:::
:::

::: check
given: f = 1.5 | 4; s = 16 | 32; N = 128 | 512
answer: f*N/s
tolerance: 0.5%

## Bin index of {f} kHz at {s} kHz, N = {N}?

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

::: check
given: A = 2 | 5; N = 32 | 256
answer: A*N/2
tolerance: 0.5%

## |X[k₀]| for amplitude {A}, N = {N}?

::: misses
- [any] AN/2.
:::
:::
:::
