# Capture legitimate huge increase/drop in fuel level from noisy measurements

I have a fuel sensor installed in a lorry fuel tank. The sensor measures the current fuel level (in percentage) of the tank. However, due to the fact that the fuel is sloshing in the tank, the fuel level fluctuates wildly — sometimes more than 10%.

I decided to use Kalman filter to predict the actual fuel level, using the following model:

F(k+1) = F(k) - C
C = fuel consumed between k and k+1, which is estimated using
the distance travelled between k and k+1 (measured using GPS),
and the average fuel efficiency (fixed) of the lorry (e.g. 35 L/100km).


So far, Kalman filter works perfectly when the lorry is moving and consuming its fuel. However, it fails when 2 events happened that are not captured by the model:

• refuel (large increase in fuel level in a short span of time)
• fuel theft (large decrease in fuel level in a short span of time)

You can see an example below, on the left side of the graph, where a huge drop in fuel level (most probably due to fuel theft) and a huge increase in fuel level (due to refuel) are smoothed out by Kalman filter, thus the information are lost. On the right side of the graph, where the lorry is moving and consuming fuel, the prediction works well.

So my question is, how do I smooth the fluctuations of the fuel level, and at the same time, capture refuel and fuel theft events? Any ideas? Maybe use something else other than Kalman filter?

• Nice thought to use a Kalman. I was impressed by how fuel-theft was accepted as an eventuality. – A_A Sep 3 '17 at 20:58
• I too. Nice motivation to go beyond or remodel Kalman filter for your application! – Neeks Sep 3 '17 at 21:40
• Hasyimi Bahrudin, I have the same business problem. Can you explain your solution or show the implementation of the algorithm? How did you calculate F(X)? – Whoiam Jan 31 '19 at 21:40
• maybe look into median filtering. – robert bristow-johnson Feb 1 '19 at 3:18

## 1 Answer

Presumably, fuel theft and re-fueling require the vehicle to be stopped and its engine turned off (?). Every time that you start the vehicle up, re-set the system to be tracking the fuel level in the tank at start-up. Comparing the "fuel level at (previous) shut down" to the "fuel level at start up" could tell you what is going on.

Sloshing can persist of course, even during brief stops. Adding a low pass filter / integrator to the sensor would improve instantaneous readings.

It would be interesting to obtain the Fourier Transform of the fuel level sensor signal while the vehicle is moving, provided that it is sampled quickly enough. Depending on your specifications for fuel level accuracy, you might find that a simple low pass filter / integrator might be equally effective in "dampening" the sloshing effect on the signal. This is how common "anti-slosh" modules work anyway.

Hope this helps.

• Nice alternative. Let see if it works for @Hasyimi – Neeks Sep 3 '17 at 21:41