DC Lab 07
DC Lab 07
DC Lab 07
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST,
RAWALPINDI
LabNumber 07
NIMRA AKMAL
CE 41 B
288608
Lab # 07: Demonstration of Pulse Code Modulation
Objective:
To demonstrate the principle of PCM (pulse code modulation) and implementation of a PCM
based digital communication system.
Tasks:
1. Plot a difference delta of the sampled signal and quantized signal in Fig. 3. Compare
them using different line specs on same graph. How much is the maximum quantization
error?
clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
Vmax=8;
s=Vmax.*sin(x);%Vmax.*sin(x)
subplot(4,1,1);
plot(s);
title('Source Analogue Signal');
ylabel('Amplitude');
xlabel('Time');
subplot(4,1,2);
stem(s);grid on; title('Sampled Signal'); ylabel('Amplitude');
xlabel('Time');
for i=1:l1
if(ind(i)~=0) % To make index as binary decimal so started
from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2
if(q(i)==Vmin-(delta/2)) % To make quantized value in between the
levels
q(i)=Vmin+(delta/2);
end
end
subplot(4,1,3);
stem(q,'r*');grid on; % Display the Quantized values
title('Quantized Signal');
ylabel('Amplitude');
xlabel('Time');
% subplot(4,1,4);
% stem(s);
% hold on
% stem(q,'r*');
subplot(4,1,4);
diff= s-q;
stem(diff);
title('Difference of s and q');
mx=max(diff);
2. Run your whole PCM encoder code (whose output in Fig; 3 and 4) for a 8-bit PCM i.e.,
n=8. Use a sampling period of 8 also i.e., n1 =8. Plot Fig. 3 and 4 again as above. Also
give the values of ind and code matrices as mentioned in the PCM encoding section
above.
clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
Vmax=8;
s=Vmax.*sin(x);%Vmax.*sin(x)
subplot(4,1,1);
plot(s);
title('Source Analogue Signal');
ylabel('Amplitude');
xlabel('Time');
subplot(4,1,2);
stem(s);grid on; title('Sampled Signal'); ylabel('Amplitude');
xlabel('Time');
for i=1:l1
if(ind(i)~=0) % To make index as binary decimal so started
from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2
if(q(i)==Vmin-(delta/2)) % To make quantized value in between the levels
q(i)=Vmin+(delta/2);
end
end
subplot(4,1,3);
stem(q,'r*');grid on; % Display the Quantized values
title('Quantized Signal');
ylabel('Amplitude');
xlabel('Time');
% subplot(4,1,4);
% stem(s);
% hold on
% stem(q,'r*');
subplot(4,1,4);
diff= s-q;
stem(diff);
title('Difference of s and q');
figure
code=de2bi(ind,'left-msb');
k=1;
for i=1:l1
for j=1:n
coded(k)=code(i,j);
% convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
subplot(2,1,1); grid on;
stem(coded);
% Display the encoded signal
[a b]= size(code);
axis([0 a*b -1 2]); title('Encoded Signal');
ylabel('Amplitude');
xlabel('Time Index');
Hint: Use reshape to convert the coded vector above and use bi2dec to convert the binary
indices back to decimal. Then get back the quantized value using,
q =delta*index + Vmin + (delta/2);
clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
Vmax=8;
s=Vmax.*sin(x);%Vmax.*sin(x)
subplot(4,1,1);
plot(s);
title('Source Analogue Signal');
ylabel('Amplitude');
xlabel('Time');
subplot(4,1,2);
stem(s);grid on; title('Sampled Signal'); ylabel('Amplitude');
xlabel('Time');
for i=1:l1
if(ind(i)~=0) % To make index as binary decimal so started
from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2
if(q(i)==Vmin-(delta/2)) % To make quantized value in between the
levels
q(i)=Vmin+(delta/2);
end
end
subplot(4,1,3);
stem(q,'r*');grid on; % Display the Quantized values
title('Quantized Signal');
ylabel('Amplitude');
xlabel('Time');
% subplot(4,1,4);
% stem(s);
% hold on
% stem(q,'r*');
subplot(4,1,4);
diff= s-q;
stem(diff);
title('Difference of s and q');
figure
code=de2bi(ind,'left-msb');
k=1;
for i=1:l1
for j=1:n
coded(k)=code(i,j);
% convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
subplot(2,1,1); grid on;
stem(coded);
% Display the encoded signal
[a b]= size(code);
axis([0 a*b -1 2]); title('Encoded Signal');
ylabel('Amplitude');
xlabel('Time Index');
subplot(3,1,2)
plot(q, 'r*'); hold on
plot(s, 'b--')
title('Quantized Signal and Recovered Analog Signal')
ylabel('Amplitude')
xlabel('Time')
Ind:
Index