I've been writing Elixir for a while now, but I haven't published a public Hex packages, until now.

I'm working on a project involving using data from sensors. Data from sensors is always messy. There's noise and all sorts of random stuff going on in  the real world. So you can't always trust the data you get from your sensors.

So what to do? There's different ways to mitigate this. Moving average is one simple way to smooth things out. But it has its drawbacks.

Kalman Filter

The Kalman Filter is often used to filter data where you expect some noise. It does so by looking at the previous data point and how certain we are about that data point to decide how much the current measurement should weigh in.

It was formalised by R.E Kalman in 1960 in his paper.

Or as Wikipedia explains it:

The Kalman Filter (...) is an algorithm that uses a series of measurements observed over time, containing statistical noise and other inaccuracies, and produces estimates of unknown variables that tend to be more accurate than those based on a single measurement alone...

A Good Explanation

If you are like me the math is not as intuitive as a verbal explanation. A good video on the subject is Lachlan Blackhall's 2016 talk on the filter and an implementation in Python. I used his implementation as the foundation for my implementation in Elixir.

My Implementation

The algorithm as described in the original paper is recursive. Being a functional language, Elixir lends itself very well to solving problems recursively. So instead of using an agent or GenServer to store state I implemented the filter in a strictly functional manner.

Cees de Groot had a look at it and made it a bit more user friendly. His implementation is version 1.0 of the package.

The Result

I've barely tested the code, and it comes with no warranties. But if you can find some use for it, it's licensed under the MIT license and can be found on my Gitlab.

It's published as a Hex package and the documentation is available on hexdocs.pm.

Enjoy!