www.gusucode.com > 实现不同收发天线数,不同调制方式下的误比特率测量matlab源码程序 > MIMO程序/stbc22.m

    
function [FER,FER_uncoded,SER,SER_uncoded, BER, BER_uncoded]=stbc22(channel_model,K,Num,no_tx_antennas,no_rx_antennas,modulation)

%SNR upto 20 dBs
EbNo=[0:2:20];

%N, M: number of transmit and receive antennas

N=no_tx_antennas;
M=no_rx_antennas;

%initialize count
idx = 1;

h=waitbar(0,'Percentage Completed');
set(h,'Position',[230 60 275.25 56.25]);
set(h,'name','Please wait...');
wb=9.09;

 
for SNR=EbNo

  sigma=0.5/(10^(SNR/10));  

  % Num -> number of packets
       for packet_count=1:Num
 
% we are interested in transmitting 'K' SYMBOLS not bits. Hence, K*2 for QPSK
% etc.
        switch (modulation)
            case 'BPSK '
                data=randint(K,N);
                BIT=1;
            case 'QPSK '
                data=randint(K*2,N);
                BIT=2;
            case '8PSK '
                data=randint(K*3,N);
                BIT=3;
            case '16QAM'
                data=randint(K*4,N);
                BIT=4;
            otherwise
                disp('No Modulation')
        end
tx_bits=data.';
temp=[];
temp1=[];
for i=1:N
    [temp1 s P]=tx_modulate(tx_bits(i,:),modulation);
    temp=[temp; temp1];
    temp1=0;
end

%ready to transmit symbols of length 'K'
X=temp.';

fr_length=length(X);
 
 % block coding-Alamouti
 x0=X(:,1);% required to verify a 1x1 system
 x1=X;
 x2(:,1)=-conj(X(:,2));
 x2(:,2)=conj(X(:,1));


 % form the channel matrix
 for n=1:N
     if channel_model=='AWGN    '
         Hr(n,:,:)=ones(fr_length,N);
     else
         Hr(n,:,:)=(randn(fr_length,N)+j*randn(fr_length,N))/sqrt(2);
     end
 end
      
for n=1:M
%transmission matrix   
 H=reshape(Hr(n,:,:),fr_length,N);
 Habs(:,n)=sum(abs(H).^2,2);

%received signal per receiver antenna   
 r1(:,n)=sum(H.*x1,2)/sqrt(N)+sqrt(sigma)*(randn(fr_length,1)+j*randn(fr_length,1));
 r2(:,n)=sum(H.*x2,2)/sqrt(N)+sqrt(sigma)*(randn(fr_length,1)+j*randn(fr_length,1));
 
% demodulate the received signals
 z1(:,n)=r1(:,n).*conj(H(:,1))+conj(r2(:,n)).*H(:,2);
 z2(:,n)=r1(:,n).*conj(H(:,2))-conj(r2(:,n)).*H(:,1);
end

%uncoded(1,1)
r01=H(:,1).*x0+sqrt(sigma)*(randn(fr_length,1)+j*randn(fr_length,1));


%form estimates
for m=1:P
  d01(:,m)=abs(r01-H(:,1)*s(m)).^2; % uncoded signal
%coded signals  
  d1(:,m)=abs(sum(z1,2)-s(m)).^2+(-1+sum(Habs,2))*abs(s(m))^2;
  d2(:,m)=abs(sum(z2,2)-s(m)).^2+(-1+sum(Habs,2))*abs(s(m))^2;
end
     
% determine the minimum of estimates      

%decision for detecting uncoded
      [y0,i0]=min((d01),[],2);
      s0d=s(i0).';
      clear d01 
      
%decision for detecting s1     
      [y1,i1]=min(d1,[],2);
      s1d=s(i1).';
      clear d1
%decision for detecting s2
      [y2,i2]=min(d2,[],2);
      s2d=s(i2).';
      clear d2
% form received symbols    
    Xd=[s1d s2d];

%determine symbol errors   
 error_un(packet_count)=sum(X(:,1)~=s0d);% for uncoded
 temp1=X>0;
 temp2=Xd>0;
 error(packet_count)=sum(sum(temp1~=temp2));% for coded
       end % end of FOR loop for "packet_count"
%calculate FER, SER and BER for current idx

%for uncoded signal
  SER_uncoded(idx)=sum(error_un)/(Num*K);
  BER_uncoded(idx)=SER_uncoded(idx)/BIT;
  FER_uncoded(idx)=SER_uncoded(idx)*K;

%for coded signal
  SER(idx)=sum(error)/(Num*K);
  BER(idx)=SER(idx)/BIT;
  FER(idx)=SER(idx)*K;
  
%increment idx  
  idx=idx + 1;
  
str_bar=[num2str(wb) '% Completed'];
waitbar(wb/100,h,str_bar);
wb=wb+9.09;
  
end % end of FOR loop for SNR
   
close(h);