📄 mc_cs_cdma.m
字号:
clc;
display ('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
display ('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
display ('%%% %%%')
display ('%%% Conventional MC CS-CDMA system Simulation %%%')
display ('%%% %%%')
display ('%%% by: Anvar M. Narzullaev %%%')
display ('%%% Mobile Communication Lab., Yeungnam University %%%')
display ('%%% copyright, 2006 %%%')
display ('%%% %%%')
display ('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
display ('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
clear;
%====================================%
% S Y S T E M P A R A M E T E R S %
%====================================%
for cs_bits = [1 2 3 4 5]
para = 60; % Number of parallel channel
%cs_bits = 1; % Number of Code selecting bits
fftlen = para/(cs_bits+1); % FFT length
gilen = 0; % Length of guard interval (points)
ml = 1; % Modulation Level 1-BPSK
sr = 256000.0; % symbol rate
br = sr * ml; % bit rate
nd = 100; % number of symbol
code_table=((dec2bin((0:(2^cs_bits)-1)')*2)-97);
if cs_bits == 1
sf = 31;
elseif cs_bits == 2
sf = 63
elseif cs_bits == 3
sf = 127
elseif cs_bits == 4
sf = 255
elseif cs_bits == 5
sf = 511
end
pn_code = pngen(sf,1);
spr_code = makeset(pn_code,2^cs_bits,sf);
%spr_code = hadamard(sf);
for ebn0 = 0:2:20
rfade = 1; % Rayleigh fading 0:nothing 1:consider
now1 = 2; % number of directwave + delayed wave (2 or 6)
tstp = 1 / (sr * para * 511); % time resolution
fd = 200; % doppler frequency [Hz]
flat = 1; % flat Rayleigh environment
itndel = nd * 300; % number of fading counter to skip
if now1 == 2 %2 path rayleigh fading
itau = [0, 2]; % delay time
dlvl1 = [0.1, 5.0]; % attenuation level
n0 = [6, 7]; % number of waves to generate fading
th1 = [0.0, 0.0]; % initial Phase of delayed wave
itnd1 = [3001, 4004]; % set fading counter
elseif now1 == 6 % 6 path rayleigh fading
itau = [0, 1, 2, 3, 4, 5]; % delay time
dlvl1 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]; % attenuation level
n0 = [6, 7, 6, 7, 6, 7]; % number of waves to generate fading
th1 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; % initial Phase of delayed wave
itnd1 = [1000, 2000, 3001, 4004, 5005, 6006]; % set fading counter
end
nloop=100; % Number of simulation loops
noe = 0; % Number of error data
nod = 0; % Number of transmitted data
for iii = 1:nloop
seridata = rand(1,para*nd*ml)>0.5; % Serial data
seridata2 = seridata*2-1; % BPSK
paradata = reshape(seridata2, para, nd*ml); % S-to-P convert
%%===============================================%%
%%== Select bits for spreading (each 6 bit) %%
%%===============================================%%
m=1;
for n = cs_bits+1:cs_bits+1:para % select each 6 bits for spreading
sp_bit(m,:) = paradata(n,:);
m=m+1;
end
%%************************************************%%
%%== END of Select bits for spreading (each 6 bit)%%
%%************************************************%%
%%===============================================%%
%%== Spreading Code Selection proccess %%
%%===============================================%%
for n = 1:nd*ml
l=1;
for m = 1:cs_bits+1:para
selected(l,n) = code_select(paradata(m:m+cs_bits-1,n)', code_table, cs_bits);
l=l+1;
end
end
jj = 1;
for m = 1:ml*nd
for n = 1:fftlen
code(n,jj:jj+sf-1) = spr_code(selected(n,m),:);
end
jj=jj+sf;
end
%%***********************************************%%
%%== END of Spreading Code Selection proccess %%
%%***********************************************%%
%%===============================================%%
%%== Spreading %%
%%===============================================%%
jj=1;
for m=1:ml*nd
spread_data(:,jj:jj+sf-1) = ...
spread(sp_bit(:,m), code(:,jj:jj+sf-1));
jj=jj+sf;
end
%%***********************************************%%
%%== END of Spreading %%
%%***********************************************%%
%%===============================================%%
%%== IFFT %%
%%===============================================%%
x = ifft(spread_data);
ich = real(x);
qch = imag(x);
%%***********************************************%%
%%== END of IFFT %%
%%***********************************************%%
%%===============================================%%
%% Guard Interval insertion & sum all users data%%
%%===============================================%%
ll = length(ich);
fftlen2 = fftlen+gilen;
[ich1,qch1] = giins(ich,qch,fftlen,gilen,ll);
%%***********************************************%%
%%== END of Guard interval insertion %%
%%***********************************************%%
%%***********************************************%%
%%== Fading channel %%
%%***********************************************%%
if rfade == 0
ich2 = ich1;
qch2 = qch1;
else
[ich2,qch2] = sefade(ich1,qch1,itau,dlvl1,th1,n0,itnd1, ... % fading channel
now1,length(ich1),tstp,fd,flat);
itnd1 = itnd1 + itndel;
end
%%***********************************************%%
%%== END of Fading channel %%
%%***********************************************%%
%%================ AWGN
spow = sum(rot90(ich1.^2 + qch1.^2)) / nd/ para; % attenuation Calculation
attn = sqrt(0.5 * spow * sr / br * 10^(-ebn0/10)); %attenuation calculation
[ich2, qch2]=comb(ich2,qch2,attn);
%%================ End of AWGN
%%================ Guard interval removal
[ich3,qch3]= girem(ich2,qch2,fftlen2,gilen,ll);
%%================ Guard interval removal
%%================ FFT
clear i;
rx = ich3 + qch3.*i;
ry = fft(rx);
ich4=real(ry);
%%================ End of FFT
%%===============================================%%
%% Correlation (select despread codes) %%
%%===============================================%%
for c = 1:fftlen
kk=1;
for n = 1:nd*ml
temp(c,n) = 0;
s1 = 0;
for m = 1:2^cs_bits
s2 = abs(sum(ich4(c,kk:kk+sf-1).*spr_code(m,:)));
if s2>s1
s1 = s2;
temp(c,n) = m;
end
end
kk=kk+sf;
end
end
jj = 1;
for m = 1:ml*nd
for n = 1:fftlen
despread_code(n,jj:jj+sf-1) = spr_code(temp(n,m),:);
end
jj=jj+sf;
end
%%===============================================%%
%% END of Correlation (select despread codes) %%
%%===============================================%%
%%===============================================%%
%%== DEspreading %%
%%===============================================%%
jj=1;
for m=1:ml*nd
despread_data(:,m) = ...
despread(ich4(:,jj:jj+sf-1), despread_code(:,jj:jj+sf-1));
jj=jj+sf;
end
%%***********************************************%%
%%== END of DEspreading %%
%%***********************************************%%
l=1;
orig_data = zeros(size(paradata));
for m = cs_bits+1:cs_bits+1:para
orig_data(m,:) = despread_data(l,:);
l=l+1;
end
l=1;
for n = 1:nd*ml
l=1;
for m = 1:cs_bits+1:para
orig_data(m:m+cs_bits-1,n) = code_table(temp(l,n),:)';
l=l+1;
end
end
demodata=orig_data>0;
demodata2 = reshape(demodata,1,para*nd*ml);% P-to-S
%========= BER calc. ==================%
for n = 1:length(seridata)
if seridata(n) ~= demodata2(n)
noe = noe + 1;
end
end
%========= end of BER calc. ===========%
end
ber = noe/(nloop*length(seridata));
fprintf('\n%d\t%e\t%d\n',ebn0,ber,cs_bits);
if rfade == 0
fid = fopen('mc_cs_cdma_awgn.dat','a');
fprintf(fid,'%d\t%e\t%d\n',ebn0,ber,cs_bits); % fprintf: built in function
fclose(fid);
end
if rfade == 1 && now1 == 2
fid = fopen('mc_cs_cdma_1path.dat','a');
fprintf(fid,'%d\t%e\t%d\n',ebn0,ber,cs_bits); % fprintf: built in function
fclose(fid);
end
if rfade == 1 && now1 == 6
fid = fopen('mc_cs_cdma_6path.dat','a');
fprintf(fid,'%d\t%e\t%d\n',ebn0,ber,cs_bits); % fprintf: built in function
fclose(fid);
end
end
clear;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -