forked from rodralez/NaveGo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpolate.m
151 lines (115 loc) · 4.02 KB
/
interpolate.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
function ref_i = interpolate(ref, base)
% interpolate: interpolates ref values using base time vector.
%
% Copyright (C) 2014, Rodrigo González, all rights reserved.
%
% This file is part of NaveGo, an open-source MATLAB toolbox for
% simulation of integrated navigation systems.
%
% NaveGo is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License (LGPL)
% version 3 as published by the Free Software Foundation.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this program. If not, see
% <http://www.gnu.org/licenses/>.
%
% Version: 001
% Date: 2014/09/11
% Author: Rodrigo Gonzalez <[email protected]>
% URL: https://github.com/rodralez/navego
%
% Reference:
dt_r = mean(diff(ref.t));
dt_b = mean(diff(base.t));
% Test attitude vectors
fields = nnz(isfield(ref, {'roll','pitch','yaw'}));
MAX = max(size(base.t));
% Allocate variable space
if (isa(base.t,'single'))
if fields == 3
ref_i.roll = single(zeros (MAX,1));
ref_i.pitch = single(zeros (MAX,1));
ref_i.yaw = single(zeros (MAX,1));
ref_i.DCMnb = single(zeros (MAX,9));
end
ref_i.vel = single(zeros (MAX,3));
ref_i.h = single(zeros (MAX,1));
else
if fields == 3
ref_i.roll = zeros (MAX,1);
ref_i.pitch = zeros (MAX,1);
ref_i.yaw = zeros (MAX,1);
ref_i.DCMnb = zeros (MAX,9);
end
ref_i.vel = zeros (MAX,3);
ref_i.h = zeros (MAX,1);
end
ref_i.lat = zeros (MAX,1);
ref_i.lon = zeros (MAX,1);
ref_i.t = base.t;
%%
% Normal interpolation
if (dt_b < dt_r),
gidx = 2;
if fields == 3
for i = 1:MAX
if (base.t(i) > ref.t(gidx))
gidx = gidx + 1;
end
if isempty(gidx)
fprintf('i = %d \n', i);
error ('interpolate: gidx empty');
end
td = ref.t(gidx) - ref.t(gidx-1);
tdin = base.t(i) - ref.t(gidx-1);
ref_i.roll(i) = interpolation (ref.roll(gidx-1), ref.roll(gidx), td, tdin);
ref_i.pitch(i) = interpolation (ref.pitch(gidx-1),ref.pitch(gidx), td, tdin);
ref_i.yaw(i) = interpolation (ref.yaw(gidx-1), ref.yaw(gidx), td, tdin);
dmc_nb = euler2dcm([ref_i.roll(i) ref_i.pitch(i) ref_i.yaw(i)]);
ref_i.DCMnb(i,:) = reshape(dmc_nb,1,9);
end
end
gidx = 2;
for i = 1:MAX
if (base.t(i) > ref.t(gidx))
gidx = gidx + 1;
end
if isempty(gidx)
fprintf('i = %d \n', i);
error ('interpolate: gidx empty');
end
td = ref.t(gidx) - ref.t(gidx-1);
tdin = base.t(i) - ref.t(gidx-1);
ref_i.vel(i,:) = interpolation (ref.vel(gidx-1,:),ref.vel(gidx,:), td, tdin);
ref_i.lat(i) = interpolation (ref.lat(gidx-1), ref.lat(gidx), td, tdin);
ref_i.lon(i) = interpolation (ref.lon(gidx-1), ref.lon(gidx), td, tdin);
ref_i.h(i) = interpolation (ref.h(gidx-1), ref.h(gidx), td, tdin);
end
%%
% Find the ref.t closest value to base.t
elseif (dt_b > dt_r)
for i = 1:MAX
gidx = find( abs(base.t(i) - ref.t) == min (abs (base.t(i) - ref.t)) );
ref_i.t(i) = ref.t (gidx);
if fields == 3
ref_i.roll(i) = ref.roll (gidx);
ref_i.pitch(i) = ref.pitch(gidx);
ref_i.yaw(i) = ref.yaw (gidx);
ref_i.DCMnb(i,:) = ref.DCMnb(gidx,:);
end
ref_i.vel(i,:) = ref.vel(gidx,:);
ref_i.lat(i) = ref.lat(gidx);
ref_i.lon(i) = ref.lon(gidx);
ref_i.h(i) = ref.h (gidx);
end
else
ref_i = ref;
end
ref_i.kn = MAX;
end