Quantization

Lecture 6 · Signal Processing for Communications · University of Klagenfurt, 2019

Nunzio Alexandro Letizia

This lecture

Lecture 6 of 7

  1. Quantization and the uniform quantizer
  2. Optimal quantizers: centroids and the Lloyd-Max algorithm
  3. Assignment 6: three quantizers in Matlab

From Signal Processing for Communications at the University of Klagenfurt, which I taught in winter 2019: this is that edition, rebuilt for the web, so some of it may be a little dated. All lectures.

Uniform quantization

01

Quantization: basics

Quantization is a many-to-few mapping. In digital signal processing it is applied to an analog input x(t) to get a version \hat x_c(kT) discrete in time and in amplitude:

x(t)\in\mathbb{R} Sampler every T x_c(kT) Q quantizer \hat x_c(kT)\in\mathbb{Z}(T)

  • Sampling discretizes time, with the sampling rate F_s=\frac1T>2F_{\max}.
  • Quantization discretizes the amplitude into a finite set of levels.

Quantization in Matlab

%% Example Uniform
T = 0.001;
t = [-10:T:10];
x_t = exp(-abs(0.1*t.^2)).*cos(sin(t));
x_s = downsample(x_t,200);
t_s = downsample(t,200);
bits = 3;
levels = 2^bits;
x_q = Quantization(x_s,levels);
% ...
plot(t,x_t,'LineWidth', 2.5,'Color','k')
ax = gca;
hold on
stem(t_s,x_s,'LineWidth', 1.5)
stem(t_s,x_q,'LineWidth', 1.5)

Matlab plot: a smooth bell-shaped signal in black, its samples as red stems, and the quantized samples as yellow stems, which sit on a few fixed levels slightly off the curve.

the quantized signal, saved from the run

The signal x(t) in black, sampled every 0.2 s (one sample in 200), its samples x_c(kT) in red and the quantized samples \hat x_c(kT) in yellow, stuck on a few levels. The plot shows one quantizer only: it came from an earlier version of this script, which was not kept; the excerpt is the setup both share.

The uniform quantizer

The uniform quantizer splits the amplitude axis into L=2^m levels, m bits per sample. If x(t)\in[-V,V]:

  • the quantization step is \Delta=\dfrac{2V}{L};
  • the thresholds are a_k=-V+k\Delta, with k=0,\dots,L;
  • the levels are the midpoints, x_k=a_k+\dfrac{\Delta}{2}, with k=0,\dots,L-1: Q(x)=x_k for a_k\le x<a_{k+1};
  • the quantization error is e=Q(x)-x.

A number line from minus V to V cut by nine equally spaced thresholds a0 to a8, with the eight levels x0 to x7 as dots in the middle of each cell; the width of one cell is marked Delta.

The quantization SNR

The performance of a quantizer is measured by the signal to quantization noise ratio:

SNR_Q=\frac{M_X}{M_e}=\frac{E\left[x_c^2\right]}{E\left[e^2\right]}

If x_c(kT) stays inside [-V,V] (no overload) and \Delta is small, so that f_X(x)\approx f_X(x_k) is constant for a_k\le x\le a_{k+1}, then the error is uniform in each cell, e\sim\mathcal{U}\left(-\frac\Delta2,\frac\Delta2\right), and

M_e=\frac{\Delta^2}{12}

Left, the staircase Q of x of a uniform quantizer with eight levels against the dashed identity line. Right, the error Q of x minus x, a sawtooth between minus and plus Delta over two.

Ex. 1

A uniform quantizer with L=2^m levels on [-V,V]. Compute the SNR_Q when

  1. x_c\sim\mathcal{U}(-V,V) is a uniform input;
  2. x_c\sim\mathcal{N}(0,\sigma^2) is a Normal input.

Ex. 1: solutions

1. The input fills every cell, so M_e=\frac{\Delta^2}{12} holds exactly:

M_X=\frac{1}{2V}\int_{-V}^{V}x^2\,dx=\frac{V^2}{3},\qquad M_e=\frac{\Delta^2}{12}=\frac{4V^2}{12L^2}=\frac{V^2}{3L^2}

SNR_Q=L^2=2^{2m},\qquad SNR_Q\big|_{dB}=20\log_{10}2\cdot m\approx6.02\,m\ \text{dB}

six decibels per bit.

My notes have no solution to this one: it is added for the web version.

Ex. 1: solutions, continued

2. The Gaussian is unbounded, so the range has to be chosen first: V=c\,\sigma, with the loading factor c typically 4 (overload probability 2Q(4)\approx6\cdot10^{-5}). Ignoring the overload:

M_X=\sigma^2,\qquad\Delta=\frac{2c\sigma}{L},\qquad M_e\approx\frac{\Delta^2}{12}=\frac{c^2\sigma^2}{3L^2},\qquad SNR_Q\approx\frac{3L^2}{c^2}

SNR_Q\big|_{dB}\approx6.02\,m+4.77-20\log_{10}c\ \overset{c=4}{=}\ 6.02\,m-7.27\ \text{dB}

The same six decibels per bit, with a fixed penalty: most samples use only the inner levels.

My notes have no solution to this one: it is added for the web version.

Optimal quantizers

02

The optimal uniform quantizer

Problem. With the thresholds a_k fixed and uniform, find the levels x_k, k=0,\dots,L-1, that minimize M_e=E\left[(Q(x)-x)^2\right]:

M_e=\sum_{k=0}^{L-1}\int_{a_k}^{a_{k+1}}(x_k-x)^2f_X(x)\,dx,\qquad\frac{\partial M_e}{\partial x_i}=0\iff\int_{a_i}^{a_{i+1}}2(x_i-x)f_X(x)\,dx=0

So the optimal level is the centroid of its cell, the conditional mean:

x_i=\frac{\int_{a_i}^{a_{i+1}}x\,f_X(x)\,dx}{\int_{a_i}^{a_{i+1}}f_X(x)\,dx}=E\left[x\mid a_i\le x<a_{i+1}\right]

A Gaussian density over eight equal cells: in each cell the centroid, a blue dot, sits off the midpoint, towards the peak of the density.

The Lloyd-Max quantizer

Problem. Find both the levels x_k and the thresholds a_k that minimize M_e: \ \dfrac{\partial M_e}{\partial x_i}=\dfrac{\partial M_e}{\partial a_j}=0.

  • The condition on x_i is solved above: the centroid.
  • a_i, for i=1,\dots,L-1, is the upper limit of cell i-1 and the lower limit of cell i:

\frac{\partial M_e}{\partial a_i}=(x_{i-1}-a_i)^2f_X(a_i)-(x_i-a_i)^2f_X(a_i)=0\ \Rightarrow\ a_i=\frac{x_i+x_{i-1}}{2}

each threshold halfway between its two levels, while a_0 and a_L stay at \pm V.

A number line with the eight Lloyd-Max levels for a Gaussian input and their thresholds: cells are narrow near zero, where the density is high, and wide in the tails.

The slide left the threshold condition to solve: the derivation is added for the web version. The figure uses the classical Lloyd-Max values for a Gaussian with L=8.

The Lloyd-Max algorithm

The two conditions depend on each other, so they are solved by alternating them:

  1. Initialize the intervals [a_k,a_{k+1}], for instance uniform.
  2. Set each level x_i to the centroid of its interval.
  3. Recalculate the optimal thresholds, a_i=\frac{x_i+x_{i-1}}{2}.
  4. Repeat from 2 while the decrease of M_e is significant.

M_e never increases from one step to the next, so the iteration converges, to a local minimum in general and to the global one for log-concave densities such as the Gaussian.

Assignment 6

03

Assignment 6

Write in Matlab a function to simulate the quantization process, with the structure function y = Quantization(x, N, type, k), where

  • x is the input signal to be quantized;
  • N is the number of quantization levels (generally N=2^m);
  • type is a string: 'unif' for the simple uniform quantizer, 'unif-opt' for the optimal uniform quantizer, 'Lloyd' for the Lloyd-Max algorithm;
  • k is the number of iterations, only for the Lloyd algorithm;
  • y is the quantized output.

Compare the three quantized outputs y and evaluate the quantization error e(x)=Q(x)-x. Show that the error decreases when using the optimal algorithms.

Assignment 6: the solution

On samples, replace f_X by the empirical distribution of the n samples, with the range taken from the signal:

V_-=\min x,\quad V_+=\max x,\qquad\Delta=\frac{V_+-V_-}{N},\qquad a_k=V_-+k\Delta,\ k=0,\dots,N

\texttt{unif}:\ y_j=a_k+\frac\Delta2\quad\text{for }a_k\le x_j<a_{k+1}

\texttt{unif-opt}:\ y_j=\hat x_k=\frac{1}{|C_k|}\sum_{x_l\in C_k}x_l,\qquad C_k=\{x_l:a_k\le x_l<a_{k+1}\}

\texttt{Lloyd}:\ \text{repeat }k\text{ times: }\hat x_i\leftarrow\text{mean}(C_i),\ \ a_i\leftarrow\frac{\hat x_{i-1}+\hat x_i}{2},\ i=1,\dots,N-1

\hat M_e=\frac1n\sum_j(y_j-x_j)^2

An empty cell gives 0/0: keep its previous level. Put the largest sample in the top cell. What decreases with the optimal quantizers is the mean square error \hat M_e; the error of a single sample can grow.

My notes have no written solution to this one, only the Matlab below: the solution is added for the web version.

Assignment 6 in Matlab: the quantizers

    for iter = 1:k
        % ...
        for j=1:N
            % ...
            try
                centroid(j) = total_sum(j)/occurrences(j);
            catch
                centroid(j) = levels(j)+ 1/N;
            end
        end
        % ...
        for j=2:N
            levels(j) = (centroid(j-1)+centroid(j))/2;
        end
    end

Matlab plot, Quantized Signals, 8 Levels: the bell-shaped signal and its samples with three sets of quantized stems; in the tails the uniform ones sit visibly above zero, the optimal ones close to it.

the three quantizers, 8 levels, saved from the run

The Lloyd branch of Quantization.m. In the tails the uniform levels (yellow) sit well above the signal, near zero, while the optimal ones stay close to it. Two caveats: called as Quantization(x,N,'unif') the function matches no branch and returns zeros, so the script uses the two-argument call; and in Matlab 0/0 is NaN, it does not throw, so the catch never runs on an empty cell. Neither bites on this signal.

Assignment 6 in Matlab: the error

e_q = x_q-x_s;
e_q_opt = x_q_opt-x_s;
e_l_opt = x_l_opt-x_s;
M = [norm(e_q)^2,norm(e_q_opt)^2,norm(e_l_opt)^2]./length(t_s);

figure
plot(t_s,e_q,'LineWidth', 2.5,'Color',[0.93,0.69,0.13])
ax = gca;
hold on
plot(t_s,e_q_opt,'LineWidth', 2.5,'Color',[0.49,0.18,0.56])
plot(t_s,e_l_opt,'LineWidth', 2.5,'Color',[0.47,0.67,0.19])
% ...
legend({'e(t)','e_{opt}(kT)','e_{Lloyd}(kT)'},'Location','best')

Matlab plot, Quantization Error, 8 Levels: the uniform error in yellow flat at about 0.06 in the tails, the optimal and Lloyd errors in purple and green near 0.02 there, with larger spikes than the yellow one around t equal to plus and minus 4.

the quantization error, saved from the run

In the tails the uniform error (yellow) is flat near \frac\Delta2, the optimal ones much lower; around t=\pm4, though, their spikes are larger. So the optimal quantizers lower the mean square error, M in the code, not the error everywhere. The first legend entry should read e(kT): the error exists only at the sampling instants, and stems would show that better than lines.