%
% Simulation program to realize DS-CDMA system
% DSCDMA.m
%
%**************************Preparation part*****************************
sr = 256000.0; % symbol rate
ml = 2; % number of modulation levels
br = sr*ml; % bit rate
nd = 100; % number of symbol
ebn0 = 3; % Eb/N0
%*************************filter initialization*************************
irfn = 21; % number of filter taps
IPOINT = 8; % number of oversample
alfs=0.5; %roll off factor
[xh] =hrollcoef(irfn,IPOINT,sr,alfs,1); %T FILTER
[xh2] =hrollcoef(irfn,IPOINT,sr,alfs,0); %R FILTER
%************************Spreading code initialization******************
user = 1; % number of users
seq=1; %1:M
stage = 3; % number of stages
ptap1 = [1 3]; % position of taps
ptap2 = [2 3]; %
regi1 = [1 1 1]; % initial value of register
regil2 = [1 1 1];
%*********************** Generation of the spreading code***************
% M-sequence
switch seq
case 1
code = mseq(stage,ptap1,regi1,user);
case 2
m1 = mseq(stage,ptap1,regi1);
m2 = mseq(stage,ptap2,regi2);
code=goldseq(m1,m2,user);
case 3
m1 = mseq(stage,ptap1,regi1);
m2 = mseq(stage,ptap2,regi2);
code=[goldseq(m1,m2,user),zeros(user,1)];
end
code = code*2-1;
clen = length(code);
%*********************Fading initialization*********%
rfade = 0;
itau = [0,8];
dlvl1 = [0.0,40.0];
n0 = [6,7];
th1 = [0.0,0.0];
itnd1 = [3001,4004];
now1 = 2;
tstp=1/sr/IPOINT/clen;
fd = 160;
flat = 1;
itnde1=nd*IPOINT*clen*30;
%*************************START Calculation******************************
nloop = 1000; % simulation number of times
noe = 0;
nod = 0;
for ii=1:nloop
%*************************Tranmitter*********************************
data = rand(user,nd*ml)>0.5;
[ich,qch] = qpskmod(data,user,nd,ml); % QPSK modulation
[ich1,qch1] = spread(ich,qch,code); % spreading
[ich2,qch2] = compoversamp2(ich1,qch1,IPOINT);
[ich3,qch3] = compconv2(ich2,qch2,xh);
if user == 1
ich4=ich3;
qch4=qch3;
else
ich4=sum(ich3);
qch4=sum(qch3);
end
%*************fading channel********%
if rfade==0
ich5=ich4;
qch5=qch4;
else
[ich5,qch5]=sefade(ich4,qch4,itau,dlvl1,th1,no,itnd1,now1,length(ich4),tstp,fd,flat);
itnd1=itnd1+itnde1;
end
%*********receiver*************%
spow=sum(rot90(ich3.^2+qch3.^2))/nd;
attn=sqrt(0.5*spow*sr/br*10^(-ebn0/10));
[ich6,qch6]=comb2(ich5,qch5,attn);
[ich7,qch7]=compconv2(ich6,qch6,xh2);
sampl=irfn*IPOINT+1;
ich8=ich7(:,sampl:IPOINT:IPOINT*nd*clen+sampl-1);
qch8=qch7(:,sampl:IPOINT:IPOINT*nd*clen+sampl-1);
[ich9 qch9]=despread(ich8,qch8,code);
demodata=qpskdemod(ich9,qch9,user,nd,ml);
%**************bit error rate***********%
noe2 = sum(sum(abs(data-demodata)));
nod2=user*nd*ml;
noe=noe+noe2;
nod=nod+nod2;
fprintf('%d\t%e\n',ii,noe2/nod2);
end
%**********data file***********%
ber=noe/nod;
fprintf('%d\t%d\t%d\t%e\n',ebn0,noe,nod,noe/nod);
fid=fopen('BER.dat','a');
fprintf(fid,'%d\t%e\t%f\t%f\t\n',ebn0,noe/nod,noe,nod);
fclose(fid);