Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Lossy compression: quantisation and transform coding

Lossy compression uses quantisation to reduce precision and transform coding (e.g. DCT) to pack signal energy into few coefficients, enabling large size reduction at acceptable quality loss.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Lossy compression is used when storage or bandwidth is limited and some loss of accuracy is acceptable (images, audio, video). Engineers use quantisation and transform coding in codecs (JPEG, MPEG, many audio/video systems) to trade quality for bits while keeping perceptual quality high.

Key ideas

Start with definitions (each term is defined before it is used):

  • Quantisation: the process of mapping a continuous-range signal value to a finite set of discrete levels. It reduces the number of bits needed to represent samples.
  • Quantiser: the device or algorithm that implements quantisation. A common type is the uniform quantiser, which has equally spaced output levels.
  • Transform coding: a compression method that first applies a linear transform to decorrelate samples, then quantises transform coefficients and entropy-codes them. The transform packs signal energy into fewer coefficients.
  • DCT (Discrete Cosine Transform): a fixed orthogonal transform widely used in images and video; it concentrates energy in low-frequency coefficients for typical natural signals.
  • KLT (Karhunen–Loève Transform): the optimal decorrelating linear transform for a signal in the mean-square sense; it depends on signal statistics (covariance matrix).
  • Quantisation matrix: in block-based image coding (e.g. JPEG), a matrix that sets different quantisation step sizes for different transform coefficients.
  • SQNR (signal-to-quantisation-noise ratio): the ratio of signal power to quantisation noise power, often expressed in dB.
  • Entropy coding: lossless coding (Huffman, arithmetic) applied after quantisation to remove statistical redundancy.

Assumptions used in standard derivations (state these in exam answers):

  • High-resolution assumption: quantiser step size Δ is small relative to signal variations so quantisation error can be modelled as a uniform random variable independent of the signal.
  • No overload distortion: signal values stay inside the quantiser's dynamic range. Overload distortion occurs when |x| > X_max (clipping/saturation); within the allowed range, the error is granular distortion.
  • For transform coding, the transform is orthogonal (energy preserving) so mean-square distortion in sample domain equals mean-square distortion in transform domain after inverse transform.

Derivations universities ask for (step-by-step):

  1. Variance of quantisation error for a uniform mid-rise quantiser with step Δ. Assumptions: error e is uniform in [-Δ/2, Δ/2]. Then

    • probability density p(e) = 1/Δ on [-Δ/2, Δ/2]
    • mean E[e] = 0
    • variance Var(e) = E[e^2] = ∫_{-Δ/2}^{Δ/2} e^2 (1/Δ) de = (1/Δ) · (2·(Δ/2)^3 / 3) = Δ^2 / 12. State clearly: this holds under the uniform-error assumption (high-resolution, no overload).
  2. SQNR for a sinusoid in an N-bit uniform quantiser (full-scale ±Xmax):

    • For a sinusoid x(t) = A sin(ωt), signal power P_s = A^2 / 2.
    • With N bits covering range 2 Xmax (assume A = Xmax for full-scale), step size Δ = 2 Xmax / 2^N.
    • Quantisation noise power P_q = Δ^2 / 12.
    • SQNR (linear) = P_s / P_q; in dB, SQNR_dB = 10 log10(P_s/P_q). For full-scale sinusoid and ideal assumptions this reduces to the common approximate formula SQNR_dB ≈ 6.02·N + 1.76 dB.
    • State assumptions: uniform quantiser, full-scale sinusoid, error uniform and white.
  3. Transform coding bit allocation (high-rate scalar quantisation model). For M uncorrelated transform coefficients with variances σ_i^2, and allowed total bits per vector B, minimizing total distortion (using high-rate approx D_i ∝ σ_i^2·2^{-2b_i}) with ∑ b_i = B leads to

    • optimal bits b_i = (1/2) log2(σ_i^2) + constant (same constant for all i). The constant is found from the sum constraint. This equalizes marginal distortion per bit.
    • Describe how to round b_i to integers and use a quantisation matrix.

Block diagram to draw (label exactly):

  • Draw boxes left to right: "Input block (e.g. 8x8 pixels)" -> arrow -> "2D transform (e.g. 8x8 DCT)" -> arrow -> "Quantisation (divide by Q matrix + round)" -> arrow -> "Zig-zag scan" -> arrow -> "Entropy coding (Huffman/Arithmetic)" -> arrow -> "Bitstream". Also show decoder path: "Bitstream" -> arrow -> "Entropy decoding" -> arrow -> "Inverse zig-zag" -> arrow -> "Dequantisation (multiply by Q matrix)" -> arrow -> "Inverse 2D transform" -> arrow -> "Reconstructed block".

Explain why transform coding helps: many natural signals (images, audio) have energy concentrated at low frequencies; an orthogonal transform like the DCT concentrates energy in few coefficients so coarse quantisation of high-frequency coefficients causes small distortion but saves many bits.

Formulas

Δ = (2·X_max) / 2^N — Δ is quantisation step size (signal units, e.g. volts V), X_max is maximum amplitude (V), N is number of bits (dimensionless). Applies for N-bit uniform quantiser covering ±X_max.

σ_e^2 = Δ^2 / 12 — σ_e^2 is quantisation noise variance (V^2). Applies under the uniform-error assumption (mid-rise or mid-tread uniform quantiser, high-resolution, no overload).

P_s = A^2 / 2 — for a sinusoid x(t)=A sin(...), P_s is signal power (V^2).

SQNR (linear) = P_s / σ_e^2 — ratio of signal power to quantisation noise power (dimensionless).

SQNR_dB = 10·log10(SQNR) — SQNR in decibels (dB).

PSNR = 10·log10((X_max)^2 / MSE) — Peak signal-to-noise ratio for images/video (dB), where X_max is the maximum possible sample value (e.g., 255 for 8-bit) and MSE is the mean squared error.

SQNR_dB ≈ 6.02·N + 1.76 — approximate SQNR for full-scale sinusoid in dB; N in bits. Applies under assumptions listed above.

D_i ∝ σ_i^2 · 2^{-2b_i} — high-rate scalar quantiser distortion model for transform coefficient i. D_i is distortion (power), σ_i^2 is coefficient variance, b_i is bits assigned to that coefficient (bits per coefficient).

b_i = (1/2)·log2(σ_i^2) + λ — optimal bit allocation (λ chosen so ∑ b_i = B). b_i is in bits.

Worked examples

Example 1 — Quantising a sinusoid (numerical, exam style): Given: full-scale sinusoid with amplitude A = 1.0 V (so X_max = 1.0 V), N = 8 bits. Step 1: compute Δ: Δ = (2·X_max)/2^N = 2·1 / 256 = 0.0078125 V. Step 2: quantisation noise variance σ_e^2 = Δ^2/12 = (0.0078125^2)/12 = 6.103515625e-5 / 12 = 5.0862630208e-6 V^2. Step 3: signal power P_s = A^2/2 = 1^2/2 = 0.5 V^2. Step 4: linear SQNR = P_s / σ_e^2 = 0.5 / 5.0862630208e-6 ≈ 98,304. Step 5: SQNR_dB = 10·log10(98304) ≈ 49.92 dB. Final answer (bold): SQNR ≈ 49.92 dB (matches 6.02·8 + 1.76 ≈ 49.92 dB).

Example 2 — Simple transform bit allocation (small algebraic example): Given two transform coefficients with variances σ1^2 = 4, σ2^2 = 1. Total bits per vector B = 4. High-rate model says optimal bits b_i = (1/2) log2(σ_i^2) + λ. Compute (1/2) log2 terms: for i=1: (1/2)·log2(4) = (1/2)·2 = 1. For i=2: (1/2)·log2(1) = 0. Sum constraint: b1 + b2 = (1 + λ) + (0 + λ) = 1 + 2λ = 4 → λ = 1.5. So b1 = 2.5 bits, b2 = 1.5 bits. Rounding to integers (practical): allocate 3 bits to coeff1 and 1 bit to coeff2. Compute distortions proportional to σ_i^2·2^{-2b_i} (constant cancels):

  • D1 ∝ 4·2^{-6} = 4/64 = 0.0625
  • D2 ∝ 1·2^{-2} = 1/4 = 0.25 But if we use exact fractional bits (theoretical) D1 = D2 (equalized). The fractional solution shows how bits concentrate on higher variance coefficient. Bold conclusion: optimal (fractional) allocation b1=2.5, b2=1.5; practical allocation 3 and 1 bits.

Common mistakes

  • Forgetting to carry units (e.g. V^2 for power, V for Δ) in numerical work.
  • Applying the 6.02·N + 1.76 rule to non-sinusoidal signals or when overload occurs — it is an approximation for full-scale sinusoids under uniform quantisation.
  • Not rounding bit allocations to integers and not explaining rounding impact when doing transform bit allocation problems.
  • Drawing transform coding diagram without showing inverse transform path or where entropy coding sits.

For your exam

Types of questions you will get on this topic:

  • Definitions and short notes (define quantisation, uniform quantiser, transform coding, DCT, KLT).
  • Derivations (derive quantisation noise variance Δ^2/12; derive SQNR for sinusoid; derive bit-allocation formula for transform coding under high-rate assumption).
  • Numericals (compute Δ, noise variance, SQNR in dB for given N and A; allocate bits between transform coefficients).
  • Diagrams (draw and label a transform-coder block diagram for image coding). What a full-marks answer includes:
  • Clear definitions with bold terms (if asked), explicit assumptions (high-resolution, no overload, orthogonal transform), step-by-step derivations with integrals shown, units and numerical steps carried through, and commentary about where approximations are used. For diagrams: labeled boxes and arrows, indicate where quantisation and entropy coding occur.

Quick check

  1. What is the variance of quantisation noise for a uniform quantiser with step Δ?
  2. State the approximate SQNR (dB) for an N-bit uniform quantiser with a full-scale sinusoid.
  3. Why is the DCT preferred over raw samples in image coding? Answers: Δ^2/12; 6.02·N + 1.76 dB (under assumptions); because the DCT concentrates energy into a few low-frequency coefficients (energy compaction), allowing coarse quantisation of many coefficients with small perceptual loss.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?