Skip to main content

MovingAverage

Struct MovingAverage 

Source
pub struct MovingAverage<T: TemporalSample, const N: usize> { /* private fields */ }
Expand description

Exact fixed-window moving average.

Updates are O(1) using a checked-range i64 running sum. Output begins only after all N samples have been supplied.

The per-type window cap on TemporalSample is an accumulator-safety ceiling so N copies of the widest sample still fit in i64. It is not a practical size: storage is [T; N] plus that sum, and firmware chooses N from available RAM.

Implementations§

Source§

impl<T: TemporalSample, const N: usize> MovingAverage<T, N>

Source

pub const fn new() -> Self

Construct an empty moving average.

§Panics

Panics for a zero-sized window or a window larger than the accumulator-safety ceiling of T (see TemporalSample). That ceiling is not a recommended window size; N is fixed storage.

Examples found in repository?
examples/no_std_generated_fixtures.rs (line 44)
23pub fn runtime_api_smoke(code: u16, sample: u32) -> (i32, u16, u32, bool) {
24    let transfer = PiecewiseLinearTransfer::new(
25        &SMOKE_INPUTS,
26        &SMOKE_OUTPUTS,
27        MonotonicDirection::Increasing,
28    );
29    let calibrated = match AffineCalibration::new(transfer, 1_001, 0, 1_000) {
30        Ok(value) => value,
31        Err(_) => unreachable!(),
32    };
33    let physical = calibrated.convert(code).unwrap_or_default();
34    let inverse = calibrated.invert(physical).unwrap_or_default();
35
36    let affine = match AffineTransform::new(1_001, 0, 1_000) {
37        Ok(value) => value,
38        Err(_) => unreachable!(),
39    };
40    let _ = affine
41        .apply(physical)
42        .and_then(|value| affine.unapply(value));
43
44    let mut average = MovingAverage::<u32, 1>::new();
45    let filtered = match average.update(sample) {
46        FilterOutput::Ready(value) => value,
47        FilterOutput::WarmingUp { .. } => unreachable!(),
48    };
49    let mut latch = Hysteresis::<u32>::new(100, 200);
50    (physical, inverse, filtered, latch.update(filtered))
51}
Source

pub const fn len(&self) -> usize

Number of retained samples.

Source

pub const fn is_empty(&self) -> bool

Whether no samples are retained.

Trait Implementations§

Source§

impl<T: Clone + TemporalSample, const N: usize> Clone for MovingAverage<T, N>

Source§

fn clone(&self) -> MovingAverage<T, N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + TemporalSample, const N: usize> Debug for MovingAverage<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: TemporalSample, const N: usize> Default for MovingAverage<T, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: TemporalSample, const N: usize> TemporalFilter<T> for MovingAverage<T, N>

Source§

fn update(&mut self, value: T) -> FilterOutput<T>

Push one sample and return the current filter state.
Source§

fn reset(&mut self)

Discard all retained history.

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for MovingAverage<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for MovingAverage<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for MovingAverage<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for MovingAverage<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for MovingAverage<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for MovingAverage<T, N>
where T: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for MovingAverage<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.