# Working with STM32 and digital Filters :Kalman filter.

In the previous guides, we took a look at FIR and IIR filters and in this guide, we shall take a small introduction to Kalman filter and how to use it filter ADC signal from out STM32F7 board.

In this guide, we shall cover the following:

• Kalman filter.
• Code.
• Demo.

## 1. Kalman Filter:

Kalman filtering is an algorithm that provides estimates of some unknown variables given measurements observed over time. Kalman filters have been demonstrating its usefulness in various applications. Kalman filters have relatively simple form and require small computational power. However, it is still not easy for people who are not familiar with estimation theory to understand and implement the Kalman filters.

## 2. Code:

The code for simplified Kalman filter to filter out ADC value:

```/*****************************************************
*Function name: kalman_filter
*****************************************************/
{
float x_k1_k1,x_k_k1;
float Z_k;
static float P_k1_k1;

static float Q = 0.0001;//Q: Regulation noise, Q increases, dynamic response becomes faster, and convergence stability becomes worse
static float R = 0.005; //R: Test noise, R increases, dynamic response becomes slower, convergence stability becomes better
static float Kg = 0;
static float P_k_k1 = 1;

x_k_k1 = x_k1_k1;
P_k_k1 = P_k1_k1 + Q;

Kg = P_k_k1/(P_k_k1 + R);

P_k1_k1 = (1 - Kg)*P_k_k1;
P_k_k1 = P_k1_k1;

}```

## 3. Demo:

• Blue line raw unfiltered data.
• Red line filtered data.

Happy coding ðŸ™‚

• Victor Posted March 13, 2022 12:04 am

Awesome!

• Michel Posted May 28, 2022 2:12 pm

Hi