DynamicOutlierFilter.java

  1. /* Copyright 2002-2020 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.ObservedMeasurement;

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

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

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

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

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public void modify(final EstimatedMeasurement<T> estimated) {

  60.         // Do not apply the filter if current iteration/measurement is lower than
  61.         // warmup attribute or if the attribute sigma has not been initialized yet
  62.         if ((estimated.getIteration() > getWarmup()) && (sigma != null)) {

  63.             final double[] observed    = estimated.getObservedMeasurement().getObservedValue();
  64.             final double[] theoretical = estimated.getEstimatedValue();

  65.             // Check that the dimension of sigma array is consistent with the measurement
  66.             if (observed.length != sigma.length) {
  67.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  68.                                                        sigma.length, getSigma().length);
  69.             }

  70.             // Check if observed value is not too far from estimation
  71.             for (int i = 0; i < observed.length; ++i) {
  72.                 if (FastMath.abs(observed[i] - theoretical[i]) > getMaxSigma() * sigma[i]) {
  73.                     // observed value is too far, reject measurement
  74.                     estimated.setStatus(EstimatedMeasurement.Status.REJECTED);
  75.                 }
  76.             }
  77.         }

  78.     }
  79. }