Professional Documents
Culture Documents
DC Lab 07
DC Lab 07
DC Lab 07
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST,
RAWALPINDI
Lab Number 07
Name: Syed Ali John Naqvi
CE 41 B
Reg# 307975
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?
Code:
n = input('Enter n value for n-bit PCM system: ');
n1 = input('Enter number of samples in a period: ');
L = 2^n;
% Quantization
Vmin = -Vmax;
delta = (Vmax-Vmin)/L;
part = Vmin:delta:Vmax;
code = Vmin-(delta/2):delta:Vmax+(delta/2);
[ind,q] = quantiz(s,part,code);
subplot(3,1,3);
stem(q,'r*');
grid on;
title('Quantized Signal');
ylabel('Amplitude');
xlabel('Time');
% Plot difference
figure;
diff = s - q;
plot(diff, 'b', 'LineWidth', 2);
hold on;
stem(zeros(size(diff)), 'k--');
grid on;
title('Difference between Sampled and Quantized Signals');
ylabel('Amplitude');
xlabel('Time');
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.
n = 8;
n1 = 8;
L = 2^n;
% Quantization
Vmin = -Vmax;
delta = (Vmax-Vmin)/L;
part = Vmin:delta:Vmax;
code = Vmin-(delta/2):delta:Vmax+(delta/2);
[ind,q] = quantiz(s,part,code);
subplot(3,1,3);
stem(q,'r*');
grid on;
title('Quantized Signal');
ylabel('Amplitude');
xlabel('Time');
% Plot difference
figure;
diff = s - q;
stem(diff, 'b', 'LineWidth', 2);
hold on;
stem(zeros(size(diff)), 'k--');
grid on;
title('Difference between Sampled and Quantized Signals');
ylabel('Amplitude');
xlabel('Time');
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);
Code:
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')