EE3901/EE5901 Sensor Technologies Week 4 NotesNon-linear Kalman filters
Last week we studied the Kalman filter, which is the optimal estimator for linear systems in the presence of Gaussian noise. This week we address the problem of non-linear systems.
Recap of the Kalman filter
Let’s remind ourselves of the structure of the Kalman filter, so we can consider how to extend it for the case of non-linear systems.
To design a Kalman filter, you must determine:
- Which variables go into the state vector
. - Which variables go into the control vector
. - Which variables go into the measurement vector
. - The prediction matrix
, which describes how the state vector tends to evolve over time. - The control matrix
, which describes how the state vector is perturbed by control inputs. - The process covariance
, which describes the growth in the uncertainty in the predicted state. - The measurement matrix
, which gives the mapping from state vector to measurements. - The measurement covariance
, which is the covariance in the measurements.
The Extended Kalman Filter (EKF)
Motivation
The classical Kalman filter uses a prediction model of the form
where
However, what if the system dynamics cannot be expressed as a matrix of numbers? Suppose instead we have some arbitrary function
How can we develop a Kalman filter for such a system?
Deriving the EKF
The key insight is as follows. There is no particular reason that the prediction step must be matrix multiplication. If we know that some arbitrary function
The EKF modifies the prediction step to read:
where
Similarly the measurement step can be updated to read:
where
A further insight: the state covariance and measurement covariance
can be calculated using the error propagation laws. Recall from week 2 that
we have
We will use the following notation. Let
which is the same equation as for the linear Kalman filter, except
that
Similarly the measurement residual covariance is:
Again the equation is the same, except that
All other parts of the filter can remain the same.
Intuitively, the extended Kalman filter is simply finding a linear
model that best matches the non-linear functions
The extended Kalman filter is not necessarily an optimal estimator, in the sense that the linear Kalman filter is mathematically proven to minimise the sum of squares of the error for linear systems. If the system state is linear then the EKF reduces to the normal Kalman filter, in which case it is optimal. For non-linear systems, the optimality cannot be guaranteed. However, in practice the EKF often works well.
Example of an Extended Kalman Filter (EKF)
Suppose that you want to track the motion of a pendulum (Figure 1).
Define the state vector
By analysing the forces, we can write the differential equations that describe this state:
This is our physics model, which we will now use to develop an Extended Kalman Filter.
The prediction model
We must convert our system of continuous differential equations
where
Applying the leapfrog method to Eqs.
This is our prediction model, which says how to step the state vector forward in time.
Recall that the EKF relies upon linearising the prediction model around the current operating point. In other words, we need the Jacobian of
To reduce the risk of a mistake, it is recommended that you use a computer algebra system to calculate the derivatives. The provided Matlab code shows you how to automatically find Jacobians.
The resulting Jacobian, copy-pasted from the provided Matlab code, is:
Measurement
Suppose that the measurement actually records the kinetic and potential energy, i.e. a non-linear function of the state.
where
Again we need a Jacobian matrix
EKF example code
Refer to the attached Matlab script for a complete working example.
Implementation notes
A few points to note:
- The Jacobian matrices
and vary with each time step so you must re-calculate them inside your loop. - You will typically need to tune the process covariance to get good results. Essentially you are trading off how much the filter believes the prediction vs how much it believes the measurement. As with the linear Kalman filter, a diagonal matrix with standard deviation proportional to the time step is a plausible starting point in the absence of a more detailed error model.
The Unscented Kalman Filter (UKF)
The UKF is another method for estimating a non-linear process. Its key advantage is that it does not require the computation of Jacobians. Hence it can be applied to models that are non-differentiable (for example arbitrary computer code, or models containing with discontinuous functions where derivatives would be undefined).
The key idea is that instead of propagating just the state vector
The Unscented Transform
The Unscented Transform solves this problem: given some calculation
The steps are as follows:
First define some parameters: let
Calculate
and
where chol
function.
Next, expand the vector
where +
and -
because that is the behaviour of those operators.) The matrix
Each column of
Transform each sigma point through the function to produce a new matrix
where each column of
If
We will construct the outputs
Form vectors by concatenating these weights
Note that other definitions of the weights are also possible. Different
authors use different weights. A necessary constraint is that
Then the expected value of the output is given by:
The covariance is given by
where .*
operator).
Applying the Unscented Transform to the Kalman filter
Prediction step
Given the previous state
Propagate the sigma points through the prediction model
Following the Unscented Transform, use
Finally add in the process covariance to
Measurement step
If the measurement is a linear function of the state, then you can use the normal Kalman filter from this point forwards.
If your measurement model is non-linear, then apply the Unscented Transform a second time to propagate the predicted state through the measurement model. Define a new set of sigma points based on the predicted state and predicted covariance. Then propagate these through the measurement model:
Reconstruct the predicted measurements
the measurement residual
and the measurement residual covariance
where
Update step
If the measurement model is linear, use the standard Kalman filter update step. Otherwise, proceed as below.
The update step needs a slight adjustment because there is no measurement matrix to easily link measurement space and state space. Instead we can use the cross-correlation between state and measurement:
The Kalman gain in this case is
The updated state and covariance are given by
Example of an Unscented Kalman Filter (UKF)
Refer to the attached Matlab script for a complete working example.
Implementation notes
-
The initial state covariance cannot be 0 because Cholesky factorisation is only defined for positive definite matrices (those with positive eigenvalues). This is true for any legitimate covariance matrix. If the Cholesky factorisation throws an error then most likely your prediction model is diverging or you have made a mistake in the equations.
-
A major advantage of the UKF is the ability to handle sophisticated prediction models where derivatives are impossible or unreasonable to compute. For example, your prediction model could be an entire computer program. All you need to do is run each of the sigma points through your prediction model and you can thereby use it in your Kalman filter.