-
Notifications
You must be signed in to change notification settings - Fork 3
/
femtopower.m
343 lines (315 loc) · 11.3 KB
/
femtopower.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
% NTUA Thesis code
% John Zobolas, May 2013
function femtopower
format long;
global results;
results = cell(1,10);
% the "standard" coordinates of the FAPs
x = [200 400 400 700 600 600 300 850 800 200 600 800 500 200 100 400 700 900];
y = [500 300 600 600 800 200 800 400 800 200 400 200 900 700 350 100 300 550];
% Uncomment the below to get a random placement of the FAPs inside the Macrocell
%x = randi([150 850],1,30);
%y = randi([150 850],1,30);
%length(x) = 18;
xf = zeros(1,36);
yf = zeros(1,36);
for i=1:18
while true
random = 2*randi([-18 18],1,4);
if all(random) % no zeros
break;
end
end
xf(2*i-1) = x(i)+random(1);% NRT users
yf(2*i-1) = y(i)+random(2);
xf(2*i) = x(i)+random(3);% RT users
yf(2*i) = y(i)+random(4);
end
num_FAP = input('How many FAPS?');
dx_FAP = zeros(1,num_FAP);
dy_FAP = zeros(1,num_FAP);
num_femto = 2 * num_FAP;
dx_femto = zeros(1,num_femto);
dy_femto = zeros(1,num_femto);
for i=1:num_FAP
dx_FAP(i) = x(i); % the matrix with teh FAPs' coordinates
dy_FAP(i) = y(i);
dx_femto(2*i-1) = xf(2*i-1);% NRT users
dy_femto(2*i-1) = yf(2*i-1);
dx_femto(2*i) = xf(2*i);% RT users
dy_femto(2*i) = yf(2*i);
end
num_macro = input('How many macro users?');
num_nrt = input('How many nrt macro users?');
%num_rt = num_macro-num_nrt;
dx_macro = randi([100 900],1,num_macro);
dy_macro = randi([100 900],1,num_macro);
% Matrix of all distances between stations and path gains
num_users = num_macro + num_femto;
D = zeros(num_FAP+1,num_users); % the +1 is to count the MBS (the Macrocell base station)
% rows = base stations, columns = users
Gain = zeros(num_FAP+1,num_users);
kc = 0.1;% fixed propagation loss during cellular transmissions to MBS
kf = 0.01;% fixed loss between femto-user i to their FAP(i).
w = 0.3162;% partition loss during indoor-to-outdoor propagation
w2 = 0.1;% w^2 (double loss)
a = 4;% outdoor path loss exponent
b = 3;% indoor path loss exponent
for i=1:num_FAP+1
for j=1:num_users
if (i==1)&&(j<=num_macro) % m -> MBS
D(i,j) = euklidian(dx_macro(j),dy_macro(j),500,500);
Gain(i,j) = kc * power(D(i,j),-a);
end
if (i>1)&&(j<=num_macro) % m -> FAPs
ifap = i-1;
D(i,j) = euklidian(dx_macro(j),dy_macro(j),dx_FAP(ifap),dy_FAP(ifap));
Gain(i,j) = kc * w * power(D(i,j),-a);
end
if (i==1)&&(j>num_macro) % f -> MBS
k = j - num_macro;
D(i,j) = euklidian(dx_femto(k),dy_femto(k),500,500);
Gain(i,j) = kc * w * power(D(i,j),-a);
end
if (i>1)&&(j>num_macro) % f -> FAPs
k = j - num_macro; % the femto-user
ifap = i-1; % which FAP we check
D(i,j) = euklidian(dx_femto(k),dy_femto(k),dx_FAP(ifap),dy_FAP(ifap));
if (2*ifap-1 == k)||(2*ifap == k) % f -> FAP f belongs to that FAP - he is a home user!
Gain(i,j) = kf * power(D(i,j),-b);
else % f user is from another FAP (a neighboor!)
Gain(i,j) = kc * w2 * power(D(i,j),-a);
end
end
end
end
% Initializations
Pmax = 2.0; % in Watt
Rmax = 2.4*10^6; % in bps
Rtarget = 64*10^3; % in bps
MF = 10*10^3; % in bps
Rmaxi = Rtarget + MF;
Rmini = Rtarget - MF;
W = 10^6; %bandwidth in Hz
noise = 5 * 10^(-16); %noise
e = 10^(-5);
G = W/Rmaxi;
% Power Control Algorithm
c = 10^10; %femto pricing factor
p = 0:0.00001:2;
p_users = 2*ones(1,num_users);
% in order of: nrt_m,rt_m,2 femto_users(nrt,rt)/FAP
converged = false;
k = 1;
new_p_users = p_users;
isxis = cell(1,num_users);
while (converged == false) && (k < 80)
I = Gain*p_users'; % total intereference in every BS (num_FAP+1 X 1)
for i=1:num_users
if i <= num_macro % MACRO USERS
I_i = I(1) - Gain(1,i) * p_users(i) + noise;
if i <= num_nrt % NRT macro users
U = ((log(0.001*Rmax*(power((1-exp(-3.7*(G*(Gain(1,i)*p)/I_i))),80))+1))./p);
%find the max of U
Um = U(~isnan(U));
if (1 && all(Um == 0))
root = Pmax;
else
[~,in] = max(U);
root = p(in);
end
else % RT macro users
U = (power(1-exp(-(Rmaxi*(power((1-exp(-3.7*(G*(Gain(1,i)*p)/I_i))),80))-Rmini)),2000)./p);
%find the max of U
l1 = length(U);
Um = U(~isinf(U));
l2 = length(Um);
if (l2 == 0)
root = Pmax;
else
[~,in] = max(U(~isinf(U)));
ini = l1-l2+in;
root = p(ini);
end
end
else % FEMTO USERS (i > num_macro)
j = i - num_macro;
if mod(j,2) == 1 % NRT femto users
f = (j+1)/2; % f is the FAP that the j-th femto user belongs
I_i = I(f+1) - Gain(f+1,i) * p_users(i) + noise;
U = ((log(0.001*Rmax*(power((1-exp(-3.7*(G*(Gain(f+1,i)*p)/I_i))),80))+1))./p); %-c*(exp(p)-1);
%find the max of U
Um = U(~isnan(U));
if (1 && all(Um == 0))
root = Pmax;
else
[~,in] = max(U);
root = p(in);
end
else % RT femto users
f = j/2;
I_i = I(f+1) - Gain(f+1,i) * p_users(i) + noise;
U = (power(1-exp(-(Rmaxi*(power((1-exp(-3.7*(G*(Gain(f+1,i)*p)/I_i))),80))-Rmini)),2000)./p)-c*(exp(p)-1);
%find the max of U
l1 = length(U);
Um = U(~isinf(U));
l2 = length(Um);
if (l2 == 0)
root = Pmax;
else
[~,in] = max(U(~isinf(U)));
ini = l1-l2+in;
root = p(ini);
end
end
end
new_p_users(i) = root;
end
% Test Convergence
count = 0;
for i=1:num_users
if (abs(new_p_users(i) - p_users(i)) < e)
count = count + 1;
end
end
if count == num_users
converged = true;
else
converged = false;
end
for i = 1:num_users
isxis{i}(k) = p_users(i);
end
p_users = new_p_users
k = k+1
end
% Calculate the Users' Utilities
I = Gain*p_users';
fgama = zeros(1,num_users);
Rate = zeros(1,num_users);
Util = zeros(1,num_users);
for i=1:num_users
if i <= num_macro % MACRO USERS
I_i = I(1) - Gain(1,i) * p_users(i) + noise;
if i <= num_nrt % NRT macro users
fgama(i) = power((1-exp(-3.7*(G*(Gain(1,i)*p_users(i))/I_i))),80);
Rate(i) = fgama(i) * Rmax;
Util(i) = (log(0.001*Rmax*fgama(i)+1))./p_users(i);
else % RT macro users
fgama(i) = power((1-exp(-3.7*(G*(Gain(1,i)*p_users(i))/I_i))),80);
Rate(i) = fgama(i) * Rmaxi;
Util(i) = power(1-exp(-(Rmaxi*fgama(i)-Rmini)),2000)./p_users(i);
end
else % FEMTO USERS (i > num_macro)
j = i - num_macro;
if mod(j,2) == 1 % NRT femto users
f = (j+1)/2;
I_i = I(f+1) - Gain(f+1,i) * p_users(i) + noise;
fgama(i) = power((1-exp(-3.7*(G*(Gain(f+1,i)*p_users(i))/I_i))),80);
Rate(i) = fgama(i) * Rmax;
Util(i) = ((log(0.001*Rmax*fgama(i)+1))./p_users(i));
else % RT femto users
f = j/2;
I_i = I(f+1) - Gain(f+1,i) * p_users(i) + noise;
fgama(i) = power((1-exp(-3.7*(G*(Gain(f+1,i)*p_users(i))/I_i))),80);
Rate(i) = fgama(i) * Rmaxi;
Util(i) = (power(1-exp(-(Rmaxi*fgama(i)-Rmini)),2000)./p_users(i));
end
end
end
figure;
% Power vs iterations for macro users
iterations = 1:k-1;
for i = 1:num_macro
hold on;
title('P vs iterations for macro users');
if i <= num_nrt
plot(iterations,isxis{i},'r*');
else
plot(iterations,isxis{i},'b*');
end
end
xlabel('Iterations');
ylabel('User Transmission Power in Watt');
hold off;
figure;
% Power vs iterations for femto users
iterations = 1:k-1;
for i = num_macro+1:num_users
hold on;
title('P vs iterations for femto users');
j = i - num_macro;
if mod(j,2) == 1
plot(iterations,isxis{i},'r*');
else
plot(iterations,isxis{i},'b*');
end
end
xlabel('Iterations');
ylabel('User Transmission Power in Watt');
hold off;
id = 1:num_users;
figure;bar(id,fgama,'g');title('f(γ)');
figure;bar(id,Rate,'r');title('Users Rate');
figure;bar(id,Util);title('Users Utilities');
% Draw the Macrocell with all the users
figure;
draw(dx_macro,dy_macro,dx_femto,dy_femto,dx_FAP,dy_FAP,num_nrt,num_macro,num_femto);
end
function d = euklidian(x,y,z,w)
d = sqrt((x-z).^2+(y-w).^2);
end
function draw(dx_macro,dy_macro,dx_femto,dy_femto,dx_FAP,dy_FAP,num_nrt,num_macro,num_femto)
mbsx = 500; % MBS coordinates
mbsy = 500;
num_rt = num_macro - num_nrt;
% distinguish macro users to RT and NRT
dx_macro_nrt = zeros(1,num_nrt);
dy_macro_nrt = zeros(1,num_nrt);
dx_macro_rt = zeros(1,num_rt);
dy_macro_rt = zeros(1,num_rt);
for i=1:num_macro
if i<=num_nrt
dx_macro_nrt(i) = dx_macro(i);
dy_macro_nrt(i) = dy_macro(i);
else
dx_macro_rt(i) = dx_macro(i);
dy_macro_rt(i) = dy_macro(i);
end
end
% distinguish femto users to RT and NRT
num_FAP = num_femto/2;
dx_femto_nrt = zeros(1,num_FAP);
dy_femto_nrt = zeros(1,num_FAP);
dx_femto_rt = zeros(1,num_FAP);
dy_femto_rt = zeros(1,num_FAP);
k = 1;
w = 1;
for i=1:num_femto
if mod(i,2) == 1 % 2 users/FAP, first is a NRT one, second is a RT
dx_femto_nrt(k) = dx_femto(i);
dy_femto_nrt(k) = dy_femto(i);
k = k+1;
else
dx_femto_rt(w) = dx_femto(i);
dy_femto_rt(w) = dy_femto(i);
w = w+1;
end
end
hold on; % red = NRT, blue = RT
grid on;
plot(mbsx,mbsy,'kd',dx_FAP,dy_FAP,'bd',dx_macro_nrt,dy_macro_nrt,'rx',dx_macro_rt,dy_macro_rt,'bx',dx_femto_nrt,dy_femto_nrt,'ro',dx_femto_rt,dy_femto_rt,'bo');
xlim([0 1000]);ylim([0 1000]);
circle(mbsx,mbsy,500,'r');
for i=1:num_FAP
circle(dx_FAP(i),dy_FAP(i),50,'g');
end
axis equal;
hold off;
end
function circle(x,y,r,color)
th = 0:pi/500:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
plot(xunit, yunit, color);
end