DSP Lab Report 8
DSP Lab Report 8
5 / 10 / 24
08
EE-20-A
Introduction to MATLAB
MATLAB is a high-level programming language that has been used extensively to solve complex
engineering problems.
Task # 1
Task-1: Consider the following CT signal: x(t) = sin (2 pi F0 t). The sampled version will be x(n) =sin (2 pi F0/Fs n),
where n is a set of integers and sampling interval Ts=1/Fs.
Display the signal x(n) for n = 0 to 99 for Fs = 5 kHz and F0 = 0.5, 2, 3 and 4.5 kHz. Explain the similarities and
differences among various plots. Also mention whether aliasing occurs or not.
Answers:
Following is the MATLAB function code.
% Plotting the two CTCV sinusoids
clear all;
close all;
clc;
F1 = 500;
F2 = 2000;
F3 = 3000;
F4 = 4500;
Fs = 5000;
Ts = 1/Fs;
t = 0 : 0.0005 : 0.2;
x1t = sin(2*pi*F1*t);
x2t = sin(2*pi*F2*t);
x3t = sin(2*pi*F3*t);
x4t = sin(2*pi*F4*t);
figure,
plot(t,x1t,t,x2t,t,x3t,t,x4t, 'LineWidth',2);
xlabel('cont time (sec)');
ylabel('Amp');
xlim([0 0.1]);
legend('0.5kHz','2Hz','3kHz','4.5kHz');
title('Two CTCV sinusoids plotted');
grid on;
x1n = cos(2*pi*F1*nTs);
x2n = cos(2*pi*F2*nTs);
x3n = cos(2*pi*F3*nTs);
x4n = cos(2*pi*F4*nTs);
figure,
subplot(2,2,1), stem(nTs,x1n,'filled','LineWidth',2);
title('0.5KHz sampled');
xlabel('discrete time (sec)');
ylabel('Amp');
xlim([0 0.1]);
grid on;
subplot(2,2,2)
stem(nTs,x2n,'filled','LineWidth',2);
title('2kHz sampled')
xlabel('discrete time (sec)');
ylabel('Amp');
xlim([0 0.1]);
grid on;
subplot(2,2,3)
stem(nTs,x3n,'filled','LineWidth',2);
title('3kHz sampled')
xlabel('discrete time (sec)');
ylabel('Amp');
xlim([0 0.1]);
grid on;
subplot(2,2,4)
stem(nTs,x4n,'filled','LineWidth',2);
title('4.5kHz sampled')
xlabel('discrete time (sec)');
ylabel('Amp');
xlim([0 0.1]);
grid on;
a) For fd = 1/50 cycles/sample and N = 200 samples, write a program to quantize the signal x(n), using round-off to 64,
128 and 256 quantization levels. In each case display the signals x(n), xq(n)and xe(n)and compute the corresponding
SQNR.
Answers:
clear all;
close all;
clc;
part a
fd = 1/50;
n = 0:199;
q_values = [65, 128, 256]; % Three different values for q
for i = 1:length(q_values)
q = q_values(i);
x = sin(2*pi*fd*n);
Px = sum(abs(x).^2)/length(x);
xq = round(x*10^q)/10^q;
xe = xq - x;
Pe = sum(abs(xe).^2)/length(xe);
SQNR = 10*log10(Px/Pe);
disp(['For q = ' num2str(q) ', the Signal to Quantization Noise Ratio is: '
num2str(SQNR) ' dB.']);
figure;
subplot(2,1,1);
stem(n,x); hold on;
stem(n,xq, 'r'); grid on;
xlabel('indices'); ylabel('Amplitude');
xlim([0 49]);
ylim([-2.1 2.1]);
legend('Original Signal','Quantized Signal');
title(['Signal and Quantized Signal for q = ' num2str(q)]);
subplot(2,1,2);
plot(n,xe, 'k'); grid on;
xlabel('indices'); ylabel('Error');
xlim([0 49]);
title(['Quantization Error for q = ' num2str(q)]);
end
For q = 65, the Signal to Quantization Noise Ratio is: 328.1227 dB.
For q = 128, the Signal to Quantization Noise Ratio is: 323.2797 dB.
For q = 256, the Signal to Quantization Noise Ratio is: 325.0692 dB.
part b
clear;
clc;
fd = 1/50;
n = 0 : 199;
x = sin(2*pi*fd*n);
q = 0 : 20 ; %No. of Digits after decimal points to be retained x = 2*cos(2*pi*fd*n);
Px = sum(abs(x).^2)/length(x);
for num = 1:length(q)
x1q = round(x*10^q(num))/10^q(num); x1e = x-x1q;
Pe1 = sum(abs(x1e).^2)/length(x1e); SQNR1(num) = 10*log10(Px/Pe1);
x2q = floor(x*10^q(num))/10^q(num); x2e = x-x2q;
Pe2 = sum(abs(x2e).^2)/length(x2e); SQNR2(num) = 10*log10(Px/Pe2);
x3q = ceil(x*10^q(num))/10^q(num); x3e = x-x3q;
Pe3 = sum(abs(x3e).^2)/length(x3e); SQNR3(num) = 10*log10(Px/Pe3);
end
figure
plot(q,SQNR1,q,SQNR2,q,SQNR3,'--','Linewidth', 2); legend('Round-off','Floor','Ceil')
grid;
xlabel('Significant Digits'); ylabel('SQNR (dB)');
xlim([q(1) q(end)]);
legend("Position", [0.15551,0.76932,0.20407,0.12347]);
• Rounding: Using rounding for quantization generally gives a higher SQNR compared to floor, as
rounding typically introduces less error.
• Flooring: Using floor for quantization results in a lower SQNR because it consistently rounds down,
introducing a bias and typically more error.
The theoretical SQNR for a quantizer can be computed using:
SQNR=1.76+6.02×𝑏
where 𝑏 is the number of bits.
Or.
SQNR = 1.76+6.02×log (64) = 38 dB
2
…the end!