81 static constexpr double MinModuloPeriod = 2.0 * std::numeric_limits<double>::min();
93 switch(
config.extrapolation_order) {
101 if(!std::isfinite(
config.lead_cycles) ||
config.lead_cycles < 0.0 ||
103 !std::isfinite(
config.modulo_period) ||
config.modulo_period < 0.0 ||
113 if(!
config.use_explicit_gains) {
114 if(!std::isfinite(
config.pole_radius) ||
115 config.pole_radius <= 0.0 ||
119 const double lam =
config.pole_radius;
120 const double om = 1.0 - lam;
126 a = 1.0 - lam * lam * lam;
127 b = 1.5 * om * om * (1.0 + lam);
131 if(!std::isfinite(
config.alpha) || !std::isfinite(
config.beta) ||
132 !std::isfinite(
config.gamma)) {
143 constexpr double kGainMargin = 1.0e-12;
144 constexpr double kGammaFloor = 1.0e-15;
145 constexpr double kRelMargin = 1.0e-9;
146 if(!(a > kGainMargin && a < 2.0 - kGainMargin) ||
147 !(b > kGainMargin) ||
148 !(4.0 - 2.0 * a - b > kGainMargin)) {
156 if(!(g > kGammaFloor)) {
159 const double lhs = a * b;
160 const double rhs = 0.5 * (2.0 - a) * g;
161 if(!std::isfinite(lhs) || !std::isfinite(rhs) ||
162 !(lhs > rhs * (1.0 + kRelMargin)) ||
163 !(lhs - rhs > kGammaFloor)) {
182 double alpha()
const {
return alpha_; }
183 double beta()
const {
return beta_; }
184 double gamma()
const {
return gamma_; }
192 unwrapped_raw_ = 0.0;
195 modulo_anchored_ =
false;
204 if(!std::isfinite(position) || !std::isfinite(velocity) ||
205 !std::isfinite(acceleration)) {
208 const double acc_clamped =
215 next_internal.
primed =
true;
218 const rt::ErrorCode ext = compute_extrapolated(next_internal, next_output);
222 internal_ = next_internal;
223 output_ = next_output;
224 unwrapped_raw_ = position;
227 modulo_anchored_ =
false;
239 if(!std::isfinite(new_position)) {
242 if(!internal_.primed) {
243 return prime(new_position, 0.0, 0.0);
246 next_internal.
position = new_position;
249 const rt::ErrorCode ext = compute_extrapolated(next_internal, next_output);
253 internal_ = next_internal;
254 output_ = next_output;
255 unwrapped_raw_ = new_position;
258 modulo_anchored_ =
false;
270 if(!std::isfinite(measured_position)) {
274 if(!internal_.primed) {
275 std::int64_t init_turns = 0;
276 double init_phase = 0.0;
277 bool init_anchored =
false;
278 if(config_.modulo_period > 0.0) {
280 measured_position, config_.modulo_period, init_turns, init_phase);
284 init_anchored =
true;
287 first_internal.
position = measured_position;
291 first_internal.
primed =
true;
293 const rt::ErrorCode ext = compute_extrapolated(first_internal, first_output);
297 internal_ = first_internal;
298 output_ = first_output;
299 unwrapped_raw_ = measured_position;
300 modulo_turns_ = init_turns;
301 modulo_phase_ = init_phase;
302 modulo_anchored_ = init_anchored;
308 double next_unwrapped = measured_position;
309 double raw_step = 0.0;
310 std::int64_t next_turns = modulo_turns_;
311 double next_phase = modulo_phase_;
312 bool next_anchored = modulo_anchored_;
314 measured_position, next_unwrapped, raw_step, next_turns, next_phase, next_anchored);
320 next_internal.
primed =
true;
326 config_.modulo_period > 0.0 ? next_unwrapped : measured_position;
327 const double v_new = raw_step;
328 const double a_new = sample_count_ >= 2 ? (v_new - internal_.velocity) : 0.0;
329 if(!std::isfinite(p_new) || !std::isfinite(v_new) || !std::isfinite(a_new)) {
337 const double p_pred = internal_.position + internal_.velocity +
338 0.5 * internal_.acceleration;
339 const double v_pred = internal_.velocity + internal_.acceleration;
340 const double a_pred = internal_.acceleration;
341 if(!std::isfinite(p_pred) || !std::isfinite(v_pred) || !std::isfinite(a_pred)) {
344 const double residual = next_unwrapped - p_pred;
345 if(!std::isfinite(residual)) {
348 const double p_new = p_pred + alpha_ * residual;
349 const double v_new = v_pred + beta_ * residual;
352 if(!std::isfinite(p_new) || !std::isfinite(v_new) || !std::isfinite(a_new)) {
362 const rt::ErrorCode ext = compute_extrapolated(next_internal, next_output);
367 internal_ = next_internal;
368 output_ = next_output;
369 unwrapped_raw_ = next_unwrapped;
370 modulo_turns_ = next_turns;
371 modulo_phase_ = next_phase;
372 modulo_anchored_ = next_anchored;
373 if(sample_count_ < 2) {
387 static bool sum_ge_half(
double u,
double v,
double half)
389 const double hi = u >= v ? u : v;
390 const double lo = u >= v ? v : u;
391 return hi >= 0.5 * half && lo >= (half - hi);
394 static bool sum_gt_half(
double u,
double v,
double half)
396 const double hi = u >= v ? u : v;
397 const double lo = u >= v ? v : u;
398 return hi >= 0.5 * half && lo > (half - hi);
403 std::int64_t &turns_out,
406 const double turns_mag = std::fabs(value) / period;
410 const double half = 0.5 * period;
411 double rem = std::remainder(value, period);
412 if(!std::isfinite(rem)) {
418 double q = std::round((value / period) - (rem / period));
419 if(!std::isfinite(q)) {
432 turns_out =
static_cast<std::int64_t
>(q);
438 double &unwrapped_next,
440 std::int64_t &next_turns,
442 bool &next_anchored)
const
444 if(config_.modulo_period > 0.0) {
445 const double period = config_.modulo_period;
446 std::int64_t cur_turns = modulo_turns_;
447 double cur_phase = modulo_phase_;
448 if(!modulo_anchored_) {
450 split_to_turns_phase(unwrapped_raw_, period, cur_turns, cur_phase);
458 const double half = 0.5 * period;
462 double z_rem = std::remainder(measured, period);
463 if(!std::isfinite(z_rem)) {
476 std::int64_t cand_turns = cur_turns;
477 if(z_rem >= 0.0 && cur_phase < 0.0 && sum_ge_half(z_rem, -cur_phase, half)) {
478 diff = -((half - z_rem) + (half + cur_phase));
480 }
else if(cur_phase > 0.0 && z_rem < 0.0 && sum_gt_half(cur_phase, -z_rem, half)) {
481 diff = (half - cur_phase) + (half + z_rem);
484 diff = z_rem - cur_phase;
489 const double cand_pos =
static_cast<double>(cand_turns) * period + z_rem;
490 if(!std::isfinite(diff) || !std::isfinite(cand_pos)) {
493 const double mag_turns = std::fabs(cand_pos) / period;
497 unwrapped_next = cand_pos;
499 next_turns = cand_turns;
501 next_anchored =
true;
505 const double diff = measured - unwrapped_raw_;
506 if(!std::isfinite(diff)) {
509 unwrapped_next = measured;
513 next_anchored =
false;
517 rt::ErrorCode compute_extrapolated(
const MasterFilterState &src,
518 MasterFilterState &dst)
const
521 const double lead = config_.lead_cycles;
526 dst.position = src.position + src.velocity * lead;
528 dst.position = src.position + src.velocity * lead +
529 0.5 * src.acceleration * lead * lead;
530 dst.velocity = src.velocity + src.acceleration * lead;
532 if(!std::isfinite(dst.position) || !std::isfinite(dst.velocity) ||
533 !std::isfinite(dst.acceleration)) {
539 MasterFilterConfig config_{};
543 double unwrapped_raw_ = 0.0;
544 std::int64_t modulo_turns_ = 0;
545 double modulo_phase_ = 0.0;
546 bool modulo_anchored_ =
false;
547 MasterFilterState internal_{};
548 MasterFilterState output_{};
549 std::uint8_t sample_count_ = 0;
550 bool configured_ =
false;