www.gusucode.com > MC-CDMA系统的仿真matlab源码程序 > mc-cdma/sui5/preamble1LS_LMMSE_ML.m

    % 清除内存,计时开始  SUI3 信道
clear 
tic;
%设置参数
totalwords=19200;
numusers=4;
wordsize=2;
linktype=0;   %下行链路
NumCarr=256;
procgain=16;  %扩频码的长度
guardtime=64;
guardtype=2;
frameguard=NumCarr+guardtime;  % Guard Time between successive frames (one symbol period)
% 产生要发送的双极性二进制随机数
seed=1234;
rand('seed',seed);	                    	% Set to new seed
seqnumlist = randperm(procgain);
Datatx = zeros(numusers,totalwords);
basesignal0=zeros(1,totalwords*procgain);
for k = 1:numusers,
	Datatx(k,:)=floor(rand(1,totalwords)*2^wordsize);%产生2^wordsize进制发送数据
    %进行映射
     mapping=get80216map(2^wordsize);
     B=Datatx(k,:);
     for i=1:length(B),
     Datatx1(i)=mapping(1+B(i));
     end;
    seqnum=seqnumlist(k);%取得用户k的扩频码
%扩频
    basesignal_0=tranCDMA(Datatx1,procgain,seqnum,linktype);
    basesignal0=basesignal0+basesignal_0; %各个用户的扩频信号进行合并
 end
 Tx=dec2bin(Datatx.',wordsize)-48;
%ofdm调制
%=============
% 每载波要传输多少数据
%=============
Numsymb = ceil(length(basesignal0)/192);
%如果传输的数据不是数据载波的整数倍,则在后面补零
if length(basesignal0)/192 ~= Numsymb,
	DataPad = zeros(1,Numsymb*192);
	DataPad(1:length(basesignal0)) = basesignal0;
	basesignal0 = DataPad;
end
clear DataPad;
%生成数据和导频图案
Pattern=ones(NumCarr,Numsymb+2);  % the position of data is set as 1.
Pattern(129,:)=0;                 % DC=0
Pattern([1:28,230:256],:)=0;      % guard band=0
Pattern([45:24:213],:)=4;% the position of pilot is set as 4.
Pattern(:,[1,2])=0;
Data_Pattern=find(Pattern==1);   %the pattern of data
%生成数据矩阵    
w=round(rand(1,Numsymb));                   % generate Wk
Data=zeros(size(Pattern));
for n=3:Numsymb
    Data([45,93,189,213],n)=1-2*w(n);      % [-84,-36,60,84]=1-2W
    Data([69,117,141,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,1,0,1,0,-1,0,-1,0,-1,0,1,0,-1,0,-1,0,1,0,-1,0,-1,0,-1]';
%==================================
%Find the time waveform using IFFT
%==================================
BaseSignal = ifft(Data);       %ifft是对列进行ifft变换。
%=================================
%Add a Guard Period
%=================================
BaseSignal0=[BaseSignal((end-guardtime+1):end,:); BaseSignal];
BaseSignal = reshape(BaseSignal0,1,size(BaseSignal0,1)*size(BaseSignal0,2));  %先取第一列,再取第二列,……

%===============
% CHANNEL MODEL, channel have delay
%===============
% generate channel parameter which is unknown to receiver
% 抽样频率f=4MHz,周期0.25us,信道延迟Delay=[0 4 10]us,故延迟的抽样为
% 4/0.25=16,10/0.25=40,即16和40。
% SUI-5 channel
% =======================
P = [0 -5 -10];        %db
P = 10.^(P/10);   % calculate linear power
K=[1 0 0];
s2=P./(K+1);
m2=P.*(K./(K+1));
fade1=sqrt(s2)*sqrt(1/2)*(1+j);
fade2=m2;
fade=fade1+fade2;  
path1=BaseSignal*fade(1);
path2=BaseSignal*fade(2);
path3=BaseSignal*fade(3);
path11=[path1 zeros(1,40)];                                                                                                                              
path12=[zeros(1,16) path2 zeros(1,24)];
path13=[zeros(1,40) path3];
RxSignal0=path11+path12+path13;
RxSignal0=RxSignal0(1:length(path1));

H_real=fft([fade(1) zeros(1,15) fade(2) zeros(1,23) fade(3)].',256);
H_real= H_real([29:229]);
H_real1=repmat(H_real,[1 Numsymb+2]);

ber1=[];
ber2=[];
ber3=[];
berreal=[];
mse1=[];
mse2=[];
mse3=[];
%LMMSE 参数设置
switch wordsize
    case 2
        beita=1;         % beita=E{|Ck|^2}*E{1/|Ck}^2}=constant
    case 4
        beita=17/9;      % beita=17/9 for 16-QAM , and beita=1 for QPSK
    case 6
        beita=2.6854;    % beita=2.6854 for 64-QAM
    otherwise
        error('error mapping type!')
end
             
L=40;
ratio=L/NumCarr;
for ri=1:256    %相关函数
    for ci=1:256
        if ri==ci
            Rhh(ri,ci)=1;
        else
            Rhh(ri,ci)=(1-exp(-j*2*pi*ratio*(ri-ci)))/(j*2*pi*ratio*(ri-ci));  % attention !!!
        end
    end
end

for SNR=0:4:20
    RxSignal=awgn(RxSignal0,10^(SNR/10),'measured',1234,'linear');

%==================
% RECEIVER SECTION
%==================
% remove cyclic prefix
symbwaves=reshape(RxSignal,frameguard,Numsymb+2);
symbwaves = symbwaves(guardtime+1:frameguard,:); % Strip off the guard interval
%fft变换
Yf=fft(symbwaves);
%=======================================Estimation===============================================
%估计导频处的信道频域响应
%LS
H_Pilot2=Yf([29:2:127,131:2:229],2)./Data([29:2:127,131:2:229],2);
x2=[1:2:99,103:2:201].';
y1=[1:201]';
H01=INTERP1(x2,H_Pilot2,y1,'linear'); % 帧头2估计值内插%时域估计
%LMMSE            
C=beita/10^(SNR/10);     % constant coefficient 
M=[29:229];
N=[29:2:127,131:2:229];
Rpp=Rhh(N,N);
Rhp=Rhh(M,N);
Wt=Rhp*inv(Rpp+C*eye(length(N)));            % weight matrix of LMMSE estimation
H02=Wt*H_Pilot2;
%ML
Nc=256;
n=(0:Nc-1)';
k=0:Nc-1;
F=1/sqrt(Nc)*exp(-j*2*pi*n*k/Nc); % generate an Nc by Nc unitary FFT matrix, satisfy:F'=inv(F)
Nh=41;
Fhh=F(M,1:Nh);                                    
Fuh=F(N,1:Nh);
H03=Fhh*pinv(Fuh)*H_Pilot2;
    
    H1=repmat(H01,[1 Numsymb+2]);
    H2=repmat(H02,[1 Numsymb+2]);
    H3=repmat(H03,[1 Numsymb+2]);
    H10=abs(H1);
    H20=abs(H2);
    H30=abs(H3);
    mse1=[mse1,mse(abs(H_real-H01))]
    mse2=[mse2,mse(abs(H_real-H02))]
    mse3=[mse3,mse(abs(H_real-H03))]
   Rx1=Yf;
   Rx2=Yf;
   Rx3=Yf;
   Rxreal=Yf;
   Rx1([29:229],:)=Rx1([29:229],:)./H1.*H10;
   Rx2([29:229],:)=Rx2([29:229],:)./H2.*H20;
   Rx3([29:229],:)=Rx3([29:229],:)./H3.*H30;
   Rxreal([29:229],:)=Rxreal([29:229],:)./H_real1.*abs(H_real1);
    
   DemSig1=Rx1(Data_Pattern);
   DemSig2=Rx2(Data_Pattern);
   DemSig3=Rx3(Data_Pattern);
   DemSigreal=Rxreal(Data_Pattern);
   
DemSignal1=reshape(DemSig1,1,size(DemSig1,1)*size(DemSig1,2));  % LS
DemSignal2=reshape(DemSig2,1,size(DemSig2,1)*size(DemSig2,2));  %MMSE
DemSignal3=reshape(DemSig3,1,size(DemSig3,1)*size(DemSig3,2));  %ML
DemSignalreal=reshape(DemSigreal,1,size(DemSigreal,1)*size(DemSigreal,2)); %理想
%解扩
subsignal1=[];
subsignal2=[];
subsignal3=[];
subsignalreal=[];
for k=1:numusers
    seqnum = seqnumlist(k);
[data10,subsignal10]=recCDMA(DemSignal1,procgain,seqnum,linktype);
subsignal1=[subsignal1;subsignal10];
[data20,subsignal20]=recCDMA(DemSignal2,procgain,seqnum,linktype);
subsignal2=[subsignal2;subsignal20];
[data30,subsignal30]=recCDMA(DemSignal3,procgain,seqnum,linktype);
subsignal3=[subsignal3;subsignal30];
[datareal0,subsignalreal0]=recCDMA(DemSignalreal,procgain,seqnum,linktype);
subsignalreal=[subsignalreal;subsignalreal0];
end
%去掉映射
Datarx1=invmapping(subsignal1.',mapping,wordsize);
Datarx2=invmapping(subsignal2.',mapping,wordsize);
Datarx3=invmapping(subsignal3.',mapping,wordsize);
Datarxreal=invmapping(subsignalreal.',mapping,wordsize);
%计算误码率
%计算误码率
    Rx10=dec2bin(Datarx1)-48;
    ber10=sum(sum(xor(Tx,Rx10)))/(length(Data_Pattern)*wordsize);
    Rx20=dec2bin(Datarx2)-48;
    ber20=sum(sum(xor(Tx,Rx20)))/(length(Data_Pattern)*wordsize);
    Rx30=dec2bin(Datarx3)-48;
    ber30=sum(sum(xor(Tx,Rx30)))/(length(Data_Pattern)*wordsize);
    Rx40=dec2bin(Datarxreal)-48;
    berreal0=sum(sum(xor(Tx,Rx40)))/(length(Data_Pattern)*wordsize);

ber1=[ber1,ber10]
ber2=[ber2,ber20]
ber3=[ber3,ber30]
berreal=[berreal,berreal0]
end
figure
subplot(2,2,1)
k=0:4:20
semilogy(k,ber1,'-rh')
hold on
semilogy(k,ber2,'-ro')
semilogy(k,ber3,'-rx')
semilogy(k,berreal,'-.b')
grid on
axis([0 20 10^(-4) 0.5])
xlabel('SNR/dB'),ylabel('BER')
legend('LS','LMMSE','ML','理想')
set (gcf,'color',[1 1 1])
set(gca,'xtick',[0:4:20])
hold off
subplot(2,2,2)
k=0:4:20
semilogy(k,mse1,'-rh')
hold on
semilogy(k,mse2,'-ro')
semilogy(k,mse3,'-rx')
grid on
axis([0 20 10^(-4) 1])
xlabel('SNR/dB'),ylabel('MSE')
legend('LS','LMMSE','ML')
set (gcf,'color',[1 1 1])
set(gca,'xtick',[0:4:20])
hold off
toc