INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING (EE)
EE093IU
DIGITAL SIGNAL PROCESSING LABORATORY
LAB 2
SAMPLING, QUANTIZING AND
CODING WITH MATLAB
Student name: ………………………………..
Student ID: ………………………………..
Class: ………………………………..
Date: ………………………………..
Page 1 of 4
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING (EE)
FUNDAMENTAL DISCUSSION
The conversion of an analog signal into a digital signal consists of three steps: sampling,
quantization, and coding. These are the three operations an ADC does. To illustrate them,
consider a sinusoid: 𝑥(𝑡) = 4𝑐𝑜𝑠(2𝜋𝑡). Its sampling period, according to the Nyquist sampling
rate condition, is:
𝜋
𝑇𝑠 ≤ Ω = 0.5 (sec/sample),
𝑚𝑎𝑥
as the maximum frequency of 𝑥(𝑡) is Ω𝑚𝑎𝑥 = 2𝜋. We let 𝑇𝑠 = 0.01 (sec/sample) to obtain a
sampled signal 𝑥𝑠 (𝑛𝑇𝑠 ) = 4𝑐𝑜𝑠(2𝜋𝑛𝑇𝑠 ) = 4𝑐𝑜𝑠(2𝜋 𝑛⁄100), a discrete sinusoid of period
100. The following scipt is used to get the sampled 𝑥[𝑛] and the quantized 𝑥𝑞 [𝑛] signals and
the quantization error 𝜀[𝑛]
(a) original sinusoid, (b) sampled sinusoid signal,
(c) quantized sinusoid using four levels, (d) quantization error.
Page 2 of 4
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING (EE)
The quantization of the sampled signal is implemented by comparing each of the
samples 𝑥𝑠 (𝑛𝑇𝑠 ) with four levels and assigns to each of the corresponding level. Notice the
approximation of the values given by the quantized signal samples to the actual values of the
signals. The difference between the original signal, or the quantization error, 𝜀(𝑛𝑇𝑠 ), is also
computed and shown in Figure 1.
The binary signal corresponding to the quantized signal is computed using the
binary codes ‘10’, ‘11’, ‘00’, and ‘01’ to the four possible levels of the quantizer. The
result is a sequence of 0s and 1s, each pair of digits sequentially corresponding to each
of the samples of the quantized signal.
LAB PROCEDURE
Problem 1:
Given the original signal to be 𝑥(𝑡) = 8𝑐𝑜𝑠(4𝜋𝑡). We sample the signal using the
sampling rate 𝑇𝑠 = 0.01 (𝑠𝑒𝑐/𝑠𝑎𝑚𝑝𝑙𝑒) to obtain a sampled signal 𝑥𝑠 (𝑛𝑇𝑠 ) = 8𝑐𝑜𝑠(2𝜋𝑛𝑇𝑠 ) =
𝑛
8𝑐𝑜𝑠 (4𝜋 ). Find the quantization signals 𝑥𝑞 [𝑛], given that the number of quantization
100
levels is 8. Write your own quantization function for this task.
Plot 2 figures: the sampled signal 𝑥𝑠 (𝑛𝑇𝑠 ) and the quantized signal 𝑥𝑞 [𝑛] alongside
with the original signal 𝑥(𝑡).
Problem 2:
Using the same signals 𝑥(𝑡) as in Problem 1, obtain and plot the quantization error
𝜀[𝑛], in which:
𝜀[𝑛] = 𝑥[𝑛] − 𝑥𝑞 [𝑛]
Also, obtain and display the code sequence represent the 8 levels of quantization in
Problem 1, follow by the rules:
𝑥̂(𝑛𝑇𝑠 ) = − 4∆ → 100 𝑥̂(𝑛𝑇𝑠 ) = 0 → 000
𝑥̂(𝑛𝑇𝑠 ) = − 3∆ → 111 𝑥̂(𝑛𝑇𝑠 ) = 1∆ → 001
𝑥̂(𝑛𝑇𝑠 ) = − 2∆ → 110 𝑥̂(𝑛𝑇𝑠 ) = 2∆ → 010
𝑥̂(𝑛𝑇𝑠 ) = − 1∆ → 101 𝑥̂(𝑛𝑇𝑠 ) = 3∆ → 011
With ∆ being the quantization step. Write your own coding function.
Problem 3:
Page 3 of 4
INTERNATIONAL UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING (EE)
Quantization is done by replacing each value of an analog signal 𝑥(𝑡) by the value of
the nearest quantization level. To exemplify this operation, let’s simulate a unipolar ADC
(Analog to Digital Converter) having the technical specifications: R Volts (full-scale range)
and B (number of bits), in which R and B are the to be input using input function.
(a) Write a MATLAB function y = quantized_example(x, R, B) where x and y are vectors
containing the input signal and the quantized signal, respectively;
𝑅 𝑅
(b) Test your function with an input sinusoid signal ranging from − 2 to Volts (with B
2
determines the number of steps in the quantization process);
(c) On the same graph, use the plot and stem functions to display the input signal and quantized
signal, respectively;
Page 4 of 4