2. The Discrete Fourier Transform
Decomposing signals into their frequency components
In the previous article we saw how to capture a continuous signal as a sequence of discrete numbers — samples taken at regular intervals. Now comes the fundamental question: what frequencies are hiding inside those numbers?
A recording of a musical chord contains many notes simultaneously. An electrical signal measured on a circuit carries a mixture of power-line hum, useful data, and noise at different rates. A seismic sensor captures ground vibrations across a wide range of frequencies. The raw time-domain samples tell you what happened at each moment, but they don't tell you which frequencies were responsible.
The Discrete Fourier Transform (DFT) answers that question. It takes N time-domain samples and produces N frequency-domain values, each telling you the amplitude and phase of a specific frequency component. It is one of the most important algorithms in all of applied mathematics.
The Core Idea: Any Signal Is a Sum of Sinusoids
In the early 19th century, Jean-Baptiste Joseph Fourier made a claim that seemed outrageous at the time: any periodic signal can be written as a sum of sines and cosines at integer multiples of some fundamental frequency. Smooth signal, square wave, sawtooth, anything — just a weighted sum of sinusoids.
Fourier's Insight
Sinusoids are the building blocks of all signals. Every periodic waveform — no matter how complex — is just a superposition of pure sine waves at different frequencies, amplitudes, and phases. The DFT finds those building blocks.
The collection of sinusoid amplitudes across frequencies is called the frequency spectrum of the signal. Knowing the spectrum tells you everything: you can reconstruct the original signal perfectly from its spectrum, and you can understand what's in it — which frequencies are strong, which are absent, which are noisy.
Complex Exponentials: The DFT's Language
Before writing the DFT formula, we need one key mathematical tool: the complex exponential. Euler's formula tells us:
Euler's Formula
e^(iθ) = cos(θ) + i·sin(θ)
This is not just a notational trick. The complex exponential
e^(iθ) represents a unit vector rotating in the complex
plane. The real part is the x-coordinate (cosine), the imaginary part
is the y-coordinate (sine). As θ increases, this vector sweeps around the unit
circle.
For the DFT, we use e^(-i2πkn/N) — a vector that rotates
clockwise at a frequency proportional to k, evaluated at N evenly-spaced points
around the circle.
Why use complex exponentials instead of plain sines and cosines? Because they unify both phase and amplitude into a single number, and the mathematics becomes elegant — products become rotations, convolutions become multiplications.
The DFT Formula
With that foundation, the DFT formula is:
The Discrete Fourier Transform
N-1
X[k] = Σ x[n] · e^(-i·2π·k·n/N)
n=0
Or equivalently, expanding Euler's formula:
N-1
X[k] = Σ x[n] · [cos(2πkn/N) − i·sin(2πkn/N)]
n=0
What each symbol means:
- x[n] — the n-th input sample (a real number)
- X[k] — the k-th frequency bin (a complex number)
- N — total number of samples
- k — frequency bin index, from 0 to N−1
- n — sample index, from 0 to N−1
The formula says: for each frequency k, multiply every sample by a rotating complex exponential at that frequency, then add everything up. The result X[k] is a complex number whose magnitude tells you how much of frequency k is present, and whose phase tells you where that frequency's sinusoid starts.
This is called correlation: we're asking "how well does a sinusoid at frequency k match the input signal?" A large X[k] means "very well".
What Each Frequency Bin Represents
The N output bins don't all correspond to distinct, useful frequencies. Here is how to read them:
Reading the Frequency Axis
If you have N = 64 samples taken at fs = 8000 Hz (8 kHz sample rate):
Frequency resolution: Δf = fs / N = 8000 / 64 = 125 Hz per bin
Bin 0: DC (0 Hz) — the average value of the signal
Bin 1: 125 Hz
Bin 2: 250 Hz
Bin 3: 375 Hz
...
Bin 32: 4000 Hz — Nyquist frequency (= fs / 2)
Bin 33–63: Mirrors of bins 31–1 (usually discarded)
Only bins 0 through N/2 are unique. For a real-valued input signal, X[N-k] = X[k]* (complex conjugate). The upper half is redundant. In practice, you plot only the first N/2 + 1 bins.
Magnitude and Phase
Each frequency bin X[k] is a complex number: X[k] = Re{X[k]} + i·Im{X[k]}. This encodes two things:
- Magnitude |X[k]| = √(Re² + Im²) — how much of frequency k is present. This is what the magnitude spectrum plots.
- Phase ∠X[k] = atan2(Im, Re) — where the sinusoid at frequency k starts (its phase offset). This is what the phase spectrum plots.
Why Magnitude Matters More
For most applications — audio analysis, vibration monitoring, signal filtering — we care about which frequencies are present and how strong they are. The magnitude spectrum gives us that directly. Phase matters when we need to reconstruct the signal (as in the inverse DFT) or when the relative timing of components is significant (as in radar or communications), but for visualization and analysis, magnitude is what we plot.
The magnitude at each bin has a natural interpretation. If your signal contains a pure 440 Hz sine wave (concert A) and you compute a DFT with enough frequency resolution, you'll see a spike in the magnitude spectrum at the bin nearest to 440 Hz, and near-zero values everywhere else.
The Inverse DFT
The DFT is perfectly reversible. Given the frequency-domain representation X[k], you can recover the original samples x[n] exactly:
The Inverse Discrete Fourier Transform (IDFT)
1 N-1
x[n] = ─ · Σ X[k] · e^(i·2π·k·n/N)
N k=0
The only differences from the forward DFT: the sign in the exponent is positive (instead of negative), and there's a 1/N normalization factor. This factor ensures that applying DFT then IDFT gives back the original signal unchanged.
Perfect reconstruction is only possible if you retain all N complex values X[0] through X[N-1] without modification. If you discard or modify frequency bins (e.g., to filter out noise), the reconstruction will differ from the original — which is exactly the point of filtering.
The IDFT is the mathematical backbone of signal synthesis: build a frequency spectrum with the amplitudes and phases you want, then invert it to get a time-domain signal that has exactly those properties.
Worked Example: 8-Point DFT
Let's compute the DFT of a small signal by hand. We'll use N = 8 samples of a signal that contains exactly two frequency components: a fundamental at bin 1 and a harmonic at bin 3.
Input Signal: x[n] for n = 0..7
Construct a signal: x[n] = cos(2π·1·n/8) + 0.5·cos(2π·3·n/8 + π/4)
n: 0 1 2 3 4 5 6 7
x[n]: 1.354 0.439 -0.854 -1.207 -0.646 0.207 1.268 1.040
DFT Computation: X[k] for k = 0..4 (unique bins)
For each k, compute X[k] = Σ x[n] · e^(-i2πkn/8). The angle step for bin k is: θ = -2πk/8 = -πk/4.
k=0 (DC):
X[0] = Σ x[n] · e^0 = Σ x[n] ≈ 1.354+0.439-0.854-1.207-0.646+0.207+1.268+1.040
≈ 1.601 (Re ≈ 1.601, Im ≈ 0.000)
|X[0]| ≈ 1.6 (small non-zero — rounding in sample construction)
k=1 (fundamental):
Angle per step: -π/4 ≈ -45°
X[1] = Σ x[n] · (cos(-πn/4) - i·sin(-πn/4))
≈ (4.000 + i·0.000)
|X[1]| = 4.000 ← strong peak, amplitude = 4/N × 2 = 1.0 ✓
k=2:
X[2] ≈ (0.000 + i·0.000)
|X[2]| ≈ 0.000 ← no energy at this frequency
k=3 (harmonic at 3×):
Angle per step: -3π/4
X[3] ≈ (1.414 - i·1.414)
|X[3]| ≈ 2.000 ← amplitude = 2/N × 2 = 0.5 ✓
∠X[3] = atan2(-1.414, 1.414) = -π/4 = -45° ← phase = -45° (matches input +π/4 → -π/4 convention)
k=4 (Nyquist):
|X[4]| ≈ 0.000
The magnitude spectrum clearly shows the two components at k=1 (strong) and k=3 (weaker), with near-zero values elsewhere. The DFT has successfully identified the two frequencies we put in.
Amplitude scaling: For a real signal, the physical amplitude of bin k is 2·|X[k]|/N (factor of 2 because energy is split between positive and negative frequency bins, except DC and Nyquist).
Properties of the DFT
The DFT has several properties that make it useful in practice:
| Property | What it means |
|---|---|
| Linearity | DFT(a·x + b·y) = a·DFT(x) + b·DFT(y) |
| Time shift | Delaying x[n] by d multiplies X[k] by e^(-i2πkd/N) — magnitude unchanged, phase shifts |
| Frequency shift | Multiplying x[n] by e^(i2πmn/N) shifts the spectrum by m bins |
| Convolution theorem | Circular convolution in time = pointwise multiplication in frequency |
| Parseval's theorem | Total energy is preserved: Σ |
The convolution theorem is especially powerful. Convolving two N-point signals in the time domain takes O(N²) operations. In the frequency domain, it's just N complex multiplications. Compute two DFTs, multiply, inverse DFT — this is the basis of fast convolution and FIR filter implementation.
Computational Cost
The straightforward DFT implementation loops k from 0 to N-1, and for each k loops n from 0 to N-1, computing e^(-i2πkn/N) each time. That's N² complex multiplications and N² complex additions: O(N²).
For N = 64, that's 4,096 operations — fine. For N = 1,024, it's over a million. For N = 1,048,576 (a one-second window at 1 MHz sample rate), it's a trillion. The DFT as written is too slow for large N.
Enter the FFT
The Fast Fourier Transform (FFT) computes exactly the same result as the DFT, but in O(N log N) time by exploiting redundancy in the twiddle factors e^(-i2πkn/N). The next article covers the FFT in detail. For now, understand the DFT conceptually — the FFT is just a faster way to compute it.
Sinusoid Decomposition: Seeing It Visually
The sum-of-sinusoids view makes the DFT's output directly interpretable. A complex waveform falls apart into its constituent parts:
Key Takeaways
- Any discrete signal can be expressed as a sum of complex sinusoids — the DFT finds the amplitude and phase of each
- X[k] = Σ x[n]·e^(-i2πkn/N) — for each frequency bin k, correlate the input with a rotating complex exponential
- Bin k corresponds to frequency k × (fs/N) Hz; only bins 0 through N/2 are unique for real signals
- The magnitude |X[k]| tells you how much of frequency k is present; the phase ∠X[k] tells you the offset
- The inverse DFT perfectly reconstructs the original signal from its frequency-domain representation
- Direct DFT computation is O(N²) — the next article covers the FFT, which achieves the same result in O(N log N)
Try It Yourself — DFT Visualizer
Select a preset waveform or choose Custom and click on the time-domain canvas to draw your own signal. The DFT magnitude spectrum updates instantly. Use Reconstruct to watch the inverse DFT add sinusoids one by one, or Step Through to highlight each frequency bin individually.
The DFT is one of those algorithms where the elegance of the mathematics and the utility of the result are perfectly matched. A single compact formula — sum, multiply, rotate — unlocks the full frequency content of any discrete signal. What seemed inscrutable as a sequence of 64 numbers becomes immediately legible as a spectrum with a handful of peaks.
The one catch is speed: the O(N²) direct computation becomes impractical for large N. The next article introduces the Fast Fourier Transform, which reduces the cost to O(N log N) through a divide-and-conquer factorization — and in doing so, made real-time signal processing possible on modest hardware.