A Linear Estimation Problem

We consider estimating the system state $$x_k = \begin{bmatrix} p_k \\ s_k \end{bmatrix}$$ with scalar position \(p_k\) and speed \(s_k\) of a target at discrete time steps \(k\). Its temporal evolution is modeled with a constant velocity model according to $$x_k = \mathbf{A} \cdot x_{k-1} + \mathbf{B} \cdot w \enspace,$$ with time-invariant, additive, and zero-mean white Gaussian noise \(w\) with variance \(0.1^2\). The system matrix is given by $$\mathbf{A} = \begin{bmatrix} 1 & \Delta t \\ 0 & 1 \end{bmatrix} \enspace,$$ the system noise matrix by $$\mathbf{B} = \begin{bmatrix} \Delta t \\ 1 \end{bmatrix} \enspace,$$ and \(\Delta t = 0.01\). At each time step \(k\), we receive noisy position measurements \(y_k\) according to $$y_k = \mathbf{H} \cdot x_k + v \enspace,$$ with also time-invariant, additive, zero-mean white Gaussian noise \(v\) with variance \(0.05^2\) and measurement matrix $$\mathbf{H} = \begin{bmatrix} 1 & 0 \end{bmatrix} \enspace.$$ The initial system state \(x_0\) is Gaussian distributed $$x_0 \sim \mathcal{N}(\hat{x}_0, \mathbf{C}_0)$$ with mean $$\hat{x}_0 = \begin{bmatrix} 0 \\ 0 \end{bmatrix}$$ and covariance matrix $$\mathbf{C}_0 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \enspace.$$ In the sense of the minimum mean square error (MMSE), this estimation problem can optimally be solved by using the Kalman filter (KF).

Hence, for this concrete problem we

When using linear models in the form of a LinearSystemModel or a LinearMeasurementModel in combination with a implemented Kalman filter, the state prediction/measurement update will be computed in closed-form. That is, no sampling or computation of derivatives is done at all.

The following code listing (inclusive helpful comments) shows how to implement this constellation of filter, models, and distributions in order to do the state estimation with the Nonlinear Estimation Toolbox.

StandardKFExample.m

A linear state estimation example.
function StandardKFExample()
    %%%%% Scenario
    % 2D system state
    %
    %   x(k) = [p(k) s(k)]'
    %
    % containing scalar position p(k) and scalar speed s(k)
    
    %%%%% System Model
    % Constant velocity system model
    %
    %   x(k) = A * x(k-1) + B * w
    
    % Delta t (step size)
    T = 0.01;
    
    % System matrix A
    sysMatrix      = [1 T
                      0 1];
    
    % System noise matrix B
    sysNoiseMatrix = [T
                      1];
    
    % Time-invariant, zero-mean Gaussian white noise w
    sysNoise = Gaussian(0, 0.1^2);
    
    % Create system model instance and set its parameters
    sysModel = LinearSystemModel();
    sysModel.setSystemMatrix(sysMatrix);
    sysModel.setSystemNoiseMatrix(sysNoiseMatrix);
    sysModel.setNoise(sysNoise);
    
    % A few notes on the linear system model
    %
    %  * If no system matrix is provided, it is assumed to be the identity
    %    matrix of appropriate dimensions.
    %
    %  * If no system noise matrix is provided, it is assumed that
    %
    %       dim(w(k)) == dim(x(k))
    %
    %    and the system noise matrix will be set to the identity of
    %    appropriate dimensions.
    %
    % * Hence, if both matrices are not set, the LinearSystemModel acts
    %   like a random walk system model.
    %
    % * It is possible to pass the system matrix and system noise matrix
    %   directly to the class constructor of the LinearSystemModel to avoid
    %   the additional set methods.
    %
    % * The system noise does not have to be distributed according to
    %   a Gaussian distribution. One can pass here any other distribution,
    %   as the Kalman filter internally only works with the distribution's
    %   mean and covariance. However, in such a case the KF isn't anymore
    %   the optimal MMSE estimator (only a linear MMSE estimator).
    
    %%%%% Measurement Model
    % Linear measurement model
    %   y(k) = H * x(k) + v
    
    % Measurement matrix H
    H = [1 0];
    
    % Time-invariant, zero-mean Gaussian white noise v
    measNoise = Gaussian(0, 0.05^2);
    
    % Create measurement model instance and set its parameters
    measModel = LinearMeasurementModel();
    measModel.setMeasurementMatrix(H);
    measModel.setNoise(measNoise);
    
    % A few notes on the linear measurement model
    %
    % * If no measurement matrix is provided, it is assumed to be the
    %   identity matrix of appropriate dimensions.
    %
    % * It is possible to pass the measurement matrix directly to
    %   the class constructor of LinearMeasurementModel to avoid the
    %   additional set method.
    %
    % * The measurement noise does not have to be distributed according to
    %   a Gaussian distribution. One can pass here any other distribution,
    %   as the Kalman filter internally only works with the distribution's
    %   mean and covariance. However, in such a case the KF isn't anymore
    %   the optimal MMSE estimator (only a linear MMSE estimator).
    
    %%%%% The estimator
    % As both models are linear, state prediction and measurement update
    % can be fully calculated in closed-form by a Kalman filter.
    % Consequently, all implemented Kalman filters handle these models
    % in an analytic way, e.g., without calculating derivates or using samples.
    % Hence, we simply choose to use the extended Kalman filter (EKF)
    % implementation in this example.
    filter = EKF();
    
    % Set initial state estimate
    %   E[x(0)]    = [0 0]'
    %   Cov[x(0)]  = diag([1 1])
    
    initialState = Gaussian(zeros(2, 1), eye(2));
    
    filter.setState(initialState);
    
    % A few notes on on setting the system state
    %
    %  * The initial state estimate can be of any distribution as the Kalman filter
    %    internally only works with the distribution's mean and covariance matrix.
    %    For example, a Gaussian mixture will be reduced to its mean and
    %    covariance matrix, and thus, will lose any existing multi-modality.
    %
    %  * The system state can be set to any value to any time by calling
    %    the setState() class method (e.g., when the system state estimate
    %    needs to be re-initialized due to a track loss).
    
    % Perform a prediction step
    filter.predict(sysModel);
    
    % Print predicted state mean and state covariance matrix by getting its
    % Gaussian state estimate:
    predState = filter.getState();
    
    [mean, cov] = predState.getMeanAndCov();
    
    fprintf('Predicted state mean:\n');
    disp(mean);
    
    fprintf('Predicted state covariance:\n');
    disp(cov);
    
    fprintf('\n');
    
    % Assume we receive the measurement
    measurement = 2.139;
    
    % Perform a measurement update
    filter.update(measModel, measurement);
    
    % Print the updated state estimate. However, instead of using a combination
    % of getState() and getMeanAndCov() as above, we now use the handy
    % Filter's getStateMeanAndCov() method:
    [mean, cov] = filter.getStateMeanAndCov();
    
    fprintf('Updated state mean:\n');
    disp(mean);
    
    fprintf('Updated state covariance:\n');
    disp(cov);
    
    fprintf('\n');
end

Execute the Kalman filter example with

>> StandardKFExample()
Predicted state mean:
     0
     0
Predicted state covariance:
    1.0001    0.0101
    0.0101    1.0100

Updated state mean:
    2.1337
    0.0215
Updated state covariance:
    0.0025    0.0000
    0.0000    1.0099

and you should see the output above. That's all! You successfully solved your first estimation problem with the Nonlinear Estimation Toolbox.

Type help <class name> in the MATLAB prompt to get a class description and a list of all its methods. For example

>> help Gaussian
  This class represents a multivariate Gaussian distribution.
 
  Gaussian Methods:
    Gaussian       - Class constructor.
    copy           - Copy a distribution instance.
    set            - Set the parameters of the Gaussian distribution.
    getDim         - Get the dimension of the distribution.
    getMeanAndCov  - Get mean and covariance matrix of the distribution.
    drawRndSamples - Draw random samples from the distribution.
    logPdf         - Evaluate the logarithmic probability density function (PDF) of the distribution.

Type help <class name>.<method name> in the MATLAB prompt to get a method description and information about its parameters and return values. For example

>> help EKF.getStateMeanAndCov
  Get mean and covariance matrix of the system state.
 
  Returns:
    << stateMean (Column vector)
       Mean vector of the system state.
 
    << stateCov (Positive definite matrix)
       Covariance matrix of the system state.
 
    << stateCovSqrt (Square matrix)
       Lower Cholesky decomposition of the system state covariance matrix.

Help for EKF/getStateMeanAndCov is inherited from superclass Filter

Nonetheless, as the toolbox name indicates, it is intended for nonlinear state estimation rather than only covering the simple linear cases. Hence, next we have a look at a nonlinear estimation problem.