echo off;clear all;close all;clc;
fprintf( 'OFDM仿真\n') ;
tic
% --------------------------------------------- %
% 参数定义 %
% --------------------------------------------- %
% Initialize the parameters
NumLoop = 1000;
NumSubc = 128;
NumCP = 8;
SyncDelay = 0;
%-----------------------------------------------
% 子载波数 128
% 位数/ 符号 2
% 符号数/ 载波 1000
% 训练符号数 0
% 循环前缀长度 8 (1/16)*T
% 调制方式 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);
% plot(SymQAM,'o');
% axis([-2,2,-2,2]);
% --------------------------------------------- %
% 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);%按列做ifft
% --------------------------------------------- %
% 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];
% --------------------------------------------- %
% 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');
%--------------------------------------------------------------------------
% 函数说明:
% 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(NumS