You are on page 1of 4

Appendix C: Program Code

%...............................Building a network and initializing..........................................


Clear,close all;
BorderLength=100; the length of the % square
NodeAmount=100; % total number of nodes
BeaconAmount=10; % beacon node number
UNAmount=NodeAmount-BeaconAmount;% unknown number of nodes
R=30;% communication radius
Dall=zeros(NodeAmount,NodeAmount); % distance initial matrix
X=zeros(2, UNAmount); % unknown node estimated coordinate initial matrix
D1=Dall(1:BeaconAmount,1:BeaconAmount); % Beacon Inter-Node Distance Matrix
Distance=zeros(BeaconAmount, UNAmount); % distance matrix from unknown node to beacon
node
K=BeaconAmount/UNAmount;

%~~~~~~~~~ Generate a uniformly distributed random topology in the square


area~~~~~~~~~~~~~~~~~~~~
Site=BorderLength.*rand(2,NodeAmount); % randomly generates node coordinates
Sxy=[1:NodeAmount;site]; % node coordinates with sequence number
Beacon=[Sxy(2,1:BeaconAmount);Sxy(3,1:BeaconAmount)]; % beacon node coordinates
UN=[Sxy(2,(BeaconAmount+1):NodeAmount);Sxy(3,(BeaconAmount+1):NodeAmount)];
%Unknown node coordinate matrix
% draws the node distribution map
Figure(1)
Plot (Sxy(2,1:BeaconAmount), Sxy(3,1:BeaconAmount), 'r*', Sxy(2,
(BeaconAmount+1):NodeAmount),Sxy(3,(BeaconAmount+1):NodeAmount) , 'k.')
Xlim([0,BorderLength]);% abscissa
Ylim([0,BorderLength]);% ordinate
Title ('red* indicates beacon node black. indicates unknown node')
%~~~~~~~~ Receive signal strength RSSI and distance calculation~~~~~~~~~~~~~~~~~~~~~
For i=1:BeaconAmount
For j=1: UNAmount
Distance(i,j)=((Sxy(2,i)-Sxy(2,j))^2+(Sxy(3,i)-Sxy(3,j))^2)^0.5;%Unknown Node Beacon
node distance matrix
End
End
PtW = 2*10e-004; % unit is W
Pt = 10*log10(PtW); % unit is dB
f = 2.4*10^3; % carrier frequency, the unit is Hz
n = 2; % path loss index
D0 = 20; % near ground reference distance, in units
For i=1:BeaconAmount
For j=1: UNAmount
L(j)=Distance(i,j);
End
End
c = 3*10^8; % speed of light in m/s
Lamida = c/f; % wavelength in m
%PL0 is the path loss of the near ground reference distance
%PrW = PtW*lamida^2/((4*pi)^2*d0^2) The receiving power unit at % d0 is W
%PL0 = 10*log10(Pt/Pr) % unit is dB
PL0 = -10*log10(lamida^2/((4*pi)^2*d0^2)); % unit is dB
Pr0 = Pt-PL0; % unit is dB
%PL is the path loss of the exact T-R distance; Pr is the received power of the beacon node
For i=1: UNAmount
PL(i) = PL0+10*n*log10(L(i)/d0); % The total transmission loss unit is dB
PLW(i) = 10^(PL(i)/10); % The total transmission loss unit is dB
Pr(i) = Pt - PL(i); % unit is dB
PrW(i) = 10^(Pr(i)/10); % The received power unit is W
End
For i=1: UNAmount
RSSI(i) = PrW(i); % unit is W
End
%r is the determined T-R distance; a is the parameter, which varies with distance range
%RSSI = a*(1/r)^2
a = 10; % is within the distance range of the selected beacon node, after repeated testing, this
parameter is more suitable
For i=1: UNAmount
r(i)= 1/sqrt(RSSI(i)/a);
Wc(i)=L(i)-r(i);
End
%~~~~~~~~~~~~ Use the maximum likelihood estimation method to find the coordinates of
unknown nodes~~~~~~~~~~~~
d=Distance;
a=zeros(2,BeaconAmount-1);
For i=1:2
For j=1: (BeaconAmount-1)
a(i,j)=Beacon(i,j)-Beacon(i,BeaconAmount); The coordinates of the first (BeaconAmount-1)
beacon nodes are sequentially subtracted from the coordinates of the last beacon node.
End
End
A=2*(a'); %' indicates the transposition of the matrix
B=zeros(BeaconAmount-1,1);
For m=1: UNAmount
For i=1:(BeaconAmount-1)
B(i,1)=d(BeaconAmount,m)^2-d(i,m)^2+Beacon(1,i)^2-
Beacon(1,BeaconAmount)^2+Beacon(2,i)^ 2-Beacon(2,BeaconAmount)^2; %B=b matrix
End
X1=inv(A'*A)*A'*B; %inv denotes the inverse matrix of the matrix
X(1,m)=X1(1,1)
X(2,m)=X1(2,1) % Store the coordinates of the unknown node in order into the X matrix
End
%--------------------- Simulation Error -------------------------- ---------------------------
Error=zeros(1, UNAmount); % unknown node positioning error
For i=1: UNAmount
Error(i)=wc(i);
End
Figure(2)
Stem(error)
Xlabel('unknown node number')
Title ('error of each unknown node')
Berror =sum(error)./UNAmount % average positioning error
Accuracy = Berror /R % Positioning Accuracy
%.....................................Power consumption.............................. .........................
GLSH=zeros(1, UNAmount); % unknown node power loss
For i=1: UNAmount
GLSH(i)=PLW(i);
End
Figure(3)
Plot(GLSH)
Xlabel('unknown node number')
Title ('power loss per unknown node')
B=[10 20 30 40 50 60]; %Reset the number of beacon nodes
N=100; % unknown number of nodes
For n=1: length(B) % beacon node array length
K=B(n); number of each beacon node in the % beacon node array
For u=1:K
For v=1:N
Distance(u,v)=((Sxy(2,u)-Sxy(2,v))^2+(Sxy(3,u)-Sxy(3,v))^2)^0.5;%Unknown Node Beacon
node distance matrix
End
End
For i=1:K
For j=1:N
JL(j)=Distance(i,j);
End
End

For j=1:N
PL(j)=PL0+10*n*log10(JL(j)/d0); % The total transmission loss unit is db
PLW(j) = 10^(PL(j)/10);
End
s=zeros(1, length(B));
Ss=zeros(1, length(B));
For i=1:N
GSH(i)=PLW(i); % The power consumption unit of each unknown node is W
End
s=sum(GSH)
End

You might also like