# Bayesian Filtering - Estimating Room Temperature using Sensor Fusion

Estimating the position of an object using a Kalman Filter, from Kalman and Bayesian Filtering in Python. |

## Objective #

The main objective was to be able to reliably estimate the temperature that occupants experience using only ceiling mounted temperature sensors. This is challenging since there is normally a temperature gradient from floor to ceiling, so measuring temperature at the ceiling and reporting it as the room temperature would lead lead to problems, especially when using this temperature as feedback in a temperature control system which is a primary use-case for the product.

We needed to develop a system that is robust to different ceiling heights (to an extent), and different levels of temperature stratification within the space (the floor to ceiling temperature offset varies between spaces). In order to do this, we knew we needed to take multiple measurements, account for sensor noise, incorporate different methods of sensing technology, and combine that with a model of room temperature thermal dynamics while also accounting for uncertainty in the model itself. Bayesian filters are a good candidate for this type of problem.

### What is Bayesian filtering and what problem does it solve? #

Bayesian filtering is a method for estimating the state of a dynamic system based on a sequence of noisy observations. These methods are usually recursive, meaning that they can easily be efficiently implement on a computer or microcontroller, without requiring huge amounts of compute.

### What is Sensor Fusion? #

Sensor fusion is a process of combining information from multiple sensors to create a more accurate and reliable measurement than from any individual sensor. The idea is to take the strengths of each type of sensor and combine that information into a stronger estimate, in a sense creating a virtual sensor from different physical sensors. There are many ways to implement sensor fusion such as the Kalman filter.

### The Kalman Filter #

The Kalman filter is a specific type of Bayesian filter that is well suited for problems that are (mostly) linear and where the noise in the system are normally distributed (both of these conditions are satisfied for the temperature measurement problem).

There are many excellent explainer blog posts and text books for the Kalman filter. I'll give a high level overview here and then list some of my favourite explainers below.

The Kalman filter has 2 main steps:

### 1. Predict #

Based on all of the observations we've seen thus far and our model of the system, what is our best guess of the system state? When making a prediction, all we're doing is computing the expected output of our model for the next time step. When doing this, it's important to capture the inherent uncertainty in this process (i.e. making a pointy distribution a little more flat, spread out, and less precise). The amount of uncertainty increase is based on the process noise which is a parameter of the Kalman Filter. In addition to tracking the system state, the Kalman filter also tracks the variance and covariance between the different state variables.

### 2. Update #

Compute the Kalman gain which is a parameter that tells the filter how much to trust the measurements relative to the prediction from the model. The computation for the gain is based on the ratio of variances between the model and the observations - which makes some intuitive sense. If the measurements are all over the place, it makes more sense to trust the model. On the other hand, if the measurements are very tightly grouped, then we can trust the measurements more. This parameter dynamically adapts as the Kalman filter runs. In most well designed Kalman filters, this gain eventually converges to a mostly constant value. So this step applies the Kalman gain to combine the prediction with the measurements. Again, we have to update the covariances so that they reflect our current belief and uncertainty.

That was a very quick explanation of the Kalman filter. I really like the following resources for a gentle introduction to Kalman filters:

- How a Kalman Filter Works, In Pictures: A very intuitive yet accurate explanation of Kalman filters
- A non-mathematical introduction to Kalman filters for programmers: Another very intuitive introduction. This one has a few technical errors (i.e. not propagating the uncertainty forward in the
*predict*step), but it doesn't take away from the main intuition that it tries to build up. See more about the technicalities in this hacker news discussion - Kalman and Bayesian Filtering in Python: A whole textbook in the form of a set of interactive jupyter notebooks that goes in depth about the Kalman filter and its various precursors and extensions (extended Kalman filter, Unscented Kalman filter, particle filter)

## My role #

As the Senior engineer and technical lead, my role in this project was to:

- Design and conduct experiments to learn more about temperature stratification nad how it varies
- Design and implement the algorithm (Kalman Filter) to estimate the occupant temperature and humidity
- Design an easy to follow calibration process
- Ensuring numerical stability and convergence within the Kalman filter implementation. Safe guarding against edge cases.
- Work collaboratively with a small team of engineers to build out this feature and conduct the experiments
- Implement the Kalman filter in C on an [embedded device].
- Create a plan for validating the performance and reliability of the system to ensure that it works well in a variety of environments, ceiling heights, proximity to diffusers, etc.
- Authored an industry white paper, explaining how the system works

## Impact #

- We were able to achieve 0.5 °C accuracy with high reliability
- This feature allowed for a new line of ceiling based temperature measurement products for the company
- Resulted in the company's first patent
- Opened up more lines of applied research (thermal load detection)

- Previous: Edge Analytics
- Next: Detecting Presence Using Machine Learning