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>
impl<T: TemporalSample, const N: usize> MovingAverage<T, N>
Sourcepub const fn new() -> Self
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}Trait Implementations§
Source§impl<T: Clone + TemporalSample, const N: usize> Clone for MovingAverage<T, N>
impl<T: Clone + TemporalSample, const N: usize> Clone for MovingAverage<T, N>
Source§fn clone(&self) -> MovingAverage<T, N>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug + TemporalSample, const N: usize> Debug for MovingAverage<T, N>
impl<T: Debug + TemporalSample, const N: usize> Debug for MovingAverage<T, N>
Source§impl<T: TemporalSample, const N: usize> Default for MovingAverage<T, N>
impl<T: TemporalSample, const N: usize> Default for MovingAverage<T, N>
Source§impl<T: TemporalSample, const N: usize> TemporalFilter<T> for MovingAverage<T, N>
impl<T: TemporalSample, const N: usize> TemporalFilter<T> for MovingAverage<T, N>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Performs the conversion.
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Performs the conversion.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> StrictAs for T
impl<T> StrictAs for T
Source§fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
Casts the value.
Source§impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
Source§fn strict_cast_from(src: Src) -> Dst
fn strict_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.