JOVANA
Explore Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

The Discrete Fourier Transform

Any list of N samples can be rewritten as a sum of pure waves. The discrete Fourier transform is the change of coordinates that finds the amount of each wave — a single complex matrix times your data. Here is what it computes, why it costs O(N^2), and what it quietly assumes.

A new pair of glasses for your data

Suppose you have measured something N times at equally spaced moments — a sound clip, a day of temperatures, the brightness along a row of pixels. You hold a list of numbers x_0, x_1, ..., x_{N-1}. That list is one honest description of the signal: its value at each instant. The discrete Fourier transform (DFT) gives you a second, equally complete description of the very same data, but written in a different language: not 'how big is the signal at each moment' but 'how much of each pure repeating wave is mixed into it'. Nothing is lost and nothing is added — it is a change of coordinates, like describing a point by (east, north) instead of (distance, bearing).

Why bother? Because some questions are almost invisible in one language and obvious in the other. A 60 Hz hum buried in a recording is a faint, tangled wiggle in the time list, but a single tall spike in the wave list. The same idea makes fast convolution, audio compression, and the spectral methods at the end of this rung possible. The DFT is the doorway to all of it; the rest of the rung is mostly about doing this one transform faster (the FFT) and putting it to work.

The building blocks: roots of unity

The pure waves we mix from are the complex exponentials, and it pays to meet them gently. Recall Euler's formula, e^(i*t) = cos(t) + i*sin(t): a point walking around the unit circle in the complex plane, with cos and sin its shadow on the two axes. A complex exponential carries a cosine and a sine together, which is exactly why one complex number per frequency is enough to record both how big a wave is and how it is shifted in time (its phase). It is bookkeeping, not mysticism.

Now sample one full circle at N equal steps. The N points e^(2*pi*i*k/N) for k = 0, 1, ..., N-1 are the N-th roots of unity — N equally spaced dots on the unit circle, each one a number that gives 1 when raised to the N-th power. Write w = e^(-2*pi*i/N) for the basic clockwise step (the DFT uses the minus sign by convention). Then w^0, w^1, w^2, ... march around the circle and, crucially, repeat with period N: w^N = 1, so w^{n+N} = w^n. These rotating numbers are the alphabet the DFT spells your signal in.

The transform itself, as one formula

The DFT turns the N time-samples x_0, ..., x_{N-1} into N frequency-coefficients X_0, ..., X_{N-1} by one weighted sum per coefficient. Coefficient X_k asks 'how much of the wave that completes k cycles across the window is present?' and answers by lining your data up against that wave and adding:

Forward DFT:   X_k = sum over n=0..N-1 of  x_n * w^(k*n)        with  w = e^(-2*pi*i/N)
Inverse DFT:   x_n = (1/N) * sum over k=0..N-1 of  X_k * w^(-k*n)

Matrix view:   X = F x,   where F is N x N with entries  F[k][n] = w^(k*n)
The DFT (top) and its inverse (middle). Both are the same matrix F applied to a vector — that single fact is the whole chapter.

Read it as a matrix-vector product, X = F x. The matrix F has one row per frequency k and one column per sample n, and the entry where they meet is the rotating number w^(k*n). Stacking the coefficients X_k is exactly multiplying your data vector x by this fixed N-by-N matrix F. Because every entry of F is built from the same roots of unity, F is enormously structured — far from a random matrix — and that hidden structure is precisely the loose thread the next guide pulls to collapse the cost. The inverse DFT is the same shape with w replaced by its conjugate and a 1/N out front, so you can travel back to the time samples and recover x_n exactly.

A tiny worked example: N = 4

Numbers make this concrete. Take N = 4, so w = e^(-2*pi*i/4) = -i, and the four roots of unity are 1, -i, -1, i. Feed in the four samples x = (1, 0, 1, 0) — a signal that flips on, off, on, off, the fastest pattern four samples can show. Apply the formula X_k = x_0 + x_1*w^k + x_2*w^(2k) + x_3*w^(3k) for each k. Walk it through.

  1. X_0 = x_0 + x_1 + x_2 + x_3 = 1 + 0 + 1 + 0 = 2. The k = 0 coefficient is always just the sum of all samples — it is N times the average, the signal's DC (constant) level.
  2. X_1 = x_0 + x_2*w^2 = 1 + 1*(-1) = 0, since x_1 and x_3 are zero and w^2 = (-i)^2 = -1. No energy at one cycle per window.
  3. X_2 = x_0 + x_2*w^4 = 1 + 1*(1) = 2, because w^4 = 1 (a full lap). This is the fastest frequency, two cycles per window — exactly the on-off-on-off pattern, so its coefficient is large.
  4. X_3 = x_0 + x_2*w^6 = 1 + 1*(-1) = 0, since w^6 = w^2 = -1. The result is X = (2, 0, 2, 0): all the signal's content sits in the constant level and the fastest wave, exactly as the pattern suggested.

Notice we already leaned on w^4 = w^0 and w^6 = w^2 — the powers wrap around because there are only N distinct roots. We computed the same handful of products again and again. Spotting and reusing that repetition is, in one sentence, the entire idea of the FFT in the next guide. Notice too that the answers came out as plain real numbers here only because this particular signal is symmetric; in general the X_k are complex, encoding both a size (how strong the wave is) and a phase (where its peak sits).

Why it costs O(N^2), and why that hurts

Count the work in the direct formula. Each coefficient X_k is a sum of N products, and there are N coefficients, so the bill is about N*N = N^2 complex multiply-adds. In the language of computational complexity the DFT done straight from the definition is O(N^2). For a tiny N that is nothing, but the curve is brutal: it is a matrix-vector multiply, and doubling N quadruples the work.

Put real numbers on it. A few seconds of CD-quality audio is already N in the millions. With N = 1,000,000 the direct DFT wants about 10^12 operations — a trillion — which is minutes of computer time for one transform you might need to run thousands of times. That single wall is why, for forty years, the DFT was a beautiful idea that was too slow to use at scale. The FFT in the next guide computes the exact same X_k in O(N log N) instead: for N = 1,000,000 that is about 20 million operations rather than a trillion, a speed-up of roughly fifty-thousand-fold. Same answer, different route through the arithmetic.

What the DFT quietly assumes

The DFT is exact and lossless, but it is honest only about a particular fiction: it treats your N samples as one period of a signal that repeats forever. The waves it uses all fit a whole number of cycles into the window, so the model silently wraps the end of your data around to meet the beginning. If your real signal does not start and end at the same value, that wrap creates a jump the DFT must explain using lots of high frequencies that are not really there — a smearing across the coefficients called spectral leakage. The cure (windowing, looked at later) tapers the ends so the seam is gentle.

There is a second, deeper assumption hiding in the very act of sampling: with only N samples there is a fastest wave you can possibly see, and anything faster gets misread as something slower. That is the Nyquist limit and the phenomenon of aliasing — the whole subject of guide 3 in this rung, where we make the sampling theorem precise. For now, two limits are enough to remember: the highest frequency the DFT can represent is half the sampling rate, and frequencies above it do not vanish — they fold back down and masquerade as lower ones.

Seen from yet another angle, the inverse DFT is a recipe for trigonometric interpolation: it builds the one combination of these N waves that passes exactly through your N data points. So the DFT is not just 'find the frequencies' — it is the natural way to interpolate and differentiate periodic data, which is exactly the door it opens to spectral methods at the end of this rung. Keep that thread in mind: today's matrix F is the slow but clear starting point, and almost everything ahead is about exploiting its structure rather than ever forming it.