# How to use a Kalman filter?

I have a trajectory of an object in a 2D space (a surface). The trajectory is given as a sequence of (x,y) coordinates. I know that my measurements are noisy and sometimes I have obvious outliers. So, I want to filter my observations.

As far as I understood Kalman filter, it does exactly what I need. So, I try to use it. I found a python implementation here. And this is the example that the documentation provides:

from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([[1,0], [0,0], [0,1]])  # 3 observations
kf = kf.em(measurements, n_iter=5)
(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
(smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)


I have some troubles with interpretation of input and output. I guess that measurements is exactly what my measurements are (coordinates). Although I am a bit confused because measurements in the example are integers.

I also need to provide some transition_matrices and observation_matrices. What values should I put there? What do these matrices mean?

Finally, where can I find my output? Should it be filtered_state_means or smoothed_state_means. These arrays have correct shapes (2, n_observations). However, the values in these array are too far from the original coordinates.

So, how to use this Kalman filter?

• The matrices will be estimated by the Kalman filter. You probably have to give just some starting values for the optimization algorithm or the like. – Richard Hardy Jun 24 '17 at 11:23
• You need to start by specifying a state space model, which relates your observations to the unobserved states and describes how the state evolves over time; this will give you your transition and observation matrix as well as the covariance matrix of the state error ("process noise") and the covariance matrix for the observation error (these are F, H, Q and R in the wikipedia page, A, C, Q & R at the link you give). The Kalman FIlter is simply an algorithm for estimating the (unobservable) state and its variance-covariance matrix at each time once you've specified all those things. – Glen_b -Reinstate Monica Jun 24 '17 at 11:35
• That function you link to seems to implement something a bit different from the standard KF since it can use EM to estimate some things you'd normally specify. – Glen_b -Reinstate Monica Jun 24 '17 at 11:45

Here is an example of a 2-dimensional Kalman filter that may be useful to you. It is in Python.

The state vector is consists of four variables: position in the x0-direction, position in the x1-direction, velocity in the x0-direction, and velocity in the x1-direction. See the commented line "x: initial state 4-tuple of location and velocity: (x0, x1, x0_dot, x1_dot)".

The state-transition matrix (F), which facilitates prediction of the system/objects next state, combines the present state values of position and velocity to predict position (i.e. x0 + x0_dot and x1 + x1_dot) and the present state values of velocity for velocity (i.e. x0_dot and x1_dot).

The measurement matrix (H) appears to consider only position in both the x0 and x1 positions.

The motion noise matrix (Q) is initialized to a 4-by-4 identity matrix, while the measurement noise is set to 0.0001.

Hopefully this example will allow you to get your code working.

Kalman filter is a model based predictive filter - as such a correct implementation of the filter will have little or no time delay on the output when fed with regular measurements at the input. I find it always to be more straightforward to implement kalman filter directly as opposed to using libraries because the model is not always static.

The way the filter works is it predicts current value based on previous state using mathematical description of the process and then corrects that estimate based on current sensor measurement. It is thus also capable of estimating hidden state (which is not measured) and other parameters that are used in the model so long as their relationships to measured state are defined in the model.

I would suggest you study the kalman filter in more detail because without understanding the algorithm it is very easy to make mistakes when trying to use the filter.