DynamicOutlierFilter.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.estimation.measurements.modifiers;

  18. import org.hipparchus.exception.LocalizedCoreFormats;
  19. import org.hipparchus.exception.MathIllegalArgumentException;
  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.estimation.measurements.EstimatedMeasurement;
  22. import org.orekit.estimation.measurements.EstimatedMeasurementBase;
  23. import org.orekit.estimation.measurements.ObservedMeasurement;

  24. /** Modifier that sets estimated measurement weight to 0 if residual is too far from expected domain.
  25.  * The "dynamic" aspect comes from the fact that the value of sigma can be changed on demand.
  26.  * This is mainly used when searching for outliers in Kalman filters' prediction phase.
  27.  * The value of sigma is then set to the square root of the diagonal of the matrix (H.Ppred.Ht+R)
  28.  * Note that in the case of the Kalman filter we use the "iteration" word to represent the number of
  29.  * measurements processed by the filter so far.
  30.  * @param <T> the type of the measurement
  31.  * @author Luc Maisonobe
  32.  * @since 9.2
  33.  */
  34. public class DynamicOutlierFilter<T extends ObservedMeasurement<T>> extends OutlierFilter<T> {
  35.     /** Current value of sigma. */
  36.     private double[] sigma;

  37.     /** Simple constructor.
  38.      * @param warmup number of iterations before with filter is not applied
  39.      * @param maxSigma detection limit for outlier
  40.      */
  41.     public DynamicOutlierFilter(final int warmup,
  42.                                 final double maxSigma) {
  43.         super(warmup, maxSigma);
  44.         this.sigma = null;
  45.     }

  46.     /** Get the current value of sigma.
  47.      * @return The current value of sigma
  48.      */
  49.     public double[] getSigma() {
  50.         return sigma == null ? null : sigma.clone();
  51.     }

  52.     /** Set the current value of sigma.
  53.      * @param sigma The value of sigma to set
  54.      */
  55.     public void setSigma(final double[] sigma) {
  56.         this.sigma = sigma == null ? null : sigma.clone();
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public void modify(final EstimatedMeasurement<T> estimated) {
  61.         modifyWithoutDerivatives(estimated);
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public void modifyWithoutDerivatives(final EstimatedMeasurementBase<T> estimated) {
  66.         // Do not apply the filter if current iteration/measurement is lower than
  67.         // warmup attribute or if the attribute sigma has not been initialized yet
  68.         if (estimated.getIteration() > getWarmup() && sigma != null) {

  69.             final double[] observed    = estimated.getObservedMeasurement().getObservedValue();
  70.             final double[] theoretical = estimated.getEstimatedValue();

  71.             // Check that the dimension of sigma array is consistent with the measurement
  72.             if (observed.length != sigma.length) {
  73.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  74.                         sigma.length, getSigma().length);
  75.             }

  76.             // Check if observed value is not too far from estimation
  77.             for (int i = 0; i < observed.length; ++i) {
  78.                 if (FastMath.abs(observed[i] - theoretical[i]) > getMaxSigma() * sigma[i]) {
  79.                     // observed value is too far, reject measurement
  80.                     estimated.setStatus(EstimatedMeasurement.Status.REJECTED);
  81.                 }
  82.             }
  83.         }
  84.     }

  85. }