Pulse Code Modulation & Demodulation
clc;
clear all;
close all;
a=4;
fm=2;
fs=100*fm;
t=0:1/fs:1;
x=a*sin(2*pi*fm*t);
subplot(5,1,1);
plot(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('Original message signal');
subplot(5,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('Sampled message signal');
enc=[];
for(i=1:length(x))
if (x(i)>0 && x(i)<=1)
e=[1 0 0];
xq(i)=0.5;
elseif (x(i)>1 && x(i)<=2)
e=[1 0 1];
xq(i)=1.5;
elseif (x(i)>2 && x(i)<=3)
e=[1 1 0];
xq(i)=2.5;
elseif (x(i)>3 && x(i)<=4)
e=[1 1 1];
xq(i)=3.5;
elseif (x(i)>-4 && x(i)<=-3)
e=[0 0 0];
xq(i)=-3.5;
elseif (x(i)>-3 && x(i)<=-2)
e=[0 0 1];
xq(i)=-2.5;
elseif (x(i)>-2 && x(i)<=-1)
e=[0 1 0];
xq(i)=-1.5;
else (x(i)>-1 && x(i)<=0)
e=[0 1 1];
xq(i)=-0.5;
end
enc=[enc e];
end
subplot(5,1,3);
plot(t,xq,'b');
title('Quantised signla');
% decoding(Receiver section)
X_Q=[];
for i=1:3:length(enc)-2
if(enc(i)==0 && enc(i+1)==0 && enc(i+2)==0)
x_q=-3.5;
elseif(enc(i)==0 && enc(i+1)==0 && enc(i+2)==1)
x_q=-2.5;
elseif(enc(i)==0 && enc(i+1)==1 && enc(i+2)==0)
x_q=-1.5;
elseif(enc(i)==0 && enc(i+1)==1 && enc(i+2)==1)
x_q=-0.5;
elseif(enc(i)==1 && enc(i+1)==0 && enc(i+2)==0)
x_q=0.5;
elseif(enc(i)==1 && enc(i+1)==0 && enc(i+2)==1)
x_q=1.5;
elseif(enc(i)==1 && enc(i+1)==1 && enc(i+2)==0)
x_q=2.5;
elseif(enc(i)==1 && enc(i+1)==1 && enc(i+2)==1)
x_q=3.5;
end
X_Q=[X_Q x_q];
end
subplot(5,1,4);
plot(t,X_Q);
title('Decoded signal');
[num,den]=butter(6,4*fm/fs);
recon=filter(num,den,X_Q);
subplot(5,1,5);
plot(t,recon);
title('Reconstructed signal in the receiver');
Experiment – 2
Delta Modulation
Aim: Implementation of Delta Modulated signal
clc;
clear
close all;
a=2;
t=0:2*pi/50:2*pi;
5
x=a*sin(2*pi/10*t);
l=length(x);
plot(x,'r','linewidth',2);
delta=0.2;
hold on
xn=0;
for i=1:l
if x(i)>xn(i)
d(i)=1;
xn(i+1)=xn(i)+delta;
else
d(i)=0;
xn(i+1)=xn(i)-delta;
end
end
stairs(xn,'b','linewidth',2)
hold on
plot(xn,'g','linewidth',2);
xlabel('TIME');
ylabel('AMPLITUDE');
title('DELTA MODULATION','fontsize',18);
legend('input signal','staircase appproximaton','delta modulated signal');
grid on
case 2(slopeoverload distortion)
clc;
clear
close all;
a=2;
t=0:2*pi/50:2*pi;
x=a*sin(2*pi/5*t);
l=length(x);
6
plot(x,'r','linewidth',2);
delta=0.2;
hold on
xn=0;
for i=1:l
if x(i)>xn(i)
d(i)=1;
xn(i+1)=xn(i)+delta;
else
d(i)=0;
xn(i+1)=xn(i)-delta;
end
end
stairs(xn,'b','linewidth',2)
hold on
plot(xn,'g','linewidth',2);
xlabel('TIME');
ylabel('AMPLITUDE');
title('SLOPE OVERLOAD DISTORTION','fontsize',18);
legend('input signal','staircase appproximaton','slope overload
distortedsignal');
grid on
case3(granular noise)
clc;
clear
close all;
a=2;
t=0:2*pi/50:2*pi;
x=a*sin(2*pi/20*t);
l=length(x);
plot(x,'r','linewidth',2);
delta=0.23;
hold on
xn=0;
for i=1:l
if x(i)>xn(i)
d(i)=1;
xn(i+1)=xn(i)+delta;
else
d(i)=0;
xn(i+1)=xn(i)-delta;
end
end
stairs(xn,'b','linewidth',2)
hold on
plot(xn,'g','linewidth',2);
xlabel('TIME');
ylabel('AMPLITUDE');
title('GRANULAR NOISE','fontsize',18);
legend('input signal','staircase appproximation','delta modulated signal');
grid on;
Experiment – 3
Binary Phase Shift Keying
Aim: To generate and demodulate Binary phase shift keyed (BPSK) signal using MATLAB
clc;
clear all;
close all;
N=7;
n=randi([10],1,N);
n=[1 0 0 1 1 1 0];
for ii=1:length(n)
if n(ii)==1
nn(ii)=1;
else
nn(ii)=-1;
end
end
i=1;
t=0:0.0001:length(n);
for j=1:length(t)
if t(j)<=i
y(j)=nn(i);
else
i=i+1;
end
end
subplot(4,1,1);
plot(t,y);
axis([0,length(n) -2 2]);
title('message signal-polar form');
c1=cos(2*pi*2*t);
subplot(4,1,2);
plot(t,c1);
axis([0,length(n) -2 2])
title('carrier signal')
x=y.*c1;
subplot(4,1,3);
plot(t,x);
axis([0,length(n) -2 2])
xlabel('time');
ylabel('amplitude');
title('bpsk signal');
for j=1:length(t)
if x(j)==c1(j)
det(j)=1;
else
det(j)=-1;
end
end
subplot(4,1,4);
plot(t,det);
axis([0,length(n) -2 2])
xlabel('time');
ylabel('amplitude');
title('demodulated signal');
Experiment – 4
Binary Frequency Shift Keying
Aim: To generate and demodulate Binary Frequency shift keyed (BFSK) signal using
clc;
clear all;
close all;
%NRZ polar line coding
%input seqeunce
N=10;
n=randi([0 1] ,1,N);
%polar mapping
for ii=1:length(n)
if n(ii)==1;
nn(ii)=1
else
nn(ii)=-1;
end
end
%pulse shaping
i=1
t=0:0.01:length(n);
for j=1:length(t)
if t(j)<=i
y(j)=nn(i);
else
i=i+1;
end
end
%plotting
figure(1)
subplot(2,1,1);
plot(t,y);
title('message signal');
%carrier signal
c=cos(2*pi*2*t);
figure(1)
subplot(2,1,2);
plot(t,c);
title('carrier signal');
%BPSK modulation
x=y.*c;
figure(2)
subplot(2,1,1);
plot(t,x);
title('BPSK signal');
%detection and construction
for j=1:length(t)
if x(j)==c(j)
det(j)=1;
else
det(j)=0;
end
end
figure(2)
subplot(2,1,2);
plot(t,det);
title('demodulation');
Experiment – 5
Differential Phase Shift Keying (DPSK)
Aim: To verify the bit error rate performance of DPSK Modulatioin using MATLAB
clear all;
close all;
clc;
fc=1;% carrier frequency;
samp=1000;
t=linspace(0,2*pi,samp);
ph1=cos(fc*t);
ph2=-cos(fc*t);
b=[ 1 0 0 0 0 0 0 1 1 1 1 1 0 1 0 1];
x=1;
nb(1)=not(xor(b(1),0));
for i=2:length(b)+1
nb(i)=not(xor(b(i-1),x));
x=nb(i);
end;
dpsk=[];bin1=[];bin2=[];for j=1:length(nb)
if nb(j)==0
dpsk=[dpsk,ph1];
bin1=[bin1,zeros(1,samp)];
elseif nb(j)==1
dpsk=[dpsk,ph2];
bin1=[bin1,ones(1,samp)];
end;%% end of if..
end;%% end of for
for k=1:length(b)
if b(k)==0
bin2=[bin2,zeros(1,samp)];
elseif b(k)==1
bin2=[bin2,ones(1,samp)];
end;
end;
subplot(311),plot(bin2,'k','LineWidth',3');
axis([0 samp*length(b) -0.2 1.2]);
xlabel('Time index'); ylabel('Amplitude');
title('The Binary Input','FontSize',12);
bn=num2str(b);
bx=['The Binary string is ',bn];
gtext(bx,'FontSize',12);
subplot(312),plot(bin1,'k','LineWidth',3');
axis([0 samp*length(b) -0.2 1.2]);
xlabel('Time index'); ylabel('Amplitude');
title('The Binary (differential) Input','FontSize',12);
bn=num2str(nb);
bx=['The Differential Binary string is ',bn];
gtext(bx,'FontSize',12);
subplot(313),plot(dpsk,'k','LineWidth',3');
axis([0 samp*length(b) -1.2 1.2]);
xlabel('Time index'); ylabel('Amplitude');
title('The Simulated DPSK output','FontSize',12);