0% found this document useful (0 votes)
27 views20 pages

MIMO PRACTICALS Eith MATLAB Codes

The document describes experiments conducted on MIMO systems. It includes 5 experiments on topics like free space path loss, path loss exponent, introducing gaussian noise, calculating correlation and covariance matrices of MIMO channels, and calculating BER performance of MIMO systems with BPSK modulation over Rayleigh fading channels using MMSE equalization.

Uploaded by

kjoshime23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views20 pages

MIMO PRACTICALS Eith MATLAB Codes

The document describes experiments conducted on MIMO systems. It includes 5 experiments on topics like free space path loss, path loss exponent, introducing gaussian noise, calculating correlation and covariance matrices of MIMO channels, and calculating BER performance of MIMO systems with BPSK modulation over Rayleigh fading channels using MMSE equalization.

Uploaded by

kjoshime23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

A PRACTICAL REPORT

FOR

MIMO

PEC111

Submitted by: Submitted to:

KAPIL JOSHI Ms. Devyani Ghosh

802361003 ECED Department

ELECTRONICS AND COMMUNICATION ENGINEERNG DEPARTMENT


THAPAR INSTITUTE OF ENGINEERING AND TECHNOLOGY (DEEMED
TO BE UNIVERSITY)
PATIALA, PUNJAB
AUGUST-DECEMBER 2023

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');

%% Path loss exponent (n)


do = 1000;
n_values = [1, 2, 3];
Pld = zeros(length(d), length(n_values));

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');

% Introduce Gaussian term for each n


Xg_dB = 3; % Gaussian term in dB
Xg = 10^(Xg_dB/10); % Convert dB to linear scale

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);

% Introduce Gaussian term for n = 2


7 | Page
Xg_dB = 3; % Gaussian term in dB
Xg = 10^(Xg_dB/10); % Convert dB to linear scale

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];

% Number of Monte Carlo simulations for expectation calculation


numSimulations = 10000;

% Initialize matrices for Monte Carlo simulations


H_samples = zeros(2, 2, numSimulations);

% Generate complex Gaussian samples for Monte Carlo simulations


for i = 1:numSimulations

8 | Page
H_samples(:,:,i) = (1/sqrt(2)) * (randn(2,2) + 1i * randn(2,2));
end

% Calculate correlation matrix


R = mean(H_samples .* conj(permute(H_samples, [2, 1, 3])), 3);

% Calculate expectation of H
E_H = mean(H_samples, 3);

% Calculate covariance matrix


C = mean((H_samples - E_H) .* conj(permute(H_samples - E_H, [2, 1,
3])), 3);

disp('Correlation Matrix (R):');


disp(R);
disp('Covariance Matrix (C):');
disp(C);

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 : [email protected]
% Version : 1.0
% Date : 02nd Novemeber 2008
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Script for computing the BER for BPSK modulation in a


% Rayleigh fading channel with 2 Tx, 2Rx MIMO channel
% Minimum Mean Square Error equalization

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

% Channel and noise Noise addition


y = squeeze(sum(h.*sMod,2)) + 10^(-Eb_N0_dB(ii)/20)*n;

% Receiver

% Forming the MMSE equalization matrix W =


inv(H^H*H+sigma^2*I)*H^H
% H^H*H is of dimension [nTx x nTx]. In this case [2 x 2]
% Inverse of a [2x2] matrix [a b; c d] = 1/(ad-bc)[d -b;-c a]
hCof = zeros(2,2,N/nTx) ;
hCof(1,1,:) = sum(h(:,2,:).*conj(h(:,2,:)),1) + 10^(-
Eb_N0_dB(ii)/10); % d term
hCof(2,2,:) = sum(h(:,1,:).*conj(h(:,1,:)),1) + 10^(-
Eb_N0_dB(ii)/10); % a term
hCof(2,1,:) = -sum(h(:,2,:).*conj(h(:,1,:)),1); % c term
hCof(1,2,:) = -sum(h(:,1,:).*conj(h(:,2,:)),1); % b term
hDen = ((hCof(1,1,:).*hCof(2,2,:)) -
(hCof(1,2,:).*hCof(2,1,:))); % ad-bc term
hDen = reshape(kron(reshape(hDen,1,N/nTx),ones(2,2)),2,2,N/nTx);
% formatting for division
hInv = hCof./hDen; % inv(H^H*H)

hMod = reshape(conj(h),nRx,N); % H^H operation

yMod = kron(y,ones(1,2)); % formatting the received symbol for


equalization
yMod = sum(hMod.*yMod,1); % H^H * y
yMod = kron(reshape(yMod,2,N/nTx),ones(1,2)); % formatting
yHat = sum(reshape(hInv,2,N).*yMod,1); % inv(H^H*H)*H^H*y

% receiver - hard decision decoding


ipHat = real(yHat)>0;

% counting the errors


nErr(ii) = size(find([ip- ipHat]),2);

end

simBer = nErr/N; % simulated ber


EbN0Lin = 10.^(Eb_N0_dB/10);
theoryBer_nRx1 = 0.5.*(1-1*(1+1./EbN0Lin).^(-0.5));
p = 1/2 - 1/2*(1+1./EbN0Lin).^(-1/2);
theoryBerMRC_nRx2 = p.^2.*(1+2*(1-p));

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 : [email protected]
% Version : 1.0
11 | P a g e
% Date : 23rd October 2008
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Script for computing the BER for BPSK modulation in a


% Rayleigh fading channel with 2 Tx, 2Rx MIMO channel
% Zero Forcing equalization

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

% Channel and noise Noise addition


y = squeeze(sum(h.*sMod,2)) + 10^(-Eb_N0_dB(ii)/20)*n;

% Receiver

% Forming the Zero Forcing equalization matrix W =


inv(H^H*H)*H^H
% H^H*H is of dimension [nTx x nTx]. In this case [2 x 2]
% Inverse of a [2x2] matrix [a b; c d] = 1/(ad-bc)[d -b;-c a]
hCof = zeros(2,2,N/nTx) ;
hCof(1,1,:) = sum(h(:,2,:).*conj(h(:,2,:)),1); % d term
hCof(2,2,:) = sum(h(:,1,:).*conj(h(:,1,:)),1); % a term
hCof(2,1,:) = -sum(h(:,2,:).*conj(h(:,1,:)),1); % c term
hCof(1,2,:) = -sum(h(:,1,:).*conj(h(:,2,:)),1); % b term
hDen = ((hCof(1,1,:).*hCof(2,2,:)) -
(hCof(1,2,:).*hCof(2,1,:))); % ad-bc term
hDen =
reshape(kron(reshape(hDen,1,N/nTx),ones(2,2)),2,2,N/nTx); %
formatting for division
hInv = hCof./hDen; % inv(H^H*H)

hMod = reshape(conj(h),nRx,N); % H^H operation

yMod = kron(y,ones(1,2)); % formatting the received symbol for


equalization

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

% receiver - hard decision decoding


ipHat = real(yHat)>0;

% counting the errors


nErr(ii) = size(find([ip- ipHat]),2);

end

simBer = nErr/N; % simulated ber


EbN0Lin = 10.^(Eb_N0_dB/10);
theoryBer_nRx1 = 0.5.*(1-1*(1+1./EbN0Lin).^(-0.5));
p = 1/2 - 1/2*(1+1./EbN0Lin).^(-1/2);
theoryBerMRC_nRx2 = p.^2.*(1+2*(1-p));

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 ---------------------------------------
---------

%% +Check Independent parameters ---------------------------------


---------
nOfAntennasVecTx = length(numOfTxAntennas);
nOfAntennasVecRx = length(numOfTxAntennas);
if nOfAntennasVecTx ~= nOfAntennasVecRx
error('Vectors numOfTxAntennas and numOfRxAntennas must
have the same size')
else
nOfAntennasVec = nOfAntennasVecRx; % number of elements
in antennas
end
if nOfAntennasVec < length(curveType)
error('Number of elements in numOfTxAntennas must be
smaller than %d.',...
length(curveType))
end
% -Check Independent parameters ---------------------------------
---------
%% +Dependent parameters -----------------------------------------
---------
% convert from dB to scalar
signalToNoiseRatio = 10.^(0.1*signalToNoiseRatio_dB);
% number of elements in SNR vector
nOfsignalToNoiseRatio = length(signalToNoiseRatio);
% allocate memory for capacity
CapacityVec = ...

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);

% -Post processing ----------------------------------------------


---------
%% +Figures ------------------------------------------------------
---------
% plot capacity for different number of Tx and Rx antennas vs
SNR
f1 = figure(1);

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 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';

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

You might also like