BANGLADESH UNIVERSITY OF ENGINEERING AND
TECHNOLOGY
BUET
Department of Electrical and Electronic Engineering
Course No.: EEE 312
Group No.: 4
Course Title: Digital Signal Processing I Laboratory
Experiment No.: 1
Name of the Experiment: Study of Sampling, Quantization and Encoding
Date of Performance: 10-3-2021
Date of Submission: 24-3-2021
Submitted by: Shafin Shadman Ahmed
Student ID: S201706020
Section: A (A1)
Department: EEE
Level: 3, Term: 1
Part-A
The code for x(t) = sin(2π10t + 0°) is given below:
clc
clear
close
t = 0 : 0.001 : 0.5;
x = sin(2 * pi * 10 * t);
Fs = [20 50 100 10];
for i = 1 : length(Fs)
Ts = 1 / Fs(i);
ts = 0 : Ts : 0.5;
xs = sin(2 * pi * 10 * ts);
subplot(length(Fs), 2, 2 * i - 1);
stem(ts, xs);
title(['Sampled Signal at ' num2str(Fs(i)) ' Hz']);
xhat = interp1(ts, xs, t);
subplot(length(Fs), 2, 2 * i);
plot(t, x);
hold on
plot(t, xhat);
title(['Generated and Reconstructed Signal at ' num2str(Fs(i)) '
Hz']);
end
The outputs will be as follows:
Comment: The reconstructed signal looks linear for small sampling frequencies (10 Hz, 20 Hz) and
is hard to plot alongside the original signal. When the sampling is done at 50 Hz, the reconstruction is
actually visible but far from perfect. If 100 Hz is used, the reconstruction approaches the original
signal but still minor flaws remain.
The code for x(t) = sin(2π10t) + sin(2π50t) + sin(2π100t) is given below:
clc
clear
close
t = 0 : 0.001 : 0.5;
y = sin(2 * pi * 10 * t) + sin(2 * pi * 50 * t) + sin(2 * pi * 100 *
t);
Fs = [200 500 1000];
for i = 1 : length(Fs)
Ts = 1 / Fs(i);
ts = 0 : Ts : 0.5;
ys = sin(2 * pi * 10 * ts) + sin(2 * pi * 50 * ts) + sin(2 * pi
* 100 * ts);
subplot(length(Fs), 2, 2 * i - 1);
stem(ts, ys);
title(['Sampled Signal at ' num2str(Fs(i)) ' Hz']);
yhat = interp1(ts, ys, t);
subplot(length(Fs), 2, 2 * i);
plot(t, y);
hold on
plot(t, yhat);
title(['Generated and Reconstructed Signal at ' num2str(Fs(i)) '
Hz']);
end
The outputs will be as follows:
Comment: Reconstruction was started with 200 Hz because the highest frequency was 100 Hz. The
reconstructed signal at 200 Hz hardly matches the original signal. When the sampling is done at 500
Hz, the reconstruction is a lot better but some flaws still exist at peaks. If 1000 Hz is used, the
reconstruction can be considered perfect. So, a minimum of 1000 Hz can be selected.
Part-B
The code is given below:
clc
clear
close
t = 0 : 0.001 : 0.1;
y = sin(2 * pi * 10 * t) + sin(2 * pi * 50 * t) + sin(2 * pi * 100 *
t);
Fs = 200;
Ts = 1 / Fs;
ts = 0 : Ts : 0.1;
ys = sin(2 * pi * 10 * ts) + sin(2 * pi * 50 * ts) + sin(2 * pi *
100 * ts);
B = [3 4 6];
for j = 1 : 3
b = B(j);
L = 2 ^ b;
Del = (max(ys) - min(ys)) / (L - 1);
yQ = min(ys) : Del : max(ys);
yq = ys;
for i = 1 : length(yQ) - 1
temp = yq > yQ(i) & yq < yQ(i + 1) & abs(yq - yQ(i)) <
abs(yq - yQ(i + 1));
yq(temp) = yQ(i);
temp = yq > yQ(i) & yq < yQ(i + 1) & abs(yq - yQ(i)) >
abs(yq - yQ(i + 1));
yq(temp) = yQ(i + 1);
end
subplot(3, 2, 2 * j - 1);
plot(t, y);
hold on
stairs(ts, yq);
title('Quantization')
QNP = sum((yq - ys) .^ 2) / (length(ts) - 1);
SP = sum(y .^ 2) / (length(t) - 1);
SQNR = SP ./ QNP;
SQNRdB_A = 10 * log10(SQNR);
SQNRdB_B = 1.76 + 6 * b;
disp(['QNP for ' num2str(b) ' bits = ' num2str(QNP)])
disp(['SQNR for ' num2str(b) ' bits = ' num2str(SQNR)])
disp(['SQNR from equation A for ' num2str(b) ' bits (in dB) = '
num2str(SQNRdB_A)])
disp(['SQNR from equation B for ' num2str(b) ' bits (in dB) = '
num2str(SQNRdB_B)])
yhat = interp1(ts, yq, t);
%yhat = spline(ts, yq, t);
subplot(3, 2, 2 * j)
plot(t, y);
hold on
plot(t, yhat);
title('Reconstruction')
end
The outputs will be as follows:
QNP for 3 bits = 0.033173, SQNR for 3 bits = 45.2175, SQNR from equation A for 3 bits (in dB) =
16.5531, SQNR from equation B for 3 bits (in dB) = 19.76
QNP for 4 bits = 0.0070156, SQNR for 4 bits = 213.8098, SQNR from equation A for 4 bits (in dB) =
23.3003, SQNR from equation B for 4 bits (in dB) = 25.76
QNP for 6 bits = 0.00058985, SQNR for 6 bits = 2543.0173, SQNR from equation A for 6 bits (in dB)
= 34.0535, SQNR from equation B for 6 bits (in dB) = 37.76
Comment: Quantization was done at 3, 4 and 6 bits. Arguably, the quantization for 6 bits was better
but none of the bits provided a satisfactory reconstruction. Even if in place of interp1(), spline() is
used, the output isn’t good.
Part-C
The code, performed with 4-bit quantization, is given below:
clc
clear
close
t = 0 : 0.001 : 0.3;
x = sin(2 * pi * 10 * t) + sin(2 * pi * 50 * t) + sin(2 * pi * 100 *
t);
Fs = 200;
T = 1 /Fs;
ts = 0 : T : 0.3;
xs = sin(2 * pi * 10 * ts) + sin(2 * pi * 50 * ts) + sin(2 * pi *
100 * ts);
B = 4;
L = 2 ^ B;
Del = (max(xs) - min(xs)) / (L - 1);
xQ = min(xs) : Del : max(xs);
xq = xs;
for i = 1 : length(xQ) - 1
temp = xq > xQ(i) & xq < xQ(i + 1) & abs(xq - xQ(i)) < abs(xq -
xQ(i + 1));
xq(temp) = xQ(i);
temp = xq > xQ(i) & xq < xQ(i + 1) & abs(xq - xQ(i)) > abs(xq -
xQ(i + 1));
xq(temp) = xQ(i + 1);
end
xe = round((xq - min(xq)) / Del);
encdSeq = dec2bin(xe);
disp(encdSeq);
The output is shown in 4 columns. Sequences start from the first column and then proceeds downward
until the end, when the next column has the next information.
1000 0100 0011 1011
1100 1000 0101 0111
1010 0101 1000 1010
0111 0011 0100 1100
1011 1000 0000 1000
1111 1100 0100 0011
1011 1010 1000 0101
0111 0111 0101 1000
1010 1011 0011 0100
1100 1111 0111 0000
1000 1011 1100 0100
0011 0111 1010 1000
0101 1010 0111 0101
1000 1100 1011 0011
0100 1000 1111 1000
0000
Home Task
The code is given below:
clc
clear
close
% Signal Generation
file = 'Recording.m4a';
[x, Fs] = audioread(file);
L = length(x);
T = 1 / Fs;
d = T / (L - 1);
t = 0 : d : T;
% Signal Sampling
subplot(3, 1, 1)
stem(t, x, 'r');
title('Sampled Signal');
ylim([-1.5 1.5])
% mu-law compressor
mu = 255;
xNorm = x / max(abs(x));
y = sign(xNorm) .* (log(1 + mu * abs(xNorm))) / (log(1 + mu));
subplot(3, 1, 2)
stem(t, y);
title('Output of mu-law compressor');
ylim([-1.5 1.5]);
% Uniform Quantizer
b = 4;
L = 2 ^ b;
Del = (max(y) - min(y)) / (L - 1);
yQ = min(y) : Del : max(y);
yq = y;
for i = 1 : length(yQ) - 1
temp = yq > yQ(i) & yq < yQ(i + 1) & abs(yq - yQ(i)) < abs(yq -
yQ(i + 1));
yq(temp) = yQ(i);
temp = yq > yQ(i) & yq < yQ(i + 1) & abs(yq - yQ(i)) > abs(yq -
yQ(i + 1));
yq(temp) = yQ(i + 1);
end
subplot(3, 1, 3)
stairs(t, yq, 'k');
title('Output of Uniform Quantizer');
ylim([-1.5 1.5]);
% SQNR Computation
sigmax2 = sum(x .^ 2) / (length(t));
sigmae2 = sum((yq - xNorm).^2) / (length(t));
SQNR = sigmax2 / sigmae2;
SQNR_dB = 10 * log10(SQNR);
disp(['SQNR for this operation is = ' num2str(mean(SQNR))]);
disp(['SQNR for this operation in dB is = ' num2str(mean(SQNR_dB))
'dB']);
The outputs are as follows:
SQNR for this operation is = 0.017729, SQNR for this operation in dB is = −17.5133dB
Discussions:
In this experiment, the implementations of sampling, quantization and coding were observed. In part
A, two signals were sampled and reconstructed. It can be said that although according to Nyquist
criteria, sampling can be done with at least twice the given/maximum frequency, the reconstruction is
not actually good for twice the frequency. Frequency needs to be increased for better reconstruction.
In part B, it is seen that the Quantization Noise Power (QNP) decreases if bits are increased, but
SQNR increases. Implementation of either interp1() or spline() shows that the signal needs more bits
to be reconstructed properly. In part C, the input signal was encoded at 4 bits. For different time limits
and different bit values, the results would vary. The last part was to check the function of mu-law
compressor and uniform quantization. The signal was sampled, then compressed, quantized and
SQNR was performed. The signal produced less SQNR, indicating that the quantization was handled
much well.
________________