The Discrete Fourier Transform
Computers cannot integrate over all time; they have samples. The discrete Fourier transform (DFT) is the Fourier transform made finite: numbers in, frequencies out, and an exact inverse. It is a change of basis in , to a basis of sampled complex exponentials, and the FFT computes it fast.
Definition
For :
measures the frequency cycles per samples; with sampling rate that is Hz. Indices represent negative frequencies , because is periodic in .
As a matrix
The DFT is multiplication by the matrix with entries , where . Its columns are orthogonal, and (the conjugate, since is symmetric).
import numpy as np
N = 4
k, n = np.meshgrid(np.arange(N), np.arange(N), indexing="ij")
F = np.exp(-2j * np.pi * k * n / N)
print("F for N = 4 (rounded):")
for row in np.round(F, 3):
print(" ", [complex(z.real + 0.0, z.imag + 0.0) for z in row])
x = np.array([1.0, 2.0, 0.0, -1.0])
print("F @ x :", np.round(F @ x, 6).tolist())
print("np.fft.fft :", np.round(np.fft.fft(x), 6).tolist())
print("F^H F / N = I:", np.allclose(F.conj().T @ F / N, np.eye(N)))F for N = 4 (rounded):
[(1+0j), (1+0j), (1+0j), (1+0j)]
[(1+0j), -1j, (-1+0j), 1j]
[(1+0j), (-1+0j), (1+0j), (-1+0j)]
[(1+0j), 1j, (-1+0j), -1j]
F @ x : [(2+0j), (1-3j), 0j, (1+3j)]
np.fft.fft : [(2+0j), (1-3j), 0j, (1+3j)]
F^H F / N = I: True
For the matrix contains only ; computing it by hand is a good exercise.
Reading the output
import numpy as np
fs, N = 64, 64 # 1 second at 64 Hz: bin k is k Hz
t = np.arange(N) / fs
x = 0.5 + np.cos(2 * np.pi * 5 * t) + 2 * np.sin(2 * np.pi * 12 * t)
X = np.fft.fft(x)
big = np.where(np.abs(X) > 1e-9)[0]
for k in big:
freq = k if k <= N // 2 else k - N
print(f"bin {k:2d} ({freq:+3d} Hz): X = {complex(round(X[k].real, 3) + 0.0, round(X[k].imag, 3) + 0.0)}")bin 0 ( +0 Hz): X = (32+0j)
bin 5 ( +5 Hz): X = (32+0j)
bin 12 (+12 Hz): X = -64j
bin 52 (-12 Hz): X = 64j
bin 59 ( -5 Hz): X = (32+0j)
- : the DC term.
- The cosine of amplitude 1 gives at Hz (real).
- The sine of amplitude 2 gives at Hz (imaginary).
So amplitude for .
In software
np.fft.fft/ifftuse exactly these formulas (no scaling forward, backward);norm="ortho"uses both ways, making unitary.- The DFT assumes the samples are one period of a periodic signal. When they are not, spectral leakage appears.
Exercises
Exercise 1: A 2-point DFT
Compute the DFT of .
Answer
.
Solution
With , . So (sum) and (difference). This butterfly is the building block of the FFT.
Exercise 2: Bin frequency
With Hz and , what frequency does bin 100 represent, and what is the bin spacing?
Answer
About 1076.7 Hz; spacing about 10.77 Hz.
Solution
Spacing Hz, so bin 100 is at about 1076.7 Hz.
Exercise 3: Inverse by the matrix (code)
Build for and recover from using .
Answer
The original vector comes back.
Solution
import numpy as np
N = 8
k, n = np.meshgrid(np.arange(N), np.arange(N), indexing="ij")
F = np.exp(-2j * np.pi * k * n / N)
x = np.array([3, 1, 4, 1, 5, 9, 2, 6], dtype=float)
X = F @ x
print((np.round((F.conj() @ X / N).real, 10) + 0.0).tolist())[3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0, 6.0]
Test yourself
Bin of an -point DFT at sampling rate corresponds to…
- Hz
- Hz
- Hz
- Hz
For real input, equals…
- 0
The DFT matrix satisfies…
- is singular