Você está na página 1de 14

# All Matlab Code for problem 4:

% Richard Pon
% used to solve num4
clear
global node_list;
basestation = [1000,1000; 3000,1000];
node_list = [
1000,350;
820,1050;
1100,980;
1950,1650;
2050,900;
2800,1900;
3300,1000;
3850,1900;
basestation]; %basestation 1 = node 9, bs 10 = node 10
%scatter(node(:,1),node(:,2),5); hold on
%scatter(basestation(:,1),basestation(:,2),200);
global noise;
global BER;
global alph;
noise=1e-15; %Watts
BER=1e-4;
alph = make_alpha;
user=1;
bs=1;
channel=1;
b1=[8 8 8 8 8 8]; %b = bits/symbol
b2=[8 8 6 8 7 8]; %b = bits/symbol
%for each channel, choose a b
for i=1:6
sinr_thresh1(i) = sinr_threshold(b1(i));
sinr_thresh2(i) = sinr_threshold(b2(i));
end

% compute_user_gains
% make_alpha(User,Basestation,Channel)
% SINR(node,bs,channel,power_table)
%g = G(node,basestation,channel)
channel = [1:6];
%each row is a different channel
%Ptx_table(ch,node)
global power_table;
power_table=[
[0 1 0 0 0 0 1 0];
[0 0 1 0 0 40 0 0];
[0 1 0 0 0 15 0 0];
[0 1 0 0 0 0 1 0];
[0 1 0 0 0 11 0 0];
[0 0 1 0 0 80 0 0]];
for u=1:8
for bs=9:10
for ch=1:6
sinr_table(u,bs,ch)= sinr(u,bs,ch);
end
end
end
% SINR(node,bs,channel,power_table)
bs1(1)=sinr(2,9,1);
bs1(2)=sinr(3,9,2);
bs1(3)=sinr(2,9,3);
bs1(4)=sinr(2,9,4);
bs1(5)=sinr(2,9,5);
bs1(6)=sinr(3,9,6);
bs2(1)=sinr(7,10,1);
bs2(2)=sinr(6,10,2);
bs2(3)=sinr(6,10,3);
bs2(4)=sinr(7,10,4);
bs2(5)=sinr(6,10,5);
bs2(6)=sinr(6,10,6);
bs1>=sinr_thresh1;
bs2>=sinr_thresh2;

%Richard Pon
% Solves 4b
global node_list;
basestation = [1000,1000; 3000,1000];
node_list = [
1000,350;
820,1050;
1100,980;
1950,1650;
2050,900;
2800,1900;
3300,1000;
3850,1900;
basestation]; %basestation 1 = node 9, bs 10 = node 10
%scatter(node(:,1),node(:,2),5); hold on
%scatter(basestation(:,1),basestation(:,2),200);
global noise;
global BER;
global alph;
global power_table;
global b_table;
global channel_usage_table;
noise=1e-15; %Watts
BER=1e-4;
alph = make_alpha;
% make_alpha(User,Basestation,Channel)
% SINR(node,bs,channel,power_table)
%g = G(node,basestation,channel)
global b_table;
%modulation level for each users,
%[ch1 ch2 ... ch6]
b_table=[
[8 8 8 8 8 8];%bs1
[8 8 8 8 8 8]];%bs2
%[user1 ... user8]

## %a one means that jth user is using ith channel

channel_usage_table=[
[0 0 1 0 0 0 1 0];
[0 0 1 0 0 0 1 0];
[0 0 1 0 0 0 1 0];
[0 1 0 0 0 0 1 0];
[0 0 1 0 0 0 1 0];
[0 0 1 0 0 1 0 0]];
a=iterate4b(105)
%Richard Pon
%This file was used to calculate 4c
global node_list;
basestation = [1000,1000; 3000,1000];
node_list = [
1000,350;
820,1050;
1100,980;
1950,1650;
2050,900;
2800,1900;
3300,1000;
3850,1900;
basestation]; %basestation 1 = node 9, bs 10 = node 10
%scatter(node(:,1),node(:,2),5); hold on
%scatter(basestation(:,1),basestation(:,2),200);
global noise;
global BER;
global alph;
global power_table;
global b_table;
global channel_usage_table;
noise=1e-15; %Watts
BER=1e-4;
alph = make_alpha;
% make_alpha(User,Basestation,Channel)
% SINR(node,bs,channel,power_table)
%g = G(node,basestation,channel)

global b_table;
%modulation level for each users,
%[ch1 ch2 ... ch6]
b_table=[
[7 7 2 3 3 7];%bs1
[7 2 7 3 3 7]];%bs2
%best b settings for bmin=4
b_table=[
[4 4 2 1 1 4];%bs1
[4 1 4 2 1 4]];%bs2
%bmin=3
b_table=[
[3 3 1 1 1 3];%bs1
[3 1 3 1 1 3]];%bs2
%[user1 ... user8]
%a one means that jth user is using ith channel
channel_usage_table=[
[1 0 0 0 1 0 0 0];%ch1
[0 0 1 0 0 0 0 1];%ch2
[0 0 0 1 0 0 1 0];%ch3
[0 0 0 1 0 0 0 1];%ch4
[0 0 0 1 0 0 0 1];%ch5
[0 1 0 0 0 1 0 0]];%ch6

## % power_table= make_power_table([.01 .01 .01 .01 .01 .01 .01 .01]);

a=iterate4c(400)
% Richard Pon
% estimates required transmitted power
% calc_ptx(1:8,9:10,1:6)
%calc_ptx(user,bs,ch)
function ptx = calc_ptx(user,bs,ch)
global noise;
global power_table
global b_table;
noise = 1e-15;

% numer = power_table(channel,node)*G(node,bs,channel);
%compute sum of interferers over all other users besides src_node
denom=0;
for cur_node=1:8;
if (cur_node ~= user),
denom = denom + power_table(ch,cur_node)*G(cur_node,bs,ch);
end
end
thresh=sinr_threshold(b_table(bs,ch));
denom = noise + denom;
ptx = denom*thresh/G(user,bs,ch);

% Richard Pon
% computes relative gains of all users over all channels for a
% given basestation bs
function result = compute_user_gains(bs)
% node,basestation,channel)
% result(node,bs-8,ch)=G(node,bs,ch);
for node=1:8
% for bs=1:2
for ch=1:6
r(ch,node)=G(node,bs,ch);
end
% end
end
result = r;
% Richard Pon
% computes relative gains of all users over all channels for a
% given basestation bs

## function result = compute_user_gains(bs)

% node,basestation,channel)
% result(node,bs-8,ch)=G(node,bs,ch);
for node=1:8
% for bs=1:2
for ch=1:6
r(ch,node)=G(node,bs,ch);
end
% end
end
result = r;
% Richard Pon
% computes the channel gain
%basestation={1,2} (basestation 1 = node 9, basestation 2 = node 10)
%node={1:8}
%channel={1:6}
function g = G(node,basestation,channel)
%make_alpha is a function
%alpha(User,Basestation,Channel)
global alph;
g=alph(node,basestation,channel)/distance(basestation+8,node)^4;
% Richard Pon
% computes the distance between two nodes
function d = distance(node1,node2)
global node_list;
x1=node_list(node1,1);
y1=node_list(node1,2);
x2=node_list(node2,1);
y2=node_list(node2,2);
d = sqrt((x1-x2)^2 + (y1-y2)^2);

% Richard Pon
% computes the channel gain
%basestation={1,2} (basestation 1 = node 9, basestation 2 = node 10)
%node={1:8}
%channel={1:6}
function g = G(node,basestation,channel)
%make_alpha is a function
%alpha(User,Basestation,Channel)
global alph;
g=alph(node,basestation,channel)/distance(basestation+8,node)^4;
%Richard Pon
%iterates n times
% every iteration, calculates a new Ptx for each user based
% upon previous values
function results = iterate4b(n)
global power_table
global b_table
% calc_ptx(1:8,1:2,1:6)
%calc_ptx(user,bs,ch)
% function ptx = calc_ptx(user,bs,ch)
%power_table= make_power_table([.00001 .00001 .00001 .00001 .00001 .00001 .00001 .
00001]);
power_table= make_power_table([0 .00001 .00001 0 0 .00001 .00001 0]);
new_power_table=power_table;%initialize the size
for i=1:n
for user=1:8
for ch=1:6
if power_table(ch,user)==0
new_power_table(ch,user)=0;
else
new_power_table(ch,user)=calc_ptx(user,get_bs(user),ch);
end

end %ch
end %user
if power_table == new_power_table
display([i]);
result=new_power_table;
break
else
power_table = new_power_table;
end
end %n
% power_table=new_power_table;
results=new_power_table;
function bs = get_bs(user)
if user <= 4
bs = 1;
else
bs=2;
end
%Richard Pon
%iterates n times
% every iteration, calculates a new Ptx for each user based
% upon previous values
function results = iterate4c(n)
global power_table
global b_table
% calc_ptx(1:8,1:2,1:6)
%calc_ptx(user,bs,ch)
% function ptx = calc_ptx(user,bs,ch)
power_table= make_power_table([.00001 .00001 .00001 .00001 .00001 .00001 .00001 .00001]);
% power_table= make_power_table([0 .00001 .00001 0 0 .00001 .00001 0]);
new_power_table=power_table;%initialize the size

for i=1:n
for user=1:8
for ch=1:6
if power_table(ch,user)==0
new_power_table(ch,user)=0;
else
new_power_table(ch,user)=calc_ptx(user,get_bs(user),ch);
end
end %ch
end %user
if abs(power_table - new_power_table) <= .001*ones(6,8)
display([i]);
result=new_power_table;
break
else
power_table = new_power_table;
end
end %n
% power_table=new_power_table;
results=new_power_table;
function bs = get_bs(user)
if user <= 4
bs = 1;
else
bs=2;
end
%Richard Pon
%Creates the variable alpha for the channel gain calculation
%alpha(User,Basestation,Channel)
%
function alpha = make_alpha()
alpha (
1
,
1
,
1
)
=
alpha (
1
,
1
,
2
)
=
alpha (
1
,
1
,
3
)
=
alpha (
1
,
1
,
4
)
=
alpha (
1
,
1
,
5
)
=
alpha (
1
,
1
,
6
)
=
alpha (
1
,
2
,
1
)
=
alpha (
1
,
2
,
2
)
=

1.2082920167121200;
0.68261128219347100;
0.46665453479093500;
1.28358287336034000;
0.17679910389410900;
1.06043895583797000;
0.00106383511857202;
0.4923951714934110 ;

alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha
alpha

(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(

1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
4
4
5
5
5
5
5
5

,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,

2
2
2
2
1
1
1
1
1
1
2
2
2
2
2
2
1
1
1
1
1
1
2
2
2
2
2
2
1
1
1
1
1
1
2
2
2
2
2
2
1
1
1
1
1
1

,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,

3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6

)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)

=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

0.13254885218084800;
4.30127377326362000;
1.22388086573371000;
0.46750104478000100;
4.55912433438642000;
0.07985703967081870;
0.54631641845505600;
4.11841523535562000;
0.78284229055203700;
0.65515135394772900;
3.02323106454352000;
1.11941781044456000;
0.58423245156986400;
0.77407282231312100;
0.60003833553657900;
0.62920192971153300;
1.32718842027916000;
0.87390761895332200;
0.44912487366534700;
0.14142028230494300;
0.14139331315944100;
0.96568349877922800;
0.20014460833586900;
0.77026863889668100;
1.04450536996719000;
0.29547624938298000;
0.28034988976780900;
1.01617721288608000;
0.35422354534531700;
0.82662608177929300;
2.83331207111518000;
1.19553343062508000;
0.82904916541473800;
0.51933886719918700;
1.95968510224995000;
0.10474386099658800;
1.47844364410655000;
0.60902779808170700;
0.92104818520407300;
0.56164753183103700;
1.47993207597116000;
0.76644644857303100;
1.72738108366992000;
0.58572176463862200;
0.25904035377227300;
0.49895207101675700;

alpha (
5
,
2
,
1
)
=
0.67532136694630000;
alpha (
5
,
2
,
2
)
=
0.96364207424385300;
alpha (
5
,
2
,
3
)
=
0.20475735751873400;
alpha (
5
,
2
,
4
)
=
1.60800010531151000;
alpha (
5
,
2
,
5
)
=
0.79919543567861300;
alpha (
5
,
2
,
6
)
=
0.56700922865947200;
alpha (
6
,
1
,
1
)
=
0.18800571326331200;
alpha (
6
,
1
,
2
)
=
2.07504033643432000;
alpha (
6
,
1
,
3
)
=
0.28583916948069700;
alpha (
6
,
1
,
4
)
=
1.48928738183080000;
alpha (
6
,
1
,
5
)
=
0.67319944452682300;
alpha (
6
,
1
,
6
)
=
0.70107560510366100;
alpha (
6
,
2
,
1
)
=
1.22926802762430000;
alpha (
6
,
2
,
2
)
=
1.64240392357392000;
alpha (
6
,
2
,
3
)
=
0.78525602936619100;
alpha (
6
,
2
,
4
)
=
0.06253342357687580;
alpha (
6
,
2
,
5
)
=
1.73285252395857000;
alpha (
6
,
2
,
6
)
=
1.07068255908631000;
alpha (
7
,
1
,
1
)
=
0.27463175970529700;
alpha (
7
,
1
,
2
)
=
2.03456519151509000;
alpha (
7
,
1
,
3
)
=
0.63579854515257000;
alpha (
7
,
1
,
4
)
=
0.32460552840826500;
alpha (
7
,
1
,
5
)
=
2.81226732657977000;
alpha (
7
,
1
,
6
)
=
1.98052122391254000;
alpha (
7
,
2
,
1
)
=
2.54543414927573000;
alpha (
7
,
2
,
2
)
=
0.02200078923866870;
alpha (
7
,
2
,
3
)
=
0.08063290688898080;
alpha (
7
,
2
,
4
)
=
5.12952995906612000;
alpha (
7
,
2
,
5
)
=
4.85869046212440000;
alpha (
7
,
2
,
6
)
=
0.00908101341978757;
alpha (
8
,
1
,
1
)
=
0.22564103063575000;
alpha (
8
,
1
,
2
)
=
1.30920535918950000;
alpha (
8
,
1
,
3
)
=
1.34081887647757000;
alpha (
8
,
1
,
4
)
=
1.91470370151754000;
alpha (
8
,
1
,
5
)
=
0.06484580521826640;
alpha (
8
,
1
,
6
)
=
1.16573276510242000;
alpha (
8
,
2
,
1
)
=
0.12298047183185700;
alpha (
8
,
2
,
2
)
=
0.31558071516195700;
alpha (
8
,
2
,
3
)
=
0.01746755851894170;
alpha (
8
,
2
,
4
)
=
0.66173573526648800;
alpha (
8
,
2
,
5
)
=
0.43249542747910000;
alpha (
8
,
2
,
6
)
=
0.24332077540024000;
% Richard Pon
% Makes a power table showing Ptx for each user on each channel
function p_table = make_power_table(user_power_table)

global channel_usage_table;
%represents power for jth user
% user_power_table=[1.19e3 1.5 1 2.92e3 9.96e3 5.45e3 8.12e2 1.35e4];
% user_power_table=[1 1 1 1 1 1 1 1];
user_power_table_1 = [user_power_table;
user_power_table;
user_power_table;
user_power_table;
user_power_table;
user_power_table;];
p_table=channel_usage_table.*user_power_table_1;
%Richard Pon
% Is a helper function
clear
%computer gains for bs 1
for node=1:4
for ch=1:6
bs1(node,ch)=G(node,9,ch);
end
end
bs1

%Richard Pon
% computes the Q function
function ans = Q(z)
ans = .5*erfc(z/sqrt(2));
% Richard Pon
% computes the Q inverse function
function q_inv = Q_inv(x)
q_inv = sqrt(2)*erfcinv(2*x);
%Richard Pon
%
% computes SINR

## % node - is transmitting node

% bs - receiving basestation
% power_table = [P1 P2 P3 .. P8] where
%
Pn is the power transmitted by node n
function result = SINR(node,bs,channel)
global noise;
global power_table
noise = 1e-15;
numer = power_table(channel,node)*G(node,bs,channel);
%compute sum of interferers over all other users besides src_node
denom=0;
for cur_node=1:8;
if (cur_node ~= node),
denom = denom + power_table(channel,cur_node)*G(cur_node,bs,channel);
end
end
denom = noise + denom;
result = numer/denom;

% Richard Pon
%
% computes gamma, the SINR threshold
% BER = bit error rate
% b = symbols per bit
function gam = SINR_threshold(b)
global BER;
BER=1e-4;
gam = (Q_inv(BER/4))^2*(2^b-1)/3;