Matlab program for Pulse code modulation.
Matlab code
clc; close all; clear all;
A=1;
fc=input('Enter the frequency of Carrier signal (Pulse-train) = ');
N=0.0001;
t = 0:N:1/fm;
Sm=A+A*sin(2*pi*fm*t); % Message signal
Ssqw=square(2*pi*fc*t); % Square Wave
% CONVERSION OF SQUARE WAVE TO PULSE TRAIN (Sp = Carrier signal)
for i=1:length(Ssqw)
if (Ssqw(i)<=0)
Sp(i)=0;
else
Sp(i)=1;
end
end
Spam=Sp.*Sm; % Pulse Amplitude Modulated Signal
% QUANTIZATION OF PAM SIGNAL (LEVEL ASSIGNMENT)
m1=[];
tc=0:N:1/fc; % tc = Time period of one Pulse train
for j=1:length(tc)-1:length(Spam)-1
p=Spam(j:j+length(tc)-2);
m2=max(p);
if m2<=0.25
m3=1;
elseif m2<=0.5;
m3=2;
elseif m2<=0.75;
m3=3;
elseif m2<=1;
m3=4;
elseif m2<=1.25;
m3=5;
elseif m2<=1.5;
m3=6;
elseif m2<=1.75;
m3=7;
elseif m2<=2;
m3=8;
end;
m1=[m1 m3];
end;
m1
% CONVERSION OF QUANTIZED VALUE TO DIGITAL CODE (CODE ASSIGNMENT)
Sq=[];
for i=1:length(m1)
if m1(i)==1
n1=[0,0,0];
elseif m1(i)==2
n1=[0,0,1];
elseif m1(i)==3
n1=[0,1,0];
elseif m1(i)==4
n1=[0,1,1];
elseif m1(i)==5
n1=[1,0,0];
elseif m1(i)==6
n1=[1,0,1];
elseif m1(i)==7
n1=[1,1,0];
elseif m1(i)==8
n1=[1,1,1];
end;
Sq=[Sq n1];
end;
Sq
% GENERATING PCM SIGNAL (BIT STREAM)
Spcm=[];
t1=[];
t2=0;
tpb=(1/fc)/3;% tpb = Time per 3 bits(3 bits are sent per 1 Time period of pulse train)
for i=1:length(Sq)
if Sq(i)==0
t3=t2:0.0001:t2+tpb;
Bs=zeros(1,length(t3));
else
t3=t2:0.0001:t2+tpb;
Bs=ones(1,length(t3));
end;
t1=[t1 t3];
Spcm=[Spcm Bs];
t2=t2+tpb;
end;
subplot(4,1,1);
plot(t,Sm);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Message Signal');
subplot(4,1,2);
plot(t,Sp);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Carrier signal (Pulse-train)');
axis([0 1/fm -A-1 A+1]);
subplot(4,1,3);
plot(t,Spam);
grid;
xlabel('Time-->');
ylabel('Amplitude-->');
title('Pulse Amplitude Modulated Signal (PAM)');
subplot(4,1,4);
plot(t1,Spcm);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Pulse Code Modulated Signal (PCM)');
axis([0 1/fm -1 2]);
Command window
Enter the frequency of Message signal = 2
Enter the frequency of Carrier signal (Pulse-train) = 20
m1 =
6 8 8 8 7 5 2 1 1 3
Sq =
Columns 1 through 17
1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0
Columns 18 through 30
0 0 0 1 0 0 0 0 0 0 0 1 0
No comments:
Post a Comment