-
Notifications
You must be signed in to change notification settings - Fork 32
/
Nav_TOA_TDOA_2D_demo.m
330 lines (262 loc) · 10.1 KB
/
Nav_TOA_TDOA_2D_demo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
% Nav_TOA_TDOA 2D_demo.m
close all;
clear all;
clc;
%% parameters
n_base_points = 4;
area_width_m = 1000;
max_depth_m = -100;
propagation_velocity = 1500;
max_iterations = 2000;
precision_threshold = 1E-12;
precision_threshold_hjs = 1E-3;
simplex_size = 3;
hjs_step_size = 3;
contour_levels = 32;
range_measurements_error = 0.01; % 0.01 means 1% of corresponding slant range
%% actual target location
r_ = rand * area_width_m / 2;
az_ = rand * 2 * pi;
actual_target_x = r_ * cos(az_);
actual_target_y = r_ * sin(az_);
actual_target_z = rand * max_depth_m;
%% base points
figure
hold on
grid on
base_points = zeros(n_base_points, 4);
for n = 1:n_base_points
r_ = area_width_m / 2 - rand * area_width_m / 4;
az_ = (n - 1) * 2 * pi / n_base_points;
base_x = r_ * cos(az_);
base_y = r_ * sin(az_);
base_z = -1.5;%rand * max_depth_m;
dist_to_target = Nav_dist_3d(base_x, base_y, base_z, actual_target_x, actual_target_y, actual_target_z);
base_points(n, :) = [ base_x base_y base_z dist_to_target ];
end
N =1:n_base_points;
plot3(actual_target_x, actual_target_y, actual_target_z,...
'p',...
'MarkerFaceColor', 'red',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
plot3(base_points(N, 1), base_points(N, 2), base_points(N, 3),...
'o',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
for n = 1:n_base_points
line([ actual_target_x, base_points(n, 1) ], [ actual_target_y, base_points(n, 2) ], [ actual_target_z, base_points(n, 3) ]);
end
view(45, 15);
legend('target', 'base points');
title('Placement of base stations and the target');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
zlabel('Z coordinate, m');
% adding range measurement errors
base_points(N, 4) = base_points(N, 4) + base_points(N, 4) *...
(rand * range_measurements_error - range_measurements_error / 2);
% error surface tiles
tile_size_m = 10;
n_tiles = area_width_m / tile_size_m;
%% TOA solution
error_surface_toa = zeros(n_tiles, n_tiles);
for t_x = 1:n_tiles
for t_y = 1:n_tiles
error_surface_toa(t_y, t_x) = Nav_eps_toa3d(base_points,...
(t_x - 1) * tile_size_m - area_width_m / 2,...
(t_y - 1) * tile_size_m - area_width_m / 2,...
actual_target_z);
end
end
figure
surf_a = [1:n_tiles] * tile_size_m - area_width_m / 2;
surf(surf_a, surf_a, error_surface_toa);
title('TOA solution: Residual function');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
view(45, 15);
figure
hold on
contourf(surf_a, surf_a, error_surface_toa, contour_levels);
plot(actual_target_x, actual_target_y,...
'p',...
'MarkerFaceColor', 'red',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
plot(base_points(N, 1), base_points(N, 2),...
'o',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
[ x_prev, y_prev ] = Nav_toa_circles_1d_solve(base_points, actual_target_z, pi / 180, 10, 0.1);
[ x_best, y_best, rerr, it_cnt ] = Nav_toa_nlm_2d_solve(base_points, x_prev, y_prev, actual_target_z,...
max_iterations, precision_threshold, simplex_size);
[ x_best_hjs, y_best_hjs, rerr_hjs, it_cnt_hjs ] = Nav_toa_hjs_2d_solve(base_points, x_prev, y_prev, actual_target_z,...
max_iterations, precision_threshold_hjs, hjs_step_size);
plot(x_best, y_best,...
'd',...
'MarkerFaceColor', 'yellow',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
plot(x_best_hjs, y_best_hjs,...
'd',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
title('TOA Solution: Residual function.');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
legend('Residual function value',...
'Actual target location',...
'Base points',...
sprintf('Estimated target location (NLM), E_{radial} = %.3f m in %d iterations', rerr, it_cnt),...
sprintf('Estimated target location (HJS), E_{radial} = %.3f m in %d iterations', rerr_hjs, it_cnt_hjs));
figure
hold on
grid on
dx_nlm = actual_target_x - x_best;
dy_nlm = actual_target_y - y_best;
dx_hjs = actual_target_x - x_best_hjs;
dy_hjs = actual_target_y - y_best_hjs;
dx = max(dx_nlm, dx_hjs);
dy = max(dy_nlm, dy_hjs);
plot(0, 0,...
'p',...
'MarkerFaceColor', 'red',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
plot(dx_nlm, dy_nlm,...
'd',...
'MarkerFaceColor', 'yellow',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
plot(dx_hjs, dy_hjs,...
'd',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
plot(-dx * 2, -dy * 2, '.w');
plot(dx * 2, dy * 2, '.w');
d_delta_nlm = Nav_dist_3d(actual_target_x, actual_target_y, actual_target_z, x_best, y_best, actual_target_z);
d_delta_hjs = Nav_dist_3d(actual_target_x, actual_target_y, actual_target_z, x_best_hjs, y_best_hjs, actual_target_z);
title('TOA Solution: Actual vs. Estimated location');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
legend('Actual target location',...
sprintf('Estimated target location (NLM), abs. error: %.3f m', d_delta_nlm),...
sprintf('Estimated target location (HJS), abs. error: %.3f m', d_delta_hjs));
%% TDOA solution
% since TDOA works with time difference of arriaval,
% we must recalculate base point's distances to times
base_points(N,4) = base_points(N,4) / propagation_velocity;
base_lines = Nav_build_base_lines(base_points, propagation_velocity);
error_surface_tdoa = zeros(n_tiles, n_tiles);
for t_x = 1:n_tiles
for t_y = 1:n_tiles
error_surface_tdoa(t_y, t_x) = Nav_eps_tdoa3d(base_lines,...
(t_x - 1) * tile_size_m - area_width_m / 2,...
(t_y - 1) * tile_size_m - area_width_m / 2,...
actual_target_z);
end
end
figure
hold on
title('Helder-Mead (red) vs. Hooke-Jeeves (green) comparison on TDOA problem');
contourf(surf_a, surf_a, error_surface_tdoa, contour_levels);
plot(actual_target_x, actual_target_y, '*w');
text(actual_target_x, actual_target_y, '\leftarrow Target', 'color', 'white');
plot(base_points(N, 1), base_points(N, 2),...
'o',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
centroid = Nav_centroid(base_points);
text( centroid(1), centroid(2), '\leftarrow Starting point', 'color', 'white');
[ x_best, y_best, rerr, it_cnt, fe_cnt ] = Nav_tdoa_nlm_2d_solve_demo(base_lines, centroid(1), centroid(2), actual_target_z,...
max_iterations, precision_threshold, simplex_size);
[ x_best_hjs, y_best_hjs, rerr_hjs, it_cnt_hjs, fe_cnt_hjs ] = Nav_tdoa_hjs_2d_solve_demo(base_lines, centroid(1), centroid(2), actual_target_z,...
max_iterations, precision_threshold_hjs, hjs_step_size);
text(actual_target_x, actual_target_y, '\leftarrow Target', 'color', 'white');
text(centroid(1), centroid(2), '\leftarrow Starting point', 'color', 'white');
text(-area_width_m / 2 + 10, area_width_m / 2 - 50,...
sprintf('Nelder-Mead finished in %d iterations (%d function evals)\r\nHooke-Jeeves finsihed in %d iterations (%d function evals)',...
it_cnt, fe_cnt, it_cnt_hjs, fe_cnt_hjs),...
'color', 'white');
figure
surf(surf_a, surf_a, error_surface_tdoa);
title('TDOA Solution: Residual function');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
view(45, 15);
figure
hold on
contourf(surf_a, surf_a, error_surface_tdoa, contour_levels);
plot(actual_target_x, actual_target_y,...
'p',...
'MarkerFaceColor', 'red',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
plot(base_points(N, 1), base_points(N, 2),...
'o',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
centroid = Nav_centroid(base_points);
[ x_best, y_best, rerr, it_cnt ] = Nav_tdoa_nlm_2d_solve(base_lines, centroid(1), centroid(2), actual_target_z,...
max_iterations, precision_threshold, simplex_size);
[ x_best_hjs, y_best_hjs, rerr_hjs, it_cnt_hjs ] = Nav_tdoa_hjs_2d_solve(base_lines, centroid(1), centroid(2), actual_target_z,...
max_iterations, precision_threshold_hjs, hjs_step_size);
plot(x_best, y_best,...
'd',...
'MarkerFaceColor', 'yellow',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
plot(x_best_hjs, y_best_hjs,...
'd',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
title('TDOA Solution: Residual function.');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
legend('Residual function value',...
'Actual target location',...
'Base points',...
sprintf('Estimated target location (NLM), E_{radial} = %.3f m in %d iterations', rerr, it_cnt),...
sprintf('Estimated target location (HJS), E_{radial} = %.3f m in %d iterations', rerr_hjs, it_cnt_hjs));
figure
hold on
grid on
dx_nlm = actual_target_x - x_best;
dy_nlm = actual_target_y - y_best;
dx_hjs = actual_target_x - x_best_hjs;
dy_hjs = actual_target_y - y_best_hjs;
dx = max(dx_nlm, dx_hjs);
dy = max(dy_nlm, dy_hjs);
plot(0, 0,...
'p',...
'MarkerFaceColor', 'red',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 15);
plot(dx_nlm, dy_nlm,...
'd',...
'MarkerFaceColor', 'yellow',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
plot(dx_hjs, dy_hjs,...
'd',...
'MarkerFaceColor', 'green',...
'MarkerEdgeColor', 'blue',...
'MarkerSize', 7);
plot(-dx * 2, -dy * 2, '.w');
plot(dx * 2, dy * 2, '.w');
d_delta_nlm = Nav_dist_3d(actual_target_x, actual_target_y, actual_target_z, x_best, y_best, actual_target_z);
d_delta_hjs = Nav_dist_3d(actual_target_x, actual_target_y, actual_target_z, x_best_hjs, y_best_hjs, actual_target_z);
title('TDOA Solution: Actual vs. Estimated location');
xlabel('X coordinate, m');
ylabel('Y coordinate, m');
legend('Actual target location',...
sprintf('Estimated target location (NLM), abs. error: %.3f m', d_delta_nlm),...
sprintf('Estimated target location (HJS), abs. error: %.3f m', d_delta_hjs));