MIMO Exp5
MIMO Exp5
KAPIL JOSHI
802361003
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
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
16 | P a g e
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 -----------------------------------------
---------
%% +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
17 | P a g e
% 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);
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')
18 | P a g e
% plot distribution of capacity at SNR = 5dB
f2 = figure(2);
clf
targetSNRdB = 5;
Ind = find(signalToNoiseRatio_dB == targetSNRdB);
for n = 1 : nOfAntennasVec % loop over number of antennas
capacities = CapacityVec(n,Ind,:);
[y,x] = hist(capacities(:),30);
% update y
y = y ./ sum(y) ; % normalization
plot(x,cumsum(y),curveType{n})
hold on
end
xlabel('Capacity [b/s/Hz]')
ylabel('Probability of Capacity < GivenCapacity')
grid on
title(sprintf('CDF of the Capacity at SNR =
%ddB',targetSNRdB))
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';
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');
19 | P a g e
%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 -
20 | P a g e
21 | P a g e