(04-15-2017, 06:52 PM)rnagesh Wrote: Can anybody please provide me MIMO-OFDM code for 16QAM?
Thanks
Soniya Kapur
% function [ofdm chan] = MIMO_OFDM_LSE_CHAN_EST(ofdmIn,chanIn)
clc
clear all
close all
%% Parameters
% system parameters (independent)
ofdm.Nb = 500; % number of blocks
ofdm.Nt = 1; % number of transmit antenna
ofdm.Nr = 1; % number of receive antenna
ofdm.K = 200; % number of subcarriers
ofdm.G = 1/4; % Guard interval percentage
ofdm.Mod = 4; % QPSK Modulation
ofdm.PSpace = 1; % pilot space between two pilots
% channel parameters
chan.L = 6; % number of channel taps between each transmit-receive antenna
% control parameters
ofdm.ifDemodulateData = 1; % (1,0) if 1, the code demodulates the transmitted data via LS algorithm, and calculates the BER
ofdm.ifDisplayResults = 1; % (1,0) if 1, displays the results in the command window
% dependent parameters
ofdm.PPos = 1:(ofdm.PSpace+1):ofdm.K; % OFDM pilot positionss
ofdm.PL = length(ofdm.PPos); % Length of pilot subcarriers
ofdm.DPos = setxor(1:ofdm.K,ofdm.PPos); % OFDM data positions
ofdm.DL = length(ofdm.DPos); % Length of data subcarriers
ofdm.BER = 0; % set the BER to zero
% normalization of the energy for the constelation %
% normalization factor attainig process happends
temp = 0:ofdm.Mod-1; % possible symbols
temp = qammod(temp,ofdm.Mod); % modulated symbols
temp = abs(temp).^2; % power of each point in the constellation
temp = mean(temp); % average energy of the constellation
ofdm.ModNorm = 1/sqrt(temp); % normaliztion factor
chan.SNR_dB = [0 1 2 3 4 5 6 7 8 9 10];% EbN0 in dB
F11=randi(255,200,10);
Etxt=F11;
Etxt11=reshape(Etxt,2000,1);
Etxt_bin=de2bi(Etxt11,'left-msb');
for i = 1:length(chan.SNR_dB)
% Make random data (0/1)
ber64qam(i) = 0;
for dtlp=1:8
tx=Etxt_bin(:,dtlp);
%%
%Interleaving coded data
s2=size(tx',2);
j=s2/4;
matrix=reshape(tx,j,4);
intlvddata = matintrlv(matrix',2,2)'; % Interleave.
intlvddata=intlvddata';
%% Binary to decimal conversion
dec=bi2de(intlvddata','left-msb');
%% 64-QAM Modulation
y(1,:) = qammod(dec,16);
for lp=1:200
ofdm.dMod(lp,:)=y;
end
%% Pilot insertion
for nt = 1 : ofdm.Nt
ofdm.dMod(ofdm.PPos,:,nt) = repmat(exp(-sqrt(-1)*2*pi*(nt-1)*chan.L*(1:ofdm.PL).'/ofdm.PL),1,ofdm.Nb); %
end
% checking the power of the transmit signal (it has to be 1 after normalization)
ofdm.pow = var(ofdm.dMod(:))+abs(mean(ofdm.dMod(:)))^2;
%% IFFT operation
ofdm.ifft = zeros(ofdm.K,ofdm.Nb,ofdm.Nt); % memory allocation for the ofdm blocks transmitted from each Tx antenna after ifft
for nt = 1 : ofdm.Nt
ofdm.ifft(:,:,nt) = sqrt(ofdm.K)*ifft(ofdm.dMod(:,:,nt),ofdm.K); % ifft is square root of the subcarrier values multiplied by ifft transformation
end
%% Cyclic perfix
% copy the end of signal to the begining of signal
ofdm.ifftG = [ofdm.ifft(ofdm.K*(1-ofdm.G)+1:ofdm.K,:,:);ofdm.ifft]; % the gaurd intervel position is taken frm the 75 percent value of the original ifft data to final values of the data
%% Channel
% for each block we generate a rayleigh fading MIMO channel which is fixed over a block
chan.Coeff = 1/sqrt(2)*1/sqrt(chan.L)*(randn(ofdm.Nt,ofdm.Nr,chan.L,ofdm.Nb)+sqrt(-1)*randn(ofdm.Nt,ofdm.Nr,chan.L,ofdm.Nb));
Rhh=chan.Coeff;
%% Channel pass and filter
chan.sigma = sqrt(10^(-0.1*chan.SNR_dB(i))); % noise power
if ofdm.K*ofdm.G < chan.L+1
error('Guard interval is shorter than channel length, and the system does not function properly')
end
ofdm.Y = zeros(ofdm.K*(1+ofdm.G),ofdm.Nb,ofdm.Nr);
% adding noise
ofdm.Y = ofdm.ifftG + chan.sigma*1/sqrt(2)*( randn(ofdm.K*(1+ofdm.G),ofdm.Nb,ofdm.Nr)+...
sqrt(-1)*randn(ofdm.K*(1+ofdm.G),ofdm.Nb,ofdm.Nr) );
%% Cyclic prefix removal
ofdm.fftG = ofdm.Y(ofdm.K*ofdm.G+1:ofdm.K*(1+ofdm.G),:,:);
%% FFT operation
ofdm.fft = zeros(ofdm.K,ofdm.Nb,ofdm.Nr);
for nr = 1 : ofdm.Nr
ofdm.fft(:,:,nr) = 1/sqrt(ofdm.K)*fft(ofdm.fftG(:,:,nr),ofdm.K);
end
%% Channel estimation
% building the first L columns of the fft matrix
F = dftmtx(ofdm.K);
F = F(:,1:chan.L);
% Memory allocation for the estimated channel coefficients
chan.CoeffEst = zeros(ofdm.Nt,ofdm.Nr,chan.L,ofdm.Nb);
for nb = 1 : ofdm.Nb
for nr = 1 : ofdm.Nr
% Building matrix A (see the paper)
chan.A = zeros(ofdm.PL,chan.L*ofdm.Nt);
for nt = 1 : ofdm.Nt
chan.A(:,(1:chan.L)+(nt-1)*chan.L) = diag(ofdm.dMod(ofdm.PPos,nb,nt))*F(ofdm.PPos,:);
end
ChanEst = pinv(chan.A)*ofdm.fft(ofdm.PPos,nb,nr);
for nt = 1 : ofdm.Nt
chan.CoeffEst(nt,nr,:,nb) = ChanEst((1:chan.L)+(nt-1)*chan.L);
end
end
end
%% Demodulation
if ofdm.ifDemodulateData == 1 && ofdm.DL > 0
% Building channel coefficients in frequency domain
chan.CoeffEstFreq = zeros(ofdm.K,ofdm.Nt,ofdm.Nr,ofdm.Nb);
for nb = 1 : ofdm.Nb
for nr = 1 : ofdm.Nr
for nt = 1 : ofdm.Nt
chan.CoeffEstFreq(:,nt,nr,nb) = F*squeeze(chan.CoeffEst(nt,nr,:,nb));
end
end
end
% demodulation
ofdm.dDemod = zeros(ofdm.DL,ofdm.Nb,ofdm.Nt);
for nb = 1 : ofdm.Nb
for dl = 1 : ofdm.DL
ofdm.dDemod(dl,nb,:) = pinv(reshape(chan.CoeffEstFreq(ofdm.DPos(dl),:,:,nb),ofdm.Nt,ofdm.Nr).')...
*squeeze(ofdm.fft(ofdm.DPos(dl),nb,:));
end
end
%% detection
%% Demodulation
dem_data= qamdemod(ofdm.dDemod,16);
%% Decimal to binary conversion
bin=de2bi(dem_data(1,:)','left-msb');
bin = imresize(bin,[500 4]);
bin=bin';
%% De-Interleaving
deintlvddata = matdeintrlv(bin,2,2); % De-Interleave
deintlvddata=deintlvddata';
rx=reshape(deintlvddata,2000,1);
dtrxd(:,dtlp)=rx;
% disp('processing ... ')
end
end
dtrxd = uint8(dtrxd);
data_rxd=bi2de(dtrxd,'left-msb');
ch_ext=reshape(data_rxd,200,10);
Dtext=ch_ext;
% Hacker scenario chaos code disabled
hack_ext=reshape(data_rxd,200,10);
F11_rp=reshape(F11,2000,1);
Dtext_rp=reshape(Dtext,2000,1);
hack_rp=reshape(hack_ext,2000,1);
% F11_bin=de2bi(F11_rp);
% hack_ext_bin=de2bi(hack_rp);
% Dtext_bin=de2bi(Dtext_rp);
Hacker_ber1x(i,:)=(length(find(F11_rp~=hack_rp)))/1000;
% Hack_ber=
orig_ue_ber1x(i,:)=(length(find(F11_rp~=Dtext_rp)))/(16*1000);
% orig_ber=
end % for j`
% Get average of BER
% disp(['BER :',num2str(berbpsk)]);
save A11x Hacker_ber1x
save B11x orig_ue_ber1x
% Plot the result
semilogy(chan.SNR_dB, Hacker_ber1x, 'r*-');
hold on
semilogy(chan.SNR_dB, orig_ue_ber1x, 'g*-');
xlabel('SNR [dB]')
ylabel('MSE of LSE channel estimation')
grid on
legend('Nc=16 Eavesdropper','Nc=16 Legitimate');
% title(['Nt = ',num2str(ofdm.Nt),', Nr = ',num2str(ofdm.Nr),',BPSK']);