ExtendedKalmanFilter.java

  1. /*
  2.  * Licensed to the Hipparchus project under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The Hipparchus project 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.  *      https://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.hipparchus.filtering.kalman.extended;

  18. import org.hipparchus.exception.MathRuntimeException;
  19. import org.hipparchus.filtering.kalman.AbstractKalmanFilter;
  20. import org.hipparchus.filtering.kalman.Measurement;
  21. import org.hipparchus.filtering.kalman.ProcessEstimate;
  22. import org.hipparchus.linear.MatrixDecomposer;
  23. import org.hipparchus.linear.RealMatrix;
  24. import org.hipparchus.linear.RealVector;

  25. /**
  26.  * Kalman filter for {@link NonLinearProcess non-linear process}.
  27.  * @param <T> the type of the measurements
  28.  * @since 1.3
  29.  */
  30. public class ExtendedKalmanFilter<T extends Measurement> extends AbstractKalmanFilter<T> {

  31.     /** Process to be estimated. */
  32.     private final NonLinearProcess<T> process;

  33.     /** Simple constructor.
  34.      * @param decomposer decomposer to use for the correction phase
  35.      * @param process non-linear process to estimate
  36.      * @param initialState initial state
  37.      */
  38.     public ExtendedKalmanFilter(final MatrixDecomposer decomposer,
  39.                                 final NonLinearProcess<T> process,
  40.                                 final ProcessEstimate initialState) {
  41.         super(decomposer, initialState);
  42.         this.process = process;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public ProcessEstimate estimationStep(final T measurement)
  47.         throws MathRuntimeException {

  48.         // prediction phase
  49.         final NonLinearEvolution evolution = process.getEvolution(getCorrected().getTime(),
  50.                                                                   getCorrected().getState(),
  51.                                                                   measurement);

  52.         final RealMatrix stm = evolution.getStateTransitionMatrix();
  53.         predict(evolution.getCurrentTime(), evolution.getCurrentState(),
  54.                 stm, evolution.getProcessNoiseMatrix());

  55.         // correction phase
  56.         final RealMatrix h          = evolution.getMeasurementJacobian();
  57.         final RealMatrix s          = computeInnovationCovarianceMatrix(measurement.getCovariance(), h);
  58.         final RealVector innovation = (h == null) ? null : process.getInnovation(measurement, evolution, s);
  59.         correct(measurement, stm, innovation, h, s);

  60.         if (getObserver() != null) {
  61.             getObserver().updatePerformed(this);
  62.         }

  63.         return getCorrected();

  64.     }

  65. }