Nothing Special   »   [go: up one dir, main page]

DC Lab 07

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST,
RAWALPINDI

EC-431 Digital Communication

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;

% % Signal Generation + Sampling Operation


x=0:2*pi/n1:4*pi; %4pi to plot two cycles for visibility and n1 number
of samples per period have to be selected

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');

%Quantizer for our PCM encoder case


Vmin=-Vmax;
delta=(Vmax-Vmin)/L; %step size delta
part=Vmin:delta:Vmax; % Levels are
between Vmin and Vmax with difference of delta- Without quantization
code=Vmin-(delta/2):delta:Vmax+(delta/2); % Contains
Quantized values
[ind,q]=quantiz(s,part,code);
% Now, ind will contain index number and q contain quantized values
l1=length(ind);
l2=length(q);

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;

% % Signal Generation + Sampling Operation


x=0:2*pi/n1:4*pi; %4pi to plot two cycles for visibility and n1 number of
samples per period have to be selected

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');

%Quantizer for our PCM encoder case


Vmin=-Vmax;
delta=(Vmax-Vmin)/L; %step size delta
part=Vmin:delta:Vmax; % Levels are
between Vmin and Vmax with difference of delta- Without quantization
code=Vmin-(delta/2):delta:Vmax+(delta/2); % Contains
Quantized values
[ind,q]=quantiz(s,part,code);
% Now, ind will contain index number and q contain quantized values
l1=length(ind);
l2=length(q);

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(2,1,2); grid on;


stairs(coded);
% Display the encoded signal
[a b]= size(code);
axis([0 a*b -1 2]);
%title('Encoded Signal');
ylabel('Amplitude');
xlabel('Time Index');
3. Demodulation of PCM signal
Demodulate the PCM encoded signal above to recover the original analogue signal. Plot
the two on same graph with different color and line specs to compare results.

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;

% % Signal Generation + Sampling Operation


x=0:2*pi/n1:4*pi; %4pi to plot two cycles for visibility and n1 number
of samples per period have to be selected

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');

%Quantizer for our PCM encoder case


Vmin=-Vmax;
delta=(Vmax-Vmin)/L; %step size delta
part=Vmin:delta:Vmax; % Levels are
between Vmin and Vmax with difference of delta- Without quantization
code=Vmin-(delta/2):delta:Vmax+(delta/2); % Contains
Quantized values
[ind,q]=quantiz(s,part,code);
% Now, ind will contain index number and q contain quantized values
l1=length(ind);
l2=length(q);

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(2,1,2); grid on;


stairs(coded);
% Display the encoded signal
[a b]= size(code);
axis([0 a*b -1 2]);
%title('Encoded Signal');
ylabel('Amplitude');
xlabel('Time Index');

% Demodulation of PCM encoded signal


decode = reshape(coded, n, length(coded)/n)';
index = bi2de(decode, 'left-msb')';
q = delta*index + Vmin + (delta/2);

% Plot the quantized values and recovered analog signal


figure
subplot(3,1,1)
stem(q, 'r*');
title('Recovered 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

You might also like