LinearKalmanFilter.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.linear;

  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 LinearProcess linear process}.
  27.  * @param <T> the type of the measurements
  28.  * @since 1.3
  29.  */
  30. public class LinearKalmanFilter<T extends Measurement> extends AbstractKalmanFilter<T> {

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

  33.     /** Simple constructor.
  34.      * @param decomposer decomposer to use for the correction phase
  35.      * @param process linear process to estimate
  36.      * @param initialState initial state
  37.      */
  38.     public LinearKalmanFilter(final MatrixDecomposer decomposer,
  39.                               final LinearProcess<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.         final LinearEvolution evolution = process.getEvolution(measurement);

  49.         // prediction phase
  50.         final RealMatrix      a         = evolution.getStateTransitionMatrix();
  51.         final RealMatrix      b         = evolution.getControlMatrix();
  52.         final RealVector      u         = (b == null) ? null : evolution.getCommand();
  53.         final RealMatrix      q         = evolution.getProcessNoiseMatrix();

  54.         RealVector predXk = a.operate(getCorrected().getState());
  55.         if (b != null) {
  56.             predXk = predXk.add(b.operate(u));
  57.         }

  58.         predict(measurement.getTime(), predXk, a, q);

  59.         // correction phase
  60.         final RealMatrix h          = evolution.getMeasurementJacobian();
  61.         final RealMatrix s          = computeInnovationCovarianceMatrix(measurement.getCovariance(), h);
  62.         final RealVector innovation = (h == null) ? null : measurement.getValue().subtract(h.operate(predXk));
  63.         correct(measurement, a, innovation, h, s);

  64.         if (getObserver() != null) {
  65.             getObserver().updatePerformed(this);
  66.         }

  67.         return getCorrected();

  68.     }

  69. }