DSPRelated.com

DCT

Category: Transforms | Also known as: Discrete Cosine Transform

The Discrete Cosine Transform (DCT) is a linear transform that expresses a finite sequence of data points as a sum of cosine functions oscillating at different frequencies. It is closely related to the Discrete Fourier Transform (DFT) but operates on real-valued inputs and produces real-valued outputs, making it computationally efficient and well-suited to energy compaction in signals where most information concentrates in a small number of low-frequency coefficients.

In practice

The DCT is most commonly encountered in embedded systems that handle audio, image, or video compression, though it also appears in signal analysis, feature extraction, and some communications contexts. JPEG image encoding, MPEG video, and MP3/AAC audio all rely on variants of the DCT (typically DCT-II, often called "the" DCT by convention) to transform blocks of spatial or time-domain samples into frequency coefficients. The resulting coefficients can then be quantized and entropy-coded, discarding perceptually unimportant detail. On MCUs or DSPs with sufficient horsepower, such as Cortex-M4/M7 class parts with SIMD and FPU support, real-time JPEG encoding or audio decoding pipelines are feasible.

There are eight standard DCT variants (DCT-I through DCT-VIII), though in practice most developers will only encounter the first four types; DCT-II and its inverse (DCT-III) dominate practical use. An N-point DCT-II can be computed via a length-2N FFT of a symmetrically extended or reordered input (the exact construction varies by algorithm and boundary conditions), so platforms that already have an optimized FFT library can derive a DCT without a fully separate implementation. The relationship between the DCT and the FFT means that many algorithmic techniques such as the split-radix approach carry over, and twiddle-factor computation methods used for FFTs can often be adapted to DCT implementations as well, though DCT-specific symmetries and scaling may require some modification.

A key practical property is energy compaction: for signals with smooth, correlated content (typical of audio and natural images), the DCT concentrates most signal energy into a small number of low-index coefficients. This makes quantization and compression far more effective than applying the same process in the time or spatial domain. In embedded compression pipelines, fixed-point DCT implementations are common to avoid floating-point overhead; care must be taken with coefficient scaling and rounding to avoid accumulation of quantization error across the transform.

On very resource-constrained devices (8-bit or 16-bit MCUs), a full block DCT is often too expensive for real-time use, and pre-computed lookup tables or hardware accelerators (found in some application-processor SoCs, such as the i.MX series from NXP or various Allwinner/Rockchip SoCs) are used instead. When hardware acceleration is absent, profiling the inner butterfly loops and using architecture-specific SIMD intrinsics (for example, ARM NEON or Cortex-M DSP extension instructions) can yield the necessary throughput.

Frequently asked

What is the difference between the DCT and the DFT?
The DFT operates on complex inputs and outputs, representing a signal with both sine and cosine components. The DCT restricts itself to cosine basis functions and works with real-valued data. Because of implicit symmetry in its input, the DCT avoids the complex arithmetic of the DFT and achieves better energy compaction for typical audio and image signals, meaning fewer non-zero coefficients are needed to represent the same information. The DFT is more general and handles arbitrary periodic signals, while the DCT is typically the better choice for compression applications.
Which DCT variant is used in JPEG and MP3?
JPEG uses DCT-II applied to 8x8 pixel blocks. MP3 uses a Modified Discrete Cosine Transform (MDCT), which is a lapped transform derived from DCT-IV applied to overlapping blocks of audio samples. The MDCT is also used in AAC, Vorbis, and Opus. When people refer to 'the DCT' without qualification in a compression context, they almost always mean DCT-II.
Can I compute the DCT using an existing FFT implementation?
Yes. An N-point DCT-II can be computed by constructing a 2N-point real FFT of a symmetrically extended or reordered input, then extracting and phase-rotating the result. This approach lets you reuse an optimized FFT library rather than writing a dedicated DCT routine. The computational cost is comparable to an N-point FFT. The relationship also means that techniques for computing twiddle factors and optimizing FFT butterflies can often be adapted to DCT implementations, though DCT-specific symmetries may require some modification.
How is a fixed-point DCT implemented on a resource-constrained MCU?
Fixed-point DCT implementations replace floating-point multiplications with integer multiplications and bit shifts. The cosine coefficients are pre-scaled to integers (commonly Q15 or Q31 format on ARM Cortex-M devices). The main pitfall is intermediate overflow: intermediate products in an 8x8 block DCT can require 32-bit or even 64-bit accumulators depending on input word width. Libraries such as CMSIS-DSP provide optimized fixed-point DCT-II/III routines for Cortex-M targets, using DSP extension instructions (SMLAD, etc.) where available.
Is the DCT reversible?
Yes. The inverse of DCT-II is DCT-III (scaled appropriately), and the inverse of DCT-IV is DCT-IV itself. In lossless use, the transform is perfectly invertible. In compression pipelines the lossy step is quantization, not the transform itself. Accumulated fixed-point rounding across the forward and inverse transforms can introduce small errors, which is why JPEG defines specific precision requirements for compliant DCT implementations.

Differentiators vs similar concepts

The DCT is commonly confused with the DFT/FFT and with other cosine-based transforms such as the MDCT. The FFT is an algorithm for computing the DFT efficiently; the DFT is complex-valued and suited to general spectral analysis. The DCT is real-valued, requires no complex arithmetic, and provides superior energy compaction for correlated signals, which is why it dominates lossy compression standards rather than the DFT. The DCT is also distinct from the Discrete Wavelet Transform (DWT), which uses localized basis functions and provides both frequency and time/spatial localization; the DCT uses globally supported cosine bases and does not provide time-frequency localization. JPEG uses the DCT; JPEG 2000 uses the DWT.