📄 aloha2.m
字号:
clear;
disp(' ★ ★ ★ ★ ★ ★ ★ALOAH算法模拟实现 ★ ★ ★ ★ ★ ★ ★ ');
disp(' ★ created: 2009/04/17 ★ ');
disp(' ★ filename: aloha2.h ★ ');
disp(' ★ purpose: 比较不同标准下的算法优劣 ★ ');
disp(' ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ');
tag_count=input('输入标签数量(大于1小于65535): ');
A(1,tag_count)=0;
disp('输入您要选择的类型:');
disp('§1、随机生成连续标签');
disp('§2、随机生成标签');
choose=input('输入:')
if choose==1
i=1;
for j=tag_count:-1:1%初始化标签数组,ID为连续,降序排列处理方便
A(i)=j;
i=i+1;
end
dec2bin(A)
else if choose==2
fid=fopen('testdata.txt','r');
A=fread(fid,tag_count,'int32')
fclose(fid); %随机生成16位的标签
end
dec2bin(A)
end
zhen=input('输入每帧时隙数(大于1小于255): ');
B(1,tag_count)=0;
request_time=0;
answer_time=0;
k=1;
C(1,tag_count)=0;
D(1,tag_count)=0;
n=1;
identify_tag(1,tag_count)=0;
m=1;
p=0;
while tag_count>0
request_time=request_time+1;
for i=1:tag_count
B(i)=ceil((rand(1)*zhen));
end
n=1;
for j=1:zhen
k=1;
answer_time=answer_time+1;
for i=1:tag_count
if j==B(i)
C(k)=i;
k=k+1;
end
end
if k==1
;
elseif k==2
D(n)=C(1);
n=n+1;
identify_tag(m)=A(C(1));
m=m+1;
else
;
end
end
n-1;
for i=1:tag_count
flag=0;
for j=1:(n-1)
if i==D(j)
flag=1;
D(j)=0;
break;
end
end
if flag==1
if p==0
p=i;
end
else
if p~=0
A(p)=A(i);
p=p+1;
end
end
end
tag_count=tag_count-(n-1);
end
request_time
answer_time
time=request_time+answer_time
identify_tag
disp('其中空闲时隙数为:')
f0=zhen*((1-(1/zhen))^length(A))
disp('其中成功的时隙数为:')
f1=length(A)*((1-(1/zhen))^(length(A)-1))
disp('其中冲突的时隙数为:')
fz=zhen-f0-f1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -