There have been a couple of times over the past few of years where I have had cause to model a noisy value that is changing at a constant rate. One of the standard ways of doing this is by using a Kalman Filter, specifically a one dimensional constant velocity Kalman Filter. Unfortunately whilst this is almost as simple as you can get I’ve never seen the recipe written out. So one every occasion I have to read Greg Welch’s tutorial and knock the rust of my maths crunching through the derivation; it’s not that it’s that hard you just have to remember where you can simplify and assume stuff (and remember useful snippets like covariance matrices are symmetric).

Any how here’s the recipe:

You need to keep track of five numbers, the current estimate of the position x, the current estimate of the velocity v, and the uncertainty of measurements which I’ll call Pxx, Pvv, Pxv (technically these are the variance of x, v and their covariance respectively).

You need to have initial estimates of these five values. An easy way of doing this is to collect a couple of data points at the beginning, set the estimate of x to the second, v to the rate of change between the two and the uncertainty to some arbitrary value (like 1). (Fiddling with how this method will change the filter’s start up behaviour but not the long term effect).

You also need to have three values that stay constant: estimates of the measurement noise, R; and estimates for the noise in the real process for the position and velocity (Nx and Nv respectively). The best way is to get values of these is to get some real data and then run the filter over it at a range of values and pick those that look best. Roughly speaking R tells the filter how much to trust its model against the current estimate. The larger the value the more the model will be trusted so the smoother the predictions, but the slower it will be to update to changes in the process.

When I have a chance to format it I plan to post the derivation for this from the base Kalman equations.

And by way of a demonstration here’s a Python script that you can play with.