-
大小: 5KB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-06-18
- 語言: Matlab
- 標簽: MATLABLEACH??
資源簡介
本代碼是基于MATLAB平臺的無線傳感器網絡中LEACH協(xié)議的仿真源代碼,注釋清楚,適合初學者學習

代碼片段和文件信息
clear;
%PARAMETERS
%?dimensions?x(m)?y(m)
xm=100;
ym=100;
%x?and?y?Coordinates?of?the?Sink
sink.x=0.5*xm;
sink.y=0.5*ym;
%Number?of?Nodes?in?the?field
n=100
%Optimal?Election?Probability?of?a?node?to?become?cluster?head
p=0.05;?%Variable
%Energy?Model?(all?values?in?Joules)
%Initial?Energy?
Eo=0.02;
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit?Amplifier?types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data?Aggregation?Energy
EDA=5*0.000000001;
%maximum?number?of?rounds
rmax=50
%PARAMETERS?Done
%Computation?of?do
do=sqrt(Efs/Emp);
%Creation?of?the?random?Sensor?Network
figure(1);
hold?off;
for?i=1:1:n
???%?S(i).xd=rand(11)*xm;
???%?XR(i)=S(i).xd;
????%S(i).yd=rand(11)*ym;
????%YR(i)=S(i).yd;
????load(‘XR.mat‘)
????load(‘YR.mat‘)
????S(i).xd=XR(i);
????S(i).yd=YR(i);
????S(i).G=0;?%這個函數小于等于0才能去選拔簇頭,也是為了讓當選的簇頭不再重復當選簇頭設置的變量
????S(i).E=Eo;?%初始能量
????%initially?there?are?no?cluster?heads?only?nodes
????S(i).type=‘N‘;
???plot(?XR(i)YR(i)‘o‘);
????
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xdS(n+1).yd‘x‘);
????????
%First?Iteration
figure(1);
%counter?for?CHs
countCHs=0;
%counter?for?CHs?per?round
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;?%這幾句代碼是干什么用的?
for?r=1:1:rmax
????r
??%Operation?for?epoch
??if(mod(r?round(1/p)?)==0)?%滿足這個條件就清零
????for?i=1:1:n
????????S(i).G=0;
????????S(i).cl=0;
????end
??end
hold?off;
%Number?of?dead?nodes
dead=0;
%counter?for?bit?transmitted?to?bases?Station?and?to?Cluster?Heads
packets_TO_BS=0;
packets_TO_CH=0;
%counter?for?bit?transmitted?to?bases?Station?and?to?Cluster?Heads??per?round
PACKETS_TO_CH(r+1)=0;
PACKETS_TO_BS(r+1)=0;
figure(1);
for?i=1:1:n
????%checking?if?there?is?a?dead?node
????if?(S(i).E<=0)
????????plot(S(i).xdS(i).yd‘red?.‘);
????????dead=dead+1;
????????hold?on;????
????end
????if?S(i).E>0
????????S(i).type=‘N‘;
????????plot(S(i).xdS(i).yd‘o‘);
????????hold?on;
????end
end
plot(S(n+1).xdS(n+1).yd‘x‘);?%基站用x表示
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
%When?the?first?node?dies
if?(dead==1)
????if(flag_first_dead==0)
????????first_dead=r???%第一個節(jié)點死亡的輪數
????????flag_first_dead=1;
????end
end
countCHs=0;
cluster=1;%定義一個從1開始的下標參數,真正簇頭個數應該減去1(因為從1開始,所以第一個選出的簇頭應該是2-1=1,如果從0開始,就不用減去1)
for?i=1:1:n
???if(S(i).E>0)
???temp_rand=rand;?%rand是函數?,沒有括號表示生成0-1的隨機數???
???if?(?(S(i).G)<=0)??%表示該節(jié)點可以進行簇頭選舉
?%Election?of?Cluster?Heads
?if(temp_rand<=?(p/(1-p*mod(rround(1/p)))))??%簇頭選舉公式
????????????countCHs=countCHs+1;
????????????packets_TO_BS=packets_TO_BS+1;
????????????PACKETS_TO_BS(r+1)=packets_TO_BS;
????????????
????????????S(i).type=‘C‘;?%C表示什么?
????????????S(i).G=round(1/p)-1;?%????賦予一個大于0的數,該節(jié)點就不能進行簇頭選取(當選過的簇頭不能進行簇頭選舉)
????????????C(cluster).xd=S(i).xd;
????????????C(cluster).yd=S(i).yd;?%簇頭位置坐標
????????????plot(S(i).xdS(i).yd‘k*‘);
????????????
????????????distance(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1003??2018-03-29?20:30??XR.mat
?????文件????????1003??2018-03-29?20:30??YR.mat
?????文件?????????420??2018-03-29?20:28??xy.m
?????文件????????6951??2018-04-24?18:59??LEACH2_G.m
- 上一篇:視頻車輛檢測
- 下一篇:LEACH多跳傳輸協(xié)議matlab實現(xiàn)
評論
共有 條評論