Skip to main content

ExponentialAverage

Struct ExponentialAverage 

Source
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>,

Source

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.

  • alpha is used when the new sample is greater than or equal to the current average (positive change).
  • beta is 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].

Source

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

pub fn value(&self) -> Fixed<V, V_FRAC>

Returns the current moving average.

Source

pub fn variance(&self) -> Fixed<V, V_FRAC>

Returns the current 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>,

Source

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.

Auto Trait Implementations§

§

impl<Val, Alpha, Beta> Freeze for ExponentialAverage<Val, Alpha, Beta>
where Val: Freeze, Alpha: Freeze, Beta: Freeze,

§

impl<Val, Alpha, Beta> RefUnwindSafe for ExponentialAverage<Val, Alpha, Beta>
where Val: RefUnwindSafe, Alpha: RefUnwindSafe, Beta: RefUnwindSafe,

§

impl<Val, Alpha, Beta> Send for ExponentialAverage<Val, Alpha, Beta>
where Val: Send, Alpha: Send, Beta: Send,

§

impl<Val, Alpha, Beta> Sync for ExponentialAverage<Val, Alpha, Beta>
where Val: Sync, Alpha: Sync, Beta: Sync,

§

impl<Val, Alpha, Beta> Unpin for ExponentialAverage<Val, Alpha, Beta>
where Val: Unpin, Alpha: Unpin, Beta: Unpin,

§

impl<Val, Alpha, Beta> UnsafeUnpin for ExponentialAverage<Val, Alpha, Beta>
where Val: UnsafeUnpin, Alpha: UnsafeUnpin, Beta: UnsafeUnpin,

§

impl<Val, Alpha, Beta> UnwindSafe for ExponentialAverage<Val, Alpha, Beta>
where Val: UnwindSafe, Alpha: UnwindSafe, Beta: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = !

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.