template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
class FilteredAnalog< Precision, FilterShiftFactor, FilterType, Upsample >
A class that reads and filters an analog input.
A map function can be applied to the analog value (e.g. to compensate for logarithmic taper potentiometers or to calibrate the range). The analog input value is filtered using an exponential moving average filter. The default settings for this filter can be changed in Settings.hpp.
After filtering, hysteresis is applied to prevent flipping back and forth between two values when the input is not changing.
- Template Parameters
-
Precision | The number of bits of precision the output should have. |
FilterShiftFactor | The number of bits used for the EMA filter. The pole location is \( 1 - \left(\frac{1}{2}\right)^{\mathrm{FilterShiftFactor}} \).
A lower shift factor means less filtering ( \(0\) is no filtering), and a higher shift factor means more filtering (and more latency). |
FilterType | The type to use for the intermediate types of the filter.
Should be at least \( 10 + \mathrm{Upsample} + \mathrm{FilterShiftFactor} \) bits ( \(10\) is the number of bits of the ADC). |
Upsample | The number of bits to upsample the analog reading by. |
- Examples
- 2.VU-Meter-OLED-USB-DAC.ino, and FilteredAnalog.ino.
Definition at line 47 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
Specify a mapping function that is applied to the raw analog value before filtering.
- Parameters
-
fn | A function pointer to the mapping function. This function should take the filtered value (of ADC_BITS + Upsample bits wide) as a parameter, and should return a value of ADC_BITS + Upsample bits wide. |
- Note
- Applying the mapping function before filtering could result in the noise being amplified to such an extent that filtering it afterwards would be ineffective.
Applying it after hysteresis would result in a lower resolution.
That's why the mapping function is applied after filtering and before hysteresis.
Definition at line 74 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
void FilteredAnalog< Precision, FilterShiftFactor, FilterType, Upsample >::invert |
( |
| ) |
|
|
inline |
Invert the analog value.
For example, if the precision is 10 bits, when the analog input measures 1023, the output will be 0, and when the analog input measures 0, the output will be 1023.
- Note
- This overrides the mapping function set by the
map
method.
Definition at line 83 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
bool FilteredAnalog< Precision, FilterShiftFactor, FilterType, Upsample >::update |
( |
| ) |
|
|
inline |
Read the analog input value, apply the mapping function, and update the average.
- Return values
-
true | The value changed since last time it was updated. |
false | The value is still the same. |
Definition at line 97 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
Get the filtered value of the analog input with the mapping function applied.
- Returns
- The filtered value of the analog input, as a number of
Precision
bits wide.
Definition at line 113 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
float FilteredAnalog< Precision, FilterShiftFactor, FilterType, Upsample >::getFloatValue |
( |
| ) |
const |
|
inline |
Get the filtered value of the analog input with the mapping function applied as a floating point number from 0.0 to 1.0.
- Returns
- The filtered value of the analog input, as a number from 0.0 to 1.0.
Definition at line 122 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(analog_t) * CHAR_BIT - ADC_BITS)>
Read the raw value of the analog input any filtering or mapping applied, but with its bit depth increased by Upsample
.
Definition at line 130 of file FilteredAnalog.hpp.