File size: 9,848 Bytes
5ab75a9 | 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 | function [ri,vi,na,ra,va,nb,rb,vb] = interpState(T,eph,NoWeighting,ExtrapPars)
% interpState - Interpolate a position/velocity state from an ephemeris.
%
% Syntax: [ri, vi] = interpState(T, eph);
% [ri, vi] = interpState(T, eph, NoWeighting);
% [ri, vi] = interpState(T, eph, NoWeighting, ExtrapPars);
% [ri, vi, na, ra, va, nb, rb, vb] = interpState(__);
%
% =========================================================================
%
% Copyright (c) 2026 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description:
%
% Interpolate a position/velocity state from an ephemeris table at the
% single scalar time T.
%
% By default, for points not near the edges of the ephemeris span,
% this function performs "weighted" Lagrange interpolation.
% Specifically, this means that it provides a weighted average of two
% interpolations:
% "a" uses the 5 points centered on the nearest eph. time, and
% "b" uses the 5 points centered on the 2nd nearest eph. time.
% Typically these two eph. times "bracket" the interpolated time,
% satisfying t(na) <= T <= t(nb).
% The default position (or velocity) interpolant is a weighted sum
% ri = (1-w)*ra + w*rb with 0 <= w <= 1 (similarly for vi)
% with ra denoting the 5-pt Lagrange interpolant centered on point a,
% and rb denoting the 5-pt Lagrange interpolant centered on point b.
%
% This weighting algorithm provides continuous interpolant curves
% r(T) and v(T). If no weighting is used, then the interpolant curves
% have discontinuities near the mid-points of the eph. intervals, due
% to the 5-pt set of points used in the Lagrange interpolation
% changing discretely. The differences between the a and b
% interpolants also provide an indication of the interpolation
% uncertainty.
%
% This function also (optionally) allows state extrapolation, using
% the approximation of constant velocity, rectilinear motion beyond
% the eph. bounds. By default, this capability is not allowed, and is
% not recommended except for short duration extrapolations.
%
% =========================================================================
%
% Input:
%
% T - Scalar time for the interpolation
%
% eph - Ephemeris structure with fields:
% .T - Ephemeris times [1xN]
% .X - State vectors [6xN]
% .N - Number of points
% .SecPerTimeUnit - Seconds per time
% unit (optional,
% default = 86400)
%
% NoWeighting - Flag to suppress weighting algorithm
% (optional, default = false)
%
% ExtrapPars - Extrapolation parameters
% (optional, default = 0, no extrapolation)
%
% =========================================================================
%
% Output:
%
% (ri,vi) - Weighted position/velocity state interpolants for
% time T, each [1x3]
%
% (na,nb) - Indices for the bracketing ephemeris points a and b.
% Point a corresponds to the closest ephemeris time to
% T. If T exactly coincides with an ephemeris point,
% T = t(na), then nb = na.
%
% (ra,va) - Interpolated position/velocity state at time T,
% calculated using Lagrange interpolation with the
% 5-pt set {na-2 : na+2}, each [1x3]
%
% (rb,vb) - Interpolated position/velocity state at time T,
% calculated using Lagrange interpolation with the
% 5-pt set {nb-2 : nb+2}, each [1x3]
%
% =========================================================================
%
% Initial version: Feb 2024; Latest update: Apr 2026
%
% ----------------- BEGIN CODE -----------------
% Default is to use weighted interpolation for smooth interpolant curves
Nargin = nargin;
if Nargin < 3; NoWeighting = []; end
if isempty(NoWeighting); NoWeighting = false; end
% Default state extrapolation parameters
% ExtrapPars - Allows state extrapolation outside of eph. time bounds:
% ExtrapPars <= 0 => No extrapolation allowed; attempting
% to do so results in an error
% (default).
% ExtrapPars = N > 0 => Extrapolation allowed within N
% timesteps of ephemeris endpoints with
% a warning; attempting longer
% extrapolations results in an error.
% ExtrapPars = [M, N] => Extrapolation allowed within M
% timesteps of allowed with no warning;
% extrapolation within M to N timesteps
% performed with a warning;
% extrapolation beyond N timesteps
% results in an error.
% Notes: Setting N = [Inf Inf] allows arbitrarily long extrapolations
% with no warnings or errors.
if Nargin < 4; ExtrapPars = []; end
if isempty(ExtrapPars); ExtrapPars = 0; end
if all(ExtrapPars <= 0)
ExtrapolationAllowed = false;
else
ExtrapolationAllowed = true;
Nex = numel(ExtrapPars);
if Nex == 1
% Issue warnings for any extrapolations
ExtrapPars = [0 ExtrapPars];
elseif Nex ~= 2
error('Incorrect ExtrapPars parameter dimensions');
% elseif ExtrapPars(1) >= ExtrapPars(2)
% warning('ExtrapPars(1) >= ExtrapPars(2) is not a sensible way to use the extrapolation mode');
end
ExtrapPars = round(ExtrapPars);
end
% Perform extrapolation, if required
if ExtrapolationAllowed
if T < eph.T(1)
% Early ephemeris extrapolation
Extrapolating = true;
n0 = 1;
dTextrap = eph.T(1)-T;
dTstep = eph.T(2)-eph.T(1);
elseif T > eph.T(eph.N)
% Late ephemeris extrapolation
Extrapolating = true;
n0 = eph.N;
dTextrap = T-eph.T(eph.N);
dTstep = eph.T(eph.N)-eph.T(eph.N-1);
else
% No extrapolation required
Extrapolating = false;
end
if Extrapolating
% Issue extrapolation error or warning
if dTextrap > ExtrapPars(2)*dTstep
error(['Attempting extrapolation exceeding maximum allowed ' num2str(ExtrapPars(2)) ' timesteps']);
elseif dTextrap > ExtrapPars(1)*dTstep
warning(['Performing extrapolation exceeding ' num2str(ExtrapPars(1)) ' timesteps']);
end
% Get eph. time units
if ~isfield(eph,'SecPerTimeUnit') || isempty(eph.SecPerTimeUnit)
SecPerTimeUnit = 86400; % Assume time units of days
else
SecPerTimeUnit = eph.SecPerTimeUnit;
end
% Assume rectilinear, force-free motion for the extrapolation
r0 = eph.X(1:3,n0)'; % Initial position
vi = eph.X(4:6,n0)'; % Constant velocity for force-free motion
ri = r0 + vi*(T-eph.T(n0))*SecPerTimeUnit;
% Remaining output parameters
na = n0; nb = n0; ra = ri; rb = ri; va = vi; vb = vi;
return;
end
end
% Find best five bracketing eph points for interpolation
[~,na] = min(abs(T-eph.T));
n1 = na-2;
n2 = na+2;
if (n1 < 1)
n1 = 1; n2 = 5;
elseif (n2 > eph.N)
n1 = eph.N-4; n2 = eph.N;
end
% Interpolate state (but not covariance)
i = n1:n2;
[ra,va] = StateCovInterp(T, eph.T(i), eph.X(1:3,i)', eph.X(4:6,i)');
% If appropriate, interpolate states with the alternate five bracketing
% points, and create weighted average states
if NoWeighting || (T == eph.T(na))
nb = na;
rb = ra; vb = va;
ri = ra; vi = va;
else
% If not at eph point, generate the alternate five bracketing points
if T > eph.T(na)
nb = na+1;
else
nb = na-1;
end
k1 = nb-2;
k2 = nb+2;
if (k1 < 1)
k1 = 1; k2 = 5;
elseif (k2 > eph.N)
k1 = eph.N-4; k2 = eph.N;
end
if (k1 == n1)
% The "alternate" bracketing points are same as the original five
nb = na;
rb = ra; vb = va;
ri = ra; vi = va;
else
% Alternate bracketing points are different than the originals,
% so perform the interpolation
i = k1:k2;
[rb,vb] = StateCovInterp(T, eph.T(i), eph.X(1:3,i)', eph.X(4:6,i)');
% Weight the two alternate sets of states by time differences
w = (T-eph.T(na)) / (eph.T(nb)-eph.T(na));
if (w <= 0) || (w >= 1)
error('Invalid weight');
end
omw = 1-w;
ri = omw*ra+w*rb; vi = omw*va+w*vb;
end
end
return
end
% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% D. Hall | 2024-Feb-26 | Initial version.
% J. Halpin | 2026-Apr-22 | Added header and footer
% =========================================================================
%
% Copyright (c) 2026 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% ========================================================================= |