Professional Documents
Culture Documents
MIMO PRACTICALS Eith MATLAB Codes
MIMO PRACTICALS Eith MATLAB Codes
FOR
MIMO
PEC111
1 | Page
Table of Contents
EXPERIMENT-1 ...................................................................................................................................................... 3
EXPERIMENT – 1.1 ............................................................................................................................................ 3
EXPERIMENT – 1.2 ............................................................................................................................................ 4
EXPERIMENT – 1.3 ............................................................................................................................................ 5
EXPERIMENT – 1.4 ............................................................................................................................................ 7
EXPERIMENT – 2 ................................................................................................................................................... 8
EXPERIMENT – 3 ................................................................................................................................................... 9
EXPERIMENT – 4 ................................................................................................................................................. 11
EXPERIMENT – 5 ................................................................................................................................................. 14
2 | Page
Lab-MIMO
KAPIL JOSHI
802361003
EXPERIMENT-1
EXPERIMENT – 1.1
% 1_1 (Free space path loss)
clc;
clear all;
close all;
d = 1:10;
pl = zeros(d);
Pt = 1;
Gt = 1;
Gr = 1;
f = 1.5*10^9;
c = 3*10^8;
l = c/f;
Pl = [];
for i = 1:10
Pl(i) = 20*log(4*pi*d(i)/l);
end
figure(1);
plot(d,Pl);
hold on;
Gt = 1;
Gr = 0.5;
Pt = 1;
%Pr = Pt*Gt*Gr*l^2/(4*pi*d)^2;
Pn = [];
for i = 1:10
Pn(i) = 10*log((4*pi*d(i))^2/(Gt*Gr*l^2));
end
plot(d,Pn);
hold on;
Gt = 0.5;
Gr = 0.5;
Pt = 1;
Pn = [];
for i = 1:10
Pn(i) = 10*log((4*pi*d(i))^2/(Gt*Gr*l^2));
end
plot(d,Pn);
xlabel('Distance');
3 | Page
ylabel('Path loss');
title('Large scale fading') ;
legend('Gt=1, Gr=1', 'Gt=1, Gr=0.5', 'Gt=0.5, Gr=0.5');
Plot
EXPERIMENT – 1.2
% 1_2 Path loss exponent(n)
clc;
clear all;
close all;
d = 1:10;
pl = zeros(1, 10);
Pt = 1;
Gt = 1; % Assuming Gt is a scalar
Gr = 0.5; % Assuming Gr is a scalar
f = 1.5e9;
c = 3e8;
lb = c/f;
Plf = zeros(1, 10);
for i = 1:10
Plf(i) = 20*log10(4*pi*d(i)/lb);
end
figure;
plot(d, Plf);
xlabel('Distance (m)');
ylabel('Path Loss (dB)');
title('Free Space Path Loss');
for j = 1:length(n_values)
4 | Page
for i = 1:length(d)
Pld(i, j) = Plf(i) + 10*n_values(j)*log10(d(i)/do);
end
end
figure;
plot(d, Pld(:, 1), d, Pld(:, 2), d, Pld(:, 3));
xlabel('Distance (m)');
ylabel('Path Loss with Log-distance model (dB)');
title('Log-distance Path Loss Model');
legend('n = 1', 'n = 2', 'n = 3');
PLOT-
EXPERIMENT – 1.3
clc;
clear all;
close all;
d = 1:10;
pl = zeros(1, 10);
Pt = 1;
Gt = 1; % Assuming Gt is a scalar
Gr = 0.5; % Assuming Gr is a scalar
f = 1.5e9;
c = 3e8;
lb = c/f;
Plf = zeros(1, 10);
for i = 1:10
Plf(i) = 20*log10(4*pi*d(i)/lb);
end
5 | Page
figure;
plot(d, Plf);
xlabel('Distance (m)');
ylabel('Path Loss (dB)');
title('Free Space Path Loss');
do = 1000;
n_values = [1, 2, 3];
Pld = zeros(length(d), length(n_values));
for j = 1:length(n_values)
for i = 1:length(d)
Pld(i, j) = Plf(i) + 10*n_values(j)*log10(d(i)/do);
end
end
figure;
plot(d, Pld(:, 1), d, Pld(:, 2), d, Pld(:, 3));
xlabel('Distance (m)');
ylabel('Path Loss with Log-distance model (dB)');
title('Log-distance Path Loss Model');
legend('n = 1', 'n = 2', 'n = 3');
num_samples = length(d);
Plg = zeros(num_samples, 3);
for j = 1:3
Plg(:, j) = Pld(:, j) + normrnd(0, Xg, num_samples, 1);
end
figure;
plot(d, Plg(:, 1), d, Plg(:, 2), d, Plg(:, 3));
xlabel('Distance (m)');
ylabel('Path Loss with Gaussian term (dB)');
title('Path Loss with Gaussian Term');
legend('n = 1', 'n = 2', 'n = 3');
PLOT –
6 | Page
EXPERIMENT – 1.4
% 1_4 (gaussian)
clc;
clear all;
close all;
d = 1:10;
pl = zeros(1, 10);
Pt = 1;
Gt = 1; % Assuming Gt is a scalar
Gr = 0.5; % Assuming Gr is a scalar
f = 1.5e9;
c = 3e8;
lb = c/f;
Plf = zeros(1, 10);
for i = 1:10
Plf(i) = 20*log10(4*pi*d(i)/lb);
end
figure;
plot(d, Plf);
xlabel('Distance (m)');
ylabel('Path Loss (dB)');
title('Free Space Path Loss');
do = 1000;
n = 2; % Fixed value for n
Pld = Plf + 10*n*log10(d/do);
num_samples = 4;
Plg = Pld + normrnd(0, Xg, num_samples, 1);
figure;
plot(d, Plg);
xlabel('Distance (m)');
ylabel('Path Loss with Gaussian term (dB)');
title('Path Loss with Gaussian Term (n = 2)')
PLOT –
EXPERIMENT – 2
CODE –
clc;
clear all;
close all;
% MIMO channel matrix
H = [1,2,3,4];
8 | Page
H_samples(:,:,i) = (1/sqrt(2)) * (randn(2,2) + 1i * randn(2,2));
end
% Calculate expectation of H
E_H = mean(H_samples, 3);
PLOT -
EXPERIMENT – 3
CODE -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All rights reserved by Krishna Pillai, http://www.dsplog.com
% The file may not be re-distributed without explicit authorization
% from Krishna Pillai.
% Checked for proper operation with Octave Version 3.0.0
% Author : Krishna Pillai
% Email : krishna@dsplog.com
% Version : 1.0
% Date : 02nd Novemeber 2008
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
N = 10^6; % number of bits or symbols
Eb_N0_dB = [0:25]; % multiple Eb/N0 values
nTx = 2;
nRx = 2;
for ii = 1:length(Eb_N0_dB)
9 | Page
% Transmitter
ip = rand(1,N)>0.5; % generating 0,1 with equal probability
s = 2*ip-1; % BPSK modulation 0 -> -1; 1 -> 0
sMod = kron(s,ones(nRx,1)); %
sMod = reshape(sMod,[nRx,nTx,N/nTx]); % grouping in
[nRx,nTx,N/NTx ] matrix
h = 1/sqrt(2)*[randn(nRx,nTx,N/nTx) + j*randn(nRx,nTx,N/nTx)]; %
Rayleigh channel
n = 1/sqrt(2)*[randn(nRx,N/nTx) + j*randn(nRx,N/nTx)]; % white
gaussian noise, 0dB variance
% Receiver
end
10 | P a g e
close all
figure
semilogy(Eb_N0_dB,theoryBer_nRx1,'bp-','LineWidth',2);
hold on
semilogy(Eb_N0_dB,theoryBerMRC_nRx2,'kd-','LineWidth',2);
semilogy(Eb_N0_dB,simBer,'mo-','LineWidth',2);
axis([0 25 10^-5 0.5])
grid on
legend('theory (nTx=2,nRx=2, ZF)', 'theory (nTx=1,nRx=2, MRC)', 'sim
(nTx=2, nRx=2, MMSE)');
xlabel('Average Eb/No,dB');
ylabel('Bit Error Rate');
title('BER for BPSK modulation with 2x2 MIMO and MMSE equalizer
(Rayleigh channel)');
PLOT -
EXPERIMENT – 4
CODE –
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% All rights reserved by Krishna Pillai, http://www.dsplog.com
% The file may not be re-distributed without explicit authorization
% from Krishna Pillai.
% Checked for proper operation with Octave Version 3.0.0
% Author : Krishna Pillai
% Email : krishna@dsplog.com
% Version : 1.0
11 | P a g e
% Date : 23rd October 2008
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
close all;
clear all;
N = 10^6; % number of bits or symbols
Eb_N0_dB = [0:25]; % multiple Eb/N0 values | energy per bit to
noise power spectral density ratio from 0 to 25 dB
nTx = 2; % no. of Tx antenna
nRx = 2; % no. of rx antenna
for ii = 1:length(Eb_N0_dB)
% Transmitter
ip = rand(1,N)>0.5; % generating 0,1 with equal probability
s = 2*ip-1; % BPSK modulation 0 -> -1; 1 -> 0
sMod = kron(s,ones(nRx,1)); %
sMod = reshape(sMod,[nRx,nTx,N/nTx]); % grouping in
[nRx,nTx,N/NTx ] matrix
h = 1/sqrt(2)*[randn(nRx,nTx,N/nTx) + j*randn(nRx,nTx,N/nTx)];
% Rayleigh channel
n = 1/sqrt(2)*[randn(nRx,N/nTx) + j*randn(nRx,N/nTx)]; % white
gaussian noise, 0dB variance
% Receiver
12 | P a g e
yMod = sum(hMod.*yMod,1); % H^H * y collapsing to 1D array
yMod = kron(reshape(yMod,2,N/nTx),ones(1,2)); % formatting
reshaping 1D to 2D maatrix | kron replicates each column twice
horizontally
yHat = sum(reshape(hInv,2,N).*yMod,1); % inv(H^H*H)*H^H*y |
reshapes into 2xN matrix |sum collapses result into 1 D array |
stap in zF equalization
end
close all
figure
semilogy(Eb_N0_dB,theoryBer_nRx1,'bp-','LineWidth',2);
hold on
semilogy(Eb_N0_dB,theoryBerMRC_nRx2,'kd-','LineWidth',2);
semilogy(Eb_N0_dB,simBer,'mo-','LineWidth',2);
axis([0 25 10^-5 0.5])
grid on
legend('theory (nTx=1,nRx=1)', 'theory (nTx=1,nRx=2, MRC)', 'sim
(nTx=2, nRx=2, ZF)');
xlabel('Average Eb/No,dB');
ylabel('Bit Error Rate');
title('BER for BPSK modulation with 2x2 MIMO and ZF equalizer
(Rayleigh channel)');
PLOT -
13 | P a g e
EXPERIMENT – 5
CODE -
clc;
clear all;
close all;
%% +Overview -----------------------------------------------------
---------
% In this code, we drive the Capacity of a MIMO system over
Rayleigh fading
% channel with different number of transmit and receiver antennas.
For each
% setting of a MIMO channel, after calculating the singular values
of the
% channel matrix, transmit power is distributed based on water-
filling
% algorithm. The obtained capacity is the best that MIMO system
can deliver
% as the full channel state information is used both at TX and RX
side.
% -Overview -----------------------------------------------------
---------
%% +Clear --------------------------------------------------------
---------
clear % clear all variables in the workspace
close all % close all open figures
clc % clear command window
% -Clear --------------------------------------------------------
---------
%% +Independent parameters ---------------------------------------
---------
% system parameters
14 | P a g e
numOfTxAntennas = [1 2 3 2 4]; % a vector to hold number of
antennas at
% the transmitter side
numOfRxAntennas = [1 2 2 3 4]; % a vector to hold number of
antennas at
% the receiver side
% related to AWGN channel
noisePower = 1e-4; % AWGN noise power
signalToNoiseRatio_dB = -10:3:20; % a vector to hold SNR
values
% loop parameters
numOfIterations = 1e4; % number of iterations (should
be above
% 1e3 for accurate result)
% related to plots
curveType = {'b.-';'rs-';'g+--';'k--^';'m--d'};
% -Independent parameters ---------------------------------------
---------
zeros(nOfAntennasVec,nOfsignalToNoiseRatio,numOfIterations);
% allocate memory for singular value decompostion
lambdaVec = ...
zeros(nOfAntennasVec,nOfsignalToNoiseRatio,numOfIterations,nOfAnte
nnasVec);
% -Dependent parameters -----------------------------------------
---------
15 | P a g e
%% +Main ---------------------------------------------------------
---------
for n = 1 : nOfAntennasVec % loop over number of antennas
nTx = numOfTxAntennas(n); % num of Tx antennas in this
iteration
nRx = numOfRxAntennas(n); % num of Rx antennas in this
iteration
for j = 1 : nOfsignalToNoiseRatio
% load SNR for this iteration
snr = signalToNoiseRatio(j); % SNR at this
iteration
% calaculate transmit power
txPower = noisePower*snr; % we know SNR =
txPower/noisePower
% with the assumption
that
% there is no
pathloss. It
% means here txPower =
rxPower;
for i = 1 : numOfIterations % loop over number of
iterations
% generate channel coefficients
h = 1/sqrt(2)*(randn(nRx,nTx)+1i*randn(nRx,nTx));
% calaculate singular value decomposition
S = svd(h);
% store values of lambda in SVD
lambdaVec(n,j,i,1:min(nRx,nTx)) = S;
% find carrier to noise rations
cnr = S.^2/noisePower;
% find allocated power based on waterfilling
algorithm
allocatedPower = waterFilling(cnr,txPower);
% calculate the capacity
capacity = sum(log2(1+allocatedPower.*cnr));
% store the value
CapacityVec(n,j,i) = capacity;
end
end
end
% -Main ---------------------------------------------------------
---------
%% +Post processing ----------------------------------------------
---------
% take the average of the capacity
CapacityVecAvg = mean(CapacityVec,3);
16 | P a g e
clf
legendStr = cell(nOfAntennasVec,1);
for n = 1 : nOfAntennasVec % loop over number of antennas
plot(signalToNoiseRatio_dB,CapacityVecAvg(n,:),curveType{n})
hold on
legendStr{n} = sprintf('nTx = %d, nRx = %d',...
numOfTxAntennas(n),numOfRxAntennas(n));
end
xlabel('SNR [dB]')
ylabel('Capacity [b/s/Hz]')
grid on
title('Avg Capacity of a MIMO system for different num of tx-
rx antennas')
legend(legendStr,'location','best')
% plot pdf of lambda at target SNR for a given nTx and nRx
n = 5; % select a setting from nTx and nRx
nTx = numOfTxAntennas(n); % num of Tx antennas in this
setting
nRx = numOfRxAntennas(n); % num of Rx antennas in this
setting
f3 = figure(3);
clf
legendStr = cell(min(nTx,nRx),1);
for k = 1 : min(nTx,nRx)
lambdas = lambdaVec(n,Ind,:,k);
[y,x] = hist(lambdas(:),30);
% update y
y = y ./ sum(y) ; % normalization
plot(x,y,curveType{k})
hold on
if k == 1
legendStr{k} = '1st singular value';
elseif k == 2
legendStr{k} = '2nd singular value';
17 | P a g e
else
legendStr{k} = sprintf('%d-th singular value',k);
end
end
ylabel('Nolmalized probability')
xlabel('singular value')
legend(legendStr)
grid on
title(sprintf('normalized pdf of svd values for nTx=%d,
nRx=%d',nTx,nRx))
% -Figures ------------------------------------------------------
---------
%% +Save Figures -------------------------------------------------
---------
%saveas(f1,'avgCapacity','jpg');
%saveas(f2,'cdfCapacity','jpg');
%saveas(f3,'pdfLambdas','jpg');
% -Save Figures -------------------------------------------------
---------
%%
function P = waterFilling(CNR,Pmax)
%#codegen
% initial power allocation
initP = (Pmax + sum(1./CNR)) ./ ( length(CNR) ) - 1./CNR;
% waterfilling algorithm
while any( initP < 0 )
negIndex = initP <= 0;
posIndex = initP > 0;
NkRem = nnz(posIndex);
CNRRem = CNR(posIndex);
powAllcTemp = (Pmax + sum(1./CNRRem)) ./ (NkRem) -
1./CNRRem;
initP(negIndex) = 0;
initP(posIndex) = powAllcTemp;
end
P = initP;
end
PLOT -
18 | P a g e
19 | P a g e
20 | P a g e