HEAD ======= >>>>>>> c467bb8b4126460c1e18ba28ee4392f49fdc6552
kalman-cpp
Implementation of Kalman Filter in C++
|
A linear system is described as follows.
\[x_k = Ax_{k-1} + Bu_{k-1} + v_{k-1}\]
\[z_k = Hx_k + w_k\]
where:
\(v\) is the process noise (Gaussian with covariance Q)
\(w\) is the measurement noise (Gaussian with covariance R)
\(A\) is the system matrix
\(B\) is the input matrix
\(H\) is the output matrix
\(x\) is the state vector
\(z\) is the output vector
\(u\) is the input vector
The noise covariance matrices must follow these conditions:
\[Q = Q^T\]
\[R = R^T\]
An example is provided in main1.cpp solves the car voltmeter problem using this library. The system model for this problem is governed by:
=======
An example provided in main1.cpp solvesthe car voltmeter problem using this library. The system model for this problem is governed by:
>>>>>>> c467bb8b4126460c1e18ba28ee4392f49fdc6552
\[x_k = 12 + v_{k-1}\]
\[z_k = x_k + w_ḳ\]
Let us extract the model parameters from the equation above: \(A = 0\), \(B = 1\), \(H = 1\), \(u = 12\), \(x_0 = 12\), \(Q=4\), and \(R=4\). Now that we know all the parameter values, we can then set up the following piece of code:
The first and second line of the code above are used to create all the necessary matrices with correct dimensions. In the following lines, we apply the correct value for each matrix.
Our next step is to create an instance of class class and initialize it with the previously created \(A\), \(B\), \(Q\), \(H\), and \(R\) matrices.
And or our last step, we run the Kalman procedure repetitively as shown in the code below.
A non-linear system is described by:
\[x_k = f(x_{k-1}, u_{k-1}) + v_{k-1}\]
\[z_k = h(x_k) + w_k\]
where:
\(f\) is the dynamic model of the system
\(h\) is the measurement model of the system
\(v\) is the process noise (Gaussian with covariance Q)
\(w\) is the measurement noise (Gaussian with covariance R)
\(x\) is the state vector
\(z\) is the output vector
\(u\) is the input vector
The noise covariance matrices must follow these conditions:
\[Q = Q^T\]
\[R = R^T\]
Here, let us take main4.cpp as an example. This content of this file is adapted from this
MATLAB Central page.
Let us define a nonlinear system as follows.
\[ f = \begin{bmatrix} \sin(x_2(k-1))(k-1) \\ x_2(k-1) \end{bmatrix} \]
\[ h = \begin{bmatrix} x_1(k) \\ x_2(k) \end{bmatrix} \]
\[ x_0 = \begin{bmatrix} 0 \\ \frac{1 \pi}{500} \end{bmatrix}\]
A nonlinear function does not have a strict tempate as in a linear function. Therefore, we need to let the user to derive the EKF class for flexible implementation of a nonlinear model for both the process and the the measurement.
The next steps below are very similiar to the previous example in the linear Kalman filter section. First, we start by creating all necessary matrices and set their values accordingly. Afterward, we continue with initializing. the covariance matrices as well as the state vector. In the final step, we run the EKF procedurerepetitively while logging necessary data.
Another type of Kalman Filter for a nonlinear system is the Unscented Kalman Filter. It addresses the accuracy problem which arises during linearization process of an Extended Kalman filter when Jacobian is used. The Unscented Kalman Filter is implemented in class UKF and the steps for using the provided UKF class is exactly similiar as in using the EKF class.
The implementation of the UKF in this C++ library is based on the following paper:
Wan, E. A., & Van Der Merwe, R. (2006). The unscented Kalman filter for nonlinear estimation. Proceedings of the IEEE 2000 Adaptive Systems for Signal Processing, Communications, and Control Symposium (Cat. No.00EX373), 31(2), 153–158.
The paper above can be downloaded from here.
Here, we take main10.cpp as an example. This example is adapted from this
This example is taken fromhere. The nonlinear system is described as follows.
\[ f = \begin{bmatrix} x_2(k) \\ x_3(k) \\ 0.005 \, x_1(k) \bigg(x_2(k) + x_3(k) \bigg) \end{bmatrix} \]
\[ h = \begin{bmatrix} x_1(k) \end{bmatrix} \]
\[ x_0 = \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}\]
The following lines of codes show how to use the UKF class. As mentioned before, we need to first derive the UKF class and define the virtual functions f (process function) and h (output function).
These are the plots that show comparisons between measurements and estimations of the three states: \(x_1\), \(x_2\) and \(x_3\).
The examples we have so far are theoretical. Very often, what we would like to do is to reduce noise from pre-acquired measurement data. There are several reasons why we want to use Kalman filter. For example, noise has a vast spectrum. Thus, using a frequency-based filter hurts the data.
Principally, there are two scenarios of using the Kalman filtering techniquie here. The first scenario is by first simulating the system, as shown in the figure below.
In this scenario, we only need to supply \(u_k\) to the Kalman filter function. The Kalman procedure will give us four outputs as a result: \(x_k\), \(z_k\), \(\hat{x}_k\), and \(\hat{z}_k\). \(x_k\) and \(z_k\) are called the true states and the true outputs, respectvely. They are noisy. \(\hat{x}_k\), and \(\hat{z}_k\) are called the estimated states and the estimated outputs, respectively. They are filtered, look smoother and closer to the noiseless true states.
The function prototype for the scenario above can be written as:
[ \(x_k\), \(z_k\), \(\hat{x}_k\), \(\hat{z}_k\)] = function kalmanf( \(u_k\))
The second scenario is used when the measurements are available. Thus, simulating the system becomes unnecessary. In such a scenario, we need to supply both \(z_k\) and \(u_k\) to the kalman filter function. The Kalman filter will give us 2 outputs: \(\hat{x}_k\) (the estimated system sates) and \(\hat{z}_k\) (the estimated system outputs).
The function prototype for this scenario can be written as:
[ \(\hat{x}_k\), \(\hat{z}_k\)] = function kalmanf( \(z_k\), \(u_k\))
The second scenario is useful for smoothing noisy measurment data. However, both scenarios are availabe in this library.
As one example, let us see the problem provided in main1.cpp. This time, however, we assume we have already had several noisy data points from measurements. Therfore, we will not need the Kalman procedure to simulate the system.
The system model can be formulated into:
\[x_k = x_{k-1} + v_{k-1}\]
\[z_k = x_k + w_ḳ\]
We then generate random number as the prerecorded noisy data (z_measurement), as follows.
After than, z_measurement is sent to the Kalman filter function, as the following:
The kalman.Kalmanf(z_measurement, u) function above has different parameters with what we can find in main1.cpp. Since z_measurement is available, we then send it as a parameter for the kalmanf function. The processes above are done in several iterations according to the number of the availabe data points. The final codes will look like as the following:
The values that are given to Q and R are defined heuristically since we do not know the actual variances of the noise of the data.