Lab 2
Lab 2
EE-20141
LAB SESSION 2
To observe the quantization effects on sampled signals and to understand how quantization leads to
quantization error. In this lab, we will investigate the influence of the number of quantization levels on
the quality of the digitized signal. The method of selection of ADC is also a part of this lab session.
clear all; close all; clc;
fd=1/50;
N=500; n=[0:N-1];
q=input('No. of digits after decimal points to be retained (0-9): ');
x=cos(2*pi*fd*n); Px=sum(abs(x).^2)/N;
if a==1
xq = round(x*10^q)/10^q;
elseif a==2
xq = floor(x*10^q)/10^q;
elseif a==3
xq = ceil(x*10^q)/10^q;
end
xe=xq-x; Pe=sum(abs(xe).^2)/N;
SQNR=10*log10(Px/Pe);
disp(['The signal to Quantization Noise Ratio is: ' num2str(SQNR) ' dB.' ]);
figure,
subplot(2,1,1);
stem(n,x,'filled'); hold;
stem(n,xq,'r','filled'); grid;
xlabel('indices'); ylabel('Amp');
xlim([0 49]); ylim([-2.1 2.1]);
legend('DTCV','DTDV');
subplot(2,1,2);
plot(n,xe, 'k','Linewidth',2); grid;
xlabel('indices'); ylabel('Error');
xlim([0 49]);
KINZA PERVEZ
EE-20141
LAB SESSION 2
fd = 1/50; n = [0:499];
q = [0:10]; %No. of Digits after decimal points to be retained
x = cos(2*pi*fd*n);
Px = sum(abs(x).^2)/length(x);
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)]);
title ('Relation between SQNR and Significant digits')
KINZA PERVEZ
EE-20141
LAB SESSION 2
TASK#1:
clear all; close all; clc;
x1=cos(2*pi*fd1*n); Px1=sum(abs(x1).^2)/N;
x2=cos(2*pi*fd2*n); Px2=sum(abs(x2).^2)/N;
if a1==1
xq1 = round(x1*10^q1)/10^q1;
elseif a==2
xq1 = floor(x1*10^q1)/10^q1;
elseif a==3
xq1 = ceil(x1*10^q1)/10^q1;
end
if a2==1
xq2 = round(x2*10^q2)/10^q2;
elseif a==2
xq2 = floor(x2*10^q2)/10^q2;
elseif a==3
xq2 = ceil(x2*10^q2)/10^q2;
end
xe1=xq1-x1; Pe1=sum(abs(xe1).^2)/N;
xe2=xq2-x2; Pe2=sum(abs(xe2).^2)/N;
SQNR1=10*log10(Px1/Pe1);
disp(['The signal to Quantization Noise Ratio is: ' num2str(SQNR1) ' dB.' ]);
SQNR2=10*log10(Px2/Pe2);
disp(['The signal to Quantization Noise Ratio is: ' num2str(SQNR2) ' dB.' ]);
figure,
subplot(2,2,1);
stem(n,x1,'filled'); hold;
stem(n,xq1,'r','filled'); grid;
xlabel('indices for fd1=1/25'); ylabel('Amp');
xlim([0 49]); ylim([-2.1 2.1]);
legend('DTCV','DTDV');
subplot(2,2,3);
plot(n,xe1, 'k','Linewidth',2); grid;
xlabel('indices for fd1=1/25'); ylabel('Error');
KINZA PERVEZ
EE-20141
LAB SESSION 2
xlim([0 49]);
subplot(2,2,2);
stem(n,x2,'filled'); hold;
stem(n,xq2,'r','filled'); grid;
xlabel('indices for fd2=1/50'); ylabel('Amp');
xlim([0 49]); ylim([-2.1 2.1]);
subplot(2,2,4);
plot(n,xe2, 'k','Linewidth',2); grid;
xlabel('indices fd2=1/50'); ylabel('Error');
xlim([0 49]);
OBSERVATION:
TASK#2:
F = 100; Fs = 10000;
Ts = 1/Fs; fd = F/Fs;
N = 499; n = [0:N-1];
t = [0:0.000005:N*Ts-Ts];
xt = cos(2*pi*F*t);
xn = cos(2*pi*fd*n);
bits = input('Enter no. of bits from 1-8: ');
max=2^(bits-1)-1;
xn=max*xn;
xq = floor(xn);
elseif a==3
xq = ceil(xn);
end
xn=xn/max; xq=xq/max;
figure;
subplot(2,1,1);
stem(n,xn,'m');xlim([0 100]),grid on;hold on;
stem(n,xq,'g');xlim([0 100]);hold on;
plot(n,xq,'k','linewidth',1.5); xlim([0 100]);
legend('DTCV','DTDV','LEVELS');
xlabel('Indices'); ylabel('Amplitude');
title(['Quantization With ',num2str(bits), ' bit ADC']);
subplot(2,1,2);
plot(t,xt,'m','linewidth',1.5);
xlim([(0*Ts) (100*Ts)]);
xlabel('Continuous Time(sec)'); ylabel('Amplitude');
title(['CTCV Signal']);
KINZA PERVEZ
EE-20141
LAB SESSION 2
KINZA PERVEZ
EE-20141
LAB SESSION 2
TASK#3:
clc; clear all; close all;
[xn Fs] = audioread('ee141original.wav');
Ts = 1/Fs;
t=[0:Ts:5-Ts];
bits=input('Enter no. of bits from 1-4 for ADC: ');
max=2^(bits-1)-1;
xn=max*xn;
xn=xn/max;
xq=xq/max;
sound(xn,Fs);
pause(6);
sound(xq,Fs);
No. of bits when the audio stopped improving was found to be: 12 bits.