%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% BASE BAND Direct
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sequence Spread Spectrum using GOLD CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
close all;
b=round(rand(1,40));
pattern=[];
for k=1:40
if b(1,k)==0
sig=zeros(1,6);
else sig=ones(1,6);
end
pattern=[pattern sig];
end
b1=b;
message = b1
message1 = pattern;
subplot(3,2,1)
plot(pattern);
axis([-1 200 -.5 1.5]);
ylabel('\bf Original Bit Sequence');
title('\bf\it TRANSMITTED MESSAGE');
%--------------------------------------------------------------------------
% Generation of Gold code No. 1
G=20; % Code length
K=1; % Number of Codes or code sequences
%..............Generation of first PN sequence...............
lsr1 =randsrc(1,20,[0 1]);
PN1=[];
for j=1:G
PN1=[PN1 lsr1(1)];
temp1 = xor(lsr1(4),lsr1(1));
temp2 = xor(lsr1(6),temp1);
temp3 = xor(lsr1(7),temp2);
temp4 = xor(lsr1(13),temp3);
temp5 = xor(lsr1(19),temp4);
for i=1:G-1
lsr1(i)=lsr1(i+1); %----Shifting----
end
lsr1(20)=temp5;
end
%..............Generation of Second PN sequence..............
lsr2 =randsrc(1,20,[0 1]);
PN2=[];
for j=1:G
PN2=[PN2 lsr2(1)];
temp1 = xor(lsr2(2),lsr1(1));
temp2 = xor(lsr2(3),temp1);
temp3 = xor(lsr2(5),temp2);
temp4 = xor(lsr2(7),temp3);
temp5 = xor(lsr2(9),temp4);
temp6 = xor(lsr2(10),temp5);
temp7 = xor(lsr2(13),temp6);
temp8 = xor(lsr2(14),temp7);
temp9 = xor(lsr2(16),temp8);
temp10 = xor(lsr2(17),temp9);
temp11 = xor(lsr2(19),temp10);
for i=1:G-1
lsr2(i)=lsr2(i+1); %----Shifting----
end
lsr2(20)=temp11;
end
%..............Generation of Gold Codes....................
Code_Matrix=[];
for codes=1:K
code=[];
for j=1:G
code=[code xor(PN1(j),PN2(j))];
end
Code_Matrix=[Code_Matrix code'];
end
gold_1 = Code_Matrix';
Gold_Code = gold_1;
pattern=[];
for k=1:20
if gold_1(1,k)==0
sig=zeros(1,6);
else sig=ones(1,6);
end
pattern=[pattern sig];
end
gold=pattern;
subplot(3,2,3);
plot(pattern);
axis([-1 120 -.5 1.5]);
ylabel('\bf Gold Code');
%---------------------------------------
% b1 = Message Matrix (1,20)
% gold_1 = Gold Matrix (1,20)
%---------------------------------------
% Spreading the pattern with the gold code
k=1;
for i=1:20
for j=1:20
spread(1,k)=xor(b1(1,i),gold_1(1,j));
k=k+1;
end
end
spread;
subplot(3,2,5);
plot(spread);
axis([-1 400 -.5 1.5]);
ylabel('\bf Spreaded Sequence');
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------RECEIVER----------------------------
%--------------------------------------------------------------------------
subplot(3,2,2);
plot(spread);
axis([-1 400 -.5 1.5]);
ylabel('\bf Received Sequence');
title('\bf\it RECEIVED MESSAGE');
%------GOLD CODE sequence used-----
gold_2=gold_1;
pattern=[];
for k=1:20
if gold_2(1,k)==0
sig=zeros(1,6);
else sig=ones(1,6);
end
pattern=[pattern sig];
end
gold = pattern;
subplot(3,2,4);
plot(gold);
axis([-1 120 -.5 1.5]);
ylabel('\bf Gold Code');
%-------Demodulation to get original message signal---------
gold_2inv = xor(1,gold_2);
i=1;
k=1;
while k < 400
s=0;
for j=1:20
temp(1,j) = xor(spread(1,k),gold_2(1,j));
k=k+1;
s=s+temp(1,j);
end
if(s==0)
b2(1,i) = 0;
else
b2(1,i) = 1;
end
i=i+1;
end
despreaded_signal = b2
%-----Plotting Despreaded signal------
pattern=[];
for k=1:20
if b2(1,k)==0
sig=zeros(1,6);
else
sig=ones(1,6);
end
pattern=[pattern sig];
end
subplot(3,2,6);
plot(pattern);
axis([-1 120 -.5 1.5]);
ylabel('\bf Despreaded Sequence');
% Plotting the FFT of DSSS signal
% figure,plot([1:400],abs(fft(spread)),[1:20],abs(fft(b2)))