vblast_ofdm.m
上传用户:xinxin4122
上传日期:2013-04-03
资源大小:464k
文件大小:13k
源码类别:

邮电通讯系统

开发平台:

Matlab

  1. clear all;
  2. close all;
  3. fprintf('**** Simulation starts at: ');
  4. disp(datestr(now,0));
  5. % pdp : Multipath channel power delay profile.
  6. % 相邻径之间的相对时延为一个采样周期
  7. % L 多径数目: 0至L-1径。
  8. L=3;
  9. var0=1;
  10. %%%% var0=(1-exp(-1))/(1-exp(-L)); % 多径总功率归一化
  11. ll=0:L-1;
  12. pdp=var0.*exp(-ll);
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%
  14. % 在每个子载波的范围内分配能量与比特
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%
  16. num=4;  % 平均每个天线的每个子载波分配的比特数
  17. load parameters.txt;   % 装入仿真参数
  18. cases=parameters(1);
  19. caseparas=parameters(2);
  20. for kk=1:cases
  21.     % The number of transmit antennas.
  22.     Nt=parameters((kk-1)*caseparas+3);
  23.     % Nr: The number of receive antennas.
  24.     Nr=parameters((kk-1)*caseparas+4);
  25.     % N: The number of subcarriers.
  26.     N=parameters((kk-1)*caseparas+5);
  27.     % GI: CP length.
  28.     GI=parameters((kk-1)*caseparas+6);
  29.     % framenumber: The number of frames to simulate.
  30.     framenumber=parameters((kk-1)*caseparas+7);
  31.     % minSNR: Minimum SNR in dB to simulate begin with.
  32.     minSNR=parameters((kk-1)*caseparas+8);
  33.     % maxSNR: Maximum No in dB to simulate end with.
  34.     maxSNR=parameters((kk-1)*caseparas+9);
  35.     % stepSNR: SNR step in dB to increment.
  36.     stepSNR=parameters((kk-1)*caseparas+10);
  37.     % state: Modulation format: Adaptive=0; BPSK=1; QPSK=2; 16QAM=4; 64QAM=6.
  38.     state=parameters((kk-1)*caseparas+11);
  39.     % detection: Detection method: ZF=1; MMSE=2.
  40.     detection=parameters((kk-1)*caseparas+12);
  41.     
  42.     
  43.     
  44.     % 线形星座预编码矩阵
  45.     % LCPindication=0; % 为零的时候不使用LCP预编码,用串行干扰删除;
  46.                      % 若为5表示不用串行干扰删除,且不用LCP预编码。
  47.     % if LCPindication~=0
  48.         % switch LCPindication
  49.             % case 2
  50.                 % lcp=[exp(-j*pi/4) exp(-j*5*pi/4)].';
  51.                 % lcpint=[1 1].';
  52.                 % LCP=1/sqrt(2)*[lcpint lcpint.*lcp];
  53.             % case 4
  54.                 % lcp=[exp(-j*pi/8) exp(-j*5*pi/8) exp(-j*9*pi/8) exp(-j*13*pi/8)].';
  55.                 % lcpint=[1 1 1 1].';
  56.                 % LCP=1/2*[lcpint lcpint.*lcp lcpint.*(lcp.^2) lcpint.*(lcp.^3)];
  57.             % case 5
  58.                 % LCP=eye(Nt);
  59.         % end
  60.         % invLCP=inv(LCP);
  61.     % end
  62.  
  63.     
  64.     fprintf('==== Case %d ====n',kk);
  65.     disp('---- Simulation parameter list:');
  66.     fprintf('Transmit antennas: %d.n', Nt);
  67.     fprintf('Receive antennas: %d.n', Nr);
  68.     fprintf('The number of subcarriers : %d.n', N);
  69.     fprintf('CP length : %d.n', GI);
  70.     fprintf('Total frames: %d.n', framenumber);
  71.     fprintf('SNR range to simulate: [%d:%1.1f:%d] dB.n', minSNR,stepSNR,maxSNR);
  72.     
  73.     switch(state)
  74.         case 0
  75.             disp('Adaptive modulation.');
  76.             fmod='ADPM';
  77.         case 1
  78.             disp('BPSK modulation.');
  79.             fmod='BPSK';
  80.         case 2
  81.             disp('QPSK modulation.');
  82.             fmod='QPSK';       
  83.         case 4
  84.             disp('16QAM modulation.');
  85.             fmod='QAM16';
  86.         case 6
  87.             disp('64QAM modulation.');
  88.             fmod='QAM64';
  89.     end        
  90.     switch detection
  91.         case 1
  92.             disp('ZF detection.');
  93.         case 2
  94.             disp('MMSE detection.');
  95.     end
  96.     
  97.     % fprintf('LCPindication: %d.n', LCPindication);
  98.     
  99.     disp('----------------------------');
  100.     
  101.     
  102.     SNR=[minSNR:stepSNR:maxSNR];
  103.     randn('state',0);
  104.     rand('state',1);
  105.     
  106.     frame_length=N+GI; % 每帧的长度;
  107.     err=zeros(1,length(SNR));
  108.     averBER=zeros(1,length(SNR));
  109.     Ptotmax=Nt*N;
  110.     if state==0
  111.         bitstotal=num*Nt*N*framenumber; % 每个发天线的每个子载波上平均分num个比特。对应比较2.^num阶调制。
  112.     else
  113.         bitstotal=Nt*N*framenumber*state; % 发送的总比特数
  114.     end
  115.      
  116.     for SNRIndex=1:length(SNR) 
  117.         
  118.         sig2=Nt/(10.^(SNR(SNRIndex)/10));    % 此时的噪声功率为按整个ofdm符号平均能量归一化时的
  119.         % 每个发送天线每次发送一帧
  120.         last_symbol=zeros(Nt,length(pdp)-1); %信道记忆产生的影响
  121.     
  122.         for kk=1:framenumber 
  123.             % 完成一个帧的发送与接收
  124.             % 假设信道为准静态信道,在一个ofdm符号内保持不变
  125.             % 在符号间隔处发生跃变
  126.             [H H_f]=create_channel(Nt,Nr,pdp,N,GI); 
  127.             % H_f为每个子载波对应的MIMO信道矩阵
  128.             
  129.           
  130.            
  131.             % if state==0
  132.                 % for ii=1:N
  133.                 %    Hnorm2(ii)=1/Nt/Nr*trace(H_f{ii}*H_f{ii}');
  134.                 % end
  135.                 % [Psubc,Rsubc]=BPallocRNew(Hnorm2/N/sig2,N,Ptotmax,N*num); % 每个发天线的每个子载波上平均分配的功率与比特数
  136.             % end 
  137.            
  138.             w=cell(1,N);
  139.             korder=cell(1,N);
  140.             
  141.             % G2=cell(1,N);    % 有LCP编码的时候用
  142.             
  143.        
  144.             if detection==2
  145.                 eyenew=eye(Nr);  % Detection method: ZF=1; MMSE=2.
  146.             end
  147.             
  148.             for f=1:N
  149.                 Hnew=H_f{f};
  150.                 % if LCPindication
  151.                   
  152.                   %  if detection==1  
  153.                         % ZF
  154.                    %     G2{f}=pinv(Hnew);    %pinv is the pseudoinverse
  155.                        
  156.                     % else   
  157.                         % MMSE
  158.                       %  G2{f}=Hnew'*pinv(Hnew*Hnew'+N*sig2*eyenew);
  159.  
  160.                     % end
  161.                 % else
  162.                     
  163.                 korder{f}=zeros(1,Nt);
  164.                 krest=[1:Nt];
  165.                 for nt=1:Nt
  166.                     if detection==1  
  167.                             % ZF
  168.                         G=pinv(Hnew);    %pinv is the pseudoinverse
  169.                         if state==0
  170.                             [k,postSNR]=argmaxzf(G,N*sig2,krest);  % find the maximum norm form krest columes of G
  171.                         else
  172.                             [k,postSNR]=argminzf(G,N*sig2,krest);  % find the minimum norm form krest columes of G
  173.                         end
  174.                     else   
  175.                         % MMSE
  176.                         G=Hnew'*pinv(Hnew*Hnew'+N*sig2*eyenew);
  177.                         if state==0
  178.                             [k,postSNR]=argmaxmmse(G,Hnew,N*sig2,krest);
  179.                         else
  180.                             [k,postSNR]=argminmmse(G,Hnew,N*sig2,krest);
  181.                         end
  182.                             eyenew(:,k)=zeros(Nr,1);
  183.                     end
  184.                         % 查找出最小的SNR所在的列,用零替代,产生新的信道伪逆
  185.                     korder{f}(nt)=k;
  186.                     w{f}(k,:)=G(k,:);
  187.                     krest=setdiff(krest,k); % delete the selected k
  188.                     Hnew(:,k)=zeros(Nr,1);
  189.                     if state==0
  190.                         % averpostSNR(SNRIndex,f)=averpostSNR(SNRIndex,f)+postSNR;
  191.                         SNRsubch(k,f)=postSNR;
  192.                     end
  193.                 end
  194.             end
  195.             % 产生发送数据
  196.             
  197.             
  198.             tr_data=cell(Nt,N);
  199.             symbol=zeros(Nt,N);
  200.             
  201.             if state==0
  202.                 
  203.                 SNRsubch2=reshape(SNRsubch.',1,Nt*N); % 变形后每个发天线的子载波信道对应信噪比占连续的一段。
  204.                 [P1,R1]=BPallocRNew(SNRsubch2,Nt*N,Ptotmax,num*Nt*N);
  205.                 P=reshape(P1,N,Nt).';    % 每个发天线的各子载波的能量占一行
  206.                 R=reshape(R1,N,Nt).';    % 每个发天线的各子载波的比特数占一行
  207.                 for nt=1:Nt
  208.                     for f=1:N
  209.                         tr_data{nt,f}=randn(1,R(nt,f))>0;
  210.                           
  211.                         switch R(nt,f)
  212.                             case 0
  213.                                 symbol(nt,f)=zeros(1,1);
  214.                             case 1           % 根据自适应算法比特增量为2,此调制方式没有被使用
  215.                                 symbol(nt,f)=BPSKMod(tr_data{nt,f});
  216.                             case 2
  217.                                 symbol(nt,f)=QPSKMod(tr_data{nt,f});
  218.                             case 4
  219.                                 symbol(nt,f)=QAM16Mod(tr_data{nt,f});
  220.                             case 6
  221.                                 symbol(nt,f)=QAM64Mod(tr_data{nt,f});
  222.                         end
  223.                     end
  224.                 end
  225.                 
  226.                 Proot=sqrt(P);
  227.                 symbol=Proot.*symbol;
  228.              
  229.             else
  230.                 tr_data2=randn(Nt,N*state)>0;
  231.                 for nt=1:Nt
  232.                     symbol(nt,:)=modulate(tr_data2(nt,:),state);
  233.                 end
  234.             end
  235.                 
  236.             % if LCPindication
  237.                  % symbol=LCP*symbol;
  238.             % end  
  239.             
  240.             % ofdm调制,进行ifft变换
  241.           
  242.             ofdm_symbol=ifft(symbol,[],2); 
  243.            
  244.             % 添加CP
  245.             tr_symbol=[ofdm_symbol(:,end-GI+1:end) ofdm_symbol];
  246.             
  247.             % 发送信号平均功率归一化
  248.             tr_symbol = tr_symbol*N/sqrt(N);
  249.             
  250.            
  251.         
  252.             % 通过信道
  253.             re_cp_symbol= channel(H,tr_symbol,sig2,last_symbol,Nr,Nt,pdp);
  254.         
  255.             last_symbol=tr_symbol(:,end-length(pdp)+2:end); 
  256.         
  257.             % 恢复信号的功率水平
  258.             re_cp_symbol=re_cp_symbol/sqrt(N);
  259.         
  260.             % 去除cp
  261.             re_symbol=re_cp_symbol(:,GI+1:end);
  262.     
  263.             % ofdm解调,进行fft
  264.             
  265.             de_ofdm=fft(re_symbol,[],2);
  266.          
  267.             
  268.    
  269.             % 对每个载波进行vblast检测
  270.             bitRx=cell(Nt,N);       % 存放自适应的接收比特
  271.             y=zeros(Nt,N);
  272.             
  273.             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  274.             
  275.             % if LCPindication
  276.               %  for f=1:N
  277.                %     y(:,f)=invLCP*G2{f}*de_ofdm(:,f);
  278.                  %   for nt=1:Nt
  279.                   %      if state==0
  280.                    %         switch Rsubc(f)
  281.                     %            case 0
  282.                         
  283.                      %           case 2
  284.                       %              bitRx{nt,f}=QPSKDemod(y(nt,f));
  285.                        %         case 4
  286.                         %            bitRx{nt,f}=QAM16Demod(y(nt,f));
  287.                          %       case 6
  288.                           %          bitRx{nt,f}=QAM64Demod(y(nt,f));
  289.                            % end
  290.                         %else           
  291.                          %   bitRx{nt,f}=demodulate(y(nt,f),state);
  292.                         %end
  293.                     %end
  294.                 %end
  295.             %end
  296.             
  297.             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
  298.             for f=1:N  
  299.                 symbolRx=de_ofdm(:,f);
  300.                 for nt=1:Nt  
  301.                     y=w{f}(korder{f}(nt),:)*symbolRx;
  302.                     % decision and demodulation
  303.                     if state==0
  304.                         switch R(korder{f}(nt),f)
  305.                             case 0
  306.                                 a=0;   
  307.                             case 2
  308.                                 bitRx{korder{f}(nt),f}=QPSKDemod(y/Proot(korder{f}(nt),f));
  309.                                 a=QPSKMod(bitRx{korder{f}(nt),f})*Proot(korder{f}(nt),f); 
  310.                             case 4
  311.                                 bitRx{korder{f}(nt),f}=QAM16Demod(y/Proot(korder{f}(nt),f));
  312.                                 a=QAM16Mod(bitRx{korder{f}(nt),f})*Proot(korder{f}(nt),f);
  313.                             case 6
  314.                                 bitRx{korder{f}(nt),f}=QAM64Demod(y/Proot(korder{f}(nt),f));
  315.                                 a=QAM64Mod(bitRx{korder{f}(nt),f})*Proot(korder{f}(nt),f);
  316.                         end
  317.                     else           
  318.                         bitRx{korder{f}(nt),f}=demodulate(y,state);
  319.                         a=modulate(bitRx{korder{f}(nt),f},state);
  320.                     end
  321.                         symbolRx=symbolRx-H_f{f}(:,korder{f}(nt))*a; 
  322.                 end
  323.             end
  324.             
  325.             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  326.             
  327.             tr=[];
  328.             if state==0
  329.                 for kk=1:Nt
  330.                     for f=1:N
  331.                         tr=[tr tr_data{kk,f}];
  332.                     end
  333.                 end
  334.             else
  335.                 tr=reshape(tr_data2.',1,Nt*N*state);
  336.             end
  337.             Rx=[];
  338.             for kk=1:Nt
  339.                 for f=1:N
  340.                     Rx=[Rx bitRx{kk,f}];
  341.                 end
  342.             end
  343.             err(SNRIndex)=err(SNRIndex)+sum(tr~=Rx); % 加上一帧中误比特数
  344.         end   % 对应一帧检测完
  345.         
  346.     end  % 对应SNRIndex所有帧都检测完
  347.     
  348.     averBER=err/bitstotal;
  349.     if state==0
  350.         adap4BER11=averBER;
  351.         save 'adap4BER11.mat' SNR adap4BER11; 
  352.     else 
  353.         qam16BER11=averBER;
  354.         save 'qam16BER11.mat' SNR qam16BER11; 
  355.     end
  356. end  % End one case
  357. % switch LCPindication
  358.   %  case 2
  359.    %     lcp22BER=averBER;
  360.     %    save 'lcp22BER.mat' SNR averBER
  361.     % case 4
  362.       %  lcp44BER=averBER;
  363.        % save 'lcp44BER.mat' SNR averBER
  364.     % case 5
  365.       %  nolcp_canBER44=averBER;
  366.        % save 'nolcp_canBER44' SNR averBER
  367.         
  368.         
  369. % end
  370. figure;
  371. semilogy(SNR,qam16BER11,'bo--',SNR,adap4BER11,'r^-');
  372. axis([0 30 10.^-4 1]);
  373. xlabel('average SNR (dB)');
  374. ylabel('Raw BER');
  375. legend('conventional','adptive');
  376. grid on;
  377. fprintf('n**** Ending time: ');
  378. disp(datestr(now,0));
  379.   
  380.   
  381.     
  382.   
  383.     
  384.     
  385.     
  386.     
  387.     
  388.     
  389.