clear all;
close all;
%Khoi tao
N_trial = 1e1; %so thu nghiem mo phong - number of simulation trials
v_kph =100;%Toc do user - speed of users in km/h; typical values for one LTE frame:100km/h(11 DPS-bases);50km/h(9 DPS-bases);5km/h(5 DPS-bases);1km/h(3 DPS-bases)
N_bases = 5; %or 5 for 100km/h % number of BEM basis function is used in the TV channel approximation/paramterization in the duration of LTE frame(s)
%s? ch?c n?ng c? s? BEM ???c s? d?ng trong c�c k�nh truy?n h�nh x?p x? / tham s? trong th?i gian c?a khung LTE (s)
N_rx = 2; % number of Rx antennas
N_tx = 2; % number of Tx antennas
%======LTE downlink timing settings==============
N_LTE_frame = 1;%So khung LTE - Number of LTE frames (each with a length of 10ms) where the mobile speed of each user can be assumed to be unchanged.
N_slot = 1;%So khe thoi gian trong 1 khung - choose 2 due to large memory in channel correlation matrix generation %20; %Number of time slots (each with a length of 0.5ms) in a LTE frame
N_sym_slot = 7; % So ki hieu trong 1 khe - number of OFDM symbols in a time slot in a LTE frame, normal mode in "Downlink Control Channel Design for 3GPP LTE", WCNC 2008
pilot_pos = [1 7]; % Vi tri so pilot OFDM Symbols trong 1 khe - positions of pilot OFDM symbols in a burst of "N_slot" LTE time slots
SNR = [0 5 10 15 20 25 30].';
% must be an ODD value due to the use of GCE/CE-BEM
N_iter = 5; % or 10 ??? ; Number of iterations used for Newton Raphson method in fine CFO estimation
G_coe = 2; % he so su dung trong mo hinh GCE - a coefficient used in GCE-BEM model.
%============== OFDM settings--------------
L = 5; % so luong toi da duong phan giai - number of resolvable multipaths < N_cp, could be considered as the maximum number of resolvable paths of all BS-to-user channels
N_fft =128; % kich thuoc FFT su dung theo Pilot - FFT size used according to the PILOT pre-generated <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
N_cp = 10; % chieu dai CP trong che do binh thuongCP length in normal mode, refered to "Downlink Control Channel Design for 3GPP LTE", WCNC 2008.
fs = 1.92e6; % tan so lay mau - sampling freq. corresponding to N_fft = 128 in "Downlink Control Channel Design for 3GPP LTE", WCNC 2008.
fc = 2e9; % tan so song mang - carrier frequency in Hz (in LTE standard band IV)
SigPow = 1; % cong suat tin hieu - after normalization.
N_pilot_sym = length(pilot_pos); % so pilot OFDM symbols trong 1 trung - number of used pilot OFDM symbols in LTE frame(s).
Ns = N_fft + N_cp; % Chieu dai OFDM symbol sau chen CP - OFDM symbol length (in sample) after CP insertion
PDP = exp(-[0:L-1]/4)/sum( exp(-[0:L-1]/4) );% Dien tre - Power Delay Profile in according to COST 259, [L. M. Correia,"Wireless Flexible Personalised Communications", New York: Wiley 2001].
load pre_generated_pilot_pattern_10_Tx_antennas_300_OFDM_symbols_64QAM_128_FFT;
%>>> The below BEMs is generated for the entire block of N_LTE_frame.<<<<<<
%>>Khoi tao BEMS<<
[CE_bases,GCE_bases,DPS_bases,KL_bases,pInv_CE_ma,pInv_GCE_ma,pInv_DPS_ma,pInv_KL_ma,R_h_TI,R_c_CE,R_c_GCE,R_c_DPS,R_c_KL,...
CE_E_cr_cr_T,CE_E_ci_ci_T,CE_E_cr_ci_T,CE_E_ci_cr_T,GCE_E_cr_cr_T,GCE_E_ci_ci_T,GCE_E_cr_ci_T,GCE_E_ci_cr_T,...
DPS_E_cr_cr_T,DPS_E_ci_ci_T,DPS_E_cr_ci_T,DPS_E_ci_cr_T,KL_E_cr_cr_T,KL_E_ci_ci_T,KL_E_cr_ci_T,KL_E_ci_cr_T] =...
BEMs_Autocorrelation_preGen(G_coe,PDP,v_kph,N_bases,fc,N_LTE_frame,N_slot,N_sym_slot,Ns,fs,N_tx,N_rx);
%---------------------------------------------------------------------
MSE_TI_CIR_MAP= zeros(length(SNR),1);
MSE_CE_CIR_MAP = zeros(length(SNR),1);
MSE_GCE_CIR_MAP = zeros(length(SNR),1);
MSE_CE_coe_MAP = zeros(length(SNR),1); % MSE of CE-BEM coefficient estimation
MSE_GCE_coe_MAP = zeros(length(SNR),1);
%---------------------------------------------------------------------
MSE_TI_CIR_MAP_perSyn = zeros(length(SNR),1);
MSE_CE_CIR_MAP_perSyn = zeros(length(SNR),1);
MSE_GCE_CIR_MAP_perSyn = zeros(length(SNR),1);
MSE_CE_coe_MAP_perSyn = zeros(length(SNR),1); % MSE of CE-BEM coefficient estimation
MSE_GCE_coe_MAP_perSyn = zeros(length(SNR),1);
for SNR_index = 1:length(SNR)
% AWGN power determination
No = SigPow/(10^(SNR(SNR_index)/10)); % Cong suat nhieu - AWGN power
[S_CE,S_GCE,S_DPS,S_KL,S_TI,tx_pilot_sig,CE_MAP_ma,GCE_MAP_ma,DPS_MAP_ma,KL_MAP_ma,TI_MAP_ma,...
TI_MAP_ChanEst_ma,CE_MAP_ChanEst_ma,GCE_MAP_ChanEst_ma,DPS_MAP_ChanEst_ma,KL_MAP_ChanEst_ma] =...
Pilot_Tx_Sig_Gen_MAP(No,L,N_fft,N_cp,pilot_pos,pilot_subcarriers,N_tx,N_rx,...
R_h_TI,R_c_CE,R_c_GCE,R_c_DPS,R_c_KL,CE_bases,GCE_bases,DPS_bases,KL_bases);
for trial = 1:N_trial
disp(['Current SNR=', num2str(SNR(SNR_index)), '; Current trial=', num2str(trial)]);
%------CFO generation----
% TV channel generation
[CE_coe,GCE_coe,DPS_coe,KL_coe,CIR_pilot] =Zheng_BEM_CIR_Gen(N_tx,N_rx,L,PDP,v_kph,N_bases,N_LTE_frame,N_slot,N_sym_slot,...
pilot_pos,N_fft,Ns,fs,fc,pInv_CE_ma,pInv_GCE_ma,pInv_DPS_ma,pInv_KL_ma);
% Received signal generation
[rx_pilot_sig] = Rx_Sig_Gen(N_tx,N_rx,L,N_fft,N_cp,pilot_pos,No,CIR_pilot,tx_pilot_sig);
%==================================================================
% MAP Channel estimation
[CE_coe_MAP_SE,CE_CIR_MAP_SE,GCE_coe_MAP_SE,GCE_CIR_MAP_SE,DPS_coe_MAP_SE,DPS_CIR_MAP_SE,KL_coe_MAP_SE,KL_CIR_MAP_SE,...
TI_CIR_MAP_SE,...
CE_coe_MAP_perSyn_SE,CE_CIR_MAP_perSyn_SE,GCE_coe_MAP_perSyn_SE,GCE_CIR_MAP_perSyn_SE,DPS_coe_MAP_perSyn_SE,...
DPS_CIR_MAP_perSyn_SE,KL_coe_MAP_perSyn_SE,KL_CIR_MAP_perSyn_SE,TI_CIR_MAP_perSyn_SE,...
CE_coe_MAP_igCFO_SE,CE_CIR_MAP_igCFO_SE,GCE_coe_MAP_igCFO_SE,GCE_CIR_MAP_igCFO_SE,DPS_coe_MAP_igCFO_SE,...
DPS_CIR_MAP_igCFO_SE,KL_coe_MAP_igCFO_SE,KL_CIR_MAP_igCFO_SE,TI_CIR_MAP_igCFO_SE,...
CE_coe_MAP_wCoar_SE,CE_CIR_MAP_wCoar_SE,GCE_coe_MAP_wCoar_SE,GCE_CIR_MAP_wCoar_SE,DPS_coe_MAP_wCoar_SE,...
DPS_CIR_MAP_wCoar_SE,KL_coe_MAP_wCoar_SE,KL_CIR_MAP_wCoar_SE,TI_CIR_MAP_wCoar_SE,...
sq_norm_CE_coe,sq_norm_GCE_coe,sq_norm_DPS_coe,sq_norm_KL_coe] =...
MAP_channel_estimator(N_tx,N_rx,...
CIR_pilot,L,CE_coe,GCE_coe,DPS_coe,KL_coe,N_bases,...
CE_bases,GCE_bases,DPS_bases,KL_bases,pilot_pos,N_fft,N_cp,rx_pilot_sig,...
TI_MAP_ChanEst_ma,CE_MAP_ChanEst_ma,GCE_MAP_ChanEst_ma,DPS_MAP_ChanEst_ma,KL_MAP_ChanEst_ma);
MSE_TI_CIR_MAP(SNR_index,1) = MSE_TI_CIR_MAP(SNR_index,1) + TI_CIR_MAP_SE;
MSE_CE_CIR_MAP(SNR_index,1) = MSE_CE_CIR_MAP(SNR_index,1) + CE_CIR_MAP_SE;
MSE_GCE_CIR_MAP(SNR_index,1) = MSE_GCE_CIR_MAP(SNR_index,1) + GCE_CIR_MAP_SE;
MSE_CE_coe_MAP(SNR_index,1) = MSE_CE_coe_MAP(SNR_index,1) + CE_coe_MAP_SE;
MSE_GCE_coe_MAP(SNR_index,1) = MSE_GCE_coe_MAP(SNR_index,1) + GCE_coe_MAP_SE;
%===================================================================
% Under perfect Synchronization for MAP channel estimation / theo dong bo hoan hao
MSE_TI_CIR_MAP_perSyn(SNR_index,1) = MSE_TI_CIR_MAP_perSyn(SNR_index,1) + TI_CIR_MAP_perSyn_SE;
MSE_CE_CIR_MAP_perSyn(SNR_index,1) = MSE_CE_CIR_MAP_perSyn(SNR_index,1) + CE_CIR_MAP_perSyn_SE;
MSE_GCE_CIR_MAP_perSyn(SNR_index,1) = MSE_GCE_CIR_MAP_perSyn(SNR_index,1) + GCE_CIR_MAP_perSyn_SE;
MSE_C