The Discrete Cosine Transform

A real-valued cousin of the DFT, its basis images, and why it quietly powers JPEG compression.

The problem with a complex transform

The Fourier transform has one real drawback: it's complex-valued, even when the image it's transforming is purely real. Neither the sine nor the cosine function alone forms an orthogonal transform, so neither can replace the complex exponential on its own — but there's a loophole. The Fourier transform of a matrix that is both real and symmetric turns out to be purely real, with the imaginary part vanishing entirely.

That raises a natural question: can a transform be built with the same desirable properties as the DFT — orthogonal, energy-preserving, frequency-domain — but without ever leaving the real numbers? The discrete cosine transform (DCT) is the answer.

The DCT formula

It's real, orthogonal, and — like the DFT — separable, so the 2-D version just applies the same cosine kernel along both axes:

Following the pattern from the last lesson, here is the matrix whose row is the sampled cosine . Each pair corresponds to one basis image — row 's cosine multiplied against column 's cosine as an outer product. Here's the full basis set:

8 × 8 Discrete Cosine basis images

The top-left tile () is flat — pure DC, the image's average brightness. Moving right increases horizontal frequency; moving down increases vertical frequency. Every 8×8 block of a real image is, in this basis, just a weighted sum of these 64 tiles.

It's a Fourier transform in disguise

The reason the DCT manages to stay real isn't a coincidence — it's a construction trick. Mirror a signal about its endpoint (append a reversed copy of itself) and take the DFT of that doubled, symmetric sequence: the even symmetry forces every imaginary component to cancel, leaving a purely real, cosine-only result. Strip out a constant phase factor and rescale, and what's left is exactly the DCT. In other words: the DCT is what you get by feeding a symmetrized signal through the machinery you already built in the previous lesson — which also means it inherits the DFT's fast algorithms almost for free.

Why this matters: energy compaction

For ordinary photographic images, most of the DCT's energy piles up in a small cluster of low-frequency coefficients near the top-left corner of the coefficient grid. Everything else is small enough to discard with barely any visible loss — which is precisely the mechanism JPEG compression is built on.

Interactive · DCT Compression

Original

Reconstructed

This is the core trick behind JPEG: DCT energy concentrates in a handful of coefficients, so throwing away the small ones barely changes what you see. Drag toward 1 and watch it fall apart.

Check your understanding

A JPEG-style encoder keeps only the largest DCT coefficients of each 8×8 block and throws the rest away. This works well because: