View Javadoc
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  
18  package org.hipparchus.filtering.kalman.linear;
19  
20  import org.hipparchus.exception.MathRuntimeException;
21  import org.hipparchus.filtering.kalman.AbstractKalmanFilter;
22  import org.hipparchus.filtering.kalman.Measurement;
23  import org.hipparchus.filtering.kalman.ProcessEstimate;
24  import org.hipparchus.linear.MatrixDecomposer;
25  import org.hipparchus.linear.RealMatrix;
26  import org.hipparchus.linear.RealVector;
27  
28  /**
29   * Kalman filter for {@link LinearProcess linear process}.
30   * @param <T> the type of the measurements
31   * @since 1.3
32   */
33  public class LinearKalmanFilter<T extends Measurement> extends AbstractKalmanFilter<T> {
34  
35      /** Process to be estimated. */
36      private final LinearProcess<T> process;
37  
38      /** Simple constructor.
39       * @param decomposer decomposer to use for the correction phase
40       * @param process linear process to estimate
41       * @param initialState initial state
42       */
43      public LinearKalmanFilter(final MatrixDecomposer decomposer,
44                                final LinearProcess<T> process,
45                                final ProcessEstimate initialState) {
46          super(decomposer, initialState);
47          this.process = process;
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public ProcessEstimate estimationStep(final T measurement)
53          throws MathRuntimeException {
54  
55          final LinearEvolution evolution = process.getEvolution(measurement);
56  
57          // prediction phase
58          final RealMatrix      a         = evolution.getStateTransitionMatrix();
59          final RealMatrix      b         = evolution.getControlMatrix();
60          final RealVector      u         = (b == null) ? null : evolution.getCommand();
61          final RealMatrix      q         = evolution.getProcessNoiseMatrix();
62  
63          RealVector predXk = a.operate(getCorrected().getState());
64          if (b != null) {
65              predXk = predXk.add(b.operate(u));
66          }
67  
68          predict(measurement.getTime(), predXk, a, q);
69  
70          // correction phase
71          final RealMatrix h          = evolution.getMeasurementJacobian();
72          final RealMatrix s          = computeInnovationCovarianceMatrix(measurement.getCovariance(), h);
73          final RealVector innovation = (h == null) ? null : measurement.getValue().subtract(h.operate(predXk));
74          correct(measurement, a, innovation, h, s);
75          return getCorrected();
76  
77      }
78  
79  }