class::calc::LowPassFilter

class calc::LowPassFilter
  : public calc::SimpleFilter< intmedium, LowPassFilter< intsmall, intmedium, intlarge > >

This class is a template for a simple low pass filter (also known as an exponential moving average) calculation.

The filter uses the following difference equation:

$$ y[n] = \alpha * x[n] + (1 - \alpha) * y[n] $$

Alpha is a value between 0 and 1. If alpha is 0, there is no filtering. If alpha is close to 1, the cutoff frequency is closer to zero. The alpha value and the sampling frequency both affect the cutoff frequency.

There are several classes that populate the template with reasonable values:

LowPassFilterF32 is the same filter but with floating point values.

The following is an example of using the Ema class using 32-bit integers.

#include <sapi/hal.hpp>
#include <sapi/calc.hpp>
#include <cstdio>

 //first initialize a filter with a value of 0 and with an averaging constant of 0.1
LowPassFilterS32 filter(0, LOW_PASS_FILTER_S32_ALPHA(0.1));
 //Or do:  LowPassFilter<u16, s32, s64> filter(0, LOW_PASS_FILTER_S32_ALPHA(0.1));

 //now use the ADC to get some data
Adc adc(0);
adc_sample_t sample;
adc.init(1<<0); //initialize with channel 0 enabled

for(i=0; i < 100; i++){  //take 100 samples and filter as we go
   adc.read(0, &sample, sizeof(sample));
   filter << sample;
   printf("%d %d %d;\n", i, sample, filter.present_value());
}

For more information on this filter see this wiki article.

Members

LowPassFilter(

intmedium start,
intsmall alpha
)

intmedium average() const
intmedium calculate(

intmedium in
)

intmedium present_value() const
void set_average(

intmedium v
)

static intmedium small_max()

Details

LowPassFilter(

intmedium start,
intsmall alpha
)

Constructs a new Ema object.

Parameters

  • start Initial value

  • alpha Averaging value


intmedium average() const

intmedium calculate(

intmedium in
)

Calculates the next average using an input value.

Parameters

  • in Input value

Returns

The updated average (same as average())


intmedium present_value() const

Accesses the current average (no calculations are made here).

Returns

The current average value


void set_average(

intmedium v
)

Sets the average value.

Parameters

  • v The new initial value

This method can also be used to set the initial value.


static intmedium small_max()

X

Thanks for Coming!

Subscribe to news and updates