├── Kwan_EE130_ClassPresentation.m ├── Kwan_EE130_ClassPresentation.pdf └── README.md /Kwan_EE130_ClassPresentation.m: -------------------------------------------------------------------------------- 1 | %% EE130: Class Presentation Simulation 2 | 3 | % Matt Kwan 4 | % Due: 3/16/16 5 | 6 | %% Setting the parameters 7 | clear 8 | close all 9 | 10 | TIME_ITER = .001; 11 | MAX_TIMESTEPS = 25; 12 | GRAPH = [ 1 1 0 0; 13 | 1 1 1 1; 14 | 0 1 1 1; 15 | 0 1 1 1; ]; 16 | NUM_NODES = 4; 17 | 18 | % GRAPH = [ ... 19 | % 0 0 1 1 0 0 0 0 0 0; 20 | % 0 0 1 1 0 0 0 0 0 0; 21 | % 1 1 0 0 1 1 0 0 0 0; 22 | % 1 1 0 0 1 1 0 0 0 0; 23 | % 0 0 1 1 0 0 1 1 0 0; 24 | % 0 0 1 1 0 0 1 1 0 0; 25 | % 0 0 0 0 1 1 0 0 1 1; 26 | % 0 0 0 0 1 1 0 0 1 1; 27 | % 0 0 0 0 0 0 1 1 0 0; 28 | % 0 0 0 0 0 0 1 1 0 0; ]; 29 | % NUM_NODES = 10; 30 | 31 | INIT_OFFSET = [2 3 8 1 12 1 3 3 9 10] * TIME_ITER; 32 | INIT_OFFSET = [2 3 8 1 ] * TIME_ITER; 33 | rates = [1.1 .9 .75 1.3] * TIME_ITER; % values are per TIME_ITER 34 | p = .6; % Tuning -- Set by authors; see page 2293 35 | 36 | %% Applying the algorithm 37 | skew_vir = zeros(MAX_TIMESTEPS, NUM_NODES); 38 | skew_rel = cell(MAX_TIMESTEPS, 1); 39 | time_loc = zeros(MAX_TIMESTEPS, NUM_NODES); 40 | time_vir = zeros(MAX_TIMESTEPS, NUM_NODES); 41 | offset_vir = zeros(MAX_TIMESTEPS, NUM_NODES); 42 | 43 | skew_vir(1, :) = ones(1, NUM_NODES); 44 | skew_rel(:) = {GRAPH}; 45 | time_loc(1, :) = INIT_OFFSET; 46 | time_vir(1, :) = time_loc(1,:); 47 | 48 | % Assumes that there is TX/RX between all nodes at every time step 49 | for t = 2:1:MAX_TIMESTEPS 50 | time_loc(t, :) = time_loc(t-1, :) + rates; 51 | % Go through the graph for links 52 | for i=1:1:NUM_NODES 53 | for j=1:1:NUM_NODES 54 | if GRAPH(i, j) ~= 0 % link is found where i RXs from j 55 | % A. RELATIVE SKEW ESTIMATION 56 | skew_rel{t}(i,j) = p*skew_rel{t-1}(i,j) + (1-p) ... 57 | *(time_loc(t,j)-time_loc(t-1,j)) ... 58 | /(time_loc(t,i)-time_loc(t-1,i)); 59 | % B. SKEW COMPENSATION 60 | skew_vir(t,i) = p*skew_vir(t-1,i) ... 61 | + (1-p)*skew_rel{t-1}(i,j)*skew_vir(t-1,j); 62 | % C. OFFSET COMPENSATION 63 | offset_vir(t,i) = offset_vir(t-1,i) + (1-p) ... 64 | * (skew_vir(t-1,j)*time_loc(t-1,j) ... 65 | + offset_vir(t-1, j) ... 66 | - skew_vir(t-1,i)*time_loc(t-1,i) ... 67 | - offset_vir(t-1, i)); 68 | end 69 | end 70 | end 71 | time_vir(t, :) = skew_vir(t, :).*time_loc(t, :) + offset_vir(t,:); 72 | end 73 | 74 | %% Plotting the data 75 | figure; 76 | for i=1:1:NUM_NODES 77 | plot(1:1:MAX_TIMESTEPS, time_loc(:, i),'color', rand(1,3)), hold on 78 | end 79 | title('Local Time within Nodes'); 80 | xlabel('Iterations'); 81 | ylabel('Time (s)'); 82 | hold off; 83 | 84 | figure; 85 | for i=1:1:NUM_NODES 86 | plot(1:1:MAX_TIMESTEPS, time_vir(:, i),'color', rand(1,3)), hold on 87 | end 88 | title('Virtual Time within Nodes'); 89 | xlabel('Iterations'); 90 | ylabel('Time (s)'); 91 | hold off; 92 | 93 | figure; 94 | temp = zeros(1,MAX_TIMESTEPS); 95 | for i=1:1:NUM_NODES 96 | for j=1:1:MAX_TIMESTEPS 97 | temp(j) = skew_rel{j}(2,i); 98 | end 99 | plot(1:1:MAX_TIMESTEPS, temp,'color', rand(1,3)), hold on 100 | end 101 | % Note: a line at 0 indicates no link. A line at 1 indicates self. 102 | % Expectation: ([Other Node Speed] / [This Node Speed]) convergence 103 | title('Relative Skew Estimation as Seen by Node 2') 104 | xlabel('Iterations'); 105 | ylabel('Skew (rate/rate)'); 106 | hold off; 107 | 108 | figure; 109 | for i=1:1:NUM_NODES 110 | plot(1:1:MAX_TIMESTEPS, skew_vir(:, i),'color', rand(1,3)), hold on 111 | end 112 | title('Virtual Skew Estimation within Nodes'); 113 | xlabel('Iterations'); 114 | ylabel('Skew (rate/rate)'); 115 | hold off; 116 | 117 | figure; 118 | for i=1:1:NUM_NODES 119 | plot(1:1:MAX_TIMESTEPS, offset_vir(:, i),'color', rand(1,3)), hold on 120 | end 121 | title('Virtual Offset Estimation within Nodes'); 122 | xlabel('Iterations'); 123 | ylabel('Offset (sec)'); 124 | hold off; 125 | 126 | figure; 127 | error = zeros(MAX_TIMESTEPS, NUM_NODES); 128 | for j=1:1:MAX_TIMESTEPS 129 | for i=1:1:NUM_NODES 130 | error(j,i) = 100*(time_vir(j,i) - mean(time_vir(j,:)))/mean(time_vir(j,:)); 131 | end 132 | end 133 | for i=1:1:NUM_NODES 134 | plot(1:1:MAX_TIMESTEPS, error(:, i),'color', rand(1,3)), hold on 135 | end 136 | title('Error Percentage from Instantaneous Mean of Local Times'); 137 | xlabel('Iterations'); 138 | ylabel('Error Percentage (%)'); 139 | hold off; -------------------------------------------------------------------------------- /Kwan_EE130_ClassPresentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattkwan16/Distributed-Network-Consensus/3f2d4b0ef7bc651034b7ce726e9a850ffb97187b/Kwan_EE130_ClassPresentation.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Distributed-Network-Consensus 2 | # Matt Kwan 3 | 4 | MATLAB implementation of "Average TimeSync Protocol", an algorithm for clock synchronization via distributed linear iterations from IEEE publication "A Distributed Consensus Protocol for Clock Synchronization in Wireless Sensor Networks" by Luca Schenato and Giovanni Gamba presented for the 46th IEEE Conference on Decision and Control in New Orleans, LA, USA, Dec. 12-14, 2007. 5 | 6 | Created for Tufts University - Networked Estimation and Control (March 2016) 7 | --------------------------------------------------------------------------------