Course index:

- Home: Kalman filtering (for drones).
- Lesson 0: Modelling the system (noisy variables).
- Lesson 0.5: Modelling the system (dynamics).
- Lesson 1: Discrete linear Kalman filter.

We already know how to represent a state as a stochastic (noisy) variable by writing down equations when such a noise is Gaussian. In particular, we have shown that for each state we have to focus on two properties, its mean and its standard deviation via its variance. When we have a collection of states, we stack their means in the vector and we collect the standard deviations in the square matrix , where the diagonal entries stand for the variances, e.g. and the off-diagonal entries stand for the cross variances.

We also know how to describe the evolution of the means and the variances of a system in a simple way, e.g. via discrete events and where the couplings of the states are linear, namely

At this point we are going to simulate system (1) with the introduced and in the previous lessons. You can download the Python script from my GitHub.

We start with the following initial conditions

,

that means that our guess for the position, velocity and bias is centered at with a standard deviation of [m], [m/s] and [m/s²] respectively. We consider that in our initial guess, all the cross correlations are zero. We also consider that our system is at rest, so but the accelerometer is affected by Gaussian noise with and [m/s²]. Note that the is equivalent to a bias in the accelerometer, since we are shifting the real measurement by the mean of the Gaussian distribution .

Since we do not know where the position is with total accuracy, and we do not know for the velocity neither, then what (1) does is to **evaluate and evolve all the possible combinations** of positions and velocities at each time a value of the accelerometer enters into the system. Therefore, the *worst case condition* at the starting is to have a position and velocity , and next time the dynamics (1) will evaluate again all the possible combinations that for sure will be bigger than at the starting time. Hence, the confidence about the position and velocity decreases, or in other words, the vector , via the matrix , is getting bigger and bigger.

The position, velocity and estimated bias are the first, second and third plots respectively. The forth plot is the signal of the biased and noisy accelerometer. With black vertical bars we represent the actual or true values of the position, velocity and bias. Note that since in (1) we have that , the estimation of the bias does not change at all. And of course, the confidence about the position and velocity of the system is getting worse and worse.

Now we are ready for presenting the Kalman filtering technique in its **simplest version**, the Discrete linear Kalman filter. We call it discrete and linear because it deals with discrete and linear models like (1). We will see how to handle other kind of models in later lessons. Let us consider that from time to time, our system is able to measure its position, e.g. via GPS, and of course we shall describe it with a function that will be Gaussian, where we call the mean and its standard deviation .

At the moment of taking the reading from the GPS, we find ourselves with two Gaussian distributions and describing the **same information**. We have already seen in equation (5) from Lesson 0 how **to fuse** two Gaussian functions! and now we are going to show how to do that in a more general way. In order to fuse two informations, the first step is always to have them with the same coordinates, e.g. same units or you cannot compare apples and oranges. Therefore we need to extract the information from our system in an appropriated way such that we can compare it with our measurement . In our example, the GPS is measuring the position, which is the first state in , so we can write

but it might happen that we are measuring the velocity of the system with a radar, in such a case we will have that

or if we measure position and velocity at the same time

or we have a bizarre sensor that measures a linear combination of position and velocity, all in one number

That matrix is what we call an *observation model*. Sometimes is very obvious as in (2) or (3), but we will see in more advanced examples, such as determining the attitude of drones, that then we will have observation models like in (5). Note that we have assumed that the units of the position from and are the same, otherwise in (2) we should have in the corresponding factor for changing units instead of a one.

Let us rewrite again the result of fusing two **scalar **informations represented as a Gaussian variables and

where we can identify that

where and are scalar numbers. One can identify that we were fusing the mean and the standard deviation from two different scalar sources. Note that we can write (7) in the following way

However, equations (1)-(5) involve vectors and matrices, i.e. we are going to present (6)-(8) such that we are **fusing vectorial quantities**.

Let us take the Gaussians and stack them into a vector, i.e. we substitute by in (7) or (8). Then we take the corresponding Gaussians from the measurements and we also stack them into another vector and we put it in (7) or (8) in the place of . Note that we need to employ in order to compare with . Now compute all the fusions between these two vectors and as we have done for the scalars in (6), i.e. we fuse all the components of the former vector with all the components of the latter vector (very tedious and I omit here the calculations since they are not illustrative but you will see the analogy). Finally we arrive at the compact vectorial/matricial expression for (8), namely

where we have seen that is the stacked vector of the means of the measurements, is the famous Kalman gain (yeah it was Kalman the one that perform this computation and publish it!) and is the covariance matrix given by the standard deviations of the measurements. For example, in (4) we have that and the corresponding two standard deviations are from the GPS and radar sensors, and correspondingly .

One can perform a **quick identification** between (8) and (9) by assigning

If you substitute these equivalencies into (8) and write the operations in a matrix form instead of scalars, you will clearly see the similarity between (8) and (9).

**After the fusion** in (9), what we do is to consider and as the new and in (1). So we are ready for the next event iteration.

**Advices and hints**

- The
**most expensive computation**in the Kalman Filter is the matrix inversion in (9). Sometimes it is not advisable to take 892471489 measurments into (7)-(8) at the same time. For example, one can do (2) and (3) in two different updates at different times instead of (4) at the same time. With this**simple trick**you are distributing the computational effort of the filter over time! - Sometimes we are dealing with really small numbers for , i.e. high accuracy, this can result in numerical errors because of the floating operations in a computer. One should always check that is positive definite, for example by a quick inspection of the diagonal terms. If they are not positive, then one should discard the fusion. This test can be also a
**red flag**in order to check that you have implemented the filter correctly. **Never feed your filter with incorrect or false information**. For example, if you are updating your system with (4) and you do not have available at this moment the measurement from the GPS, then employ (3) to correctly update the states, but do not put a dummy velocity, e.g. a past value!! you will have worse performance by doing so!- You do not need to update your system all the time. For example, check the order of magnitude for an acceptable covariance in your system (let us say meters), you do not need all the time to poll that sensor that costs you a lot of in communications or energy (for having femto meters of accuracy) in order to fuse/update your states.
- Kalman filtering sometimes is
**an overkill**solution for your problem. If you are happy by measuring a position five times per second with an accuracy of meters, then just take the measurement of the GPS. You do not need an inertial measurement unit with a Kalman filter. For example, a big vehicle/object that moves slowly. From other point of view, if your mission lasts short enough and your inputs, e.g. accelerometer has a very small bias and standard deviation, then you do not need to fuse/update your states. The former is pretty common in space applications like a re-entry scenario with tactical grade IMUs (intercontinental missiles that do not rely in GPS is an example). - Because your system works, it does not mean necessarily that your Kalman filter is doing so. Check all the steps as we have described in this guide.

**Application example, calibrating an accelerometer**

We take (1) with the matrices F and G as in Lesson 0.5 and the model of the accerometer with and meters/sec².

with secs. Every 1000 iterations, i.e. each second, we take a measurement of the position, i.e. we use the observation model (2). Since our IMU is at rest on our table, the position does not change, so the measurement will be and we set a confidence of meters. Note that we could put a smaller value for the standard deviation (**never zero!**), but we keep it *high* for demonstration purposes.

As before, we set as initial condition for the states in (1), and an initial standard deviation .

We note in the following simulation (you can download the Python script at my GitHub) that indeed, we tell the system at every second, hey! you are at position 0 with a confidence of plus/minus 2 meters. Then, the guesses in the position and the velocity correct themselves back, going to the vertical black lines (the actual values). The system is *learning *with each measurement, the state of the bias of the accelerometer (third plot) is approaching the value of 0.07 with a higher and higher confidence in each update. We also plot in blue the guess of the bias in the accelerometer signal (bottom plot), and we see how it converges to the actual bias of the signal. After almost 15 seconds, we have calibrated the accelerometer, we have identified its bias!