Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1

DEPARTMENT OF COMPUTER &


SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI

Digital Communication
Lab Number 5

SUBMITTED TO:
LE SIR HAMZA SHAMI

SUBMITTED BY:
Abdurrafay Bin Khurram
Reg # 282912
DE- 41 Dept B

Submission Date: Mar 17, 2023


2

Tasks:

1. Effects of Quantization with variable precision levels


Simulate a DTCV sampled composite signal of 𝑓𝑑1=125 samples/sec and 𝑓𝑑2=150
samples/sec with length of the signal be 250 samples. Take the desired number of
significant digits from user as an input. Then choose the method of Quantization
(round-off, floor & ceil) and apply to the signal generated above. Compute the
quantization error signals and SQNR.

Code:
clear; close all; clc;

q = input('Enter Bit Depth (>0): ');

if (q < 1)
throw(MException('Bit Depth has to be atleast 1'));
end

i = 1;

for fd1 = [1/125 1/250]


n = 0:249;
x1 = cos(2 * pi * fd1 * n);
Px1 = sum(abs(x1) .^ 2) / length(x1);
% x1q = round(x1*10^q)/10^q;
x1q = roundtowardvec(x1, -1:2 / (2 ^ q - 1):1, 'round');
x1e = x1 -x1q;
Pe1 = sum(abs(x1e) .^ 2) / length(x1e);
SQNR = 10 * log10(Px1 / Pe1);

xlims = [0 149];
ylims = [-1.1 1.1];
disp(['SQNR for signal with Fs=' num2str(1 / fd1) ' Hz is: '
num2str(SQNR) ' dB.']);
subplot(2, 2, i);
plot(n, x1, n, x1q);
3

xlabel('Indexes');
ylabel('Amplitude');
xlim(xlims);
ylim(ylims);
legend('DTCV', 'DTDV');
title('Quantization of Signal');

subplot(2, 2, i + 1);
plot(n, x1e);
xlabel('Indexes');
ylabel('Amplitude');
xlim(xlims);
ylim(ylims);
title('Error in Quantization');
i = i + 2;
end

Output:
4

2. Simple sinusoid quantized to various bits per sample


Generate a 100 Hz sinusoid sampled at 10000 samples/sec and quantized at
1_bit/sample. Now increase the bit depth for various numbers of bits per
sample (2, 3, 4, 5, 6, 7, 8) and attach plots.

Code:
clear;
close all;
clc;

fd1 = 1/100;
n = 0:499;

xlims = [0 149];
ylims = [-1.1 1.1];

i = 1;

for bd = 1:9
x1 = sin(2 * pi * fd1 * n);
Px1 = sum(abs(x1) .^ 2) / length(x1);
x1q = roundtowardvec(x1, -1:2 / (2 ^ bd - 1):1, 'round');
x1e = x1 -x1q;
Pe1 = sum(abs(x1e) .^ 2) / length(x1e);
subplot(3, 3, i)
plot(n, x1, n, x1q);
xlabel('Indexes');
ylabel('Amplitude');
xlim(xlims);
ylim(ylims);
legend('DTCV', 'DTDV');
title(['Quantization of Signal with BitDepth=' num2str(bd)]);
i = i + 1;
end
5

Output:

3. Audio signal quantization to various bits per sample


Use your recorded voice in last session and quantize it at 1 bit /sample.
Change bit depth to 2,3,4 and then listen and take notes of your observations.
Decide no. of bits for audio until quality stops improving.

Code:
clear; clc;
[signal, Fs] = audioread('file.mp3');

i = 1;
x1 = signal(100000:150000, 1);
6

xlims = [0 length(x1)];
ylims = [-0.5 0.5];

for bd = 1:9
Px1 = sum(abs(x1) .^ 2) / length(x1);
x1q = roundtowardvec(x1, -1:2 / (2 ^ bd - 1):1, 'round');
x1e = x1 -x1q;
Pe1 = sum(abs(x1e) .^ 2) / length(x1e);
subplot(3, 3, i)
plot(x1);
hold on;
plot(x1q);
xlabel('Indexes');
ylabel('Amplitude');
xlim(xlims);
ylim(ylims);
legend('DTCV', 'DTDV');
title(['Quantization of Signal with BitDepth=' num2str(bd)]);
i = i + 1;

if (bd == 8)
sound(x1q);
end

end

Output:

Audio becomes intelligible at bit depth = 5 but it is like listening through a


radio with very weak signals. It becomes clear the more we increase bit depth,
and clear at around 8 or 10 bits.
7

You might also like