Package org.apache.commons.math3.filter
Class KalmanFilter
java.lang.Object
org.apache.commons.math3.filter.KalmanFilter
Implementation of a Kalman filter to estimate the state xk
of a discrete-time controlled process that is governed by the linear
stochastic difference equation:
xk = Axk-1 + Buk-1 + wk-1with a measurement xk that is
zk = Hxk + vk.
The random variables wk and vk represent the process and measurement noise and are assumed to be independent of each other and distributed with normal probability (white noise).
The Kalman filter cycle involves the following steps:
- predict: project the current state estimate ahead in time
- correct: adjust the projected estimate by an actual measurement
The Kalman filter is initialized with a ProcessModel and a
MeasurementModel, which contain the corresponding transformation and
noise covariance matrices. The parameter names used in the respective models
correspond to the following names commonly used in the mathematical
literature:
- A - state transition matrix
- B - control input matrix
- H - measurement matrix
- Q - process noise covariance matrix
- R - measurement noise covariance matrix
- P - error covariance matrix
- Since:
- 3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate RealMatrixThe control matrix, equivalent to B.private RealMatrixThe error covariance matrix, equivalent to P.private RealMatrixThe measurement matrix, equivalent to H.private RealMatrixThe transposed measurement matrix.private final MeasurementModelThe measurement model used by this filter instance.private final ProcessModelThe process model used by this filter instance.private RealVectorThe internal state estimation vector, equivalent to x hat.private RealMatrixThe transition matrix, equivalent to A.private RealMatrixThe transposed transition matrix. -
Constructor Summary
ConstructorsConstructorDescriptionKalmanFilter(ProcessModel process, MeasurementModel measurement) Creates a new Kalman filter with the given process and measurement models. -
Method Summary
Modifier and TypeMethodDescriptionvoidcorrect(double[] z) Correct the current state estimate with an actual measurement.voidCorrect the current state estimate with an actual measurement.double[][]Returns the current error covariance matrix.Returns a copy of the current error covariance matrix.intReturns the dimension of the measurement vector.intReturns the dimension of the state estimation vector.double[]Returns the current state estimation vector.Returns a copy of the current state estimation vector.voidpredict()Predict the internal state estimation one time step ahead.voidpredict(double[] u) Predict the internal state estimation one time step ahead.voidPredict the internal state estimation one time step ahead.
-
Field Details
-
processModel
The process model used by this filter instance. -
measurementModel
The measurement model used by this filter instance. -
transitionMatrix
The transition matrix, equivalent to A. -
transitionMatrixT
The transposed transition matrix. -
controlMatrix
The control matrix, equivalent to B. -
measurementMatrix
The measurement matrix, equivalent to H. -
measurementMatrixT
The transposed measurement matrix. -
stateEstimation
The internal state estimation vector, equivalent to x hat. -
errorCovariance
The error covariance matrix, equivalent to P.
-
-
Constructor Details
-
KalmanFilter
public KalmanFilter(ProcessModel process, MeasurementModel measurement) throws NullArgumentException, NonSquareMatrixException, DimensionMismatchException, MatrixDimensionMismatchException Creates a new Kalman filter with the given process and measurement models.- Parameters:
process- the model defining the underlying process dynamicsmeasurement- the model defining the given measurement characteristics- Throws:
NullArgumentException- if any of the given inputs is null (except for the control matrix)NonSquareMatrixException- if the transition matrix is non squareDimensionMismatchException- if the column dimension of the transition matrix does not match the dimension of the initial state estimation vectorMatrixDimensionMismatchException- if the matrix dimensions do not fit together
-
-
Method Details
-
getStateDimension
public int getStateDimension()Returns the dimension of the state estimation vector.- Returns:
- the state dimension
-
getMeasurementDimension
public int getMeasurementDimension()Returns the dimension of the measurement vector.- Returns:
- the measurement vector dimension
-
getStateEstimation
public double[] getStateEstimation()Returns the current state estimation vector.- Returns:
- the state estimation vector
-
getStateEstimationVector
Returns a copy of the current state estimation vector.- Returns:
- the state estimation vector
-
getErrorCovariance
public double[][] getErrorCovariance()Returns the current error covariance matrix.- Returns:
- the error covariance matrix
-
getErrorCovarianceMatrix
Returns a copy of the current error covariance matrix.- Returns:
- the error covariance matrix
-
predict
public void predict()Predict the internal state estimation one time step ahead. -
predict
Predict the internal state estimation one time step ahead.- Parameters:
u- the control vector- Throws:
DimensionMismatchException- if the dimension of the control vector does not fit
-
predict
Predict the internal state estimation one time step ahead.- Parameters:
u- the control vector- Throws:
DimensionMismatchException- if the dimension of the control vector does not match
-
correct
public void correct(double[] z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException Correct the current state estimate with an actual measurement.- Parameters:
z- the measurement vector- Throws:
NullArgumentException- if the measurement vector isnullDimensionMismatchException- if the dimension of the measurement vector does not fitSingularMatrixException- if the covariance matrix could not be inverted
-
correct
public void correct(RealVector z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException Correct the current state estimate with an actual measurement.- Parameters:
z- the measurement vector- Throws:
NullArgumentException- if the measurement vector isnullDimensionMismatchException- if the dimension of the measurement vector does not fitSingularMatrixException- if the covariance matrix could not be inverted
-