%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DV-Hop Localization Algorithm ~~~~~~~~~~~~~~~~~~~~~~~~
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% BorderLength-----Side length of square regions,
% NodeAmount-------The number of network nodes
% BeaconAmount--- Beacon nodes
% Sxy-------------- 3*NodeAmount matrix; (First row: Serial number for the
% nodes), (2nd & 3th row : their coordinates)
%%Beacon----------Beacon nodes coordinate matrix; BeaconAmount * BeaconAmount
%%UN-------------Unknown node coordinate matrix; 2 * UNAmount
%Distance------ distance matrix between Unknown nodes and beacon nodes ;
%h---------------matrix of initial number of hops between nodes
%X---------------Node estimates the coordinates of the initial matrix, X = [x, y]'
%R------------------Communication distance of node, typically 10-100m
clear,close all;
BorderLength=100;
NodeAmount=100;
BeaconAmount=8;
UNAmount=NodeAmount-BeaconAmount;
R=50;
h=zeros(NodeAmount,NodeAmount); %The initial number of hops is 0; BeaconAmount row, NodeAmount column
X=zeros(2,UNAmount);%Estimated initial node coordinate matrix
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Generating uniformly distributed random topology within the square area~~~~~~~~~~~~~~~~~~~~
C=BorderLength.*rand(2,NodeAmount);%Coordinate of all nodes
Sxy=[[1:NodeAmount];C];
Beacon=[Sxy(2,1:BeaconAmount);Sxy(3,1:BeaconAmount)];%Beacon nodes coordinate
UN=[Sxy(2,(BeaconAmount+1):NodeAmount);Sxy(3,(BeaconAmount+1):NodeAmount)];%Unknown node coordinates
%Draw node distribution
plot(Sxy(2,1:BeaconAmount),Sxy(3,1:BeaconAmount),'r*',Sxy(2,(BeaconAmount+1):NodeAmount),Sxy(3,(BeaconAmount+1):NodeAmount),'k.');
xlim([0,BorderLength]);
ylim([0,BorderLength]);%Limitations of the area
title('* Red beacon nodes . Black unknown node')
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~Creation of hops matrix~~~~~~~~~~~~~~~~~~~~~~
for i=1:NodeAmount
for j=1:NodeAmount
Dall(i,j)=((Sxy(2,i)-Sxy(2,j))^2+(Sxy(3,i)-Sxy(3,j))^2)^0.5;%Mutual distances between all nodes
if (Dall(i,j)<=R)&(Dall(i,j)>0)
h(i,j)=1;%The initial jump matrix
elseif i==j
h(i,j)=0;
else h(i,j)=inf;% Dall(i,j) > R
end
end
end
%~~~~~~~~~~~~~~~~~~~~~~~~~Shortest path algorithm to compute node hops~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for k=1:NodeAmount
for i=1:NodeAmount
for j=1:NodeAmount
if h(i,k)+h(k,j)<h(i,j) %min(h(i,j),h(i,k)+h(k,j))
h(i,j)=h(i,k)+h(k,j);
end
end
end
end
h
%~~~~~~~~~~~~~~~~~~~~~~~~~Seeking school each beacon node positive~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
h1=h(1:BeaconAmount,1:BeaconAmount);
D1=Dall(1:BeaconAmount,1:BeaconAmount);
for i=1:BeaconAmount
dhop(i,1)=sum(D1(i,:))/sum(h1(i,:));%Average per each beacon node hop distance
end
D2=Dall(1:BeaconAmount,(BeaconAmount+1):NodeAmount);%BeaconAmount row UNAmount column
for i=1:BeaconAmount
for j=1:UNAmount
if min(D2(:,j))==D2(i,j)
Dhop(1,j)=D2(i,j);%Unknown nodes obtained from the recent correction beacon
end
end
end
Dhop % 1*UNAmount : distance from each unknown node to its nearest beacon node
%~~~~~~~~~~~~~~~~~~~~~~~~~~~Estimated distance with hops~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hop1=h(1:BeaconAmount,(BeaconAmount+1):NodeAmount)% BeaconAmount row* UNAmount column// Unknown number of beacon nodes to jump,
for i=1:UNAmount
hop=Dhop(1,i);%hop for the school obtained from the recent positive beacon
Distance(:,i)=hop*hop1(:,i);%%UN Beacon row column;
end
% %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Least squares method for determination of unknown point coordinates~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
d=Distance;
for i=1:2
for j=1:(BeaconAmount-1)
a(i,j)=Beacon(i,j)-Beacon(i,BeaconAmount);
end
end
A=-2*(a');
% d=d1';
for m=1:UNAmount
for i=1:(BeaconAmount-1)
B(i,1)=d(i,m)^2-d(BeaconAmount,m)^2-Beacon(1,i)^2+Beacon(1,BeaconAmount)^2-Beacon(2,i)^2+Beacon(2,BeaconAmount)^2;
end
X1=inv(A'*A)*A'*B; % Least Square method
X(1,m)=X1(1,1); % x coordinate
X(2,m)=X1(2,1); % y coordinate
end
UN %True coordinates of nodes
X %Estimated coordinates of nodes using DV-HOP
for i=1:UNAmount
error(1,i)=(((X(1,i)-UN(1,i))^2+(X(2,i)-UN(2,i))^2)^0.5);
end
figure;plot(error,'-o');
title('Error of each unknown node');
error=sum(error)/UNAmount;
Accuracy=error/R;
DV_HOP.zip_dv hop_dv hop localization_dv-hop algorithm_improved
版权申诉
79 浏览量
2022-07-15
11:09:13
上传
评论 1
收藏 2KB ZIP 举报
小波思基
- 粉丝: 74
- 资源: 1万+