pub struct ExponentialAverage<Val, Alpha, Beta> { /* private fields */ }Expand description
An exponential moving average and variance tracker using fixed-point values.
This structure tracks a value’s exponential moving average and its variance
using separate filtering coefficients (alpha for positive changes and beta
for negative changes, or a single coefficient for both).
Note: If V_FRAC is 0 (integer values), the variance update might lose significant
precision or decay to zero prematurely because internal calculations (like beta * delta^2)
are rounded to the nearest integer. Users should be encouraged to use types with
enough fractional bits for the accumulator if precision in the variance is important.
Additionally, because variance calculations involve squared delta terms (\Delta^2),
there is a high risk of saturation. It is strongly recommended to use larger integer
types (e.g. i64 or i128) as the underlying type if large input ranges or high variance
values are expected.
Implementations§
Source§impl<V, const V_FRAC: usize, A, const A_FRAC: usize, B, const B_FRAC: usize> ExponentialAverage<Fixed<V, V_FRAC>, Fixed<A, A_FRAC>, Fixed<B, B_FRAC>>where
V: Bounded + NumCast + Copy + PromoteAddSub<V> + Signed,
A: Bounded + NumCast + Copy + PromoteAddSub<A>,
B: Bounded + NumCast + Copy + PromoteAddSub<B>,
V::Output: Bounded + NumCast + Copy,
A::Output: Bounded + NumCast + Copy,
B::Output: Bounded + NumCast + Copy,
Fixed<V, V_FRAC>: Copy + Default + PartialOrd + AddAssign + Sub<Output = Fixed<V::Output, V_FRAC>> + Add<Output = Fixed<V::Output, V_FRAC>>,
Fixed<V::Output, V_FRAC>: Copy + PartialOrd,
Fixed<A, A_FRAC>: Copy + PartialOrd + Sub<Output = Fixed<A::Output, A_FRAC>>,
Fixed<B, B_FRAC>: Copy + PartialOrd + Sub<Output = Fixed<B::Output, B_FRAC>>,
Value<V, 0>: ConvertValue<V, V_FRAC>,
Value<V::Output, 0>: ConvertValue<V::Output, V_FRAC>,
Value<A, 0>: ConvertValue<A, A_FRAC>,
Value<B, 0>: ConvertValue<B, B_FRAC>,
impl<V, const V_FRAC: usize, A, const A_FRAC: usize, B, const B_FRAC: usize> ExponentialAverage<Fixed<V, V_FRAC>, Fixed<A, A_FRAC>, Fixed<B, B_FRAC>>where
V: Bounded + NumCast + Copy + PromoteAddSub<V> + Signed,
A: Bounded + NumCast + Copy + PromoteAddSub<A>,
B: Bounded + NumCast + Copy + PromoteAddSub<B>,
V::Output: Bounded + NumCast + Copy,
A::Output: Bounded + NumCast + Copy,
B::Output: Bounded + NumCast + Copy,
Fixed<V, V_FRAC>: Copy + Default + PartialOrd + AddAssign + Sub<Output = Fixed<V::Output, V_FRAC>> + Add<Output = Fixed<V::Output, V_FRAC>>,
Fixed<V::Output, V_FRAC>: Copy + PartialOrd,
Fixed<A, A_FRAC>: Copy + PartialOrd + Sub<Output = Fixed<A::Output, A_FRAC>>,
Fixed<B, B_FRAC>: Copy + PartialOrd + Sub<Output = Fixed<B::Output, B_FRAC>>,
Value<V, 0>: ConvertValue<V, V_FRAC>,
Value<V::Output, 0>: ConvertValue<V::Output, V_FRAC>,
Value<A, 0>: ConvertValue<A, A_FRAC>,
Value<B, 0>: ConvertValue<B, B_FRAC>,
Sourcepub fn new(
value: Fixed<V, V_FRAC>,
alpha: Fixed<A, A_FRAC>,
beta: Fixed<B, B_FRAC>,
) -> Self
pub fn new( value: Fixed<V, V_FRAC>, alpha: Fixed<A, A_FRAC>, beta: Fixed<B, B_FRAC>, ) -> Self
Creates a new ExponentialAverage tracker with separate positive (alpha)
and negative (beta) filtering coefficients.
alphais used when the new sample is greater than or equal to the current average (positive change).betais used when the new sample is less than the current average (negative change).
§Panics
Panics if alpha or beta is not within the range [0, 1].
Sourcepub fn add_sample(&mut self, sample: Fixed<V, V_FRAC>)
pub fn add_sample(&mut self, sample: Fixed<V, V_FRAC>)
Adds a new sample to the tracker, updating both the moving average and variance.
Source§impl<V, const V_FRAC: usize, A, const A_FRAC: usize> ExponentialAverage<Fixed<V, V_FRAC>, Fixed<A, A_FRAC>, Fixed<A, A_FRAC>>where
V: Bounded + NumCast + Copy + PromoteAddSub<V> + Signed,
A: Bounded + NumCast + Copy + PromoteAddSub<A>,
V::Output: Bounded + NumCast + Copy,
A::Output: Bounded + NumCast + Copy,
Fixed<V, V_FRAC>: Copy + Default + PartialOrd + AddAssign + Sub<Output = Fixed<V::Output, V_FRAC>> + Add<Output = Fixed<V::Output, V_FRAC>>,
Fixed<V::Output, V_FRAC>: Copy + PartialOrd,
Fixed<A, A_FRAC>: Copy + PartialOrd + Sub<Output = Fixed<A::Output, A_FRAC>>,
Value<V, 0>: ConvertValue<V, V_FRAC>,
Value<V::Output, 0>: ConvertValue<V::Output, V_FRAC>,
Value<A, 0>: ConvertValue<A, A_FRAC>,
impl<V, const V_FRAC: usize, A, const A_FRAC: usize> ExponentialAverage<Fixed<V, V_FRAC>, Fixed<A, A_FRAC>, Fixed<A, A_FRAC>>where
V: Bounded + NumCast + Copy + PromoteAddSub<V> + Signed,
A: Bounded + NumCast + Copy + PromoteAddSub<A>,
V::Output: Bounded + NumCast + Copy,
A::Output: Bounded + NumCast + Copy,
Fixed<V, V_FRAC>: Copy + Default + PartialOrd + AddAssign + Sub<Output = Fixed<V::Output, V_FRAC>> + Add<Output = Fixed<V::Output, V_FRAC>>,
Fixed<V::Output, V_FRAC>: Copy + PartialOrd,
Fixed<A, A_FRAC>: Copy + PartialOrd + Sub<Output = Fixed<A::Output, A_FRAC>>,
Value<V, 0>: ConvertValue<V, V_FRAC>,
Value<V::Output, 0>: ConvertValue<V::Output, V_FRAC>,
Value<A, 0>: ConvertValue<A, A_FRAC>,
Sourcepub fn new_single_rate(value: Fixed<V, V_FRAC>, alpha: Fixed<A, A_FRAC>) -> Self
pub fn new_single_rate(value: Fixed<V, V_FRAC>, alpha: Fixed<A, A_FRAC>) -> Self
Creates a new ExponentialAverage tracker using a single coefficient
for both positive and negative changes.