• PUDN用户
    了解作者
  • matlab
    开发工具
  • 3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 2
    下载次数
  • 2015-05-06 10:11
    上传日期
适用于无线传感器网络MATLAB环境下的用于路由的仿真源代码
LEACH.zip
  • LEACH.m
    8.3KB
内容介绍
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % SEP: A Stable Election Protocol for clustered % % heterogeneous wireless sensor networks % % % % (c) Georgios Smaragdakis % % WING group, Computer Science Department, Boston University % % % % You can find full documentation and related information at: % % http://csr.bu.edu/sep % % % % To report your comment or any bug please send e-mail to: % % gsmaragd@cs.bu.edu % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % This is the LEACH [1] code we have used. % % The same code can be used for FAIR if m=1 % % % % [1] W.R.Heinzelman, A.P.Chandrakasan and H.Balakrishnan, % % "An application-specific protocol architecture for wireless % % microsensor networks" % % IEEE Transactions on Wireless Communications, 1(4):660-670,2002 % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%% %Field Dimensions - x and y maximum (in meters) 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.1; %Energy Model (all values in Joules) %Initial Energy Eo=0.5; %Eelec=Etx=Erx 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; %Values for Hetereogeneity %Percentage of nodes than are advanced m=0.1; %\alpha a=1; %maximum number of rounds rmax=10 %%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%% %Computation of do do=sqrt(Efs/Emp); %Creation of the random Sensor Network figure(1); for i=1:1:n S(i).xd=rand(1,1)*xm; XR(i)=S(i).xd; S(i).yd=rand(1,1)*ym; YR(i)=S(i).yd; S(i).G=0; %initially there are no cluster heads only nodes S(i).type='N'; temp_rnd0=i; %Random Election of Normal Nodes if (temp_rnd0>=m*n+1) S(i).E=Eo; S(i).ENERGY=0; plot(S(i).xd,S(i).yd,'o'); hold on; end %Random Election of Advanced Nodes if (temp_rnd0<m*n+1) S(i).E=Eo*(1+a) S(i).ENERGY=1; plot(S(i).xd,S(i).yd,'+'); hold on; end end S(n+1).xd=sink.x; S(n+1).yd=sink.y; plot(S(n+1).xd,S(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=0: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; %Number of dead Advanced Nodes dead_a=0; %Number of dead Normal Nodes dead_n=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).xd,S(i).yd,'red .'); dead=dead+1; if(S(i).ENERGY==1) dead_a=dead_a+1; end if(S(i).ENERGY==0) dead_n=dead_n+1; end hold on; end if S(i).E>0 S(i).type='N'; if (S(i).ENERGY==0) plot(S(i).xd,S(i).yd,'o'); end if (S(i).ENERGY==1) plot(S(i).xd,S(i).yd,'+'); end hold on; end end plot(S(n+1).xd,S(n+1).yd,'x'); STATISTICS(r+1).DEAD=dead; DEAD(r+1)=dead; DEAD_N(r+1)=dead_n; DEAD_A(r+1)=dead_a; %When the first node dies if (dead==1) if(flag_first_dead==0) first_dead=r flag_first_dead=1; end end countCHs=0; cluster=1; for i=1:1:n if(S(i).E>0) S(i).type = 'N'; temp_rand=rand; if ( (S(i).G)<=0) %Election of Cluster Heads if(temp_rand<= (p/(1-p*mod(r,round(1/p))))) countCHs=countCHs+1; packets_TO_BS=packets_TO_BS+1; PACKETS_TO_BS(r+1)=packets_TO_BS; S(i).type='C'; S(i).G=round(1/p)-1; C(cluster).xd=S(i).xd; C(cluster).yd=S(i).yd; plot(S(i).xd,S(i).yd,'k*'); distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 ); C(cluster).distance=distance; C(cluster).id=i; X(cluster)=S(i).xd; Y(cluster)=S(i).yd; cluster=cluster+1; %Calculation of Energy dissipated distance; if (distance>do) S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance )); end if (distance<=do) S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance )); end end end end end STATISTICS(r+1).CLUSTERHEADS=cluster-1; CLUSTERHS(r+1)=cluster-1; %Election of Associated Cluster Head for Normal Nodes for i=1:1:n if ( S(i).type=='N' && S(i).E>0 ) if(cluster-1>=1) min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 ); min_dis_cluster=1; for c=1:1:cluster-1 temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) ); if ( temp<min_dis ) min_dis=temp; min_dis_cluster=c; end end %Energy dissipated by associated Cluster Head min_dis; if (min_dis>do) S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis)); end if (min_dis<=do) S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis)); end %Energy dissipated if(min_dis>0) S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 ); PACKETS_TO_CH(r+1)=n-dead-cluster+1; end S(i).min_dis=min_dis; S(i).min_dis_cluster=min_dis_cluster; end end end hold on; countCHs; rcountCHs=rcountCHs+countCHs; %%%%%%%%%%%%以下程序仅作测试用%%%%%%%%%%%%% Count=0; for i=1:n if S(i).type == 'C' Count = Count+1; CH_ID(Count) = i;%CH_ID记录簇头节点的ID end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Code for Voronoi Cells %Unfortynately if there is a small %number of cells, Matlab's voronoi %procedure has some problems %[vx,vy]=voronoi(X,Y); %plot(X,Y,'r*',vx,vy,'b-'); % hold on; % voronoi(X,Y); % axis([0 xm 0 ym]); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STATISTICS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % DEAD : a rmax x 1 array of number of dead nodes/round % DEAD_A : a rmax x 1 array of number of dead Advanced nodes/round % DEAD_N : a rmax x 1 array of number of dead Normal nodes/ro
评论
    相关推荐
    • matlabcnhelp.rar
      matlab中文帮助很难找的,快速下载
    • MobilePolice.rar
      移动警察,车牌识别,车牌定位系统源代码,已经运用在移动车载稽查系统中。
    • SVM(matlab).rar
      支持向量机(SVM)实现的分类算法源码[matlab]
    • svm.zip
      用MATLAB编写的svm源程序,可以实现支持向量机,用于特征分类或提取
    • Classification-MatLab-Toolbox.rar
      模式识别matlab工具箱,包括SVM,ICA,PCA,NN等等模式识别算法,很有参考价值
    • VC++人脸定位实例.rar
      一个经典的人脸识别算法实例,提供人脸五官定位具体算法及两种实现流程.
    • QPSK_Simulink.rar
      QPSK的Matlab/Simulink的调制解调仿真系统,给出接收信号眼图及系统仿真误码率,包含载波恢复,匹配滤波,定时恢复等重要模块,帮助理解QPSK的系统
    • LPRBPDemo2009KV.rar
      车牌识别,神经网络算法,识别率高达95%,识别时间低于80ms。
    • MODULATION.RAR
      这个源程序代码包提供了通信系统中BPSK,QPSK,OQPSK,MSK,MSK2,GMSK,QAM,QAM16等调制解调方式 用matlab的实现,以及它们在AWGN和Rayleigh信道下的通信系统实现及误码率性能
    • algorithms.rar
      十大算法论文,包括遗传算法,模拟退火,蒙特卡罗法等等,对于初学者很有帮助!!