⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_mc_cdma.asv

📁 mc-cdma matlab下的性能仿真
💻 ASV
字号:
clear
% function ber = mc_cdma(snr,num_user)
%***********************************************************
% SS_MC_MA.m
%
% Function to perform SS_MC_MA through multipath channel
%
% 
%***********************************************************

N = 256;           % number of symbols in a single OFDM symbol
GI = 40;           % guard interval
Mt = 1;             % number of Tx antennas
Mr = 1;             % number of Rx antennas
M = 8;              % max constellation bit number
num_subc = 8;        % number of subcarriers
num_user = 2;       % number of users
num_data = 16000;
mod_level = 2;
spreadLength=8;

snr=0;
en = 10^(snr/10);
sigma = 1/sqrt(2*en);



cSpread=[1 1 1 1 1 1 1 1;1 -1 1 -1 1 -1 1 -1;...
        1 1 -1 -1 1 1 -1 -1;1 -1 -1 1 1 -1 -1 1;...
        1 1 1 1 -1 -1 -1 -1;1 -1 1 -1 -1 1 -1 1;...
        1 1 -1 -1 -1 -1 1 1;1 -1 -1 1 -1 1 1 -1];



%% GSM TU channel: six rays
%% power    dB----->value     delay(us)
%%          -3.0	0.1897	    0        
%%           0		0.3785      0.2     
%%          -2.0	0.2388    	0.5       
%%          -6.0	0.0951    	1.6        
%%          -8.0	0.0600   	2.3        
%%          -10.0	0.0379   	5.0        

multipath = [sqrt(0.1897)  0  sqrt(0.3785) 0 0 sqrt(0.2388) 0 0 0  0 sqrt(0.0951) 0 0 0 0 sqrt(0.06) 0 0 0 0 0 0 sqrt(0.0379)]; %% power
multipath_channel0 = zeros(1,length(multipath));



signal_tx = zeros(num_data*spreadLength/N,N + GI);
for loop_user=1:num_user
    
% generate bits

msg = randint(num_data,1);



% channel encoder
code1 = [];
trel = poly2trellis(6,[53 75]);
code1 = [code1 convenc(msg,trel)];
code = code1;


if loop_user == 1
    msg_user1 = msg;
    code_user1 = code1;
end


% modulate
num_data1 = 2*num_data;
[iout,qout] = qpsk(code,1,num_data1,mod_level);
inputData = iout+i*qout;

% spreading
[S] = mc_spreading(inputData,1,num_data,cSpread(loop_user,:),spreadLength);




% ifft, with cyclic prefix 
% ofdm_symbol = [];
% ofdm_symbol = ifft_tx(S,N,GI)*sqrt(N);
for m=1:num_data*spreadLength/N
    ofdm_symbol((m-1)*(N+GI)+1:m*(N+GI)) = ifft_cp_tx_blk(S((m-1)*N+1:m*N),N,GI)*sqrt(N);
end
    
% ofdm_symbol = ifft_tx_multiuserOpt(S,loop_user,num_data,N,GI)*sqrt(N);
% ofdm_symbol==ofdm_symbol1
% ofdm_symbol2 = reshape(ofdm_symbol,Mt*(N+GI),100);



for p = 1:(num_data*spreadLength/N)
        
% multipath channel
% y = zeros(1,N + GI);
 % rayleigh fading
% for p_user = 1:num_user
multipath_channel(p,:) = multipath.*(randn(1,length(multipath_channel0))+j*randn(1,length(multipath_channel0)))*sqrt(0.5);

if loop_user==1
    multipath_channel_user1(p,:) = multipath_channel(p,:);
end

signal_tx_p = filter(multipath_channel(p,:),[1],ofdm_symbol((N+GI)*(p-1)+1:(N+GI)*p));% passing through the multipath channel
signal_tx_loop(p,:) = signal_tx_p;
end
signal_tx = signal_tx + signal_tx_loop;
end



% add noise
% le = length(y);
noise =sigma*(randn(num_data*spreadLength/N,N+GI) + j*randn(num_data*spreadLength/N,N+GI));
signal_rx = signal_tx + noise; %% add the AWGN .


y2 = [];
for q =1:(num_data*spreadLength/N)
% fft
rec_symbol = [];
rec_symbol = [rec_symbol; fft_cp_rx_blk(signal_rx(q,:),N,GI)/sqrt(N)];
rec_symbol2 = reshape(rec_symbol,Mt*N,1);


% channel estimation
h = [multipath_channel_user1(q,:),zeros(1,N+GI-length(multipath))];
Hf1 = fft(h,N);
% Hf2 = Hf1(1:num_subc);
% Hf = diag(Hf2,0);% transform to diagonal matrix

for n = 1:N/spreadLength
    y1=0;
    for m = 1:spreadLength
        y1 = y1 + cSpread(1,m)*conj(Hf1((n-1)*spreadLength+m))*rec_symbol2((n-1)*spreadLength+m);
    end
    temp(n) = y1;
end

% detection
% G = Hf';
% rec_symbol3 = rec_symbol2(1:num_subc);
% y1 = cSpread'*G*rec_symbol3;


y2 = [y2,temp];
end


y = reshape(y2,1,num_data);

% demodulate
idata=real(y);
qdata=imag(y);
y_demod = deqpsk(idata,qdata,1,num_data,mod_level);

% deinterleaver
% channel decoder
y_demod = y_demod';
%ncode=rem(code+randerr(2000,1,[0 1;.95 .05]),2);
tblen=10;
decoded1=vitdec(y_demod,trel,tblen,'cont','hard');
%ucode=1-2*y_demod;
%decoded2=vitdec(ucode,trel,tblen,'cont','unquant');
% [x,qcode]=quantiz(1-2*ncode,[-.75 -.5 -.25 0 .25 .5 .75],[7 6 5 4 3 2 1 0]);
% decoded3=vitdec(qcode',trel,tblen,'cont','soft',3);
[n1,r1]=biterr(decoded1(tblen+1:end),msg_user1(1:end-tblen,1));
% [n2,r2]=biterr(decoded2(tblen+1:end),msg(1:end-tblen));
% [n3,r3]=biterr(decoded3(tblen+1:end),msg(1:end-tblen));
% disp(['The bit error rates are: ',num2str([r1 r2 r3])])

% comparison
% totolmod = sum(xor(y_demod,code_user1))
ber = r1;






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -