MR-Addict / Digital-Filter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Digital Filter

1. Preview

Typically, we use Digital Filter to process digital signal and sensor data, smoothing or filting data. There are many digital filting algorithm, most popular filters are:

  • FIR Filter
  • MAF Filter
  • IIR Filter

FIR Filter is more powerful than MAF and IIR filter, abeling to stablizie, smooth, and filt data, but it comsumes more computing power.

MAF Filter is a simplified FIR Filter that only averaging your singal data, but it's still working very well.

IIR Filter is much more faster, but it's less powerfull than FIR and MAF Filters.

Image below is raw roll and pitch data from MPU6050.

Raw-Data

After filting using MAF Filter, data signal looks like below, and the result is promising.

Filted-Data

2. Drawbacks

The biggest problem of FIR Filte is that filter will cause lagging data. Higher frequeny, more lagger data processing.

You can clearly see it showing below.

Drawback

Lagging problem is less if you're using MAF or IIR Filters. All in all, sensor fusing is really important.

3. Filter Implement

3.1 FIR Filter

3.1.1 FIR Online Designer

FIR Filter is the most difficult Filters comparing with MAF and IIR Filters, you need to calculating your own filter taps. We often using programmes or Online Designers to do this job.

There are plenty of FIR online desingers if you google it. What I using is TFilter. You can specify your design needs on this website. For example, my sampling rate is 100Hz, and my desired frequency is 5 Hz, I want to cutoff 25-50Hz signal, after clicking DESIGN FILTER I get my filter taps.

TFilter

Generated filter taps are use for FIR Filter Implement after.

3.1.2 FIR Filter Implement

You can directly use FIR Libarary for you own application, which I referred from Phil’s Lab who did a great job on Control System.

First you need to include FIRFilter in your project.

#include "FIRFilter.h"

Then you need to define a data buffer for storing your data and an arrey of your filter taps.

float buffer[9] = { 0 };
float taps[] = {
    0.02341152899192398, 0.06471122356467367, 0.12060371719780817,
    0.16958710211144923, 0.1891554348168665, 0.16958710211144923,
    0.12060371719780817, 0.06471122356467367, 0.02341152899192398
};

And you need to declear a FIRFilter struct and pass above two arreies to it.

FIRFilter FIRRoll;
FIRFilter_Init(&FIRRoll, taps, buffer, sizeof(taps)/sizeof(float));

After all of this, you can now use FIR Filter in your project. For example, if I need to filt my Roll data, I can do below.

char message[50] = { 0 };
float roll = MPU_GetRoll();
float rollFilted = FIRFilter_Update(&FIRRoll, roll);
sprintf(message, "%.2f\t%.2f\r\n", roll, rollFilted);
HAL_UART_Transmit(&huart1, (uint8_t*) message, sizeof(message), 10);

3.2 MAF Filter Implement

MAF Filter usage is much more easier than FIR Filter, you do not need to design filter taps, because you are averaging your data.

So you just need to including MAFFilter libaray, declear a MAFFilter struct and initilize MAF Filter.

#include "MAFFilter.h"
MAFFilter MAFRoll;
MAFFileter_Init(&MAFRoll);

After that, you can using MAF Filter just like before.

char message[50] = { 0 };
float roll = MPU_GetRoll();
float rollFilted = MAFFilter_Update(&MAFRoll, roll);
sprintf(message, "%.2f\t%.2f\r\n", roll, rollFilted);
HAL_UART_Transmit(&huart1, (uint8_t*) message, sizeof(message), 10);

3.3 IIR Filter Implement

IIR Filter is as easy as MAF Filter, you just need to specify alpha you want to use for your filting.

#include "IIRFilter.h"
IIRFilter IIRRoll;
IIRFilter_Init(&IIRRoll, 0.5);

Then using IIR Filter.

char message[50] = { 0 };
float roll = MPU_GetRoll();
float rollFilted = IIRFilter_Update(&IIRRoll, roll);
sprintf(message, "%.2f\t%.2f\r\n", roll, rollFilted);
HAL_UART_Transmit(&huart1, (uint8_t*) message, sizeof(message), 10);

Happy Coding!

About


Languages

Language:C 100.0%