www.gusucode.com > MSK调制方式误码率源码程序 > MSK调制方式误码率源码程序/Hadamard_bpsk.m

    %%%%%   Hadamard正交编码+BPSK误码性能分析  %%%%%%%%%%%%%%%%
NVec = [8 16 32 64 128  ]; % Values of N to consider(编码效率)
EbNoVec =3:9;SNRVec=zeros(1,length(NVec));
nSamp = 1; 
M = 2;                      % Size of signal constellation
k = log2(M);                % Number of bits per symbol
cont=0;
hMod = modem.pskmod(M);         % Create a M-PSK modulator
hMod.InputType = 'Bit';         % Accept bits as inputs
hMod.SymbolOrder = 'Gray';      % Accept bits as inputs
hDemod = modem.pskdemod(hMod);

number_of_errors = zeros(length(NVec),length(EbNoVec));
bit_error_rate1  = zeros(length(NVec),length(EbNoVec));
 
for idxN = 1:length(NVec)
 N=NVec(idxN);%  符号比特数;
 H=hadamard(N);   % 生成Hadamard矩阵  
 h = log2(N);  % 消息比特数;
 coderate=h/N;%符号率
  for i=1:N                   % 符号替换
      for j=1:N 
        if (H(i,j)==-1),
            H(i,j)=0;
        end;
    end;
end 

n = h*floor(100000   /h); % Number of bits to process
  for idxEbNo = 1:length(EbNoVec)                       
  figure(1);      

%% Simulation loops

      
    x = randi([0 1],n,1); % Random binary data stream
    xsym = bi2de(reshape(x,h,length(x)/h).','left-msb');% 产生(0 to h-1)
    x_enc=xsym+1 ;% 从1到 h范围的信息符号字
   
   codeword=zeros(length(xsym),N); 
   decodeword=zeros(length(xsym),N); 
   zsym=zeros(length(xsym),1);
   
  for l=1:length(x_enc)   
   codeword(l,:)=H(x_enc(l),:) ; %  对每个信息符号相当的符号字
   f=codeword(l,:)';
   y = modulate(hMod,f) ; 
   yTx = y;
   
   %% Channel
   % Send signal over an AWGN channel.
   EbNo = EbNoVec(idxEbNo); % In dB
   SNR = EbNo +10*log10(k*coderate) - 10*log10(nSamp);
   SNRVec(idxEbNo)=SNR;
   yNoisy = awgn(yTx,SNR,'measured');
   
   %% Received Signal
   yRx = yNoisy;   
   
   %%  Demodulating 
   rcvword_C = demodulate(hDemod,yRx) ;
   %% Hadamard_decoding
   rcvword =rcvword_C';
   tt=xor(f,rcvword_C);%tt'
   minnum=1 ;
   min=N ;
for i=1:N
    d=0;
    for j=1:N %计算接受符号和Hadamard行列各行之间的汉明距离
        if (rcvword(1,j)~=H(i,j)),
            d=d+1;
        end;
    end;
    if (d<min),%探索最小汉明距离
        min=d; 
        minnum=i; 
    end;
end;
  zsym(l)=minnum-1;
  decodeword(l,:)=H(minnum,:);
  if((f~=rcvword_C  )), %&&  (decodeword(l,:)==codeword(l,:) )
      cont=cont+1;
  end;
 end  ;    
  z = de2bi(zsym',h,'left-msb'); % Convert integers to bits.
  % Convert z from a matrix to a vector.
  z = reshape(z.',numel(z),1);  
  
 
  %% BER Computation
      % Compare x and z to obtain the number of errors and
      % the bit error rate.
      [number_of_errors(idxN,idxEbNo),bit_error_rate1(idxN,idxEbNo)] = ...
          biterr(x,z) 
    
 
  end % End of loop over EbNo values

 markerchoice1 =   '.xo*sd';
 markerchoice2 =   'bgcmyk';
 figure(1);
   plotsym = [markerchoice1(idxN) markerchoice2(idxN) '-']; % Plotting style for this curve
   semilogy(EbNoVec,bit_error_rate1(idxN,:),plotsym); % Plot one curve.
   drawnow; % Update the plot instead of waiting until the end.
   hold on; % Make sure next iteration does not remove this curve.
  figure(2);
  plotsym = [markerchoice1(idxN) markerchoice2(idxN) '-']; % Plotting style for this curve
   semilogy(SNRVec,bit_error_rate1(idxN,:),plotsym); % Plot one curve.
   drawnow; % Update the plot instead of waiting until the end.
   hold on; % Make sure next iteration does not remove this curve.
end  % End of loop over N values

%% Complete the plot.



%%%%%%%%%%%%%%%%%%%%%%% Noncoded-BPSK %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
M = 2;                      % Size of signal constellation
k = log2(M);                % Number of bits per symbol
n=100000                    % Number of bits to process
nSamp = 1; 

hMod = modem.pskmod(M);         % Create a MPSK modulator
hMod.InputType = 'Bit';         % Accept bits as inputs
hMod.SymbolOrder = 'Gray';      % Accept bits as inputs
hDemod = modem.pskdemod(hMod);


%% Preallocate space for results.

bit_error_rate2   = zeros(1,length(EbNoVec));

  for idxEbNo = 1:length(EbNoVec)                
           
    x = randi([0 1],n,1); % Random binary data stream
    y = modulate(hMod,x) ; 
   yTx = y;
   
   %% Channel
   % Send signal over an AWGN channel.
   EbNo = EbNoVec(idxEbNo); % In dB
   SNR = EbNo + 10*log10(k) - 10*log10(nSamp);
   SNRVec(idxEbNo)=SNR;
   yNoisy = awgn(yTx,SNR,'measured');
   
   %% Received Signal
   yRx = yNoisy;   
   
   %%  Demodulating 
  z  = demodulate(hDemod,yRx) ;
   
  %% BER Computation
      % Compare x and z to obtain the number of errors and
      % the bit error rate.
      [number_of_errors(1,idxEbNo),bit_error_rate2(1,idxEbNo)] = ...
          biterr(x,z)  
      
  end % End of loop over EbNo values
  
figure(1)
   semilogy(EbNoVec,bit_error_rate2(1,:),'-rd'); % Plot one curve.
   drawnow; % Update the plot instead of waiting until the end.
   hold on; % Make sure next iteration does not rermove this curve.
figure(2)
   semilogy(SNRVec,bit_error_rate2(1,:),'-rd'); % Plot one curve.
   drawnow; % Update the plot instead of waiting until the end.
   hold on; % Make sure next iteration does not rermove this curve.

figure(1)
title('Performance of Hadamard-BPSK for Varying N');
xlabel('EbNo (dB)'); ylabel('BER');
 legend('N =8','N =16','N =32','N=64','N=128','Noncoded BPSK',...
   'Location','SouthWest')  
figure(2)
title('Performance of Hadamard-BPSK for Varying N');
xlabel('SNR (dB)'); ylabel('BER');
legend('N =8','N =16','N =32','N=64','N=128','Noncoded BPSK',...
   'Location','SouthWest')