⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventsimulation.m

📁 run these simulations of wireless sensor networks written in matab.event model,event simulations.
💻 M
字号:
clear all;
close all;

inf = 1e4; %infinite distance

R = 100;  %side of the square region.
N = 500;   %number of sensors deployed in the region.
B = 512; %packet size in bits
et = 50e-9 * B; % energy spent in transmission electronics
er = 50e-9 * B; % energy spent in reception
ed = 100e-12 * B; % energy spent in the transmit amplifier
Eo = 3; % intial energy of each sensor node
E_th = er;
network_lifetime = 0; % network lifetime
p = 2; %path loss exponent

X_max = R/2;
Y_max = R/2;

r_sense = 5;
r_radio = 2*r_sense;

X_sink = 0;
Y_sink = 0;

node_data = zeros(10,N); % 1&2 - node co-ordinates, 3- node energy, 4&5 - index and distance to next relay node, 6 - indication that it is within the commn. range of sink. 7 - event sense indicator, 8-packet received, 8 - packet generated, 9 - packet transmitted.
node_data(4,:) = inf;
node_data(5,:) = inf;
node_data(3,:) = Eo;


node_connect = zeros(50,2,N);


%%%%%%%%% Uniform distribution of all nodes in the given square area %%%%%%%%%

for n = 1:N
    
    x = ceil(R*rand(1,1)-(R/2));
    y = ceil(R*rand(1,1)-(R/2));
    
    if (x < -X_max | x > X_max | y < -Y_max | y > Y_max | (x == 0 & y == 0))
        
        n = n-1;
    
     else
        
        node_data(1,n) = x;
        node_data(2,n) = y;
    
      end
end

hold on

for n = 1:N
    
    plot(node_data(1,n),node_data(2,n),'.');
end

plot(X_sink,Y_sink,'r*');

hold off
    
%%%%%%%%% Procedure for finding the nearest neighbour of each node %%%%%%%%%


%%%%%%%%% Finding the nodes within the radio range of a particular node %%%%%%%%%%

for i = 1:N
    
    k = 1;
    
    if((node_data(1,i)^2 + node_data(2,i)^2)^0.5 < r_radio)
        
        node_connect(k,2,i) = (node_data(1,i)^2 + node_data(2,i)^2)^0.5;
        node_connect(k,1,i) = 0;
        k = k+1;
    end
    
    for j = 1:N
        
        if(((node_data(1,j) - node_data(1,i))^2 + (node_data(2,j) - node_data(2,i))^2)^0.5 <= r_radio & j ~= i)
                
            node_connect(k,2,i) = (((node_data(1,j) - node_data(1,i))^2 + (node_data(2,j) - node_data(2,i))^2)^0.5);
            node_connect(k,1,i) = j;
            k = k+1;
            
        end
    end
end

k = 0;

%%%%%%%%% Finding those nodes for whom sink is within communication range and sorting them in ascending order %%%%%%%%%

for i = 1:N
    
    if((node_data(1,i)^2 + node_data(2,i)^2)^0.5 < r_radio)

        k = k+1;
        sink_neighbors(k,1) = i;
        sink_neighbors(k,2) = (node_data(1,i)^2 + node_data(2,i)^2)^0.5;
        %k = k+1;
        
        node_data(4,i) = 0;
        node_data(5,i) = sink_neighbors(k,2);
        node_data(6,i) = 1;
    end
end

[row col] = size(sink_neighbors);


for i = 1:(row-1)
    
    %temp = sink_neighbors(1,:);
    
    for j = (i+1):row
        
        if(sink_neighbors(i,2) > sink_neighbors(j,2))
            
            temp = sink_neighbors(i,:);
            sink_neighbors(i,:) = sink_neighbors(j,:);
            sink_neighbors(j,:) = temp;
            temp = [];
        end
    end
end

arr = size(sink_neighbors);

neighbor_found = zeros(3,N); % 1st row for indicating whether its neighbour has been found  2&3 rows - index and distance of neighbouring node.
neighbor_found_count = 0; %number of nodes whose neighbour has been identified.

for i = 1:arr(1,1)
    
    node_temp = sink_neighbors(i,:);
    neighbor_found(1,node_temp(1,1)) = 1;
    neighbor_found(2,node_temp(1,1)) = 0;
    neighbor_found(3,node_temp(1,1)) = node_temp(1,2);

    neighbor_found_count = neighbor_found_count + 1;
end

node_temp = sink_neighbors(:,1)'; %temp array to store the last set of nodes whose neighbours have been found out.



%%%%%%%%%  Procedure to find nearest neighbour for each node and thus establish a path from each node in sensor network to the sink without any loops.%%%%%%%%%
%%%%%%%%%  ******************************************************************************************************************************************  

while(neighbor_found_count <= N) % neighbor_found_count - number of nodes whose neighbours have been found.
    
    temp_count = 1; % initialising the count for the temp_count variable which stores the number of nodes in the next tier which are within the commn range of the immediate previous set of nodes whose neighbours towards the sink have been identified
    
    for i = 1:length(node_temp) %node_temp - array which contains those set of nodes for which the neighbours have been recently found out in the previous iteration.
        
        n_count = 0; % count of neighbors of a particular node.
        
        for j = 1:N
            
            if(neighbor_found(1,j) ~= 1) % checking if the chosen node's neighbour has already been found or not.
                
                distance = ((node_data(1,node_temp(i)) - node_data(1,j))^2 + (node_data(2,node_temp(i)) - node_data(2,j))^2)^0.5;
                
                if(distance <= r_radio)
                    
                    n_count = n_count + 1;
                    node_neighbors(n_count,1,i) = j; % node_neighbors - stores the index and distance of those nodes which are within commn range of each of the particular node selected in the outer loop.
                    node_neighbors(n_count,2,i) = distance;
                    
                    if(temp_count == 1)
                        
                        node_temp_next(1,temp_count) = j;
                        temp_count = temp_count + 1;
                        
                    else
                        
                        flag = 0; % ensuring that each node in the next tier which are neighbours to nodes in the present tier are not repeated ( which can be the case if a node in next tier is within commn range of more than one node in the present tier.
                        
                        for k = 1:length(node_temp_next)
                            
                            if(j == node_temp_next(k))
                                
                                flag = 1;
                                break;
                            end
                        end
                        
                        if(flag == 0)
                            
                            node_temp_next(1,temp_count) = j;
                            temp_count = temp_count + 1;
                        end
                    end
                end
            end
        end
    end
    
    for i = 1:length(node_temp_next) % procedure to find the neighbours of each of the nodes in the next tier.
        
        temp_distance = ((node_data(1,node_temp_next(1,i)) - node_data(1,node_temp(1,1)))^2  + (node_data(2,node_temp_next(1,i)) - node_data(2,node_temp(1,1)))^2)^0.5;
        temp_index = 1;
        
        for j = 1:length(node_temp)
            
            dist = ((node_data(1,node_temp_next(1,i)) - node_data(1,node_temp(1,j)))^2  + (node_data(2,node_temp_next(1,i)) - node_data(2,node_temp(1,j)))^2)^0.5;
            
            if(dist < temp_distance)
                
              temp_distance = ((node_data(1,node_temp_next(1,i)) - node_data(1,node_temp(1,j)))^2  + (node_data(2,node_temp_next(1,i)) - node_data(2,node_temp(1,j)))^2)^0.5;
              temp_index = j;
            end
        end
        
        neighbor_found(1,node_temp_next(1,i)) = 1;
        neighbor_found(2,node_temp_next(1,i)) = node_temp(1,temp_index);
        neighbor_found(3,node_temp_next(1,i)) = temp_distance;

        neighbor_found_count = neighbor_found_count + 1;
    end
    
    node_temp = []; % clearing the array containing the present tier with the nodes in the next tier 
    
    node_temp = node_temp_next;
    
    node_temp_next = [];
    
    node_neighbors = [];
end

node_data(4:5,:) = neighbor_found(2:3,:); % updating the details of neighbours of each node to the node_data array.

            
                            %%%%%%%%%  Network Framework Established %%%%%%%%%              
                            %%%%%%%%%  *****************************. %%%%%%%%%
                            
 lifetime_over = 0; %indictes whether network is still alive or not
 pkt_throughput = 0; %number of packets successfully reaching the sink before network is dead.
 
 while(lifetime_over == 0)
     
     X_event = ceil(R*rand(1,1)-(R/2)); % X and Y co-ordinates of the events.
     Y_event = ceil(R*rand(1,1)-(R/2));
     
     network_lifetime = network_lifetime + 1
     
     k = 0; %count of sensors detecting an event.
     
     for i = 1:N
         
         if(((X_event - node_data(1,i))^2 + (Y_event - node_data(2,i))^2)^0.5 <= r_sense)
             
             k = k+1;
             event_sense(1,k) = i;
             node_data(9,i) = 1; %setting the packet generation row of the sensor to 1 as event has occured within its sensing range.
         end
         
         node_data(10,i) = node_data(8,i) + node_data(9,i); %number of packets transmitted by a node = number generated + number previously received.
         
         node_data(8,i) = 0; % clearing previously packet received number
         node_data(9,i) = 0; % clearing packet generated number
         
         node_data(3,i) = node_data(3,i) - ((et + ed*(node_data(5,i)^p))*node_data(10,i)); % energy spent in transmission of packets.

     end
     
     for i = 1:N
         
         if(node_data(4,i) ~= 0 )
             
             node_data(8,node_data(4,i)) = node_data(8,node_data(4,i)) + node_data(10,i);
             
             node_data(3,node_data(4,i)) = node_data(3,node_data(4,i)) - er*node_data(10,i);

         
         elseif(node_data(4,i) == 0)
             
             pkt_throughput = pkt_throughput + node_data(10,i);
         end
         
         
         node_data(10,i) = 0; % clearing transmitted packet number
         
     end
     
     for i = 1:N
         
         if(node_data(3,i) <= E_th)
             
             i
             lifetime_over = 1;
             break;
         end
     end
     
 end
         
        
    
            
     
                          
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -