%Generate Gold Sequences of length 31 by taking the modulo-2 sum of two
%m-sequence generators...............
MYBER=[];
clear
clc
G=93; % Code length
x=[];
%...............Generation of first perferred PN sequence............
sd1 =[0 0 0 0 1]; % Initial State of Register.
PS1=[];
for j=1:G
PS1=[PS1 sd1(5)];
if sd1(1)==sd1(4)
temp1=0;
else temp1=1;
end
sd1(1)=sd1(2);
sd1(2)=sd1(3);
sd1(3)=sd1(4);
sd1(4)=sd1(5);
sd1(5)=temp1;
end
x=[x PS1];
%.................Generation of Second Preferred sequnces..............
PS2=[];
PS2(1)=x(1);
for i=1:G/3-1
j=(3*i)+1;
PS2(i+1)=x(j);
end
PS2=[PS2];
%.................Shifting and Storing of PS1 in Matrix 'y'............
for k=1:G/3
for j=1:G/3
y(k,j)=x(j+k-1);
end
end
%..................Generation of Gold Sequences........................
Gold_Seq(1,:)=[PS1(1,(1:G/3))];
Gold_Seq(2,:)=[PS2];
for i=1 :1:G/3
Gold_Seq(i+2,:)=xor(PS2,y(i,(1:G/3)));
end
for j=1:G/3+2
for(i=1:1:G/3)
if(Gold_Seq(j,i)==1)
Gold_Seq(j,i)=1;
else
Gold_Seq(j,i)=-1;
end
end
subplot(11,3,j)
stem(Gold_Seq(j,:))
axis([1 32 0 1.5])
end
%% Transmitter Part
br=[];
usr=[5];
SNR=[-5 0 5 10 15];
for(pp=1:1:length(SNR))
snr=SNR(pp);
for(tt=1:1:length(usr))
K=usr(tt);
nbits=512;
A=randsrc(1,nbits);
for(i=1:1:K)
%
msg(i,:)=A;
end
%% Enhance the Message Space
newMsg=[];
g=31;
for(i=1:1:K)
A=msg(i,:);
B=[];
for(k=1:1:length(A))
x=A(k);
for(j=1:1:g)
B=[B x];
end
end
newMsg(i,:)=B;
end
%% Now Spreading the Message with GOLD Codes
for i=1:1:K
sp=[];
for j=1:g:nbits*g
n=1;
for k=j:1:j+g-1
SPREAD(i,k)=xor(newMsg(i,k),Gold_Seq(i,n));
n=n+1;
end
end
end
%% CHANNEL PART( Mixing)
channel=[];
for(j=1:1:g*nbits)
sm=SPREAD(1,j);
for(p=2:1:K)
%sm=XOR(sm,SPREAD(p,j) );
sm=sm+SPREAD(p,j) ;
end
channel(j)=sm;
if(channel(j)<=0)
channel(j)=-1;
else
channel(j)=1;
end
end
%% Noise adding
channel=awgn(channel,snr);
for(m=1:1:length(channel))
if(channel(m)<=0)
channel(m)=-1;
else
channel(m)=1;
end
end
%% decoding
for(i=1:1:K)
sp=[];
p=1;
for(j=1:g:nbits*g)
n=1;
sm=0;
for(k=j:1:j+g-1)
% DESPREAD(i,k)=XOR(SPREAD(i,k),Gold_Seq(i,n));
DESPREAD(i,n)=xor(channel(k),Gold_Seq(i,n));
sm=sm+DESPREAD(i,n);
n=n+1;
end
if(sm<=0)
decMsg(i,p)=-1;
else
decMsg(i,p)=1;
end
p=p+1;
end
end
%% BER Checking
err=0;
for(i=1:1:K)
for(j=1:1:nbits)
if(msg(i,j)~=decMsg(i,j))
err=err+1;
end
end
end
clc;
BER=err/(nbits*K)
if(BER==0)
BER=.00001
end
br=[br BER];
end
%br=[ ]
end
semilogy(SNR,br),grid on, hold on