Showing posts with label QPSK and MFSK in Matlab. Show all posts
Showing posts with label QPSK and MFSK in Matlab. Show all posts

Thursday, 22 May 2014

Simulation of line coding scheme in Matlab


Manchester Coding Scheme:
Program :
data=[ 1 0 1 0 1 0  ];

i=1:length(data);
j=1.49:length(data)+0.49;
I=1.50:length(data)+0.50;
J=1.99:length(data)+0.99;
N=length(data);
time=[];
for k=1:length(data);
time=[time i(k),j(k),I(k),J(k)];
end

signal=[];
a=1;
prebit=1;

for t=1:length(data);
  
if(data(t)==0)
    signal(a:a+1)=1;
    signal(a+2:a+3)=-1;
      
elseif(data(t)==1)
    signal(a:a+1)=-1;
    signal(a+2:a+3)=1;
    
   
end
   
a=a+4;

end

plot(time,signal,'linewidth',4);
xlabel('time')
ylabel('signal')





Simulation Results









Differential Manchester Coding Scheme :
Program :
data=[ 1 0 1 0 1 0  ];

i=1:length(data);
j=1.49:length(data)+0.49;
I=1.50:length(data)+0.50;
J=1.99:length(data)+0.99;
N=length(data);
time=[];
for k=1:length(data);
time=[time i(k),j(k),I(k),J(k)]; %Time vector
end

signal=[];
a=1;
prebit=1;

%Signal Generation
for t=1:length(data);
    if(data(t)==0 && prebit==1)
        signal(a:a+1)=-1;
        signal(a+2:a+3)=1;
        prebit=1;
    elseif(data(t)==0 && prebit==-1)
        signal(a:a+1)=1;
        signal(a+2:a+3)=-1;
        prebit=-1;
    elseif(data(t)==1 && prebit==-1)
        signal(a:a+1)=-1;
        signal(a+2:a+3)=1;
        prebit=1;
    elseif(data(t)==1 && prebit==1) 
        signal(a:a+1)=1;
        signal(a+2:a+3)=-1;
        prebit=-1;

end
           a=a+4;
end
plot(time,signal,'linewidth',4); %Plot signal
title('Differential Manchester');
xlabel('Time');
ylabel('Differential Manchester');

%constellation diagram
   M = 4;
x = [0:M-1];
scatterplot(modulate(modem.pskmod(M),x));

Simulation Results :


  

Implementing  BPSK , QPSK and MFSK in Matlab

Program Code For BPSK 
clc;
clear all;
message=[1 1 0 0 1 0];
f_one=1.5;
fs=1000;% frequency
t=[0:1/fs:1];
A=5;
x=A*cos(2*pi*f_one*t+pi); %binary 0
y=A*cos(2*pi*f_one*t); %binary 1
time=t;
for i=1:length(message)-1;
    time=[time i+t];
end
%ask signal logic
ask_signal=[];
for i=1:length(message);
   if(message(i)==0)
       ask_signal=[ask_signal x];
   else
       ask_signal=[ask_signal y];
   end
end
   
plot(time,ask_signal,'linewidth',2);
xlabel('no of bits per second')
ylabel('signal value')

%constellation diagram BPSK
M = 2;
x = [0:M-1];
scatterplot(modulate(modem.pskmod(M),x));

Simulation :



  


Programme Code For MFSK :
clc; %clears the command window
clear all; %clears the workspace
message=[1 1 0 0 1 0 1 0 0]; %binary input is
                                                 
if (rem(length(message),3)~=0)     %for don’t care bits
    display('Error in Message'); 
end
f_one=1;
fs=1000; %frequency
t=[0:2/fs:1];
A=5;
w=A*cos(2*pi*f_one*t); %binary 000   (1 Hz)
x=A*cos(4*pi*f_one*t); %binary 001   (2 Hz)
y=A*cos(6*pi*f_one*t); %binary 010   (3 Hz)
z=A*cos(8*pi*f_one*t); %binary 011   (4 hz)
W=A*cos(10*pi*f_one*t);%binary 100   (5 Hz)
X=A*cos(12*pi*f_one*t);%binary 101   (6 Hz)
Y=A*cos(14*pi*f_one*t);%binary 110   (7 Hz)
Z=A*cos(16*pi*f_one*t);%binary 111   (8 Hz)
time=t;
for i=1:(length(message)/3)-1;
    time=[time i+t];
end
%ask signal logic
ask_signal=[];
for i=1:3:length(message);
   if(message(i)==0 && message(i+1)==0 && message(i+2)==0)
       ask_signal=[ask_signal y];
   elseif(message(i)==0 && message(i+1)==0 && message(i+2)==1)
       ask_signal=[ask_signal z];
   elseif(message(i)==0 && message(i+1)==1 && message(i+2)==0)
       ask_signal=[ask_signal x];
   elseif(message(i)==0 && message(i+1)==1 && message(i+2)==1)
       ask_signal=[ask_signal w];
   elseif(message(i)==1 && message(i+1)==0 && message(i+2)==0)
       ask_signal=[ask_signal W];
   elseif(message(i)==1 && message(i+1)==0 && message(i+2)==1)
       ask_signal=[ask_signal X];
   elseif(message(i)==1 && message(i+1)==1 && message(i+2)==0)
       ask_signal=[ask_signal Y];
   elseif(message(i)==1 && message(i+1)==1 && message(i+2)==1)
       ask_signal=[ask_signal Z];
   end
end
   %figure
   plot(time,ask_signal,'linewidth',2);
   xlabel('no of bits per second');
   ylabel('signal value');
    
% constellation diagram
inphase = [1 1 1 1 1 1 1 1];
quadr = [0 0 0 0 0 0 0 0];
 const = inphase + j*quadr;
scatterplot(const,1,0,'*');
axis([-3 3 -3 3]); 

Simulation :
  




Programme Code For QPSK :

clc; %clears the command window
clear all; %clears the workspace
message=[1 1 0 0 1 0]; %binary input is
                                                 
if (rem(length(message),2)~=0)     %for don’t care bits
    message(1,length(message)+1)=1; 
end
f_one=1.5;
fs=1000; %frequency
t=[0:2/fs:1];
A=5;
w=A*cos(2*pi*f_one*t+pi/4);%binary 11
x=A*cos(2*pi*f_one*t+3*pi/4); %binary 01
y=A*cos(2*pi*f_one*t-3*pi/4); %binary 00
z=A*cos(2*pi*f_one*t-pi/4); %binary 10
time=t;
for i=1:(length(message)/2)-1;
    time=[time i+t];
end
%ask signal logic
ask_signal=[];
for i=1:2:length(message);
   if(message(i)==0 && message(i+1)==0)
       ask_signal=[ask_signal y];
   elseif(message(i)==1 && message(i+1)==0)
       ask_signal=[ask_signal z];
   elseif(message(i)==0 && message(i+1)==1)
       ask_signal=[ask_signal x];
   elseif(message(i)==1 && message(i+1)==1)
       ask_signal=[ask_signal w];

end
end

%figure
plot(time,ask_signal,'linewidth',2);
xlabel('no of bits per second');
ylabel('signal value');
    
% constellation diagram
inphase = [1 -1 1 -1];
quadr = [1 1 -1 -1];
inphase = [inphase; inphase]; inphase = inphase(:);
quadr = [quadr; quadr]; quadr = quadr(:);
const = inphase + j*quadr;
scatterplot(const,1,0,'*');
     
axis([-3 3 -3 3]);


Simulation :