Lecture 6 · Signal Processing for Communications · University of Klagenfurt, 2019
Lecture 6 of 7
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.
01
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)
%% 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)
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 splits the amplitude axis into L=2^m levels, m bits per sample. If x(t)\in[-V,V]:
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}
A uniform quantizer with L=2^m levels on [-V,V]. Compute the SNR_Q when
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.
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.
02
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]
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.
\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.
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 two conditions depend on each other, so they are solved by alternating them:
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.
03
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.
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.

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.
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')
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.
letizia.tech · Signal Processing for Communications · University of Klagenfurt