Skip to main content

MovingAverage

Struct MovingAverage 

Source
pub struct MovingAverage<T, TCALC, const N: usize>
where T: Sized + PartialEq + TryFrom<TCALC, Error: Debug> + Clone + Copy, TCALC: Sized + Add<TCALC, Output = TCALC> + Sub<TCALC, Output = TCALC> + Div<Output = TCALC> + Mul<Output = TCALC> + PartialEq + PartialOrd + From<T> + TryFrom<usize, Error: Debug> + Clone + Copy,
{ /* private fields */ }
Expand description

§Intent

Creates a Moving Average filter for integer values, in a nostd context. The filter uses a minimal calculation approach, and does not sum the entire buffer when finding the average.

§Instantiating MovingAverage

The MovingAverage type is generic over three values:

  • T - the data type being averaged
  • TCALC - a larger data type for calculating the average
    • Must fit the value N * T::MAX
  • N - the depth of the average
    • Must be non-zero

§Example

use no_std_moving_average::MovingAverage;

let mut sut = MovingAverage::<u32, u64, 2>::new();
let first: u32 = 22;
let second: u32 = 44;
let third: u32 = 66;
let expected = (second + third) / 2;
let _ = sut.average(first);
let _ = sut.average(second);
let result = sut.average(third);

assert_eq!(expected, result);

§Static and Allocation Asserts

A combination of compile-time and allocation time assertions are used to ensure MovingAverage is instantiated correctly. Once instantiated, there are no known Panics when operating MovingAverage.

§T and TCALC must be Integer/Unsigned types

use no_std_moving_average::MovingAverage;
let _sut = MovingAverage::<f32, u64, 2>::new();
use no_std_moving_average::MovingAverage;
let _sut = MovingAverage::<u32, f64, 2>::new();
use no_std_moving_average::MovingAverage;
let _sut = MovingAverage::<f32, f64, 2>::new();

§TCALC must be larger than T

use no_std_moving_average::MovingAverage;
let _sut = MovingAverage::<u32, u32, 1>::new();

§N must be non-zero

use no_std_moving_average::MovingAverage;
let _sut = MovingAverage::<u32, u64, 0>::new();

§N * T::MAX must fit in TCALC

use no_std_moving_average::MovingAverage;
let _sut = MovingAverage::<u8, u16, 512>::new();

Implementations§

Source§

impl<T, TCALC, const N: usize> MovingAverage<T, TCALC, N>
where T: Sized + PartialEq + TryFrom<TCALC, Error: Debug> + Clone + Copy, TCALC: Sized + Add<TCALC, Output = TCALC> + Sub<TCALC, Output = TCALC> + Div<Output = TCALC> + Mul<Output = TCALC> + PartialEq + PartialOrd + From<T> + TryFrom<usize, Error: Debug> + Clone + Copy,

Source

pub fn new() -> Self

Source

pub fn average(&mut self, input: T) -> T

§Panics

Panics if unable to convert from TCALC to T. This panic should never occur due to compile-time assert checks.

Trait Implementations§

Source§

impl<T, TCALC, const N: usize> Default for MovingAverage<T, TCALC, N>
where T: Sized + PartialEq + TryFrom<TCALC, Error: Debug> + Clone + Copy, TCALC: Sized + Add<TCALC, Output = TCALC> + Sub<TCALC, Output = TCALC> + Div<Output = TCALC> + Mul<Output = TCALC> + PartialEq + PartialOrd + From<T> + TryFrom<usize, Error: Debug> + Clone + Copy,

§Panics

Panics if TCALC not larger than T, compile-time assert. Panics if N is zero, compile-time assert. : These panics should never occur due to compile-time assert checks. Panics if unable to convert from usize to TCALC. Panics if N * T::MAX won’t fit in TCALC. : These panics happen at allocation time, so should be found predictably.

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<T, TCALC, const N: usize> UnwindSafe for MovingAverage<T, TCALC, N>
where TCALC: UnwindSafe, 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> 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<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<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.