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

📄 ofdm_all_channel_2.m

📁 OFDM链路级别仿真,包括数字调制与解调,AWGN信道,CP的添加与消除,零填充,
💻 M
字号:
% --------------------------------------------- %
%   摘录自:   未知                               %
%   作者:   未知                                %
%   整理:  lavabin                              %
%           2006.04.24                          %
% --------------------------------------------- %
echo off;clear all;close all;clc;
fprintf( 'OFDM仿真\n') ;
tic
% --------------------------------------------- %
%                   参数定义                     %
% --------------------------------------------- %
% Initialize the parameters
NumLoop = 100;  % 符号数
NumSubc = 128;   % 子载波数
NumCP = 8;       % 循环前缀长度
SyncDelay = 0;   % 严格同步
%-----------------------------------------------
% 子载波数            128   由于子载波数与IFFT点数一致,故不需要零比特填充
% 位数/ 符号          2
% 符号数/ 载波        1000
% 训练符号数          0
% 循环前缀长度        8   (1/16)*T   为IFFT时间的1/16
% 调制方式            4-QAM
% 多径信道数          3 
% IFFT Size           128 
% 信道最大时延        2
%-----------------------------------------------
% --------------------------------------------- %
%                   QAM MODULATION              %
% --------------------------------------------- %
% Generate the random binary stream for transmit test
BitsTx = floor(rand(1,NumLoop*NumSubc)*2);
% Modulate (Generates QAM symbols)
% input: BitsTx(1,NumLoop*NumSubc); output: SymQAM(NumLoop,NumSubc/2)
SymQAMtmp = reshape(BitsTx,2,NumLoop*NumSubc/2).';
SymQAMtmptmp = bi2de(SymQAMtmp,2,'left-msb');
%--------------------------------------------------------------------------
% 函数说明:
% bin2dec(binarystr) interprets the binary string binarystr and returns the
% equivalent decimal number.
%   bi2de是把列向量的每一个元素都由2进制变为10进制
%     D = BI2DE(...,MSBFLAG) uses MSBFLAG to determine the input orientation.
%     MSBFLAG has two possible values, 'right-msb' and 'left-msb'.  Giving a
%     'right-msb' MSBFLAG does not change the function's default behavior.
%     Giving a 'left-msb' MSBFLAG flips the input orientation such that the
%     MSB is on the left.
% % %   D = BI2DE(...,P) converts a base P vector to a decimal value.
% %     Examples:
% %     >> B = [0 0 1 1; 1 0 1 0];
% %     >> T = [0 1 1; 2 1 0];
% %  
% %     >> D = bi2de(B)     >> D = bi2de(B,'left-msb')     >> D = bi2de(T,3)
% %     D =                 D =                            D =
% %         12                   3                             12
% %          5                  10                              5
%--------------------------------------------------------------------------
% QAM modulation 
% 00->-1-i,01->-1+i,10->1-i,11->1+i
% 利用查表法进行QAM星座映射
QAMTable = [-1-i -1+i 1-i 1+i];
SymQAM = QAMTable(SymQAMtmptmp+1);
% --------------------------------------------- %
%                   IFFT                        %
% --------------------------------------------- %
% input: SymQAM(NumLoop,NumSubc/2); output: SymIFFT(NumSubc,NumLoop)
SymIFFT = zeros(NumSubc,NumLoop);
SymIFFTtmp = reshape(SymQAM,NumSubc/2,NumLoop);
SymIFFTtmptmp = zeros(NumSubc,NumLoop);
SymIFFTtmptmp(1,:) = real(SymIFFTtmp(1,:)); % 实数
SymIFFTtmptmp(NumSubc/2+1,:) = imag(SymIFFTtmp(1,:)); % 实数
% 这么安排矩阵的目的是为了构造共轭对称矩阵
% 共轭对称矩阵的特点是 在ifft/fft的矢量上 N点的矢量
% 在0,N/2点必须是实数 一般选为0
% 1至N/2点 与 (N/2)+1至N-1点关于N/2共轭对称
SymIFFTtmptmp(2:NumSubc/2,:) = SymIFFTtmp(2:NumSubc/2,:);
SymIFFTtmptmp((NumSubc/2+2):NumSubc,:) = flipdim(conj(SymIFFTtmp(2:NumSubc/2,:)),1);
%--------------------------------------------------------------------------
% 函数说明:
% B = flipdim(A,dim) returns A with dimension dim flipped.
% When the value of dim is 1, the array is flipped row-wise down. When dim is 2,
% the array is flipped columnwise left to right. flipdim(A,1) is the same as
% flipud(A), and flipdim(A,2) is the same as fliplr(A).
%--------------------------------------------------------------------------
% %  >> a = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
% %  a =
% %      1     2     3
% %      4     5     6
% %      7     8     9
% %     10    11    12
% %  >> b = flipdim(a,1)
% %  b =
% %     10    11    12
% %      7     8     9
% %      4     5     6
% %      1     2     3
SymIFFT = ifft(SymIFFTtmptmp,NumSubc,1);
% --------------------------------------------- %
%             Add cyclic prefix                 %
% --------------------------------------------- %
% input: SymIFFT(NumSubc,NumLoop); output: SymCP(NumSubc + NumCP,NumLoop)
NumAddPrefix = NumSubc + NumCP;
SymCP = zeros(NumAddPrefix,NumLoop);
RowPrefix = (NumSubc - NumCP + 1):NumSubc;
SymCP = [SymIFFT(RowPrefix,:);SymIFFT];

% SymIFFT1=reshape(SymIFFT,12800,1);
% f= linspace(-6400,6400,1);
% figure(2);
% semilogy(f,abs(fft(SymIFFT1)));
% grid on;






% --------------------------------------------- %
%             Go through the channel            %
% --------------------------------------------- %
% input: SymCP(NumSubc + NumCP,NumLoop); output: SymCh(1,(NumSubc + NumCP)*NumLoop)
SymCh = zeros(1,(NumSubc + NumCP)*NumLoop);
SymChtmp = SymCP(:).';   % 进行这个转置操作之后就成了一个矢量
                         % 相当于把矩阵的列向量依次排列 改变为一个行向量

Ch = [1 1/2 1/4];
SymChtmptmp = filter(Ch,1,SymChtmp);
%--------------------------------------------------------------------------
% 函数说明:
% Firlter data with an infinite impulse response (IIR) or finite impulse response
% (FIR) filter
% y = filter(b,a,X) filters the data in vector X with the filter described by
% numerator coefficient vector b and denominator coefficient vector a. If a(1) is
% not equal to 1, filter normalizes the filter coefficients by a(1). If a(1) equals
% 0, filter returns an error.
%--------------------------------------------------------------------------
% If X is a matrix, filter operates on the columns of X. If X is a multidimensional
% array, filter operates on the first nonsingleton dimension.
%-------------------------------------------------------------------------
% Add the AWGN
BerSnrTable = zeros(20,3);
for snr=0:19;  % = SNR + 10*log10(log2(2));
    BerSnrTable(snr+1,1) = snr;
SymCh = awgn(SymChtmptmp,snr,'measured');
SymCh = SymCP(:).';
%--------------------------------------------------------------------------
% 函数说明:
%  AWGN Add white Gaussian noise to a signal.
%     Y = AWGN(X,SNR) adds white Gaussian noise to X.  The SNR is in dB.
%     The power of X is assumed to be 0 dBW.  If X is complex, then 
%     AWGN adds complex noise.
%  ------------------------------------------------------------------------
%     Y = AWGN(X,SNR,SIGPOWER) when SIGPOWER is numeric, it represents 
%     the signal power in dBW. When SIGPOWER is 'measured', AWGN measures
%     the signal power before adding noise.
% ------------------------------------------------------------------------- 
%     Y = AWGN(X,SNR,SIGPOWER,STATE) resets the state of RANDN to STATE.
%  
%     Y = AWGN(..., POWERTYPE) specifies the units of SNR and SIGPOWER.
%     POWERTYPE can be 'db' or 'linear'.  If POWERTYPE is 'db', then SNR
%     is measured in dB and SIGPOWER is measured in dBW.  If POWERTYPE is
%     'linear', then SNR is measured as a ratio and SIGPOWER is measured
%     in Watts.
%  
%     Example: To specify the power of X to be 0 dBW and add noise to produce
%              an SNR of 10dB, use:
%              X = sqrt(2)*sin(0:pi/8:6*pi);
%              Y = AWGN(X,10,0);
%  
%     Example: To specify the power of X to be 0 dBW, set RANDN to the 1234th
%              state and add noise to produce an SNR of 10dB, use:
%              X = sqrt(2)*sin(0:pi/8:6*pi);
%              Y = AWGN(X,10,0,1234);
%  
%     Example: To specify the power of X to be 3 Watts and add noise to
%              produce a linear SNR of 4, use:
%              X = sqrt(2)*sin(0:pi/8:6*pi);
%              Y = AWGN(X,4,3,'linear');
%  
%     Example: To cause AWGN to measure the power of X, set RANDN to the 
%              1234th state and add noise to produce a linear SNR of 4, use:
%              X = sqrt(2)*sin(0:pi/8:6*pi);
%              Y = AWGN(X,4,'measured',1234,'linear');
% --------------------------------------------- %
%            Remove Guard Intervals             %
% --------------------------------------------- %
% input: SymCh(1,(NumSubc + NumCP)*NumLoop); output: SymDeCP(NumSubc,NumLoop)
SymDeCP = zeros(NumSubc,NumLoop);
SymDeCPtmp = reshape(SymCh,NumSubc + NumCP,NumLoop);
SymDeCP = SymDeCPtmp((NumCP+1+SyncDelay):NumAddPrefix+SyncDelay,:);
% --------------------------------------------- %
%                     FFT                       %
% --------------------------------------------- %
% input: SymDeCP(NumSubc,NumLoop); output: SymFFT(NumSubc,NumLoop)
SymFFT = fft(SymDeCP,NumSubc,1);



% --------------------------------------------- %
%        Make Decision(Include DeQAM)           %
% --------------------------------------------- %
% SymFFT(NumSubc,NumLoop); output: SymDec(NumSubc,NumLoop)
SymDec = zeros(NumSubc,NumLoop);
SymEqtmp(1,:) = SymFFT(1,:)+i*SymFFT(NumSubc/2+1,:);
SymEqtmp(2:NumSubc/2,:) = SymFFT(2:NumSubc/2,:);
for m = 1:NumLoop
    for n = 1:NumSubc/2
        Real = real(SymEqtmp(n,m));
        Imag = imag(SymEqtmp(n,m));
        
        if( abs((Real -1)) < abs((Real +1)))
            SymDec(2*n-1,m) = 1;
        else
            SymDec(2*n-1,m) = 0;
        end
        if( abs((Imag -1)) < abs((Imag +1  )) )  
            SymDec(2*n,m) = 1;
        else
            SymDec(2*n,m) = 0;
        end
    end
end
% -------------------------------------------------------------------------
% Test by lavabin
% Another way to DeQAM
%  QAMTable = [-1-i -1+i 1-i 1+i];
%  00->-1-i,01->-1+i,10->1-i,11->1+i
TestSymDec = zeros(NumSubc,NumLoop);
TestSymEqtmp(1,:) = SymFFT(1,:)+i*SymFFT(NumSubc/2+1,:);
TestSymEqtmp(2:NumSubc/2,:) = SymFFT(2:NumSubc/2,:);
TestSymEqtmp1 = reshape(TestSymEqtmp,1,NumSubc*NumLoop/2);
min_d = zeros(size(TestSymEqtmp1));
min_ddd = zeros(1,NumSubc*NumLoop);
d = zeros(4,1);
min_index = 0;
for ii = 1:1:(NumSubc*NumLoop/2)
    for jj = 1:4
        d(jj) = abs(TestSymEqtmp(ii) - QAMTable(jj));
    end
     [min_d(ii),min_index] = min(d);
% % [Y,I] = MIN(X) returns the indices of the minimum values in vector I.    
      switch min_index
  case 1
     min_ddd(2*ii-1) = 0 ;
         min_ddd(2*ii) =  0 ;
  case 2
     min_ddd(2*ii-1) = 0 ;
         min_ddd(2*ii) =  1 ;
  case 3
     min_ddd(2*ii-1) = 1 ;
         min_ddd(2*ii) =  0 ;
  case 4
     min_ddd(2*ii-1) = 1 ;
         min_ddd(2*ii) =  1 ;
          otherwise
      fprintf('Impossible error!!! \n\n');
      end
end
%--------------------------------------------------------------------------
% 函数说明:
% % C = min(A) returns the smallest elements along different dimensions of an
% % array.
% % If A is a vector, min(A) returns the smallest element in A.
% % If A is a matrix, min(A) treats the columns of A as vectors, returning a row
% % vector containing the minimum element from each column.
% % [C,I] = min(...) finds the indices of the minimum values of A, and returns
% % them in output vector I. If there are several identical minimum values, the
% % index of the first one found is returned.
% Bit Error
BitsRx = zeros(1,NumSubc*NumLoop);
BitsRx = SymDec(:).';
[Num,Ber] = symerr(BitsTx,BitsRx)
BerSnrTable(snr+1,2) = Num ;
BerSnrTable(snr+1,3) = Ber ;
end
%--------------------------------------------------------------------------
% Test by lavabin
if min_ddd == BitsRx 
    fprintf('DeQAM two ways the same results \n\n');
else
     fprintf('DeQAM two ways the different results');
end
% ------------------------------------------------------------------------
figure(1);
subplot(2,1,1);
semilogy(BerSnrTable(:,1),BerSnrTable(:,2),'o-');
subplot(2,1,2);
semilogy(BerSnrTable(:,1),BerSnrTable(:,3),'o-');


% ------------------------------------------------------------------------
time_of_sim = toc
echo on;
% --------------------------------------------- %
%                   The END                     %
% --------------------------------------------- %

⌨️ 快捷键说明

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