📄 matlab_wan_ldpc.m
字号:
function wman1()
clear all;
fprintf('Start! Please waiting ...\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Parameters for system configuration %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fc = 2.4e9; %Carrier frequency
fs = 20e+6; %System bandwith is 5MHz
SubCarrNum = 256; %size fourier transform to generate signal. It
%is equal to the number of samples in the OFDM
%symbol, and also it is the size of FFT.
GuardNum = 16; %Total guard time in samples.
OfdmSize = (SubCarrNum + GuardNum);
ModType = 2; %Allowable values are 1, 2, 3, 4, 6
%BPSK,QPSK,8PSK,16QAM,64QAM respectely.
Mf = 16; %Pilot space in frequency, continued in time direction.
NormFactor = sqrt(SubCarrNum);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Parameters for simulating the multipath channel and additive Gaussian noise %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PowerDelayProfile = [0,-1,-9,-10,-15,-20];
NonZeroPaths = [1,3,5,7,10,14]; %normalized by 1/fs
CHLen = max(NonZeroPaths);
InitTime = 0.14; %channel simulator initial time.
v = 0; %vehicle speed km/h
EbN0db = [20]; %Eb/N0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Parameters for simulation number %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SymbolNum = 2;%number of OFDM symbols,must be int
CalFrameNum = 1; %according to the SNR the frame number is varying
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Parameters for pilot based channel estimation %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PilotSymbol = (1-j)/sqrt(2);
PilotIndex = 1:Mf:SubCarrNum;
TmpIndex = zeros(1,SubCarrNum);
TmpIndex(PilotIndex) = PilotIndex;
DataIndex = find((1:SubCarrNum) ~= TmpIndex);
clear('TmpIndex');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Parameters for turbo code or ldpc code %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
puncture = 0; %puncture = 0,rate=1/2;puncture = 1, puncture rate =1/3
CodeRate = 1/(2+puncture);
%the length of turbo coding,it should match the Mf=16,Mt=4, if others,it should use rate matching.
%CodeLen = length(DataIndex)*SymbolNum*ModType*CodeRate; %CodeLen = 3328
%beta = interleave(CodeLen*(2+puncture),100)+1;%interleaver for frame.1~CodeLen*rate
fprintf('Interleaver generate successed\n'); %may be some bugs,see interleave.c
load 960.mat;
[H,inv_phi1,g]=ldpc_lintimeenc_parameter(H);
[m,n]=size(H)
beta = interleave(n,100)+1;
% Write some information about this program
fpname = strcat(mfilename, '.txt');
fp=fopen(fpname,'a+');
fprintf(fp,'**************************************************************************\n');
fprintf(fp,'+ + + + OFDM simulator by XJ, NJUPT + + + +\n');
fprintf(fp,'+ + + + Please be patient. Wait a while to get the result. + + + +\n');
fprintf(fp,'**************************************************************************\n\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Start %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
for nEN = 1:length(EbN0db)
en = 10^(EbN0db(nEN)/10);
sigma2 = 1/(2*ModType*CodeRate*en);
errs0(nEN) = 0;
t0 = InitTime;
for nframe = 1:CalFrameNum
nframe
%The information bit that to transmit without the tail bit.
TxBits = sparse(randint(1,n-m));
%TxBits = randint(1,CodeLen ,2);
%FEC channel coding, use Turbo code
TxBitsCode=sparse(1,n);
TxBitsCode = ldpc_linetime_encode(TxBits,H,inv_phi1,g);
%Inter-frame interleave.
TxBitsInter = full(TxBitsCode(beta));
%TxSig_Repeat=kron(TxBitsInter,ones(1,SF));
%Modulation by QPSK.
ModData = 1/sqrt(2)*(TxBitsInter(1:2:end)+j*TxBitsInter(2:2:end));
%Form the frame
FreqSymbol = ones(SymbolNum,SubCarrNum);
FreqSymbol(:,DataIndex) = vec2mat(ModData,length(DataIndex));
FreqSymbol(:,PilotIndex) = FreqSymbol(:,PilotIndex)*PilotSymbol;
%IFFT and then add CP
OfdmSymbol = NormFactor*ifft(FreqSymbol,SubCarrNum,2);
OfdmSymbol = [OfdmSymbol(:,SubCarrNum-GuardNum+1:SubCarrNum) OfdmSymbol];
%parallel to serial convertion
TransSignal = reshape(OfdmSymbol.',1,prod(size(OfdmSymbol)));
% Pass through the multipath Rayleigh fading channel and AWGN
ChannelOutput = MultiPath0(fc, v, fs, t0, length(TransSignal), PowerDelayProfile, NonZeroPaths, TransSignal, sigma2);
t0 = t0 + length(TransSignal)/fs;
% convert vector to matrix
ChannelOutput = vec2mat(ChannelOutput,OfdmSize);
%removal of CP and FFT
ChannelOutput = ChannelOutput(:,GuardNum+1:OfdmSize);
RecSymbol = 1/NormFactor * fft(ChannelOutput,SubCarrNum,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% channel estimation %%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%one dimension channel estimation algorithm via LS to get the pilot estimation
PilotFreqEst = RecSymbol(:,PilotIndex)./PilotSymbol;
%estimation based on FFT
%EstInTime = sqrt(size(PilotFreqEst,2))*ifft(PilotFreqEst,size(PilotFreqEst,2),2);
EstInTime = ifft(PilotFreqEst,size(PilotFreqEst,2),2);
%transform to frequency domain,get the final estimation
FreqEst = fft(EstInTime(:,1:CHLen),SubCarrNum,2);
% coherent demodulation
EstData = RecSymbol(:,DataIndex).*conj(FreqEst(:,DataIndex))./abs(FreqEst(:,DataIndex));
% EstData = RecSymbol(:,DataIndex)./FreqEst(:,DataIndex);
%convert matrix to vector
EstData0 = reshape(EstData.',1,prod(size(EstData)));
% Demodulate the data with soft demodulation
DemodData0 = zeros(1,length(EstData0)*ModType);
DemodData0(1:2:end) = real(EstData0);
DemodData0(2:2:end) = imag(EstData0);
% Deinterleave
DemodDataDeInter0(beta) = DemodData0;
% Decode the data by Sova algorithm for turbo code.
[Decodedata,success,kk]=ldpc_decode_logforOFDM1(DemodDataDeInter0,sqrt(sigma2),H);
%kk
DecodeData0=Decodedata(1:n-m);
% Number of bit errors in current EbN0
errs0(nEN) = errs0(nEN) + sum(DecodeData0 ~= TxBits);
end
errs0(nEN) = errs0(nEN)/(CodeLen*CalFrameNum(nEN));
save errs1 errs0
fprintf(fp,'%f %8.6e %8.6e\n', EbN0db(nEN),errs0(nEN));
fprintf('****** Eb/N0 = %f dB BER = %8.6e ********\n', EbN0db(nEN),errs0(nEN));
fclose(fp);
fp=fopen(fpname,'a+');
end
TimeElapse = toc/3600; % Simulation time (hour)
fprintf('****** The total time that used is : %3.1f hour ********', TimeElapse);
fprintf(fp,'\n\n****** The total time that used is : %3.1f hour ********\n', TimeElapse);
fclose(fp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -