keep showing error at line 21 and the plot for CDF of capacity not appering?
2 次查看(过去 30 天)
显示 更早的评论
N = 100; % Total number of cell users
CellRadius = 1000; % Circular cell radius in meters
BSx1 = 0; % X-coordinate of base station 1
BSy1 = 0; % Y-coordinate of base station 1
BSx2 = 950; % X-coordinate of base station 2
BSy2 = 1650; % Y-coordinate of base station 2
BSx3 = 1950; % X-coordinate of base station 3
BSy3 = 0; % Y-coordinate of base station 3
Freq = 2600; % Carrier frequency expressed in MHz
Ptx = 39; % Base station transmit power in dBm
AntGain = 10; % Antenna gain in dB
bandwidth = 10*10^6; % Bandwidth in MHz
% Creating cells 1 2 3 with 100 users
[UserDists, UserCoordinates1, UserCoordinates2, UserCoordinates3] = PlotCell(N, CellRadius, BSx1, BSy1, BSx2, BSy2, BSx3,BSy3);
% Calculate path loss using the "CalculatePL" function
[TotalPL] = CalculatePL(N, UserDists, Freq);
% Calculate link budget using the "CalculateLinkBudget" function
[linkBudget] = CalculateLinkBudget(N, Ptx, AntGain, TotalPL);
% Calculating interferance
[interferance] = calculateInterference(Ptx,AntGain,N,Freq,BSx1,BSy1,BSx2,BSy2,BSx3,BSy3,UserCoordinates1, UserCoordinates2, UserCoordinates3);
% Calculating the SINR value for all users
[SINR] = CalculateSINR(linkBudget, interference);
% Calculating the shannon capacity data rate
[capacity] = CalculateShannonCapacity(SINR);
% Plotting CDF of Capacity for all users
x = capacity(:);
figure(2)
cdfplot(x)
xlabel('Shannon Capacity')
ylabel('CDF of Shannon Capacity')
function [x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx, BSy)
% Equations to create 50 random x, y points.
ran = CellRadius* sqrt(rand(1, N));
th = 2* pi * rand(1, N);
X = BSx + ran.* cos(th);
Y = BSy + ran.* sin(th);
% Creating Cells
th2 = linspace(0,2*pi);
x = CellRadius*cos(th2) + BSx;
y = CellRadius*sin(th2) + BSy;
end
function [UserDists, UserCoordinates1, UserCoordinates2, UserCoordinates3] = PlotCell(N, CellRadius, BSx1, BSy1, BSx2, BSy2, BSx3, BSy3)
% Plotting first cell with random users (N) and given BS axis
[x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx1, BSy1);
% Plotting the first cell
plot(x, y, "k", BSx1, BSy1, 'rx', X, Y, "bo"); %
UserDists1 = ran;
% Saving coordinates of all users of first cell in a matrix
UserCoordinates1 = [X, Y];
hold on
axis equal
% Plotting second cell
[x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx2, BSy2);
plot(x, y, "k", BSx2, BSy2, 'rx', X, Y, "bo");
UserDists2 = ran;
% Saving coordinates of all users of second cell in a matrix
UserCoordinates2 = [X, Y];
% Plotting third cell
[x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx3, BSy3);
plot(x, y, "k", BSx3, BSy3, 'rx', X, Y, "bo");
UserDists3 = ran;
% Saving coordinates of all users of third cell in a matrix
UserCoordinates3 = [X, Y];
hold off
axis equal
UserDists = [UserDists1, UserDists2, UserDists3]; % This variable stores the distances of all users according to their cell's Base Station in a matrix
end
function [TotalPL] = CalculatePL(N, UserDists, Freq)
% This function calculates and returns the path loss in dB for each user.
d0 = 1;
size=N;
n=2;
variance=8;
lambda = (3 * 10^8) / Freq;
% Calculating path loss in dB due to propagation.
PL = 20 * log10(4 * pi * d0 ./ lambda) + 10 * 2 * log10(UserDists / d0);
SF = randn(size,1)* variance;
% Calculating total path loss in the vector "TotalPL".
TotalPL = PL + SF;
end
function [linkBudget] = CalculateLinkBudget(Ptx, AntGain, TotalPL)
% This function computes the total link budget based on the path loss.
linkBudget = Ptx + AntGain - TotalPL;
end
function [Udist] = CalculateUserDistances(BSx, BSy, UserCords)
% The function finds the distances from the user to the base station of other cell.
arr = [];
for r = 1:length(UserCords)
Cell = [UserCords(r, 1), UserCords(r, 2); BSx, BSy];
d = pdist(Cell, 'euclidean');
arr(r) = d;
end
% Returns user distance from the base station
Udist = transpose(arr);
end
function [interference] = calculateInterference(Ptx, AntGain, N, Freq, BSX1, BSY1, BSX2, BSY2, BSX3, BSY3, UserCoordinates1, UserCoordinates2, UserCoordinates3)
% This function calculates interference for all the users in 3 cells.
de = 1;
n = 2; % Path loss exponent
variance = 8;
lambda = (3 * 10^8) / Freq;
size = N;
% Calculating Interference for the users of cell 1 to the Base station of cell 2 & 3
[Udist] = CalculateUserDistances(BSX2, BSY2, UserCoordinates1); % Calculating user distances from the users of cell 1 to the BS of cell 2
Udist1_2 = Udist;
PL1_2 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist1_2 / de); % Calculating Path loss for the users of cell 1 to the BS of cell 2
SF1_2 = randn(size, 1) * variance; % Calculating shadowing loss for the users of cell 1 to the BS of cell 2
TotalPL1_2 = PL1_2 + SF1_2; % Calculating Total path loss for the users of cell 1 to the BS of cell 2
interference1_2 = Ptx + AntGain - TotalPL1_2; % Calculating interference for the users of cell 1 to the BS of cell 2
[Udist] = CalculateUserDistances(BSX3, BSY3, UserCoordinates1); % Calculating user distances from the users of cell 1 to the BS of cell 3
Udist1_3 = Udist;
PL1_3 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist1_3 / de); % Calculating Path loss for the users of cell 1 to the BS of cell 3
SF1_3 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 1 to the BS of cell 3
TotalPL1_3 = PL1_3 + SF1_3; % Calculating Total path loss for the users of cell 1 to the BS of cell 3
interference1_3 = Ptx + AntGain - TotalPL1_3; % Calculating interference for the users of cell 1 to the BS of cell 3
TotalInterference1 = interference1_2 + interference1_3; % Merging the total interference received by each user in cell 1
% Calculating Interference for the users of cell 2 to the Base station of cell 1 & 3
[Udist] = CalculateUserDistances(BSX1, BSY1, UserCoordinates2); % Calculating user distances from the users of cell 2 to the BS of cell 1
Udist2_1 = Udist;
PL2_1 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist2_1 / de); % Calculating Path loss for the users of cell 2 to the BS of cell 1
SF2_1 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 2 to the BS of cell 1
TotalPL2_1 = PL2_1 + SF2_1; % Calculating Total path loss for the users of cell 2 to the BS of cell 1
interference2_1 = Ptx + AntGain - TotalPL2_1; % Calculating interference for the users of cell 2 to the BS ofcell 3
[Udist] = CalculateUserDistances(BSX3, BSY3, UserCoordinates2); % Calculating user distances from the users of cell 2 to the BS of cell 3
Udist2_3 = Udist;
PL2_3 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist2_3 / de); % Calculating Path loss for the users of cell 2 to the BS of cell 3
SF2_3 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 2 to the BS of cell 3
TotalPL2_3 = PL2_3 + SF2_3; % Calculating Total path loss for the users of cell 2 to the BS of cell 3
interference2_3 = Ptx + AntGain - TotalPL2_3; % Calculating interference for the users of cell 2 to the BS of cell 3
TotalInterference2 = interference2_1 + interference2_3; % Merging the total interference received by each user in cell 2
% Calculating Interference for the users of cell 3 to the Base station of cell 1 & 2
[Udist] = CalculateUserDistances(BSX1, BSY1, UserCoordinates3); % Calculating user distances from the users of cell 3 to the BS of cell 1
Udist3_1 = Udist;
PL3_1 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist3_1 / de); % Calculating Path loss for the users of cell 3 to the BS of cell 1
SF3_1 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 3 to the BS of cell 1
TotalPL3_1 = PL3_1 + SF3_1; % Calculating Total path loss for the users of cell 3 to the BS of cell 1
interference3_1 = Ptx + AntGain - TotalPL3_1; % Calculating interference for the users of cell 3 to the BS of cell 1
[Udist] = CalculateUserDistances(BSX2, BSY2, UserCoordinates3); % Calculating user distances from the users of cell 3 to the BS of cell 2
Udist3_2 = Udist;
PL3_2 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist3_2 / de); % Calculating Path loss for the users of cell 3 to the BS of cell 2
SF3_2 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 3 to the BS of cell 2
TotalPL3_2 = PL3_2 + SF3_2; % Calculating Total path loss for the users of cell 3 to the BS of cell 2
interference3_2 = Ptx + AntGain - TotalPL3_2; % Calculating interference for the users of cell 3 to the BS of cell 2
TotalInterference3 = interference3_1 + interference3_2; % Merging the total interference received by each user in cell 3
% Calculating Total Interference
interference = [TotalInterference1; TotalInterference2; TotalInterference3]; % Combining the interference from all cells
end
function [SINR] = CalculateSINR (linkBudget, interference)
% This function calculate SINR for all the users.
Bandwidth = 10*10^6;
% Boltzmann's constant
K = 1.38 * 10^-23;
% Temperature in Kelvin
T = 288;
% Noise figure constant in dB
NF = 9;
% Noise in dB
NO = 10 * log10(K * T * Bandwidth) + NF;
SINR = linkBudget - NO - interference;
function [capacity] = CalculateShannonCapacity(SINR)
% this function calculates the shannon capacity and return it
BW = 10*10^6;
c = BW*log2(1+SINR);
capacity = real(c);
end
end
0 个评论
采纳的回答
Voss
2023-12-22
There were several problems with the code. Compare the code below with yours to see the differences, and verify that my changes are correct.
N = 100; % Total number of cell users
CellRadius = 1000; % Circular cell radius in meters
BSx1 = 0; % X-coordinate of base station 1
BSy1 = 0; % Y-coordinate of base station 1
BSx2 = 950; % X-coordinate of base station 2
BSy2 = 1650; % Y-coordinate of base station 2
BSx3 = 1950; % X-coordinate of base station 3
BSy3 = 0; % Y-coordinate of base station 3
Freq = 2600; % Carrier frequency expressed in MHz
Ptx = 39; % Base station transmit power in dBm
AntGain = 10; % Antenna gain in dB
bandwidth = 10*10^6; % Bandwidth in MHz
% Creating cells 1 2 3 with 100 users
[UserDists, UserCoordinates1, UserCoordinates2, UserCoordinates3] = PlotCell(N, CellRadius, BSx1, BSy1, BSx2, BSy2, BSx3,BSy3);
% Calculate path loss using the "CalculatePL" function
TotalPL = CalculatePL(N, UserDists, Freq);
% Calculate link budget using the "CalculateLinkBudget" function
linkBudget = CalculateLinkBudget(Ptx, AntGain, TotalPL);
% Calculating interference
interference = calculateInterference(Ptx,AntGain,N,Freq,BSx1,BSy1,BSx2,BSy2,BSx3,BSy3,UserCoordinates1, UserCoordinates2, UserCoordinates3);
% Calculating the SINR value for all users
SINR = CalculateSINR(linkBudget, interference);
% Calculating the shannon capacity data rate
capacity = CalculateShannonCapacity(SINR);
% Plotting CDF of Capacity for all users
x = capacity(:);
figure(2)
cdfplot(x)
xlabel('Shannon Capacity')
ylabel('CDF of Shannon Capacity')
function [x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx, BSy)
% Equations to create 50 random x, y points.
ran = CellRadius* sqrt(rand(1, N));
th = 2* pi * rand(1, N);
X = BSx + ran.* cos(th);
Y = BSy + ran.* sin(th);
% Creating Cells
th2 = linspace(0,2*pi);
x = CellRadius*cos(th2) + BSx;
y = CellRadius*sin(th2) + BSy;
end
function [UserDists, UserCoordinates1, UserCoordinates2, UserCoordinates3] = PlotCell(N, CellRadius, BSx1, BSy1, BSx2, BSy2, BSx3, BSy3)
% Plotting first cell with random users (N) and given BS axis
[x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx1, BSy1);
% Plotting the first cell
plot(x, y, "k", BSx1, BSy1, 'rx', X, Y, "bo"); %
UserDists1 = ran;
% Saving coordinates of all users of first cell in a matrix
UserCoordinates1 = [X(:), Y(:)];
hold on
axis equal
% Plotting second cell
[x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx2, BSy2);
plot(x, y, "k", BSx2, BSy2, 'rx', X, Y, "bo");
UserDists2 = ran;
% Saving coordinates of all users of second cell in a matrix
UserCoordinates2 = [X(:), Y(:)];
% Plotting third cell
[x, y, X, Y, ran] = GenerateCell(N, CellRadius, BSx3, BSy3);
plot(x, y, "k", BSx3, BSy3, 'rx', X, Y, "bo");
UserDists3 = ran;
% Saving coordinates of all users of third cell in a matrix
UserCoordinates3 = [X(:), Y(:)];
hold off
axis equal
UserDists = [UserDists1, UserDists2, UserDists3]; % This variable stores the distances of all users according to their cell's Base Station in a matrix
end
function [TotalPL] = CalculatePL(N, UserDists, Freq)
% This function calculates and returns the path loss in dB for each user.
d0 = 1;
size=N;
n=2;
variance=8;
lambda = (3 * 10^8) / Freq;
% Calculating path loss in dB due to propagation.
PL = 20 * log10(4 * pi * d0 ./ lambda) + 10 * 2 * log10(UserDists / d0);
SF = randn(size,1)* variance;
% Calculating total path loss in the vector "TotalPL".
TotalPL = PL + SF;
end
function [linkBudget] = CalculateLinkBudget(Ptx, AntGain, TotalPL)
% This function computes the total link budget based on the path loss.
linkBudget = Ptx + AntGain - TotalPL;
end
function [Udist] = CalculateUserDistances(BSx, BSy, UserCords)
% The function finds the distances from the user to the base station of other cell.
arr = [];
for r = 1:length(UserCords)
Cell = [UserCords(r, 1), UserCords(r, 2); BSx, BSy];
d = pdist(Cell, 'euclidean');
arr(r) = d;
end
% Returns user distance from the base station
Udist = transpose(arr);
end
function [interference] = calculateInterference(Ptx, AntGain, N, Freq, BSX1, BSY1, BSX2, BSY2, BSX3, BSY3, UserCoordinates1, UserCoordinates2, UserCoordinates3)
% This function calculates interference for all the users in 3 cells.
de = 1;
n = 2; % Path loss exponent
variance = 8;
lambda = (3 * 10^8) / Freq;
size = N;
% Calculating Interference for the users of cell 1 to the Base station of cell 2 & 3
[Udist] = CalculateUserDistances(BSX2, BSY2, UserCoordinates1); % Calculating user distances from the users of cell 1 to the BS of cell 2
Udist1_2 = Udist;
PL1_2 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist1_2 / de); % Calculating Path loss for the users of cell 1 to the BS of cell 2
SF1_2 = randn(size, 1) * variance; % Calculating shadowing loss for the users of cell 1 to the BS of cell 2
TotalPL1_2 = PL1_2 + SF1_2; % Calculating Total path loss for the users of cell 1 to the BS of cell 2
interference1_2 = Ptx + AntGain - TotalPL1_2; % Calculating interference for the users of cell 1 to the BS of cell 2
[Udist] = CalculateUserDistances(BSX3, BSY3, UserCoordinates1); % Calculating user distances from the users of cell 1 to the BS of cell 3
Udist1_3 = Udist;
PL1_3 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist1_3 / de); % Calculating Path loss for the users of cell 1 to the BS of cell 3
SF1_3 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 1 to the BS of cell 3
TotalPL1_3 = PL1_3 + SF1_3; % Calculating Total path loss for the users of cell 1 to the BS of cell 3
interference1_3 = Ptx + AntGain - TotalPL1_3; % Calculating interference for the users of cell 1 to the BS of cell 3
TotalInterference1 = interference1_2 + interference1_3; % Merging the total interference received by each user in cell 1
% Calculating Interference for the users of cell 2 to the Base station of cell 1 & 3
[Udist] = CalculateUserDistances(BSX1, BSY1, UserCoordinates2); % Calculating user distances from the users of cell 2 to the BS of cell 1
Udist2_1 = Udist;
PL2_1 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist2_1 / de); % Calculating Path loss for the users of cell 2 to the BS of cell 1
SF2_1 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 2 to the BS of cell 1
TotalPL2_1 = PL2_1 + SF2_1; % Calculating Total path loss for the users of cell 2 to the BS of cell 1
interference2_1 = Ptx + AntGain - TotalPL2_1; % Calculating interference for the users of cell 2 to the BS ofcell 3
[Udist] = CalculateUserDistances(BSX3, BSY3, UserCoordinates2); % Calculating user distances from the users of cell 2 to the BS of cell 3
Udist2_3 = Udist;
PL2_3 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist2_3 / de); % Calculating Path loss for the users of cell 2 to the BS of cell 3
SF2_3 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 2 to the BS of cell 3
TotalPL2_3 = PL2_3 + SF2_3; % Calculating Total path loss for the users of cell 2 to the BS of cell 3
interference2_3 = Ptx + AntGain - TotalPL2_3; % Calculating interference for the users of cell 2 to the BS of cell 3
TotalInterference2 = interference2_1 + interference2_3; % Merging the total interference received by each user in cell 2
% Calculating Interference for the users of cell 3 to the Base station of cell 1 & 2
[Udist] = CalculateUserDistances(BSX1, BSY1, UserCoordinates3); % Calculating user distances from the users of cell 3 to the BS of cell 1
Udist3_1 = Udist;
PL3_1 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist3_1 / de); % Calculating Path loss for the users of cell 3 to the BS of cell 1
SF3_1 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 3 to the BS of cell 1
TotalPL3_1 = PL3_1 + SF3_1; % Calculating Total path loss for the users of cell 3 to the BS of cell 1
interference3_1 = Ptx + AntGain - TotalPL3_1; % Calculating interference for the users of cell 3 to the BS of cell 1
[Udist] = CalculateUserDistances(BSX2, BSY2, UserCoordinates3); % Calculating user distances from the users of cell 3 to the BS of cell 2
Udist3_2 = Udist;
PL3_2 = 20 * log10(4 * pi * de ./ lambda) + 10 * n * log10(Udist3_2 / de); % Calculating Path loss for the users of cell 3 to the BS of cell 2
SF3_2 = randn(size, 1) * variance; % Calculating Shadowing loss for the users of cell 3 to the BS of cell 2
TotalPL3_2 = PL3_2 + SF3_2; % Calculating Total path loss for the users of cell 3 to the BS of cell 2
interference3_2 = Ptx + AntGain - TotalPL3_2; % Calculating interference for the users of cell 3 to the BS of cell 2
TotalInterference3 = interference3_1 + interference3_2; % Merging the total interference received by each user in cell 3
% Calculating Total Interference
interference = [TotalInterference1; TotalInterference2; TotalInterference3]; % Combining the interference from all cells
end
function [SINR] = CalculateSINR (linkBudget, interference)
% This function calculate SINR for all the users.
Bandwidth = 10*10^6;
% Boltzmann's constant
K = 1.38 * 10^-23;
% Temperature in Kelvin
T = 288;
% Noise figure constant in dB
NF = 9;
% Noise in dB
NO = 10 * log10(K * T * Bandwidth) + NF;
SINR = linkBudget - NO - interference.';
end
function [capacity] = CalculateShannonCapacity(SINR)
% this function calculates the shannon capacity and return it
BW = 10*10^6;
c = BW*log2(1+SINR);
capacity = real(c);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Signal Transmission 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!