The Discrete Fourier Transform

A signal is a list of numbers, and the list often looks like a mess. Change the basis, the set of building blocks you describe it with, and a few spikes can say it all.

Learn how to rewrite a signal in a new basis, why the spinning-arrow basis of the DFT is so special, and what every property of the transform means in pictures.

Pick a signal, then drag numbers kept down. Switch between Samples and Frequencies and watch how few numbers rebuild each signal. Then we build the transform piece by piece, from vectors to spinning arrows to a matrix.

You need vectors and dot products and complex numbers (we recap both in plain words). We build the rest from zero.

Based on the Lecture 3 deck of Stanford EE269: Signal Processing for Machine Learning (Jan 2019), taught by Mert Pilanci. Course slides

Keep the biggest numbers

6

Illustrative signals of 64 samples, made for this page: tones at whole bins 5 and 12, tones at bins 4, 9 and 17 plus seeded hiss, one click of height 5 at sample 20, and a square wave of period 16. The DFT here is the lecture’s unitary form, with 1/√N in front.

Chapter 0

Why Change the Basis

See why the same list of numbers can hide its meaning in one description and shout it in another

Imagine you record one second of a guitar. Your computer hands you a list of 44,100 numbers, one for each tiny slice of time. You scroll through them: 0.02, 0.07, 0.11, −0.04, … Is that an A or an E? Nothing in the list says so.

Now describe the same second a different way. Do not say what the air pressure did at each instant. Say how much of each pure pitch is in the sound. Suddenly one pitch stands out, with a few smaller ones above it. The note is obvious.

Both descriptions hold exactly the same information. You can go from one to the other and back without losing a digit. This lecture is about that trade, and about the one new description that matters most in signal processing: the discrete Fourier transform, or DFT.

A basis is a set of building blocks

The lecture opens with a recap. A signal here is just a list of numbers, x[0], x[1], and so on up to x[N − 1]. Think of the list as a point in a space with N directions, the way (2, 5) is a point on a flat page with two.

A basis is a set of building blocks. Pick M signals, each one a list of numbers. If you can make any other signal by scaling those blocks and adding them up, the blocks are a basis. The scale factors are the coefficients.

x[n]
The signal: one number for each position n.
∑
Add up the terms, one for each building block, from block 0 to block M − 1.
c[k]
The coefficient of block k: how much of that block the signal contains.
y(k)[n]
Building block number k, itself a list of numbers. The small k in brackets names the block, not a power.

Changing the basis means swapping one set of blocks for another. The signal does not change. Only the list of coefficients changes, and with it what you can see.

The four jobs, and what each one wants

Slide 3 asks why anyone would bother. It gives four jobs. Each one wants the coefficients c[k] to have a particular shape.

In all four, a good basis makes the signal sparse: a few big coefficients and many near zero. No basis is best for every signal. The hero at the top of this page showed it: a click is sparse in the samples and spread across every frequency. This lecture builds the basis that is sparse for waves.

Try it: which speaker is talking?

Here is the classify job at toy size. Two speakers hum a steady pitch. Speaker A hums at 6 cycles per window of 64 samples. Speaker B hums at 10. A microphone adds hiss. Look at the samples first, then at the frequency bars.

Hear through the hiss

Top: the 64 noisy samples. Bottom: the size of each Fourier coefficient. Choose a speaker, raise the hiss, and press New draw. Marks A and B show where each speaker’s pitch lives.

2.0

Toy signals made for this page: a pure tone of height 1 with a random start, plus Gaussian hiss. The accuracy is measured live on 300 fresh draws, half from each speaker, by asking which of bins 6 and 10 is bigger.

At low hiss the tone is plain in both views. Raise the hiss to 2 and the samples look like pure noise: the tone has height 1, the hiss is twice as big. Yet one bar still rises. The tone put all its energy into a single coefficient, while the hiss spread thinly across all of them. A basis can concentrate a signal and spread noise. That asymmetry is the whole game.

The road

The lecture walks one path. It builds the idea of a basis, makes it orthonormal, builds the Fourier basis, and turns that into the DFT. Then it learns the transform by its pairs and its properties, and ends with a matrix.

  1. Bases and independence: building blocks, coordinates, and when a set fails to be a basis.
  2. Orthonormal bases: why dot products give the coefficients, and how to change basis.
  3. Gram-Schmidt: turning any basis into an orthonormal one.
  4. The Fourier basis: spinning arrows, and a proof that they are orthonormal.
  5. The DFT: analysis and synthesis, with a full worked example.
  6. DFT pairs: the transform of a click, a constant, a wave, a cosine and a pulse.
  7. Bins and Nyquist: which real frequency each coefficient means, and the mirror.
  8. Leakage and extraction: what goes wrong when a tone falls between bins.
  9. Properties: linearity, duality, symmetry, shifts and real signals.
  10. The matrix view: the DFT as one matrix multiplication.
  11. The field guide: numbers, checklist and code.
A tone is buried in hiss, and you cannot see it in the samples. Why can the Fourier coefficients still show it?

Chapter 1

Bases and Independence

Name any point with a different set of directions, and learn when a set of directions fails to work

You ask a stranger for the cafe. “Go three blocks east, then four blocks north.” Two numbers, and you arrive. The stranger used two directions, east and north, and a count along each.

A different stranger says, “Go five blocks along the diagonal avenue, then a block along the cross street.” Different directions, different counts, same cafe. That is a change of basis, and it needs no new maths. It only needs us to say what the directions are.

The standard basis: the one you already use

A list of N samples is already written in a basis. The building blocks are the shifted deltas. A delta, written δ[n], is a signal that is 1 at position 0 and 0 everywhere else. Shift it by m and the 1 moves to position m.

x[n]
The signal.
∑
Add the blocks, one delta for each position.
c[m]
The coefficient of the delta at position m. It equals x[m], the sample itself.
δ[n − m]
A 1 at position m, zeros elsewhere. The slide calls the whole set the canonical or standard basis.

So the samples are the coefficients. When we say “the signal in the time domain”, we mean the signal written in the standard basis. It is the description you get for free. Every other basis asks you to do some work.

How do you know a set of directions is a basis?

Slide 5 gives the test. For signals of length N, a set of N signals is a basis if the signals are linearly independent. That phrase has a plain meaning: no block can be built from the others.

The formal version says: try to cancel the blocks to zero, using weights β0 to βN−1. If the only way is to use zero of every block, the set is independent. If some non-zero weights also cancel, the set is linearly dependent.

∑
Add up all N blocks, each one scaled.
βm
A weight you choose for block m. Greek beta, just a number.
y(m)[n]
Block m.
βm = 0
The only weights that make the sum vanish are all zero. That is independence.

A tiny dependent set shows why this matters. Take y(0) = (1, 2) and y(1) = (2, 4). The second is twice the first. Choose β0 = 2 and β1 = −1. Then 2(1, 2) − (2, 4) = (0, 0), with non-zero weights. Every mix of these two blocks lands on one line through the origin. The point (2, 5) is off that line, so no weights reach it.

Why exactly N blocks? With fewer, the blocks reach only a flat slice of the space, like the line above. With more, some block is a mix of the rest, so the coefficients are not unique. N independent blocks give every point exactly one set of coefficients.

Solve one by hand

Use the lecture’s point x = (2, 5). Take the blocks a = (1, 1) and b = (1, −1). We want c0 and c1 with c0a + c1b = x. That is two equations:

c0 + c1 = 2   and   c0 − c1 = 5

Add them: 2c0 = 7, so c0 = 3.5. Subtract the second from the first: 2c1 = −3, so c1 = −1.5. Check: 3.5(1, 1) − 1.5(1, −1) = (2, 5). It works, but we had to solve a system. In N dimensions that is a large job. Chapter 2 shows a special kind of basis where you skip the solving.

Now play. Block 0 is fixed along the horizontal. You turn block 1. Drag the point x anywhere, and watch the parallelogram: walk c0 along block 0, then c1 along block 1, and you land on x.

Turn a block, reach a point

Purple arrows are the two blocks, the dashed lines extend them. Teal is the point x. Drag x on the canvas, or turn block 1 with the slider. Push the angle to 0 or 180 degrees and watch the coefficients explode.

90°

Toy plane with two blocks of length 1. Block 0 points right, block 1 points at the angle you set. The coefficients solve c0b0 + c1b1 = x exactly; the test number is sin(angle), the determinant of the two blocks.

At a right angle, the coefficients are just the horizontal and vertical positions. Tilt block 1 to 45 degrees and both coefficients change, but a point is still reached by exactly one pair. Squeeze the angle toward zero and the blocks nearly lie on top of each other. To reach a point off their shared line you must walk far along one and far back along the other. The coefficients become huge, and a small error in x becomes a huge error in c.

That is the failure mode of a poor basis, told as symptom, measurement and fix. The symptom: giant coefficients that cancel each other. The measurement: the determinant, here sin(angle), falls toward zero. The fix: choose blocks that point in clearly different directions. The best choice is blocks at right angles, which is where the next chapter goes.

The blocks (1, 2) and (3, 6) are used in the plane. What is wrong?

Chapter 2

Orthonormal Bases and Changing Basis

Find every coefficient with one dot product, and move a signal into a new basis in two plain steps

Hold a stick upright in the sun. Its shadow on the ground tells you how far the stick leans east. The shadow on a wall tells you how far it leans north. Two shadows, two numbers, and you can rebuild the stick. Why does that work so easily? Because east and north are at right angles. Neither shadow knows anything about the other direction.

The last chapter solved two equations by hand to find coefficients. With blocks at right angles we can skip that. Each coefficient is one shadow, and a shadow is a dot product.

The inner product: a shadow with a name

The inner product of two signals multiplies them position by position and adds the results. It measures how much the two point the same way. For complex signals the lecture conjugates the first one. The conjugate of a complex number a + bj is a − bj, with the sign of the imaginary part flipped. For real signals, the conjugate changes nothing.

⟨u, v⟩
One number that says how much u and v overlap. It is 0 when they are perpendicular.
∑
Add over all N positions.
u[n]∗
The conjugate of u at position n. The star means flip the sign of the imaginary part.
v[n]
The second signal at position n.

The norm of a signal is its length, ⟨x, x⟩ under a square root. A signal of norm 1 has unit length.

What orthonormal means

Slide 6 defines it in two conditions. A basis is orthonormal when every pair of different blocks is orthogonal (inner product 0) and every block has norm 1. In symbols, ⟨y(k), y(l)⟩ = 0 for k ≠ l, and ‖y(k)‖ = 1. The slide points out that the delta basis already qualifies: two different deltas never overlap, and each has a single 1.

Why one dot product gives one coefficient

Suppose x is built from an orthonormal basis: x = c0y(0) + c1y(1) + … We want ck. Take the inner product of both sides with y(k):

⟨y(k), x⟩ = c0⟨y(k), y(0)⟩ + c1⟨y(k), y(1)⟩ + … = ck · 1

Every term vanishes except the one where the block meets itself. That term is ck times the norm squared, which is ck times 1. So the coefficient is just ⟨y(k), x⟩. No equations to solve. This is the entire reason orthonormal bases are loved.

The two-step recipe for changing basis

Slide 9 turns this into a recipe. To move a signal x into an orthonormal basis:

  1. Analysis. Take the inner product of x with every block. These numbers are the expansion coefficients ⟨y(k), x⟩.
  2. Synthesis. Scale each block by its coefficient, then add the scaled blocks. You get x back.
x[n]
The signal, rebuilt.
∑
Add the N scaled blocks.
⟨y(k), x⟩
The analysis step: the shadow of x on block k.
y(k)[n]
The synthesis step: block k, scaled by its shadow.

A worked example, every step

Slide 10 starts with x = (2, 5) in the standard basis. The formula gives x = 2(1, 0) + 5(0, 1). Each coefficient is the sample itself, as Chapter 1 said.

Slide 11 then picks a new orthonormal basis: y(0) = (3, 1)/√10 and y(1) = (1, −3)/√10. Check it first. The dot product (3, 1) · (1, −3) = 3 − 3 = 0, so they are perpendicular. Each has length √(9 + 1)/√10 = 1. Good.

Step 1, analysis. ⟨y(0), x⟩ = (3·2 + 1·5)/√10 = 11/√10 ≈ 3.479. And ⟨y(1), x⟩ = (1·2 − 3·5)/√10 = −13/√10 ≈ −4.111.

Step 2, synthesis. (11/√10)(3, 1)/√10 = (33, 11)/10. And (−13/√10)(1, −3)/√10 = (−13, 39)/10. Add: (20, 50)/10 = (2, 5). We are back at x.

One more check, called Parseval’s identity: an orthonormal change keeps the total energy. Before: 22 + 52 = 29. After: (11/√10)2 + (13/√10)2 = 121/10 + 169/10 = 29. Nothing was lost; the same 29 units of energy are just shared out differently. Turn the basis yourself below.

Read the shadows

Purple lines are the two blocks. Orange dots are the shadows of x on them, the coefficients. Drag x, turn the basis, then tilt block 1 to break the right angle and watch the rebuild miss.

18.4°
0°

Toy plane. Block 0 points at the angle you set. Block 1 is perpendicular to it, then tilted by the second slider. The coefficients are always the dot products ⟨y, x⟩. The lecture’s basis (3, 1)/√10 sits at 18.4 degrees.

With the tilt at 0 the two shadows always rebuild x exactly, whichever way you turn the basis, and the energies always add to |x|². Add a few degrees of tilt and the rebuilt point drifts away from x. The two shadows now overlap: block 1 carries a little of block 0’s direction, so that direction is counted twice.

Read it as symptom, measurement and fix. The symptom: the rebuild lands somewhere else. The measurement: ⟨y(0), y(1)⟩ is no longer 0, and the energies no longer add up. The fix: first make the basis orthonormal. The next chapter shows how.

In the orthonormal basis y(0) = (3, 1)/√10 and y(1) = (1, −3)/√10, what is the first coefficient of x = (2, 5)?

Chapter 3

Gram-Schmidt: Squaring Up a Basis

Turn any set of independent directions into right-angled unit blocks, one block at a time

You inherit a set of rulers that meet at odd angles. They do reach every point, so they are a basis. But the coefficients are painful to find. You would like square rulers instead. Can you build them from the ones you have?

Yes, and the recipe is gentle. Keep the first ruler. For the second, remove whatever part points along the first, so the leftover is at a right angle. Then trim it to length 1. Repeat for each new ruler, removing its shadow on all the earlier ones. This is Gram-Schmidt orthogonalisation, slide 12.

The recipe in symbols

Start with a basis φ1, φ2, …, φN. Produce an orthonormal basis ψ1, ψ2, … with the same reach. Step 1 simply normalises the first vector. Step k removes the old shadows, then normalises.

ψk′
The leftover of the new vector after the old shadows are removed. The prime marks it as not yet unit length.
φk
The k-th vector of the original, skewed basis.
∑
Add over every earlier square block, from the first to number k − 1.
⟨ψi, φk⟩ψi
The shadow of φk on the earlier block ψi: a dot product, then that much of the block.
ψk
The leftover divided by its own length, so the new block has norm 1.

Why the leftover is perpendicular

Check the second vector. Take the inner product of ψ1 with the leftover: ⟨ψ1, φ2⟩ − ⟨ψ1, φ2⟩⟨ψ1, ψ1⟩. The block has norm 1, so the last factor is 1, and the two terms cancel to 0. You removed exactly the part that overlapped, and nothing else. The same argument works for every earlier block.

A worked example, every step

Take φ1 = (3, 1) and φ2 = (2, 2). They are not perpendicular: 3·2 + 1·2 = 8, not 0.

  1. Normalise the first. ‖φ1‖ = √(9 + 1) = √10, so ψ1 = (3, 1)/√10. This is the first block of the last chapter.
  2. Find the shadow. ⟨ψ1, φ2⟩ = (3·2 + 1·2)/√10 = 8/√10. The shadow vector is (8/√10)(3, 1)/√10 = (24, 8)/10 = (2.4, 0.8).
  3. Subtract it. ψ2′ = (2, 2) − (2.4, 0.8) = (−0.4, 1.2).
  4. Normalise. ‖ψ2′‖ = √(0.16 + 1.44) = √1.6 ≈ 1.265. So ψ2 = (−0.4, 1.2)/1.265 = (−1, 3)/√10.

Test: ⟨ψ1, ψ2⟩ = (3·(−1) + 1·3)/10 = 0. Both have length 1. You have an orthonormal basis, and it is (up to a flipped sign) the one from the lecture’s example. Step through it below, and turn φ2 to see what happens as it nears φ1.

Subtract the shadow

Grey arrows are the skewed vectors. Press a step to see the shadow, the leftover and the final unit block. Then turn φ2 with the slider.

45°

Toy vectors: φ1 = (3, 1) is fixed and φ2 has the lecture-sized length 2.83 and the direction you set. At 45 degrees it is (2, 2), the worked example above.

At 45 degrees the leftover has length 1.26. Turn φ2 toward φ1, which sits at 18.4 degrees, and the leftover shrinks toward nothing. At exactly 18.4 degrees the two are parallel, the leftover is the zero vector, and the last step divides by zero.

This is the failure mode in numbers. The symptom: a leftover almost zero. The measurement: ‖ψk′‖, which tells you how independent the new vector really was. The fix: drop that vector and choose another, because the set was not a basis. Near this edge, even a tiny rounding error in the leftover gets blown up by the division, so the new block points the wrong way.

In Gram-Schmidt, why is the leftover ψ′ of the second vector perpendicular to the first block?

Chapter 4

The Fourier Basis: Spinning Arrows

Meet N special signals made of one spinning arrow each, and prove that they are orthonormal

Picture a wheel turning under a strobe light. Each flash catches one frame. If the wheel turns a little between flashes, you see a smooth spin. If it turns almost a full circle between flashes, the picture looks like a slow spin in the other direction. A strobe samples a spinning thing. The Fourier basis is built from exactly this picture.

A quick recap: the spinning arrow

A complex number is a point on a flat page, written a + bj. The a is how far right, the b is how far up. Draw an arrow from the middle to that point and you can see it as an arrow instead.

The special number ejθ is the arrow of length 1 that points at angle θ. Its written form is cosθ + j sinθ. The cosine is the horizontal part. The sine is the vertical part. If θ grows steadily, the arrow spins.

The definition

Slide 7 defines the normalized Fourier basis. There are N blocks, numbered m = 0 to N − 1. Block m is a list of N complex numbers, one for each position n.

wm[n]
Block number m, at position n. One complex number.
1/√N
A shrink factor chosen so that the whole block has length 1.
ej(2π/N)nm
An arrow of length 1, turned by 2πnm/N radians. Each step in n turns it by a further m/N of a full circle.

Read it as a recipe for a spinning arrow. At position 0 the arrow points right. At each next position it turns by m/N of a full circle. Block 0 never turns: all its entries are the same. Block 1 makes one full turn over the N positions. Block 2 makes two. The real parts of these arrows are cosines, and the imaginary parts are sines, so each block is a sampled wave.

The length of each block is easy to check. It has N entries. Each entry is an arrow of length 1, shrunk by 1/√N, so its squared length is 1/N. Add N of them: N · (1/N) = 1. The norm is 1.

Spin one block

Left: the arrow’s tip at each position n, joined in order. Right: its cosine part (purple) and sine part (pink). Drag m and see how many turns fit.

3
16

The tips are drawn at full size (length 1). The real signal entries carry the extra 1/√N shrink. Try m larger than half of N: the arrow turns more than half a circle per step, so it looks like a slower spin the other way.

Notice what happens near the top of the slider. At N = 16 and m = 15, the arrow turns 15/16 of a circle per step. That is the same as turning 1/16 of a circle backwards. Block 15 looks exactly like a slow backward spin. We will use that in Chapter 7 to explain the mirror in every spectrum.

The proof that they are orthonormal

Slide 8 poses the question: what is ⟨wk, wr⟩? Use the inner product of Chapter 2, with the conjugate on the first block. Conjugating a spinning arrow just reverses its spin, so ejθ becomes e−jθ.

⟨wk, wr⟩ = (1/N) ∑n e−j2πkn/N ej2πrn/N = (1/N) ∑n ωn,   where ω = ej2π(r−k)/N

The product of spinning arrows is one arrow with the spins combined. So we are adding the powers ω0, ω1, up to ωN−1. Two cases.

Case k = r. Then ω = e0 = 1, every term is 1, and the sum is N. After the factor 1/N the answer is 1. That is the norm check again.

Case k ≠ r. Then ω is not 1. The sum of a geometric series is (1 − ωN)/(1 − ω). Here ωN = ej2π(r−k) = 1, because r − k is a whole number of full turns. So the top is 1 − 1 = 0, and the bottom is not zero. The sum is 0.

So ⟨wk, wr⟩ is 1 when k = r and 0 otherwise. The N spinning blocks are orthonormal. Since there are N of them in an N-dimensional space, they form a basis. The picture of the proof is simple, and you can play it.

Add the arrows

Each arrow is the term ωn, chained head to tail. Choose the difference d = r − k. When the chain closes into a loop, the sum is zero and the two blocks are orthogonal.

1
8

Computed live: the sum of ωn for n = 0 to N − 1, then divided by N. The tiny leftovers such as 0.00 are rounding in the computer, not in the maths.

With d = 0 every arrow points right, and the chain is a straight line of length N. With any other d the arrows turn a little each step, and after N steps they have gone round a whole number of times and come home. A closed polygon has zero gap between end and start, which is zero sum.

Why does the sum of the arrows ω0 to ωN−1 equal 0 when k ≠ r?

Chapter 5

The DFT: Analysis and Synthesis

Apply the two-step recipe to the spinning-arrow basis and get the transform, and its undo button

You now own two tools. Chapter 2 gave a recipe for any orthonormal basis: dot products down, scaled blocks back up. Chapter 4 gave a very special orthonormal basis. Put them together and you have the transform that signal processing is built on.

Analysis: ask each arrow how much it matches

Take block k, the arrow that turns k/N of a circle per step. Take its inner product with your signal x. The conjugate flips the spin, so the sum is:

X[k]
The k-th DFT coefficient: how much of the spinning arrow k the signal contains. A complex number.
1/√N
The unit-length factor that makes the transform keep energy.
∑
Add over all N positions of the signal.
x[n]
The signal at position n.
WNnk
An arrow of length 1 turned backwards by 2πnk/N. This is the conjugate of block k.
WN
The one basic arrow: a clockwise turn of 1/N of a circle. Every entry of the transform is a power of it. Engineers call it the twiddle factor.

This is the discrete Fourier transform. Slide 14 gives it the name and calls the formula analysis, because it takes the signal apart into its arrows.

Synthesis: stack the arrows back up

The second step scales each block by its coefficient and adds. The blocks are e+j…, so the result has the opposite sign in the exponent:

x[n]
The signal, rebuilt exactly.
1/√N
The same factor again, so the round trip returns the same size.
∑
Add the N scaled arrows.
X[k]
How much of arrow k to use.
WN−nk
Arrow k at position n, spinning forwards again.

Slide 14 adds a warning that matters when you read other books or code libraries. The factor 1/√N can be split between the two formulas in different ways. Some books put 1/N on the synthesis and nothing on the analysis. Slide 15 will do exactly that. The shapes of the results are the same. Only a constant scale differs. In this lesson we use the symmetric version above until the pairs chapter, and then say so.

A worked example, every step

Take N = 4 and x = (1, 2, 0, −1). The twiddle factor is W4 = e−j2π/4 = e−jπ/2 = −j. Its powers go round a quarter circle each time: W0 = 1, W1 = −j, W2 = −1, W3 = j.

  1. X[0], the arrow that does not turn: 1 + 2 + 0 − 1 = 2. Divide by √4 = 2: 1.
  2. X[1], weights 1, −j, −1, j: 1·1 + 2·(−j) + 0·(−1) + (−1)·j = 1 − 3j. Divide by 2: 0.5 − 1.5j.
  3. X[2], weights 1, −1, 1, −1: 1 − 2 + 0 + 1 = 0. So 0.
  4. X[3], weights 1, j, −1, −j: 1 + 2j + 0 + j = 1 + 3j. Divide by 2: 0.5 + 1.5j.

Check the energy. In time: 1 + 4 + 0 + 1 = 6. In frequency: 1 + (0.25 + 2.25) + 0 + (0.25 + 2.25) = 6. Same. Now synthesise one sample as a test, x[1]. The weights are the conjugates: 1, j, −1, −j. We get (1·1 + (0.5 − 1.5j)·j + 0 + (0.5 + 1.5j)·(−j))/2 = (1 + (1.5 + 0.5j) + (1.5 − 0.5j))/2 = 4/2 = 2. That is x[1]. The imaginary parts cancelled by themselves.

Notice X[3] is the conjugate of X[1]. That is no accident, because x was real, and Chapter 7 explains why. Now try your own signal. Draw samples by dragging on the top panel. Then limit the rebuild to the first few frequencies to watch synthesis add arrows one pair at a time.

Draw it, transform it, rebuild it

Drag across the top panel to set the samples. The bars below are |X[k]|. Slide Frequencies used to rebuild with only the lowest few, and watch the orange line close in on your signal.

8

Toy signals you edit yourself. The transform is the unitary one above, computed live by the sum of N² terms. “Frequencies used” keeps bin k and its mirror bin N − k together, up to the number you choose.

With all frequencies the orange line passes through every sample, whatever you draw. That is the promise of a basis: nothing is lost. Drop the highest frequencies and the line smooths out. Drop the lowest and the shape tilts. A smooth signal needs few frequencies, and a jagged one needs many. That link between roughness and high frequency will keep returning.

The DFT analysis step computes X[k] as an inner product with block k. Why does the exponent have a minus sign?

Chapter 6

DFT Pairs: A Dictionary of Signals

Learn five signals and their spectra, then build the spectrum of almost anything from them

A traveller does not learn a language by memorising every sentence. They learn a few hundred words, and a few rules for joining them. The DFT works the same way. A handful of transform pairs, a signal and its spectrum, plus one joining rule, cover most of what you will meet.

From this chapter on, the lecture drops the 1/√N factor from the analysis formula. So slides 15 to 19 use the plain sum, and we do too:

X[k]
The spectrum at bin k. This version is √N times bigger than the symmetric one.
∑
Add over all positions.
x[n]
The signal.
e−j2πnk/N
The backwards-spinning arrow for bin k.

Pair 1: a click becomes every frequency

Let x[n] = δ[n − n0], a single 1 at position n0. In the sum, every term is zero except n = n0. So X[k] = e−j2πn0k/N. Its size is 1 for every k: a flat spectrum. A click contains all frequencies equally. Where the click sits only changes the phase, the angle of each arrow, and the angle grows in proportion to k.

Pair 2: a constant becomes one spike

Let x[n] = 1 for every n. Then X[k] is the sum of e−j2πnk/N over n. That is the chain of arrows from Chapter 4. At k = 0 each term is 1 and the sum is N. At any other k the chain closes and the sum is 0. So X[k] = Nδ[k]: a single spike of height N at bin 0. Flat in time means all the energy at zero frequency.

Pair 3: a spinning arrow becomes one spike

Let x[n] = ej2πk0n/N, a spinning arrow with k0 turns per window. Multiply it by the backwards arrow for bin k. The product spins at a rate k0 − k. That chain closes unless k = k0. So X[k] = Nδ[k − k0]: one spike at k0. Notice how pairs 1 and 3 mirror each other. A click in time is a spinning arrow in frequency, and the reverse. Chapter 9 names this duality.

Pair 4: a cosine is two arrows, by linearity

A cosine is not a single arrow. But it is the average of two. Slide 18 writes the identity: cos(2πLn/N + φ) = ½(ej(2πLn/N + φ) + e−j(2πLn/N + φ)). The transform is linear: the transform of a sum is the sum of the transforms. Apply pair 3 to each half:

X[k] = (N/2) ejφ δ[k − L] + (N/2) e−jφ δ[k + L]

Two spikes, each of height N/2, at k = L and at k = −L. The second one sits at N − L because bins wrap around. For N = 64 and L = 5 with no phase shift, the spikes are 32 high at bins 5 and 59. Check with energy: in time the cosine holds 32 units. The plain-sum rule is that frequency energy is N times larger: (32² + 32²)/64 = 32. They agree.

Pair 5: a pulse becomes a ringing curve

Slide 19 asks for the spectrum of a rectangle: x[n] = 1 for n up to M − 1 and 0 afterwards. This is a sum of M arrows, a geometric series with ratio ω = e−j2πk/N:

X[k] = (1 − ωM)/(1 − ω) = e−jπ(M−1)k/N · sin(πMk/N) / sin(πk/N)

The last step pulls half of each power out of the top and the bottom: 1 − e−j2a = e−ja · 2j sin a. The phase factor is the pulse sitting off-centre. The ratio of sines is the size. At k = 0 it gives M. It first hits zero at k = N/M. So a wide pulse has a narrow central lobe, and a narrow pulse has a wide one. The ratio of sines is called the Dirichlet kernel.

Browse the dictionary

Pick a pair. The top panel shows the signal (real part), the bottom its spectrum size |X[k]|. Move the parameter and compare the computed bars with the lecture’s formula, the dots.

5

N = 32 samples, plain-sum DFT as on the slides. The dots are the closed forms from slides 15 to 19: 1, Nδ[k], Nδ[k − k0], N/2 at k = L and N − L, and the ratio of sines. The arrow signal is complex, so its real part is drawn.

Try the pulse. At M = 4 the lobe is wide: the first zero is at bin 8. Stretch the pulse to M = 16 and the first zero moves to bin 2. The bars and dots agree to the last digit, because the formula is exact. Squeeze the pulse to M = 1 and it becomes a click, whose spectrum is flat. You have just crossed from pair 5 back to pair 1.

The trade is the first of many in this subject. Concentrate a signal in time and it spreads in frequency. Concentrate it in frequency and it spreads in time. Neither can be narrow in both at once. Later lessons on the short-time transform and wavelets are about managing exactly this.

A pulse of M = 8 ones sits in N = 32 samples. Where is the first zero of its spectrum size?

Chapter 7

Bins, Nyquist and the Mirror

Turn bin numbers into real frequencies in hertz, and see why half of every spectrum is a reflection

A microphone takes 44,100 readings every second. You grab the next 1,024 of them and run the DFT. Out come 1,024 complex numbers, numbered 0 to 1,023. A friend asks, “Is there a 440 hertz note in there?” You cannot answer from the bin numbers alone. You need a translation from bin to hertz.

From bins to hertz

The signal is x[n] = x(nT): a continuous signal, read every T seconds. The sampling frequency is fs = 1/T readings per second, in hertz (Hz, cycles per second).

Take a continuous spinning arrow at f hertz. At time nT its angle is 2πf·nT = 2π(f/fs)n. Now compare it with the DFT block for bin k, which has angle 2π(k/N)n. The two match when f/fs = k/N. That gives the translation, slide 20:

f
The frequency in hertz that bin k stands for.
k
The bin number, from 0 to N − 1.
fs / N
The gap between neighbouring bins, in hertz: the sampling rate divided by the window length.

The lecture’s numbers are fs = 44.1 kHz and N = 1024. The gap is 44,100 / 1,024 = 43.07 Hz. So bin 0 is 0 Hz. Bin 1 is 43.1 Hz. Bin 2 is 86.1 Hz. Bin 3 is 129.2 Hz. Bin 511 is 511 × 43.07 = 22,006.9 Hz. Bin 512 would be 22,050 Hz, exactly half the sampling rate.

Your friend’s 440 Hz note sits at 440 / 43.07 = bin 10.2. It lands between bin 10 (430.7 Hz) and bin 11 (473.8 Hz). Chapter 8 shows what happens then.

The gap fs/N has a pleasant reading. The window lasts N/fs seconds, 23.2 milliseconds here. The gap is one divided by that duration. A longer window gives finer bins. To tell two notes 10 Hz apart, you need a window of at least 0.1 seconds.

The mirror and the Nyquist frequency

The lecture lists the bins X[0], X[1], …, X[511], X[512], X[513], …, X[1023] and leaves a blank: conjugate symmetry X[N − k] = ?. Fill it in. Start from the definition:

X[N − k] = ∑ x[n] e−j2πn(N−k)/N = ∑ x[n] e−j2πn e+j2πnk/N = ∑ x[n] e+j2πnk/N

The factor e−j2πn is 1, because n is a whole number of full turns. What is left is the DFT sum with the opposite spin. If x is real, then x equals its own conjugate, so that sum is the conjugate of X[k]. The answer is X[N − k] = X*[k].

So for a real signal, bins 513 to 1023 are mirror images of bins 511 down to 1. Same size, opposite phase. They carry no new information. Bin N − k is the negative frequency −k, the backwards-turning twin. A real cosine always needs both: one arrow forward and one back, as in pair 4.

The turning point is bin N/2, the Nyquist frequency fs/2 = 22,050 Hz. Above it, a tone looks the same as a slower tone below it. A tone at fs − f is indistinguishable from one at f. That false copy is called an alias.

fftshift: put zero in the middle

Slide 21 shows fftshift. The plain DFT lists bin 0 first, then the positive frequencies, then the negative ones. Humans prefer a number line with zero in the middle. fftshift cuts the list in half and swaps the halves, so the order runs from −fs/2 up to just below +fs/2. Nothing is computed. Only the order of the bars changes.

Try it. The device uses N = 64 samples at an illustrative fs of 8,000 Hz, so each bin is 125 Hz wide. Slide the tone up through the Nyquist frequency of 4,000 Hz.

Sweep a tone past Nyquist

The top panel shows the 64 samples. The bottom shows every bin, labelled in hertz. Sweep the tone and switch the layout. The dashed line is the Nyquist frequency.

1,500 Hz

Illustrative: a unit cosine sampled at fs = 8,000 Hz, N = 64, so bins are 125 Hz apart and the tone always lands exactly on a bin. The spectrum is the live plain-sum DFT. The lecture’s own example is fs = 44.1 kHz and N = 1024.

At 1,500 Hz you see two spikes: bin 12 and its mirror bin 52. After fftshift they sit at −1,500 and +1,500 Hz, a symmetric pair around the middle. Slide up to 4,000 Hz and the two spikes meet at the Nyquist bin. Keep going to 5,000 Hz. The spikes cross over and now read 3,000 Hz. The tone is real and 5,000 Hz, but the DFT shows 3,000. At 8,000 Hz, the tone shows up at zero frequency, as if it were constant.

That is the failure mode. The symptom: a pitch that is wrong, often lower than the real one. The measurement: the tone was above fs/2. The fix: sample faster, or filter out everything above fs/2 before sampling. The earlier lesson on discrete signals shows the sampling side of this story.

With fs = 44,100 Hz and N = 1,024, which frequency does bin 3 stand for?

Chapter 8

Spectral Leakage and Frequency Extraction

Find out why a pure tone sometimes smears across many bins, and how to read a pitch from a spectrum anyway

You play a pure 440 Hz note and record 1,024 samples at 44,100 Hz. You expect one clean spike. Instead you get a tall hill of bins with long skirts on both sides. The note is perfect. The recording is perfect. What went wrong?

Nothing went wrong with the tone. The trouble is the cut. The DFT only knows blocks that make a whole number of turns inside the window. Your window holds 440 / 43.07 = 10.2 turns. It does not fit.

Why a tone that does not fit smears

The DFT quietly treats the window as one period of a signal that repeats forever. If the tone makes a whole number of turns, the end of the window joins the start without a seam. If it makes 10.2 turns, the end does not meet the start. Repeat the window and you get a small jump at every seam. A jump is like a click, and Chapter 6 showed that a click contains every frequency. The energy that should sit in one bin leaks into the others. That is spectral leakage, the topic of slides 22 and 23.

The maths is the pulse pair again. Let the tone sit at a fractional bin f0, so x[n] = ej2πf0n/N. Then:

|X[k]|
The size of the spectrum at bin k.
|sin(π(f0 − k))|
The top. It is exactly 0 whenever f0 − k is a whole number, which is why a fitting tone has no skirts.
|sin(π(f0 − k)/N)|
The bottom. It is small for k near f0, so the ratio is large there, and it fades slowly as k moves away.

If f0 is a whole number, only k = f0 has a zero bottom, and the ratio there is N. Every other bin has a zero top. One clean spike. If f0 sits halfway between two bins, say 8.5, the worst case, the two nearest bins get about 2N/π, which is 64% of the full height. The rest of the energy trails away on both sides, falling off only slowly with distance.

The cost of reading a pitch

Slide 24 names the use: frequency extraction. You find the tallest bin kpeak and report f = kpeak · fs/N. For the 440 Hz note, the tallest bin is 10, which reads 430.7 Hz. The true pitch is 9.3 Hz higher, an error of 2.1%. The best you can do is within half a bin, here about 21.5 Hz. Leakage does not move the peak much, but it raises the skirts, and a small tone near a big one can drown in them.

Two remedies. First, if you control the window, choose a length that holds a whole number of turns. Second, taper the window: multiply the samples by a gentle bell that fades to zero at both ends, so there is no seam. A common bell is the Hann window, 0.5 − 0.5 cos(2πn/N). The lecture’s slides do not cover it, and it is here as a taste. Tapering costs something too: the main peak gets a little wider and lower. Play with both below.

Slide a tone between bins

The tone’s frequency is measured in bins. Whole numbers fit the window. Drag to 8.5 and watch the spike spread. Then switch on the taper.

8.00

Illustrative: N = 64 samples of a unit cosine, an assumed sampling rate of 8,000 Hz so each bin is 125 Hz. Leaked energy is the share of the total energy outside the two bins nearest the tone (and their mirrors). The spectrum is the live plain-sum DFT of the windowed samples.

At f0 = 8 the spectrum is one spike, and the leaked share reads 0%. Slide to 8.5. The tall spike becomes two shorter ones, and a fringe of small bars appears on both sides. The peak estimate of the pitch is now up to half a bin wrong. Switch to the taper and the fringe collapses, though each peak is wider and lower.

Tell it as symptom, measurement and fix. The symptom: skirts under a pure tone, and a pitch reading that jumps between bins. The measurement: the leaked share of the energy, and the fractional part of f0. The fix: fit whole turns in the window, or taper its edges.

A pure tone sits at bin 8.5 and you use a plain window. What happens to its spectrum?

Chapter 9

The Properties: What Each Move Does to the Spectrum

Learn five rules that tell you the spectrum of a changed signal without computing the transform again

You have the spectrum of a song. Now you delay the song by half a second. Do you need to run the transform again? No. A short rule says what a delay does to every coefficient. The DFT properties of slides 25 to 30 are a toolbox of such rules. Each one says: do this to the signal, and that happens to the spectrum.

Linearity: mixing signals mixes spectra

If x(m) has the spectrum X(m), then a weighted sum of signals has the same weighted sum of spectra. That is slide 25. It follows because the DFT is a sum, and sums pass through sums. We used it for the cosine: two arrows added in time gave two spikes added in frequency.

Duality: time and frequency swap roles

Look at pairs 1 and 3 from Chapter 6. A click in time gives a spinning arrow in frequency. A spinning arrow in time gives a click in frequency. Slide 26 asks for the general rule. Treat the spectrum X as a signal and transform it again:

∑n X[n] e−j2πnk/N = ∑m x[m] ∑n e−j2πn(m+k)/N = N · x[(−k) mod N]

The inner sum is the arrow chain again. It is N when m + k is a multiple of N, and 0 otherwise. Only one m survives, the one with m = −k. So the transform of a spectrum is the original signal, reversed in order and scaled by N. The notation “mod N” means the index wraps around: −1 means N − 1.

Symmetries: conjugating and reversing

Slide 27 gives three. They all come from one fact: conjugating an arrow reverses its spin. The slide asks for a proof of the first, x*[n] has transform X*[−k mod N]. Here it is.

∑ x*[n] e−j2πnk/N = (∑ x[n] e+j2πnk/N)* = (∑ x[n] e−j2πn(−k)/N)* = X*[−k mod N]

First pull the conjugate outside the sum. That flips the sign in the exponent. Then read the exponent as the one for bin −k. The other two, x*[−n] with X*[k] and x[−n] with X[−k], follow the same way.

Circular shift: a delay is a phase ramp

Slide 28 is the song-delay rule. A circular shift moves every sample n0 steps to the right, and whatever falls off the right end re-enters at the left. In MATLAB it is called circshift. In the formula, the index (n − n0) mod N wraps.

Derive it. Let m = n − n0. Then ∑ x[(n − n0) mod N] Wnk = ∑ x[m] W(m + n0)k = Wn0k ∑ x[m] Wmk. The right-hand sum is X[k]. So the shifted signal has the spectrum Wkn0 X[k]. The factor has length 1: it turns every coefficient but does not change its size. The size |X[k]| is untouched. The angle is pushed by −2πkn0/N, which grows with k. A delay is a phase ramp.

The mirror image, the second line of slide 28, swaps the roles: multiplying the signal by a spinning arrow W−nk0 slides the whole spectrum k0 bins to the right, again circularly. Shifts in time are twists in frequency. Twists in time are shifts in frequency. That is duality at work.

Real signals: half the spectrum is redundant

Slides 29 and 30 collect what a real signal does. From the first symmetry, if x is real then x* = x, so X[k] = X*[−k mod N]. Break that into parts: the size is even, |X[k]| = |X[−k]|, the angle is odd, ∠X[k] = −∠X[−k], the real part is even and the imaginary part is odd. We met this in Chapter 7 as the mirror.

Do this to the signalThe spectrum does thisSlide
Add signals with weightsAdds spectra with the same weights25
Transform the spectrum againGives N times the signal, reversed26
Conjugate itConjugates and reverses the spectrum27
Reverse it in timeReverses the spectrum27
Shift it circularly by n0Multiplies bin k by Wkn0 (size unchanged)28
Multiply by W−nk0Shifts the spectrum k0 bins, circularly28
Make it realSize even, angle odd, X[k] = X*[−k]29, 30

Now watch two of these rules at work on a small real signal of 16 samples. Shift it, or reverse it, and compare the sizes and angles before and after.

Shift it, reverse it

Top: the signal, with the original faint. Middle: spectrum size. Bottom: spectrum angle at the bins that carry energy. Pick an operation and move the slider.

3

Toy signal of N = 16 real samples, chosen to be lopsided so the angles are easy to see. Both spectra are computed directly by the live DFT; the check compares them with the rule from the table to rounding error.

Slide the shift from 0 to 3. The signal slides right and wraps. The size bars do not move at all: not one is different. Yet the angle bars twist, and the higher the bin, the bigger the twist, exactly the ramp the rule predicts. Switch to reverse. Now the size bars flip left to right, and the angle bars flip and change sign.

Why is this worth knowing? Because it explains behaviours that look strange otherwise. Why does a delayed audio clip have the same magnitude spectrum? Because a delay is only a phase ramp. Why can you ignore half of a real spectrum? Because the other half is its reflection.

A signal is shifted circularly by n0 samples. What happens to X[k]?

Chapter 10

The DFT as a Matrix

See the whole transform as one table of spinning arrows, read two ways, and count what it costs

Think of a mixing desk with N sliders in and N lights out. Each light shows a weighted blend of all the sliders. A table of weights says how much each slider feeds each light. That table is a matrix, and the blending is a matrix multiplication. The DFT is such a desk. Slides 31 to 35 show how to read it.

Two ways to read a matrix times a vector

View 1, slide 31: columns. Write the matrix A as columns a1, a2, … Then y = Ax is a mix of columns: take x[1] times the first column, x[2] times the second, and add. In symbols, y[k] = ∑ an[k] x[n]. The entries of x are how much of each column to pour in.

View 2, slide 32: rows. Write the matrix B as rows b1H, b2H, … The little H means conjugate and flip the row. Then each output is one dot product: y[k] = ∑ bk*[n] x[n]. Row k asks how much of the pattern bk there is in x. Same answer, different story.

Both views give the same y. Which one to use depends on what you want to see. Columns show how outputs are built. Rows show how each output measures the input.

The DFT matrix, read both ways

Slides 33 to 35 put the DFT into this form. The DFT is X[k] = ∑ x[n] e−j2πnk/N. So the matrix F has entries F[k, n] = Wnk, with W = e−j2π/N. For N = 4, W = −j, and the matrix is:

Read it by rows. Row k is the conjugate of Fourier block k. Multiplying by F takes N dot products, one for each block. This is analysis, the DFT exactly as Chapter 5 defined it.

Read it by columns. Column n is what a click at position n looks like in frequency, pair 1 of Chapter 6. So X = ∑ x[n] × (column n) says: the spectrum of any signal is the sum of the spectra of its clicks, each scaled by its sample. That is linearity again, written as a matrix.

Run the numbers for x = (1, 2, 0, −1). Row 1 times x is 1·1 + 2·(−j) + 0·(−1) + (−1)·j = 1 − 3j. Row 2 gives 0 and row 3 gives 1 + 3j. Row 0 gives 2. That is Chapter 5’s answer, times √4 = 2 because this matrix has no 1/√N in front. Explore both readings on an 8 by 8 matrix.

Read the matrix both ways

Each cell is an arrow of length 1. Its colour shows its angle. Pick a view and an index. In the rows view you see one output built from a dot product. In the columns view you see one input contributing to all outputs.

1

Toy input x = (1, 2, 0, −1, 2, 1, 0, −2) with N = 8 and the plain-sum matrix Wnk. The colour wheel runs through the angle from −180 to 180 degrees. The outputs are the live product, bars show their size.

In the rows view, row 0 is one flat colour: it adds up all the samples. Row 1 cycles through the colours once. Row 4 alternates between two colours: the fastest possible. In the columns view, column 0 is a flat colour too, and the other columns, one for each click position, have the same size entries everywhere. A click excites all frequencies equally.

The matrix undoes itself

Chapter 4 proved that the rows are orthogonal. In matrix words, FHF = N·I, where I is the identity matrix and H is conjugate-and-flip. So the inverse of F is FH/N, which is the synthesis formula from Chapter 5. With the 1/√N factor in front of F, the matrix is unitary: its inverse is just its conjugate flip. Unitary matrices keep lengths, so the energy stays the same, as Parseval said.

What the matrix costs

Multiplying an N by N matrix by a vector takes N times N products. For N = 1,024 that is 1,048,576 complex multiplications for one window of audio. The structure of F, with its repeated arrows, allows a much faster scheme, the fast Fourier transform (FFT). It needs about N log2 N products instead: 10,240 for N = 1,024. The answer is identical.

Count the products

Both curves are on a log scale. Drag the window length and watch the gap between the plain matrix product and the fast scheme widen.

1,024

The slider sets N = 2p. The matrix count is N² complex products; the FFT count is the standard N log2 N estimate. Real programs also count additions and constants, so these are order-of-magnitude figures.

For N = 1,024 the matrix needs about 100 times more products. For a million samples, the gap grows to about 50,000. That is why nobody multiplies by F directly in practice, and why this lecture could treat the DFT as cheap enough to use everywhere.

Row k of the DFT matrix F is the conjugate of Fourier block k. What does multiplying F by x compute?

Chapter 11

The Field Guide

Keep the numbers, the checklist and the core code of the whole lecture on one page

The one-paragraph summary

A signal is a list of numbers, and a basis is a set of building blocks that can make any such list. Orthonormal blocks make the coefficients easy: one dot product each, and Gram-Schmidt can square up any basis. The Fourier basis is N spinning arrows, one for each whole number of turns per window, and the DFT is the change into that basis. Pairs, bins, Nyquist, leakage and the properties tell you how to read a spectrum. The matrix view gathers it all into one unitary table that has a fast shortcut.

The numbers that matter

WhatNumberWhy it matters
Hero signals2 tones in 64 samples need 4 coefficientsThe Fourier basis makes waves sparse
Orthonormal example, x = (2, 5)11/√10 and −13/√10; energy 29 = 29Dot products give coefficients, energy is kept
Gram-Schmidt on (3, 1) and (2, 2)(3, 1)/√10 and (−1, 3)/√10Subtract the shadow, then scale to 1
Orthogonality of the Fourier blocks⟨wk, wr⟩ = 1 if k = r, else 0Geometric series with ωN = 1
DFT of (1, 2, 0, −1)(1, 0.5 − 1.5j, 0, 0.5 + 1.5j), or twice that without 1/√NA real signal gives conjugate pairs
Cosine, N = 64, L = 5two spikes of 32, at bins 5 and 59A cosine is two arrows
Pulse of M onesX[0] = M; first zero at N/MNarrow in time is wide in frequency
Bin gap, 44.1 kHz and N = 1,02443.07 Hz; bin 511 = 22,006.9 HzResolution is 1 over the window length
Nyquist frequencyfs/2 = 22,050 HzAbove it, tones alias to fs − f
Tone at a half-bin, plain windowpeak falls to about 64% (2/π)Leakage and a worse pitch estimate
Circular shift by n0X[k] times Wkn0, size unchangedA delay is a phase ramp
Cost, N = 1,0241,048,576 products vs about 10,240The FFT is about 100 times cheaper

A checklist before you trust a spectrum

  1. Write down fs and N. The bin gap is fs/N, and it sets what you can resolve (Chapter 7).
  2. Check that every tone of interest sits below fs/2. If not, it has aliased (Chapter 7).
  3. Ask whether the tones fit the window. If not, expect leakage and a pitch error up to half a bin (Chapter 8).
  4. Know your scaling: with or without 1/√N, or 1/N. It changes heights, not shapes (Chapter 5).
  5. For a real signal, read only the first half of the bins. The other half is the mirror (Chapters 7 and 9).
  6. Use the size for “how much” and the angle for “where in its cycle”. A shift changes only the angle (Chapter 9).
  7. fftshift only reorders the bars. It computes nothing (Chapter 7).

The lecture, as code

Every chapter shows up in a few lines of NumPy. The shapes are noted in the comments: x is one vector of N real numbers, F is an N by N table of complex numbers, and X is a vector of N complex numbers.

pythonimport numpy as np

def dft_matrix(N):                                   # Ch 10: F[k, n] = W^(nk)
    n = np.arange(N)
    return np.exp(-2j * np.pi * np.outer(n, n) / N)         # shape (N, N), complex

x = np.array([1, 2, 0, -1.0])                           # shape (4,)
F = dft_matrix(4)
X = F @ x                                               # Ch 5: analysis, [2, 1-3j, 0, 1+3j]
assert np.allclose(X, np.fft.fft(x))                    # the fast version, same answer

x_back = F.conj().T @ X / 4                             # Ch 5: synthesis, F^H X / N
U = F / np.sqrt(4)                                      # Ch 5: the unitary version
assert np.allclose(U.conj().T @ U, np.eye(4))             # Ch 4: orthonormal rows
assert np.isclose(np.sum(np.abs(U @ x)**2), np.sum(x**2)) # Ch 2: Parseval, 6 = 6

fs, N = 44100, 1024
freqs = np.fft.fftfreq(N, 1 / fs)                       # Ch 7: bin k to hertz, k * fs / N
order = np.fft.fftshift(freqs)                          # Ch 7: zero in the middle
tone = np.cos(2 * np.pi * 440 * np.arange(N) / fs)      # Ch 8: 10.2 turns, does not fit
S = np.abs(np.fft.rfft(tone))                           # Ch 7: real input, N//2 + 1 bins
f_hat = np.argmax(S) * fs / N                           # Ch 8: 430.7 Hz, the true tone is 440

n0 = 3                                                  # Ch 9: a delay is a phase ramp
k = np.arange(4)
assert np.allclose(np.fft.fft(np.roll(x, n0)),
                   np.exp(-2j * np.pi * k * n0 / 4) * np.fft.fft(x))

Where this sits

The DFT is the base of nearly everything in the course. The other lessons build on it: spectral descriptors turn a spectrum into a few numbers a classifier can use, the short-time transform slides a window along a signal to see frequency change over time, and wavelets choose a different basis for signals that start and stop. The linear systems lesson shows why convolution turns into multiplication in this basis. Before all this, discrete signals covers how the samples were made.

Keep going

The takeaway. The DFT is a change of basis: the same signal, described by N spinning arrows instead of N moments in time. The blocks are orthonormal, so every coefficient is one dot product and nothing is lost. A signal made of waves then turns into a few clear spikes. That is why compression, classification and filtering all start there.
A 600 Hz tone is sampled at 1,000 Hz and you take a DFT. Where does the tone appear?

Now press Present or Teach and explain the DFT out loud, from memory: why a basis changes what you see, why orthonormal blocks give coefficients by dot product, how the spinning arrows cancel, and what a bin, a mirror and a leak mean. Then go back to the instrument and keep fewer numbers.

EE269 Lecture 6: The Discrete Fourier Transform
Based on the Lecture 3 deck of Stanford EE269, Signal Processing for Machine Learning, by Mert Pilanci (Jan 2019).
The course site · Course slides · Back to Gleams