forked from JD-Kidd/7_DataAnalysisSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImRunningOutOfNames.m
More file actions
480 lines (389 loc) · 16.1 KB
/
ImRunningOutOfNames.m
File metadata and controls
480 lines (389 loc) · 16.1 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
function [ Truth_Data, dt, theta, n_vec, offset_vec ] = ...
ImRunningOutOfNames( VANTAGE_Data, Truth_Data, should_debug )
%%% Inputs:
%%% - VANTAGE_Data: a struct with the following fields as they pertain
%%% to the VANTAGE measurements
%%% > t: Time vector
%%% > x: X position vector
%%% > y: Y position vector
%%% > z: Z position vector
%%% > d: A length 2 vector, first element: minimum distance on
%%% which ToF is trusted, second element: maximum distance on
%%% which ToF is trusted
%%% - Truth_Data: a struct with the following fields as they pertain
%%% to the Truth measurements
%%% > t: Time vector
%%% > x: X position vector
%%% > y: Y position vector
%%% > z: Z position vector
%%%
%%% Outputs:
%%% - dt: Truth data time shift
if nargin < 3
should_debug = 0;
end
%{
First we have to extract the VANTAGE measurement values we trust
%}
VANTAGE_Data_Distance = sqrt( VANTAGE_Data.x.^2 + VANTAGE_Data.y.^2 + ...
VANTAGE_Data.z.^2 );
VANTAGE_Index_Start = find( VANTAGE_Data_Distance >= VANTAGE_Data.d(1), ...
1, 'first' );
VANTAGE_Index_End = find( VANTAGE_Data_Distance <= VANTAGE_Data.d(2), ...
1, 'last' );
VANTAGE_Index = VANTAGE_Index_Start : VANTAGE_Index_End;
%{
Now we will generate a 3D linear least squares fit (f) of the trusted
VANTAGE 3D data ([x,y,z]):
f_x = m_x * t + b_x
[ m_x; b_x ] = inv( A' * A ) * ( A' * x )
A = [ t, 1 ];
same process for y and z
f = [ f_x; f_y; f_z ]
%}
VANTAGE_Fit.t = VANTAGE_Data.t( VANTAGE_Index );
A = [ VANTAGE_Fit.t, ones( size( VANTAGE_Fit.t ) ) ];
intermediary = ( A' * A ) \ ( A' * VANTAGE_Data.x( VANTAGE_Index ) );
m = intermediary(1);
b = intermediary(2);
VANTAGE_Fit.f_x = @(t) m .* t + b;
intermediary = ( A' * A ) \ ( A' * VANTAGE_Data.y( VANTAGE_Index ) );
m = intermediary(1);
b = intermediary(2);
VANTAGE_Fit.f_y = @(t) m .* t + b;
intermediary = ( A' * A ) \ ( A' * VANTAGE_Data.z( VANTAGE_Index ) );
m = intermediary(1);
b = intermediary(2);
VANTAGE_Fit.f_z = @(t) m .* t + b;
if should_debug
figure;
scatter3( VANTAGE_Data.x( VANTAGE_Index ), ...
VANTAGE_Data.y( VANTAGE_Index ), ...
VANTAGE_Data.z( VANTAGE_Index ), 2, ...
VANTAGE_Data.t( VANTAGE_Index ) )
hold on
plot3( VANTAGE_Fit.f_x( VANTAGE_Data.t( VANTAGE_Index ) ), ...
VANTAGE_Fit.f_y( VANTAGE_Data.t( VANTAGE_Index ) ), ...
VANTAGE_Fit.f_z( VANTAGE_Data.t( VANTAGE_Index ) ), ...
'-r', 'LineWidth', 2 )
title( 'VANTAGE Data & Fit' )
end
%{
Now we need to do the same for the truth data, but only over the range
which corresponds to the positions where the truth data is accurate
%}
Truth_Data_Distance = sqrt( Truth_Data.x.^2 + Truth_Data.y.^2 + ...
Truth_Data.z.^2 );
Truth_Index_Start = find( Truth_Data_Distance >= VANTAGE_Data.d(1), 1, 'first' );
Truth_Index_End = find( Truth_Data_Distance <= VANTAGE_Data.d(2), 1, 'last' );
Truth_Index = Truth_Index_Start:Truth_Index_End;
%{
Now that we know what values of the Truth data line up with the trusred
VANTAGE data, we can fit them
%}
Truth_Fit.t = Truth_Data.t( Truth_Index );
A = [ Truth_Fit.t, ones( size( Truth_Fit.t ) ) ];
intermediary = ( A' * A ) \ ( A' * Truth_Data.x( Truth_Index ) );
m = intermediary(1);
b = intermediary(2);
Truth_Fit.f_x = @(t) m .* t + b;
intermediary = ( A' * A ) \ ( A' * Truth_Data.y( Truth_Index ) );
m = intermediary(1);
b = intermediary(2);
Truth_Fit.f_y = @(t) m .* t + b;
intermediary = ( A' * A ) \ ( A' * Truth_Data.z( Truth_Index ) );
m = intermediary(1);
b = intermediary(2);
Truth_Fit.f_z = @(t) m .* t + b;
if should_debug
figure;
scatter3( Truth_Data.x( Truth_Index ), ...
Truth_Data.y( Truth_Index ), ...
Truth_Data.z( Truth_Index ), 2, ...
Truth_Data.t( Truth_Index ) )
hold on
plot3( Truth_Fit.f_x( Truth_Data.t( Truth_Index ) ), ...
Truth_Fit.f_y( Truth_Data.t( Truth_Index ) ), ...
Truth_Fit.f_z( Truth_Data.t( Truth_Index ) ), ...
'-r', 'LineWidth', 2 )
title( 'Truth Data & Fit' )
end
%{
Now we can determine the best fit lines to the distance line generated by
X_3D_Fit.vec, but this time we will be fitting function for time
%}
Truth_Fit.f_dist_3D = @(t) sqrt( Truth_Fit.f_x(t).^2 + ...
Truth_Fit.f_y(t).^2 + Truth_Fit.f_z(t).^2 );
A = [ Truth_Fit.f_dist_3D( Truth_Fit.t ), ones( size( Truth_Fit.t ) ) ];
intermediary = ( A' * A ) \ ( A' * Truth_Fit.t );
m = intermediary(1);
b = intermediary(2);
Truth_Fit.f_t = @(d) m .* d + b;
VANTAGE_Fit.f_dist_3D = @(t) sqrt( VANTAGE_Fit.f_x(t).^2 + ...
VANTAGE_Fit.f_y(t).^2 + VANTAGE_Fit.f_z(t).^2 );
A = [ VANTAGE_Fit.f_dist_3D( VANTAGE_Fit.t ), ones( size( VANTAGE_Fit.t ) ) ];
intermediary = ( A' * A ) \ ( A' * VANTAGE_Fit.t );
m = intermediary(1);
b = intermediary(2);
VANTAGE_Fit.f_t = @(d) m .* d + b;
if should_debug
figure;
plot( VANTAGE_Data.t( VANTAGE_Index ), ...
VANTAGE_Data_Distance( VANTAGE_Index ), 'ok' )
hold on
plot( VANTAGE_Data.t( VANTAGE_Index ), ...
VANTAGE_Fit.f_dist_3D( VANTAGE_Data.t( VANTAGE_Index ) ), ...
'-r', 'LineWidth', 2 )
plot( VANTAGE_Fit.f_t( VANTAGE_Fit.f_dist_3D( VANTAGE_Data.t( VANTAGE_Index ) ) ), ...
VANTAGE_Fit.f_dist_3D( VANTAGE_Data.t( VANTAGE_Index ) ), ...
'-b', 'LineWidth', 2 )
title( 'VANTAGE Distance & Dist Fit & Time Fit' )
figure;
plot( Truth_Data.t( Truth_Index ), ...
Truth_Data_Distance( Truth_Index ), 'ok' )
hold on
plot( Truth_Data.t( Truth_Index ), ...
Truth_Fit.f_dist_3D( Truth_Data.t( Truth_Index ) ), ...
'-r', 'LineWidth', 2 )
plot( Truth_Fit.f_t( Truth_Fit.f_dist_3D( Truth_Data.t( Truth_Index ) ) ), ...
Truth_Fit.f_dist_3D( Truth_Data.t( Truth_Index ) ), ...
'-b', 'LineWidth', 2 )
title( 'Truth Distance & Dist Fit & Time Fit' )
end
%{
Now we will numerically minimize the function:
( VANTAGE_Fit.f_t( VANTAGE_Fit.t ) - Truth_Fit.f_t( Truth_Fit.t + dt ) ).^2
by varying dt to adequate precision
%}
VANTAGE_t = linspace( min( VANTAGE_Fit.t ), max( VANTAGE_Fit.t ), 1000 )';
% note we need to make the distance traveled the same for both time vectors
Truth_Start_Distance = min( VANTAGE_Fit.f_dist_3D( VANTAGE_t ) );
Truth_Start_Time = Truth_Fit.f_t( Truth_Start_Distance );
Truth_Stop_Distance = max( VANTAGE_Fit.f_dist_3D( VANTAGE_t ) );
Truth_Stop_Time = Truth_Fit.f_t( Truth_Stop_Distance );
Truth_t = linspace( Truth_Start_Time, Truth_Stop_Time, 1000 )';
% initializing things for the while loop
dt_error_vec = Truth_Fit.f_t( Truth_Fit.f_dist_3D( Truth_t ) ) - ...
VANTAGE_Fit.f_t( VANTAGE_Fit.f_dist_3D( VANTAGE_t ) );
dt_error_mean = mean( dt_error_vec );
dt_error_squares = sum( dt_error_vec.^2 );
dt_sign = sign( dt_error_mean );
dt_change = dt_sign * 10^( round( log10( abs( dt_error_mean ) ) + 1 ) );
precision_current = abs( dt_change );
precision_required = 1e-12; % something sufficiently small chosen
dt = 0;
% debug stuff
if should_debug
figure;
plot( VANTAGE_Fit.f_t( VANTAGE_Fit.f_dist_3D( VANTAGE_t ) ), ...
VANTAGE_Fit.f_dist_3D( VANTAGE_t ), ...
'-k', 'LineWidth', 2 )
hold on
plot( Truth_Fit.f_t( Truth_Fit.f_dist_3D( Truth_t ) ), ...
Truth_Fit.f_dist_3D( Truth_t ), ...
'-r', 'LineWidth', 2 )
title( 'VANTAGE Time Fit & Truth Time Fit' )
prev_squares = dt_error_squares;
iteration = 1;
T_t{iteration} = Truth_Fit.f_t( Truth_Fit.f_dist_3D( Truth_t ) );
T_d{iteration} = Truth_Fit.f_dist_3D( Truth_t );
end
while precision_current > precision_required
% test out the next point in the same direction as last time
dt_error_vec_new = ...
Truth_Fit.f_t( Truth_Fit.f_dist_3D( Truth_t ) ) - ...
VANTAGE_Fit.f_t( VANTAGE_Fit.f_dist_3D( VANTAGE_t ) ) + ...
dt + dt_change;
dt_error_squares_new = sum( dt_error_vec_new.^2 );
% if it is an improvement, use it
if dt_error_squares_new < dt_error_squares
dt = dt + dt_change;
dt_error_squares = dt_error_squares_new;
else % mignt be time to switch directions
dt_error_vec_new = ...
Truth_Fit.f_t( Truth_Fit.f_dist_3D( Truth_t ) ) - ...
VANTAGE_Fit.f_t( VANTAGE_Fit.f_dist_3D( VANTAGE_t ) ) + ...
dt - dt_change;
dt_error_squares_new = sum( dt_error_vec_new.^2 );
% if it is an improvement, use it
if dt_error_squares_new < dt_error_squares
dt_change = -dt_change;
dt = dt + dt_change;
dt_error_squares = dt_error_squares_new;
else % it is at a local minimum for this stepsize, decrease stepsize
dt_change = dt_change / 10;
end
end
% update the precision
precision_current = abs( dt_change );
% degub (lol) stuff
if should_debug
if dt_error_squares < prev_squares
prev_squares = dt_error_squares;
iteration = iteration + 1;
T_t{iteration} = Truth_t + dt;
T_d{iteration} = Truth_Fit.f_dist_3D( Truth_t );
end
end
end
if should_debug
colors = [ 0, 0, 0; jet( length( T_t ) - 1 ) ];
figure;
plot( VANTAGE_t, VANTAGE_Fit.f_dist_3D( VANTAGE_t ), '-ok', ...
'LineWidth', 2 )
hold on
plot( T_t{1}, T_d{1}, '-o', 'color', colors(1,:), 'LineWidth', 2 )
for i = 2 : length( T_t )
plot( T_t{i}, T_d{i}, '-', 'color', colors(i,:), 'LineWidth', 2 )
end
end
% Apply dt correction to Truth timestamp
Truth_Data.t = Truth_Data.t + dt;
% Apply dt correction to Truth fits
Truth_Fit.t = Truth_Fit.t + dt;
Truth_Fit.x = @(t) Truth_Fit.x( t - dt );
Truth_Fit.y = @(t) Truth_Fit.y( t - dt );
Truth_Fit.z = @(t) Truth_Fit.z( t - dt );
%{
Now it is time to rotate the truth data to be aligned with the VANTAGE data
%}
VANTAGE_vector = [ VANTAGE_Fit.f_x( max( VANTAGE_t ) + 1 ), ...
VANTAGE_Fit.f_y( max( VANTAGE_t ) + 1 ), ...
VANTAGE_Fit.f_z( max( VANTAGE_t ) + 1 ) ] - ...
[ VANTAGE_Fit.f_x( min( VANTAGE_t ) ), ...
VANTAGE_Fit.f_y( min( VANTAGE_t ) ), ...
VANTAGE_Fit.f_z( min( VANTAGE_t ) ) ];
Truth_vector = [ Truth_Fit.f_x( min( VANTAGE_t ) + 1 ), ...
Truth_Fit.f_y( min( VANTAGE_t ) + 1 ), ...
Truth_Fit.f_z( min( VANTAGE_t ) + 1 ) ] - ...
[ Truth_Fit.f_x( min( VANTAGE_t ) ), ...
Truth_Fit.f_y( min( VANTAGE_t ) ), ...
Truth_Fit.f_z( min( VANTAGE_t ) ) ];
% angle between VANTAGE_vector and Truth_vector
theta = acos( dot( VANTAGE_vector, Truth_vector ) / ...
( norm( VANTAGE_vector ) * norm( Truth_vector ) ) );
% initialization of the rotation vector in case theta = 0
n_vec = [ 0, 0, 0 ];
if should_debug
figure;
scatter3( VANTAGE_Data.x, VANTAGE_Data.y, VANTAGE_Data.z, 2, ...
VANTAGE_Data.t )
hold on
plot3( VANTAGE_Fit.f_x( VANTAGE_Data.t ), ...
VANTAGE_Fit.f_y( VANTAGE_Data.t ), ...
VANTAGE_Fit.f_z( VANTAGE_Data.t ), ...
'-r', 'LineWidth', 2 )
scatter3( Truth_Data.x, Truth_Data.y, Truth_Data.z, 2, ...
Truth_Data.t )
plot3( Truth_Fit.f_x( Truth_Data.t-dt ), ...
Truth_Fit.f_y( Truth_Data.t-dt ), ...
Truth_Fit.f_z( Truth_Data.t-dt ), ...
'-r', 'LineWidth', 2 )
end
% check to make sure theta is nonzero
if theta
% normal unit vector to VANTAGE_vector and Truth_vector
n_vec = cross( Truth_vector, VANTAGE_vector );
n_vec = n_vec ./ norm( n_vec );
%{
according to Wikipedia
(https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle)
the rotation matrix for a rotation of theta about n_vec is:
%}
R = [ cos(theta) + n_vec(1)^2 * ( 1 - cos(theta) ), ...
n_vec(1) * n_vec(2) * ( 1 - cos(theta) ) - n_vec(3) * sin(theta), ...
n_vec(1) * n_vec(3) * ( 1 - cos(theta) ) + n_vec(2) * sin(theta); ...
n_vec(2) * n_vec(1) * ( 1 - cos(theta) ) + n_vec(3) * sin(theta), ...
cos(theta) + n_vec(2)^2 * ( 1 - cos(theta) ), ...
n_vec(2) * n_vec(3) * ( 1 - cos(theta) ) - n_vec(1) * sin(theta); ...
n_vec(3) * n_vec(1) * ( 1 - cos(theta) ) - n_vec(2) * sin(theta), ...
n_vec(3) * n_vec(2) * ( 1 - cos(theta) ) + n_vec(1) * sin(theta), ...
cos(theta) + n_vec(3)^2 * ( 1 - cos(theta) ) ];
% rotate Truth_Data.[x,y,z]
for i = 1 : length( Truth_Data.t )
vec = [ Truth_Data.x(i); Truth_Data.y(i); Truth_Data.z(i) ];
intermediate = R * vec;
Truth_Data.x(i) = intermediate(1);
Truth_Data.y(i) = intermediate(2);
Truth_Data.z(i) = intermediate(3);
end
% % rotate the best fit line
b_vec = [ Truth_Fit.f_x( 0 ), Truth_Fit.f_y( 0 ), Truth_Fit.f_z( 0 ) ];
Truth_vector = R * Truth_vector';
b_vec = R * b_vec';
Truth_Fit.f_x = @(t) Truth_vector(1) .* ( t - dt ) + b_vec(1);
Truth_Fit.f_y = @(t) Truth_vector(2) .* ( t - dt ) + b_vec(2);
Truth_Fit.f_z = @(t) Truth_vector(3) .* ( t - dt ) + b_vec(3);
end
if should_debug
scatter3( Truth_Data.x, Truth_Data.y, Truth_Data.z, 2, ...
Truth_Data.t )
plot3( Truth_Fit.f_x( Truth_Data.t ), ...
Truth_Fit.f_y( Truth_Data.t ), ...
Truth_Fit.f_z( Truth_Data.t ), ...
'-b', 'LineWidth', 2 )
colormap jet
title( 'Timestamp Corrected Truth Data Rotation: Before & After' )
end
%{
Finally, we are going to change the offset in the truth data such that the
areas of data we trust overlap in 3D space by making the mean point during
that span be identical for VANTAGE data and truth data
Note: VANTAGE_t from earlier is a time vector corresponding to this domain
%}
Truth_Offset_x = mean( VANTAGE_Fit.f_x( VANTAGE_t ) ) - ...
mean( Truth_Fit.f_x( VANTAGE_t ) );
Truth_Offset_y = mean( VANTAGE_Fit.f_y( VANTAGE_t ) ) - ...
mean( Truth_Fit.f_y( VANTAGE_t ) );
Truth_Offset_z = mean( VANTAGE_Fit.f_z( VANTAGE_t ) ) - ...
mean( Truth_Fit.f_z( VANTAGE_t ) );
offset_vec = [ Truth_Offset_x, Truth_Offset_y, Truth_Offset_z ];
if should_debug
figure;
scatter3( VANTAGE_Data.x, VANTAGE_Data.y, VANTAGE_Data.z, 2, ...
VANTAGE_Data.t )
hold on
plot3( VANTAGE_Fit.f_x( VANTAGE_Data.t ), ...
VANTAGE_Fit.f_y( VANTAGE_Data.t ), ...
VANTAGE_Fit.f_z( VANTAGE_Data.t ), ...
'-r', 'LineWidth', 2 )
scatter3( Truth_Data.x, Truth_Data.y, Truth_Data.z, 2, ...
Truth_Data.t )
plot3( Truth_Fit.f_x( Truth_Data.t ), ...
Truth_Fit.f_y( Truth_Data.t ), ...
Truth_Fit.f_z( Truth_Data.t ), ...
'-r', 'LineWidth', 2 )
end
% Applying offset
Truth_Data.x = Truth_Data.x + Truth_Offset_x;
Truth_Data.y = Truth_Data.y + Truth_Offset_y;
Truth_Data.z = Truth_Data.z + Truth_Offset_z;
Truth_Fit.f_x = @(t) Truth_Fit.f_x(t) + Truth_Offset_x;
Truth_Fit.f_y = @(t) Truth_Fit.f_y(t) + Truth_Offset_y;
Truth_Fit.f_z = @(t) Truth_Fit.f_z(t) + Truth_Offset_z;
if should_debug
scatter3( Truth_Data.x, Truth_Data.y, Truth_Data.z, 2, ...
Truth_Data.t )
plot3( Truth_Fit.f_x( Truth_Data.t ), ...
Truth_Fit.f_y( Truth_Data.t ), ...
Truth_Fit.f_z( Truth_Data.t ), ...
'-b', 'LineWidth', 2 )
colormap jet
title( 'Timestamp Corrected, Rotated Truth Data Translation: Before & After' )
figure;
plot( VANTAGE_t, VANTAGE_Fit.f_x( VANTAGE_t ), '-k', 'LineWidth', 2 )
hold on
plot( VANTAGE_t, Truth_Fit.f_x( VANTAGE_t ), '-r', 'LineWidth', 2 )
title( 'X VANTAGE Measurement Best Fit VS Truth Best Fit' )
figure;
plot( VANTAGE_t, VANTAGE_Fit.f_y( VANTAGE_t ), '-k', 'LineWidth', 2 )
hold on
plot( VANTAGE_t, Truth_Fit.f_y( VANTAGE_t ), '-r', 'LineWidth', 2 )
title( 'Y VANTAGE Measurement Best Fit VS Truth Best Fit' )
figure;
plot( VANTAGE_t, VANTAGE_Fit.f_z( VANTAGE_t ), '-k', 'LineWidth', 2 )
hold on
plot( VANTAGE_t, Truth_Fit.f_z( VANTAGE_t ), '-r', 'LineWidth', 2 )
title( 'Z VANTAGE Measurement Best Fit VS Truth Best Fit' )
end
end