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 halfwayImplementations§
Source§impl Smoother
impl Smoother
Sourcepub fn new(weight: f32) -> Self
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.0disables smoothing so the output follows the input; values near0.0smooth heavily and react slowly. A weight that is not a number is taken as1.0.
§Returns
A smoother awaiting its first sample.
Sourcepub fn update(&mut self, sample: f32) -> f32
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. A sample that is not a finite number, such as the NaN a failed sensor reports, is ignored: the average stays where it was and is returned as it stood.
§Arguments
sample- the latest raw reading.
§Returns
The smoothed value after including sample, or the sample itself when it is not a
finite number and no real one has arrived yet.