📄 zbtopology.m
字号:
% ZigBee physical network topology generator
rand('state', rseed);
randn('state', rnseed);
bSize=(Cm^(Lm+1)-1)/(Cm-1);
maxN=min([65536/16, bSize]); % the maximal number of node can be supported <= 4096
% maxn is the actual number of nodes in the simulation
maxn=min([n, maxN]);
maxX=100; maxY=100;
% We may later need to specify how many leaves and non-leaves out of maxn
% Internal parameters
for i=0: Lm
cSkip(i+1) = (Cm^(Lm-i+1)-1)/(Cm-1);
end
maxNeighbor=maxn-1;
maxInfo=6;
% node information includes:
% 1: id
% 2: position x
% 3: position y
% 4: number of children (at most Cm)
% 5: number of neighbors (at most maxNeighbor)
% 6: level
cc = zeros(1, 4);
% Output parameters
info=zeros(maxn, maxInfo);
neighbor=zeros(maxn, maxNeighbor) - 1;
tneighbor=zeros(maxn, Cm+1) - 1;
neighbor2=zeros(maxn, maxNeighbor+1);
% First, initialize the root (WPAN coordinator) at the center
x=1/2*maxX;
y=1/2*maxY;
info(1 , :) = [0, x, y, 0, 0, 0];
% tneighbor(1, 1) = -1; % the root does not have on-tree parent
% Associate new nodes
for i = 2: maxn
while(1)
successSign = 0;
x=rand * maxX;
y=rand * maxY;
for j = 1: (i-1)
if (sqrt((info(j,2)-x)^2+(info(j,3)-y)^2) > tranRange) % j is out of range
continue;
else
[level, child] = zblevel(Cm, Lm, info(j, 1)); % we only need 'level' here
if (info(j, 4) < min([Cm, cSkip(level+1)-1])) % j can have more children
% if level >= Lm % j is a leaf, cannot have any children
% continue;
% end
t1 = info(j, 1) + 1 + info(j, 4) * cSkip(level+1+1); % new address
if t1 >= maxN
continue;
end
info(i, 1) = t1;
info(i, 2:5) = [x, y, 0, 0];
info(i, 6) = level + 1;
tneighbor(i, 1) = j; % j is i's on-tree parent
info(j, 4) = info(j, 4) + 1;
tneighbor(j, 1+info(j, 4)) = i; % i is j's new child
successSign = 1;
break;
end
end
end
if successSign == 1
break;
end
end % while
end
if drawFigure >= 1
% colordef none, whitebg
figure(1);
axis equal
hold on;
set(gca,'Box','on');
plot(info(:, 2), info(:, 3), 'ko', 'MarkerSize', 5);
plot(info(1, 2), info(1, 3), 'k*');
title('Physical network topology');
xlabel('X');
ylabel('Y');
axis([0, maxX, 0, maxY]);
set(gca, 'XTick', [0; maxX]);
set(gca, 'YTick', [maxY]);
end
if drawFigure >= 2
% Draw on-tree links
for i = 1 : maxn
if info(i, 4) > 0
for j = 1 : info(i, 4)
k = tneighbor(i, j + 1);
line([info(i, 2), info(k, 2)], [info(i, 3), info(k, 3)], 'Color', 'k', 'LineStyle', '-', 'LineWidth', 1.5);
end
end
end
end
% Construct neighbor table
for i = 1: maxn
k=0;
for j = 1: maxn
if i == j
continue;
end
if (sqrt((info(j,2)-info(i,2))^2+(info(j,3)-info(i,3))^2) > tranRange) % j is out of range
if (sqrt((info(j,2)-info(i,2))^2+(info(j,3)-info(i,3))^2) <= tranRange)
neighbor2(i, 1) = neighbor2(i, 1) + 1;
neighbor2(i, neighbor2(i, 1) + 1) = j;
end
continue;
else
k = k + 1;
neighbor(i, k) = j;
end
end
info(i, 5) = k;
end
if drawFigure >= 3
% Draw other links
for i = 1 : maxn
if info(i, 5) > 0
nt = neighbor(i, 1:info(i, 5));
nt = setdiff(nt, tneighbor(i, 1: info(i, 4) + 1));
t1 = length(nt);
if t1 > 0
for j = 1 : t1
k = nt(j);
if i < k
line([info(i, 2), info(k, 2)], [info(i, 3), info(k, 3)], 'Color', 'k', 'LineStyle', ':', 'LineWidth', 0.5);
end
end
end
end
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -