You are on page 1of 10

Analogue and Digital Communication Lab

(EL-323)

LABORATORY MANUAL

ENGR. IHTISHAM KHALID &ENGR. MUHAMMAD ASIM

IMPLEMENTATION OF PCM IN MATLAB


(LAB # 12)
Student Name: ______________________________________________

Roll No: ________________Section: ____

Date performed: _____________, 2016

____________________________________________________________________________________________________________________________________________________________

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD

Prepared by: Engr. M. Asim, Engr. Ihtisham Khalid Version: 2.01


Last Edited by: Engr. M. Asim, Engr. Ihtisham Khalid
Verified by: Dr. Shahzad Saleem Updated: Fall 2016
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________
Lab # 12: IMPLEMENTATION OF PCM IN MATLAB
Learning Objectives:
1. To implement scaler quantization in MATLAB
2. To implement Uniform PCM in MATLAB
3. To implement Non-Uniform PCM in MATLAB
Software Required:
1. MATLAB

Scaler Quantization
In scalar quantization the range of the random variable X is divided into N nonoverlap- ping regions
Ri, for 1 <= i <= N, called quantization intervals, and within each region a single point called a
quantization level is chosen. Then all values of the random variable that fall within region Ri are
quantized to the ith quantization level, which is denoted by ^x i. This means that

Where

Obviously, a quantization of this type introduces a mean-square error of ( x− x^ i )2 . The mean-square


quantization error is therefore given by

where fx(x) denotes the probability density function of the source random variable. The signal-to-
quantization-noise ratio (SQNR) is defined as

Uniform Quantization
In uniform quantization all quantization regions except the first and the last ones—that
is, R1 and RN are of equal length, which is denoted by ∆; therefore,

The optimal quantization level in each quantization interval can be shown to be the
centroid of that interval; that is,

_____________________________________________________________________________________________
Page 2 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________

Therefore, the design of the uniform quantizer is equivalent to determining a and ∆ . After a and ∆
are determined, the values of ^x i ' s and the resulting distortion can be determined easily using above
2 equations. In some cases it is convenient to choose the quantization levels to be simply the
midpoints of the quantization regions— that is, at a distance ∆/2 from the boundaries of the
quantization regions. Plots of the quantization function Q(x) for a symmetric probability density
function of X and even and odd values of N are shown in Figures 1 and 2, respectively.

Figure-1: The uniform quantizer for N=6 (note that here a + 2∆ =0)

For the symmetric probability density functions, the problem becomes even simpler. In such a case,

_____________________________________________________________________________________________
Page 3 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________

Figure-2: The quantization for N=7 (note that here x^ 4 =0)

We see that in this case we have only one parameter A, which we have to choose to
achieve minimum distortion.

Problem # 1: Determine the centroids of the quantization regions for a zero-mean, unit-variance
Gaussian distribution, where the boundaries of the quantization regions are given by (- 5, -4, -2,0,1,
3, 5).

Problem # 2: In Problem 1, determine the mean-square error.

Problem # 3: Find the distortions when a uniform quantizer is used to quantize a zero-mean, unit-
variance Gaussian random variable. The number of quantization levels is 11, and the length of each
quantization region is 1.

Problem # 4: Determine the mean-square error for a uniform quantizer with 12 quantization levels,
each of length 1, designed for a zero-mean Gaussian source with variance of 4. It is assumed that
the quantization regions are symmetric with respect to the mean of the distribution.

Nonuniform Quantization
In nonuniform quantization the requirement that the quantization regions, except the first and the
last, have equal lengths is relaxed, and each quantization region can hav any length. Because in this
case optimization is done under more relaxed conditions, the result is obviously superior to that of
uniform quantization. The optimality conditions for this case, known as the Lloyd-Max conditions,
can be expressed as

_____________________________________________________________________________________________
Page 4 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________

From these equations we conclude that the optimal quantization levels are the centroids of the
quantization regions and the optimal boundaries between the quantization regions are the midpoints
between the quantization levels. In order to obtain the solution to the Lloyd-Max equations, we
start with a set of quantization levels ^x i. From this set we can simply find the set of quantization
region boundaries at. From this set of at's a new set of quantization levels can be obtained. This
process is continued until the improvement in distortion from one iteration to another is not
noticeable. This algorithm is guaranteed to converge to a local minimum, but in general there is no
guarantee that the global minimum can be achieved.

Problem # 5: Design a ten-level Lloyd-Max quantizer for a zero-mean, unit-variance Gaussian


source.

Pulse Code Modulation


In pulse-code modulation an analog signal is first sampled at a rate higher than the Nyquist rate, and
then the samples are quantized. It is assumed that the analog signal is distributed on an interval
denoted by [-xmax, xmax] and the number of quantization levels is large. The quantization levels
can be equal or unequal. In the first case we are dealing with a uniform PCM, and in the second
case with a nonuniform PCM.

Uniform PCM
In uniform PCM the interval [-xmax, xmax] of length 2x max is divided into N equal subintervals,
each of length ∆=2 x max / N . If N is large enough, the density function of the input in each
subinterval can be assumed to be uniform, resulting in a distortion of D = ∆ 2/12. If N is a power of
2, or N = 2V, then v bits are required for representation of each level. This means that if the
bandwidth of the analog signal is W and if sampling is done at the Nyquist rate, the required
bandwidth for transmission of the PCM signal is at least vW (in practice, 1.5vW is closer to reality).
The distortion is given by

If the power of the analog signal is denoted by X́ 2 , the SQNR is given by


_____________________________________________________________________________________________
Page 5 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________

where X̌ denotes the normalized input defined by

The SQNR in decibels is given by

After quantization, the quantized levels are encoded using v bits for each quantized level. The
encoding scheme that is usually employed is natural binary coding (NBC), meaning that the lowest
level is mapped into a sequence of all O's and the highest level is mapped into a sequence of all 1 's.
All the other levels are mapped in increasing order of the quantized value.
The m-file u_pcm.m given next takes as its input a sequence of sampled values and the number of
desired quantization levels and finds the quantized sequence, the encoded sequence, and the
resulting SQNR (in decibels).

function [sqnr,a_quan,code]=u_pcm(a,n)
%U-PCM uniform PCM encoding of a sequence
% [SQNR,A_QUAN,CODE]=U_PCM(A,N)
% a=input sequence.
% n=number of quantization levels (even).
% sqnr=output SQNR (in dB).
% a_quan=quantized output before encoding.
% code=the encoded output.
amax=max(abs(a));
a_quan=a/amax;
b_quan=a_quan;
d=2/n;
q=d.*[0:n-1];
q=q-((n-1)/2)*d;
for i=1:n
a_quan(find((q(i)-d/2 <= a_quan) & (a_quan <= q(i)+d/2)))=...
q(i).*ones(1,length(find((q(i)-d/2 <= a_quan) & (a_quan <= q(i)+d/2))));
b_quan(find( a_quan==q(i) ))=(i-1).*ones(1,length(find( a_quan==q(i) )));
end
a_quan=a_quan*amax;
nu=ceil(log2(n));
code=zeros(length(a) ,nu);
for i=1 :length(a)
for j=nu:-1:0
_____________________________________________________________________________________________
Page 6 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________
if ( fix(b_quan(i)/(2^j)) == 1)
code(i,(nu-j)) = 1;
b_quan(i) = b_quan(i) - 2^j;
end
end
end
sqnr=20*log10(norm(a)/norm(a-a_quan));

Problem 6: Generate a sinusoidal signal with amplitude 1 and co = 1. Using a uniform PCM
scheme, quantize it once to 8 levels and once to 16 levels. Plot the original signal and the quantized
signals on the same axes. Compare the resulting SQNRs in the two cases.

Problem 7: Generate a sequence of length 500 of zero-mean, unit-variance Gaussian random


variables. Using u_pcm.m, find the resulting SQNR when the number of quantization levels is 64.
Find the first five values of the sequence, the corresponding quantized values, and the
corresponding codewords.

Nonuniform PCM
Many physical signals, such as speech signals, have the characteristic that small signal
amplitudes occur more frequently than large signal amplitudes. However, a uniform
quantizer provides the same spacing between successive levels throughout the entire
dynamic range of the signal. A better approach would be to have a nonuniform
quantizer, which provides more closely spaced levels at the small signal amplitudes and
more widely spaced levels at the large signal amplitudes.
A nonuniform quantizer characteristic is usually obtained by passing the signal
through a nonlinear device that compresses the signal amplitudes, followed by a
uniform PCM quantizer. At the receiving end of the communication system, the received
signal is passed through the inverse of the nonlinear element used in the transmitter
to expand the signal amplitude. Thus, the combination of a signal compressor at the
transmitter and an expander at the receiver is called a compander. The overall effect is
equivalent to a PCM system with nonuniform spacing between levels.
For example, a logarithmic compressor employed in U.S. and Canadian
telecommunication systems for speech transmission is called a u-law compressor and has an input-
output magnitude characteristic of the form

where x is the normalized input (|x| <= 1) and jtf is a parameter that in standard jtf-law
nonlinearity is equal to 255. A plot of this nonlinearity for different values of ju is
shown in Figure 3. The inverse of u-law nonlinearity is given by

Logarithmic u = 255 is widely used in waveform encoding of speech for digital


transmission. The typical bit rate for PCM is 64,000 bps (8 bits/sample).
_____________________________________________________________________________________________
Page 7 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________
The second widely used logarithmic compressor is the A-law compander. The
characteristics of this compander are given by

where A is chosen to be 87.56. The performance of this compander is comparable to


the performance of the u-law compander.

Figure-3: The u-law compander

The two m-files mulaw.m and invmulaw.m given below implement u-law nonlinearity and its
inverse.
function [y,a]=mulaw(x,mu)
%MULAW mu-law nonlinearity for nonuniform
% Y=MULAW(X,MU).
% X= input vector.
a=max(abs(x));
y=(log(1 +mu*abs(x/a))./log(1 +mu)).*sign(x);
function x=invmulaw(y,mu)
%INVMULAW the inverse of mu-law nonlinearity
%X=INVMULAW(Y,MU) Y=normalized output of the mu-law nonlinearity.
x=(((1+mu).^(abs(y))-1)./mu).*sign(y);

The m-file mula_pcm.m is the equivalent of the m-file u_pcm.m when using a u-
law PCM scheme. This file is given next and is used with u-law PCM systems.
function [sqnr,a_quan,code]=mula_pcm(a,n,mu)
%MULAJPCM mu-law PCM encoding of a sequence

_____________________________________________________________________________________________
Page 8 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________
% [SQNR,A-QUAN,CODE]=MULAJPCM(A,N,MU).
% a=input sequence.
% n=number of quantization levels (even).
% sqnr=output SQNR (in dB).
% ajquan=quantized output before encoding.
% code=the encoded output.
[y,maximum]=mulaw(a,mu);
[sqnr,y_q,code]=u_pcm(y,n);
a_quan=invmulaw(y_q,mu);
a_quan=maximum*a_quan;
sqnr=20*log10(norm(a)/norm(a-a_quan));

Problem 8: Generate a sequence of random variables of length 500 according to an N(0, 1)


distribution. Using 16, 64, and 128 quantization levels and a u-law nonlinearity with u = 255

Student's feedback (Try giving useful feedback, e.g. did this lab session help you in learning, how to
improve student's learning experience, was the staff helpful, etc):
_____________________________________________________________________________
_____________________________________________________________________________

_____________________________________________________________________________________________
Page 9 of 10
Lab #
Analogue and Digital National University Roll No: __________
CommunicationLab
(EL323)
of Computer and Emerging Sciences
Islamabad Fall 2016
12
____________________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

Student'sSignature: _________________________________

Correctness of

TOTAL
AWARDED

Attitude

Neatness

Conclusion
Originality
Initiative
MARKS

TOTAL 10 10 10 20 20 30 100

EARNED

Lab Instructor's Comments:_______________________________________________________


_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

Lab Instructor's Signature: _________________________________

_____________________________________________________________________________________________
Page 10 of 10

You might also like