# 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.

//md2code:include
#include <sapi/hal.hpp>
#include <sapi/calc.hpp>

//md2code:main
//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
u16 sample;

for(u32 i=0; i < 100; i++){  //take 100 samples and filter as we go
arg::Location(0),
arg::DestinationBuffer(&sample),
arg::Size(sizeof(sample))
);
filter << sample;
printf("%ld %d %ld;\n", i, sample, filter.present_value());
}


### Summary

[LowPassFilter](#classcalc_1_1_low_pass_filter_1a84a5a6a88502beb477896b1b267c4498)(


intmedium start,
intsmall alpha
)

intmedium average() const

intmedium calculate(

intmedium in
)

intmedium present_value() const

void set_average(

intmedium v
)

static intmedium small_max()

### Members

##### LowPassFilter(

intmedium start,
intsmall alpha
)

Constructs a new Ema object.

#### Parameters

• start Initial value

• alpha Averaging value

##### 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.

X