Skip to main content

Smoother

Struct Smoother 

Source
pub struct Smoother { /* private fields */ }
Expand description

Smooths a noisy signal with an exponential moving average.

Cheap sensors are noisy, and a single bad sample should not trip an alarm or flip an actuator. A Smoother dampens that jitter. The technique one layer down is an exponential moving average: each output is a weighted blend of the newest sample and the previous output, so recent readings count for more without storing a history buffer.

§Examples

use pamoja_kit::Smoother;

let mut smoother = Smoother::new(0.5);
assert_eq!(smoother.update(10.0), 10.0); // the first sample seeds the average
assert_eq!(smoother.update(0.0), 5.0); // then each output blends halfway

Implementations§

Source§

impl Smoother

Source

pub fn new(weight: f32) -> Self

Creates a smoother with the given responsiveness.

§Arguments
  • weight - how much the newest sample counts, clamped to [0.0, 1.0]. 1.0 disables smoothing so the output follows the input; values near 0.0 smooth heavily and react slowly.
§Returns

A smoother awaiting its first sample.

Source

pub fn update(&mut self, sample: f32) -> f32

Folds a new sample into the average and returns the smoothed value.

The first sample seeds the average and is returned unchanged.

§Arguments
  • sample - the latest raw reading.
§Returns

The smoothed value after including sample.

Source

pub fn value(&self) -> Option<f32>

Returns the current smoothed value, or None before the first sample.

§Returns

Some(value) once at least one sample has been seen, otherwise None.

Source

pub fn reset(&mut self)

Forgets the smoothed value so the next sample seeds the average afresh.

Trait Implementations§

Source§

impl Clone for Smoother

Source§

fn clone(&self) -> Smoother

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 Copy for Smoother

Source§

impl Debug for Smoother

Source§

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

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

impl PartialEq for Smoother

Source§

fn eq(&self, other: &Smoother) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Smoother

Auto Trait Implementations§

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> 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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

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

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.