% FBMC/OQAM & OFDM
% FBMC-Multipath: BER verus SNR for FBMC/OQAM using Spreading and polyphase structure
%%------------------------------------------------------------------------------------------------------------%%
clc
clear all
close all
%%------------------------------------------------------------------------------------------------------------%%
% Intialization
%%------------------------------------------------------------------------------------------------------------%%
NumOfSymbol = 7; % number of symbols per frame
Modulation_Mode = 2; % the type of modulation : 1 is for BPSK, 2 for QPSK , 4 for 16QAM, 6 for 64QAM
N =128; % number of sub-channels
FFT_size=N;
Bandall = 10e6;
deltaf =15000;
Fs=deltaf*N; % sampling frequency
R=0.1;
K=4; % overlapping factor(good K=4) FBMC_Data_Rx
NumberOfFrame =500; % Number of Frame per SNR
OFDM_FFTsize = N; % size of FFT or IFFT for OFDM
F_OFDM_FFTsize=OFDM_FFTsize;
FBMC_FFTsize = N*K; % size of FFT or IFFT for FBMC
LengthOfGI = 9; % size of CP for OFDM
SNR_dB = 0:1:10; % signal to noise ratio
OFDM_BER_Rate = zeros(1, length(SNR_dB)); % rate of error bit for OFDM
GFDM_BER_Rate= zeros(1,length(SNR_dB));
BER_T = zeros(1, length(SNR_dB)); % Theoretical BER
NumOfRB = 3; % used RB number
NcarrierOfRB=12;
NumOfUsedSC = NcarrierOfRB * NumOfRB; % 12 * NumOfRB; % number of used subcarrier
Nc_Index = [1:NumOfUsedSC] + (N-NumOfUsedSC)/2;
%*********************************************************************************
%% Initialization
%*********************************************************************************
%% GFDM
[A]=one_filter(R,NumOfSymbol,N);
A_ZF=pinv(A'*A)*A';
%%------------------------------------------------------------------------------------------------------------%%
%% Transmitter
for snr_dB=1:length(SNR_dB)
snr_dB
NoisePower_OFDM = 10^(-SNR_dB(snr_dB)/10);
sgma_OFDM=sqrt(NoisePower_OFDM);
attn_OFDM=sgma_OFDM/sqrt(2);
% G_MMSE=pinv(NoisePower_OFDM/2*eye(N*NumOfSymbol,N*NumOfSymbol)+G'*G)*G';
for Frame=1:NumberOfFrame
%% Data generation
Data_In =randi([0,1],1,Modulation_Mode*NumOfUsedSC*NumOfSymbol );
%% QAM
[QAM_Data] =modulation36211(Data_In,2^Modulation_Mode); % 信号功率为1
%% OFDM Transmitter
[OFDM_Data_S,OFDM_In_Data]=OFDM_modulater(QAM_Data,Nc_Index,OFDM_FFTsize,NumOfSymbol,NumOfUsedSC,LengthOfGI);
%% GFDM
[GFDM_Data_S]=GFDM_Transmitter(OFDM_In_Data,NumOfSymbol,N,A,LengthOfGI);
%-----------------------------------------------------------------------------------------------------------%
%% AWGN CHANNEL
%--------------------------------------------------------------------------------------------------------------%
% OFDM
OFDM_Rx = add_awgnnoise(OFDM_Data_S,attn_OFDM);
% GFDM
GFDM_Rx = add_awgnnoise(GFDM_Data_S,attn_OFDM);
%% OFDM Receiver
[OFDM_FFT_Data]=OFDM_Receiver(OFDM_Rx,NumOfSymbol,N,LengthOfGI);
%% GFDM Receiver
[GFDM_Out_Data]=GFDM_Receiver(GFDM_Rx,NumOfSymbol,N,A_ZF,LengthOfGI);
%% 星座点
% plot(QAM_Data,'kx');
% hold on
% plot(FBMC_QAM_Data(Nc_Index,:),'ro');
%% OFDM QAM Demodulation
OFDM_In_Demod=reshape(OFDM_FFT_Data(Nc_Index,:) ,1,NumOfSymbol*NumOfUsedSC);
OFDM_rou=repmat(1/NoisePower_OFDM,1,NumOfSymbol*NumOfUsedSC);
[OFDM_Dem_Data]=newsoft_demodulation36211(OFDM_In_Demod,2^Modulation_Mode,OFDM_rou);
OFDM_Dem_Data = OFDM_Dem_Data<0;
OFDM_Dem_Data = OFDM_Dem_Data+0;
%% GFDM
GFDM_In_Demod=reshape(GFDM_Out_Data(Nc_Index,:),1,NumOfSymbol*NumOfUsedSC);
GFDM_rou=repmat(1/NoisePower_OFDM,1,NumOfSymbol*NumOfUsedSC);
[GFDM_Demod]=newsoft_demodulation36211(GFDM_In_Demod,2^Modulation_Mode,GFDM_rou);
GFDM_Demod = GFDM_Demod<0;
GFDM_Demod = GFDM_Demod+0;
GFDM_Data_Out = GFDM_Demod;
%------------------------------------------------------------------------------------------------------------%%
%% BER
%------------------------------------------------------------------------------------------------------------%%
[OFDM_err, OFDM_rate]= symerr(OFDM_Dem_Data,Data_In);
OFDM_BER_Rate(snr_dB)= OFDM_BER_Rate(snr_dB) + OFDM_err;
[GFDM_err0, F_GFDM_rate0]= symerr(GFDM_Data_Out,Data_In);
GFDM_BER_Rate(snr_dB)=GFDM_BER_Rate(snr_dB)+GFDM_err0;
end
% Average value
OFDM_BER_Rate(snr_dB)= OFDM_BER_Rate(snr_dB)/(NumberOfFrame*Modulation_Mode*NumOfUsedSC*NumOfSymbol); %
GFDM_BER_Rate(snr_dB)=GFDM_BER_Rate(snr_dB)/(NumberOfFrame*Modulation_Mode*NumOfUsedSC*NumOfSymbol);
end
%%------------------------------------------------------------------------------------------------------------%%
%% Figure
%%------------------------------------------------------------------------------------------------------------%%
figure(1);
EbNo =SNR_dB-10*log10(Modulation_Mode);
BER_T = berawgn(EbNo,'qam',2^Modulation_Mode,1);
semilogy(EbNo,OFDM_BER_Rate,'k-')
hold on
semilogy(EbNo,BER_T,'g-')
hold on
semilogy(EbNo,GFDM_BER_Rate,'b-x')
title('AWGN')
legend('OFDM','T','GFDM')
grid on;
xlabel( 'Eb/N0 in [dB]')
ylabel( 'Bit Error Rate (BER)')
% EbNo = EbNo.';
% OFDM_BER_Rate = OFDM_BER_Rate.';
% BER_T=BER_T.';
% FBMC_BER_Rate_ZF = FBMC_BER_Rate_ZF.';
% CS_FBMC_BER_Rate=CS_FBMC_BER_Rate.';
% F_OFDM_BER_Rate0=F_OFDM_BER_Rate0';
% FBMC_BER_Rate=FBMC_BER_Rate.';
% GFDM_BER_Rate=GFDM_BER_Rate.';
% save('FBMC_QAM in AWGN BER.mat','EbNo','OFDM_BER_Rate','FBMC_BER_Rate_ZF','CS_FBMC_BER_Rate','FBMC_BER_Rate');
% save('FBMC_QAM_PHYAYDS-MMSE in AWGN.mat','EbNo','FBMC_BER_Rate_ZF');