comm lab

13
clc; clear all; close all; fs=20; t=0:1/fs:1; fc1=5; c1=sin(2*pi*fc1*t); c2=0; x=[1 0 1 0 1 0 1 ]; for i=1:size(x,2) if (x(i)==1) ask_mod((i-1)*length(t)+1:i*length(t))=c1; elseif (x(i)==0) ask_mod((i-1)*length(t)+1:i*length(t))=c2; end end for i=1:size(x,2) x_corr1(i)=sum(c1.*ask_mod((i-1)*length(t)+1:i*length(t))); x_corr2(i)=sum(c2.*ask_mod((i-1)*length(t)+1:i*length(t))); if(x_corr1(i)>x_corr2(i)) demod_x(i)=1; else demod_x(i)=0; end end subplot(2,2,1); stem(x); subplot(2,2,2); plot(ask_mod); subplot(2,2,3); stem(demod_x);

Transcript of comm lab

Page 1: comm lab

clc; clear all; close all; fs=20; t=0:1/fs:1; fc1=5; c1=sin(2*pi*fc1*t); c2=0; x=[1 0 1 0 1 0 1 ]; for i=1:size(x,2) if (x(i)==1) ask_mod((i-1)*length(t)+1:i*length(t))=c1; elseif (x(i)==0) ask_mod((i-1)*length(t)+1:i*length(t))=c2; end end for i=1:size(x,2) x_corr1(i)=sum(c1.*ask_mod((i-1)*length(t)+1:i*length(t))); x_corr2(i)=sum(c2.*ask_mod((i-1)*length(t)+1:i*length(t))); if(x_corr1(i)>x_corr2(i)) demod_x(i)=1; else demod_x(i)=0; end end subplot(2,2,1); stem(x); subplot(2,2,2); plot(ask_mod); subplot(2,2,3); stem(demod_x);

Page 2: comm lab
Page 3: comm lab

clc; clear all; close all; t=0:.2/2500:.2; m=0.5.*sin(100.*pi.*t); mr=zeros(1,length(m)); m1=zeros(1,length(m)); e=zeros(1,length(m)); f=zeros(1,length(m)); s=zeros(1,length(m)); r1=zeros(1,length(m)); del=.0001; for i=1:50 e=m-mr; for j=1:length(e) if e(j)>0 f(j)=1; else f(j)=-1; end end s=del.*f; for j=1:length(m) for k=1:j mr(j)=mr(j)+s(k); end end i=i+1; end subplot(4,1,1); plot(t,m); subplot(4,1,2); plot(t,s); for j=1:length(m) for k=1:j r1(j)=r1(j)+s(k); end end subplot(4,1,3); plot(t,r1); for j=2:length(m); m1(j)=(m(j)+m(j-1))\2; end subplot(4,1,4); plot(t,m1);

Page 4: comm lab

clc; clear all; close all; t=0:.2/2500:.2; m=0.5.*sin(100.*pi.*t); mr=zeros(1,length(m)); m1=zeros(1,length(m)); e=zeros(1,length(m)); f=zeros(1,length(m)); s=zeros(1,length(m)); r1=zeros(1,length(m)); del=.0001; for i=1:50 e=m-mr; for j=1:length(e) if e(j)>0 f(j)=1; else f(j)=-1; end end s=del.*f; for j=1:length(m) for k=1:j mr(j)=mr(j)+s(k); end end i=i+1; end subplot(4,1,1); plot(t,m); subplot(4,1,2); plot(t,s); for j=1:length(m) for k=1:j r1(j)=r1(j)+s(k); end end subplot(4,1,3); plot(t,r1); for j=2:length(m); m1(j)=(m(j)+m(j-1))\2; end subplot(4,1,4); plot(t,m1);

Page 5: comm lab

0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-0.5

0

0.5

0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-2

0

2x 10

-4

0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-0.01

0

0.01

0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-200

0

200

Page 6: comm lab

clc; clear all; close all; fs=20; t=0:1/fs:1; fc1=5; fc2=2; c1=sin(2*pi*fc1*t); c2=sin(2*pi*fc2*t); x=[1 0 1 0 1 0 1 ]; for i=1:size(x,2) if (x(i)==1) Fsk_mod((i-1)*length(t)+1:i*length(t))=c1; elseif (x(i)==0) Fsk_mod((i-1)*length(t)+1:i*length(t))=c2; end end for i=1:size(x,2) x_corr1(i)=sum(c1.*Fsk_mod((i-1)*length(t)+1:i*length(t))); x_corr2(i)=sum(c2.*Fsk_mod((i-1)*length(t)+1:i*length(t))); if(x_corr1(i)>x_corr2(i)) demod_x(i)=1; else demod_x(i)=0; end end subplot(2,2,3); plot(Fsk_mod); subplot(2,2,4); stem(demod_x);

Page 7: comm lab

3

2

Page 8: comm lab

%PSK MODULATION %GENERATE CARRIER SIGNAL tb=1; t=0:tb/100:1; fc=2; c=sqrt(2/tb)*sin(2*pi*fc*t); %generate message signal N=8; m=rand(1,N); t1=0;t2=tb; for i=1:N t=[t1:0.01:t2]; if m(i)>0.5 m(i)=1; m_s=ones(1,length(t)); else m(i)=0; m_s=-1*ones(1,length(t)); end message(i,:)=m_s; %product of carrier and message signal bpsk_sig(i,:)=c.*m_s; %plot the message and bpsk modulated signal subplot(5,1,2);axis([0 N -2 2]); plot(t,message(i,:),'r'); title('message signal(POLAR FORM)'); xlabel('t---->'); ylabel('m(t)'); grid on hold on t1=t1+1.01; t2=t2+1.01; end hold off %PLOT THE INPUT BINARY DATA AND CARRIER SIGNAL subplot(5,1,1);stem(m); title('binary data bits'); xlabel('n--->'); ylabel('b(n)'); grid on subplot(5,1,3); plot(t,c); title('carrier signal'); xlabel('t--->'); ylabel('c(t)'); grid on subplot(5,1,4); plot(t,bpsk_sig(i,:)); title('PSK Signal'); xlabel('t--->'); ylabel('s(t)'); grid on %PSK DEMODULATION t1=0; t2=tb; for i=1:N t=[t1:0.01:t2]

Page 9: comm lab

%CORRELATOR x=sum(c.*bpsk_sig(i,:)); %DECISION DEVICE if x>0 demod(i)=1; else demod(i)=0; end t1=t1+1.01; t2=t2+1.01; end %PLOT THE DEMODULATED DATA BITS subplot(5,1,5); stem (demod); title('demodulated signal'); xlabel('n--->'); ylabel('b(n)'); grid on subplot(5,1,4); plot(t,bpsk_sig(i,:)); title('PSK Signal'); xlabel('t--->'); ylabel('s(t)'); grid on

Page 10: comm lab

clc; clear all; close all; fs=20; t=0:1/fs:1; fc1=5; fc2=2; c1=sin(2*pi*fc1*t); c2=-sin(2*pi*fc2*t); x=[1 0 1 0 1 0 1 ]; for i=1:size(x,2) if (x(i)==1) Fsk_mod((i-1)*length(t)+1:i*length(t))=c1; elseif (x(i)==0) Fsk_mod((i-1)*length(t)+1:i*length(t))=c2; end end for i=1:size(x,2) x_corr1(i)=sum(c1.*Fsk_mod((i-1)*length(t)+1:i*length(t))); x_corr2(i)=sum(c2.*Fsk_mod((i-1)*length(t)+1:i*length(t))); if(x_corr1(i)>x_corr2(i)) demod_x(i)=1; else demod_x(i)=0; end end subplot(2,2,3); plot(Fsk_mod); subplot(2,2,4); stem(demod_x);

Page 11: comm lab
Page 12: comm lab

%QPSK MODULATIOM-1 clc; clear all; close all; %GENERATE QUADRATURE CARRIER SIGNAL Tb=1; t=0:(Tb/100):Tb;fc=1; c1=sqrt(2/Tb)*cos(2*pi*fc*t); c2=sqrt(2/Tb)*sin(2*pi*fc*t); %GENERATE MESSAGE SIGNAL N=8; m=rand(1,N); t1=0;t2=Tb for i=1:2:(N-1) t=[t1:(Tb/100):t2] if m(i)>0.5 m(i)=1; m_s=ones(1,length(t)); else m(i)=0; m_s=-1*ones(1,length(t)); end % ODD BIT MODULATION SIGNAL odd_sig(i,:)=c1.*m_s; if m(i+1)>0.5 m(i+1)=1; m_s=ones(1,length(t)); else m(i+1)=0; m_s=-1*ones(1,length(t)); end % EVEN BITS MODULATED SIGNAL even_sig(i,:)=c2.*m_s; % qpsk signal qpsk=odd_sig+even_sig; % PLOT THE QPSK MODULATED SIGNAL subplot (3,2,4);plot(t,qpsk(i,:)); title('qpsk signal');xlabel('t--->');ylabel('s(t)');grid on; hold on t1=t1+(Tb+.01); t2=t2+(Tb+.01); end hold off % PLOT THE BINARY DATA BITS AND CARRIER SIGNAL subplot (3,2,1);stem(m); title('binary data bits');xlabel('n--->');ylabel('b(n)');grid on; subplot(3,2,2);plot(t,c1); title('carrier signal-1');xlabel('t--->');ylabel('c1(t)');grid on; subplot(3,2,3);plot(t,c2); title('carrier signal-2');xlabel('t--->');ylabel('c2(t)');grid on; % QPSK DEMODULATION t1=0;t2=Tb; for i=1:N-1 t=[t1:(Tb/100):t2] % CORRELATOR x1=sum(c1.*qpsk(i,:)); x2=sum(c2.*qpsk(i,:)); % DECISION DEVICE if (x1>0 && x2>0)

Page 13: comm lab

demod(i)=1; demod(i+1)=1; elseif(x1>0 && x2<0) demod(i)=1; demod(i+1)=0; elseif(x1<0 && x2<0) demod(i)=0; demod(i+1)=0; elseif(x1<0 && x2>0) demod(i)=0; demod(i+1)=1; end t1=t1+(Tb+.01); t2=t2+(Tb+.01); end % PLOTTING THE DEMODULATED BINARY DATA subplot(3,2,5); stem(m); title('qpsk demodulated bits');xlabel('n--->');ylabel('b(n)');grid on;

0 2 4 6-2

0

2qpsk signal

t--->

s(t)

0 2 4 6 80

0.5

1binary data bits

n--->

b(n)

3 3.5 4 4.5-2

0

2carrier signal-1

t--->

c1(t)

3 3.5 4 4.5-2

0

2carrier signal-2

t--->

c2(t)

0 2 4 6 80

0.5

1qpsk demodulated bits

n--->

b(n)