S7 · Lesson 28 · The discrete cosine transform and compression
The DCT, energy compaction, transform coding gain, bit allocation and block coders like JPEG
JPEG images, MPEG and H.264 video, AAC and MP3 audio all start the same way: transform a block of samples so that most of its energy lands in a few coefficients, then spend bits only on those. The transform they use isn't the DFT but its cousin, the discrete cosine transform. Treating the block as if it were mirrored at its ends avoids the jump a DFT sees between a block's last and first samples, so smooth signals compact into very few coefficients. How much that compaction is worth, in decibels of SNR or bits per sample, follows from the coefficients' variances.
- The curves are the transform coding gain for a first-order autoregressive (AR(1)) signal with correlation ρ between neighboring samples: for a 2-point transform, , and in the limit of long blocks, .
- Strongly correlated signals (ρ near 1, as in images and speech) gain the most.
- Most of the long-block gain is reached with blocks of 8 to 16 samples.
Predict
A ramp 0, 1, …, 7 is transformed by an 8-point orthonormal DCT. What fraction of its energy lands in the first two coefficients?
The coefficients are 9.90, −6.44, 0, −0.67, 0, −0.20, 0, −0.05: the total energy is 140, and the first two carry . A DFT of the same ramp would see a jump from 7 back to 0 at the block's edge and spread energy into every bin.
Name it
The DCT
The orthonormal DCT-II of a block of N samples is
and its inverse uses the same basis. Basis function k is a cosine of k/(2N) cycles per sample. It equals a DFT of the block extended by its mirror image, so the periodic extension has no jumps, and for smooth or strongly correlated signals it approaches the optimal (Karhunen–Loève) transform. Being orthonormal, it preserves energy: .
Coding gain and bit allocation
Quantizing coefficient k with bits leaves error proportional to . With an average of bits, the optimal allocation is
and the improvement over quantizing samples directly is the coding gain, the ratio of the arithmetic to the geometric mean of the coefficient variances:
For an AR(1) signal with correlation ρ, a 2-point transform's variances are 1 ± ρ, so ; long blocks approach .
Block coders
JPEG splits an image into 8 × 8 blocks, takes a 2D DCT, divides each coefficient by a step from a quantization table (coarser for high frequencies, where the eye is less sensitive), and entropy-codes the zigzag-ordered result, most of which is zero. Coarse steps cause blocking artifacts at block edges. Audio coders use the MDCT, overlapping blocks by half with windows chosen so the overlaps cancel their aliasing (time-domain aliasing cancellation), which avoids block edges altogether while staying critically sampled.
Worked example
An AR(1) signal has ρ = 0.9. What gain does a 2-point transform give, and the long-block limit?
Show the solution
- Two-point variances. The two coefficients have variances 1.9 and 0.1.
- Means. Their arithmetic mean is 1, and their geometric mean is
- Coding gain. or 3.6 dB.
- Long-block limit. which is 7.2 dB, worth 1.2 bits per sample at the same distortion: half of it comes from just pairing samples.
Answer a 2-point transform gives 2.29 (3.6 dB), and the long-block limit is 5.26 (7.2 dB).
Worked example
Allocate an average of 4 bits per coefficient between those two coefficients.
Show the solution
- Allocation rule.
- The variance-1.9 coefficient.
- The variance-0.1 coefficient.
- Why this split. Both then have equal quantization error, which is what minimizes the total; in practice the bits are rounded and the residue entropy-coded.
Answer 5.06 bits for the variance-1.9 coefficient and 2.94 bits for the variance-0.1 one.
Worked example
A JPEG block of 64 8-bit pixels ends up with 10 nonzero coefficients, each costing about 6 bits with its position, plus a 4-bit end-of-block code. What is the compression ratio?
Show the solution
- Raw size. 64 pixels of 8 bits is 512 bits.
- Coded size.
- Ratio. 512 to 64 is 8 : 1.
- Other blocks. Smooth regions code to even fewer coefficients, busy textures to more; the quantization table's coarseness sets the trade between size and visible error.
Answer about 8 : 1.
Check
A block of N = 8 samples, all equal to 1, is transformed by the orthonormal DCT.
(a)
What is X[0]?
(b)
What is the total energy of its coefficients?
Check
A 2-point orthonormal DCT transforms the pair [3, 1].
(a)
What is X[0]?
(b)
What is X[1]?
Check
An AR(1) signal has neighbor correlation ρ = 0.8.
(a)
What coding gain does a 2-point transform give?
(b)
What is the long-block limit?
Check
Two coefficients have variances 4 and 0.25; the average budget is 6 bits each.
(a)
How many bits go to the larger one?
(b)
What coding gain does that allocation achieve?
Check
A block of 64 pixels of 8 bits codes to 20 nonzero coefficients at 7 bits each, plus a 4-bit end-of-block code.
(a)
What compression ratio is that?
Check
Basis function k = 1 of an N = 16-point DCT.
(a)
What frequency is it, in cycles per sample?
Check
Why do image and audio coders use the DCT rather than the DFT on each block?
Check
Two transform coefficients have variances a and b; separately, an AR(1) signal has correlation r.
Step 1
What coding gain do the two coefficients give?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many more bits does the first get than the second?
Step 3
What gain does a 2-point transform give the AR(1) signal?
Check
Write dct(x) and idct(X): the orthonormal DCT-II and its inverse by direct sums, and energy_in_first(x, m): the fraction of the DCT's energy in its first m coefficients.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a transform let a coder use fewer bits for the same distortion?
Neighboring samples of images and audio are strongly correlated, so a block of them carries much less information than its raw size suggests. A transform like the DCT rotates the block into coefficients that are nearly uncorrelated and whose energy is concentrated in a few of them, typically the low frequencies. Giving more bits to the large-variance coefficients and few or none to the small ones achieves the same total quantization error with fewer bits than quantizing every sample equally. The saving, the coding gain, equals the ratio of the arithmetic to the geometric mean of the coefficients' variances, and it's largest for the most strongly correlated signals.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.