Skip to main content

Median

Struct Median 

Source
pub struct Median<const N: usize> { /* private fields */ }
Expand description

A median filter over the most recent N readings.

A single bad sample - a spike from electrical noise or a flaky contact - drags a mean or an exponential average off course, because it is blended into the result. The median ignores it: one outlier cannot move the middle value of a sorted window. That makes a Median the right filter when the noise is occasional spikes rather than steady jitter; for steady jitter reach for Smoother instead. Keep the window small and odd (3, 5, 7) so there is a single middle reading; with an even N the median is the average of the two middle readings.

§Examples

use pamoja_kit::Median;

let mut filtered = Median::<5>::new();
// A lone spike among steady readings is rejected.
for reading in [10.0, 10.0, 99.0, 10.0, 10.0] {
    filtered.update(reading);
}
assert_eq!(filtered.median(), Some(10.0));

Implementations§

Source§

impl<const N: usize> Median<N>

Source

pub fn new() -> Self

Creates an empty median filter over a window of N readings.

§Returns

A filter holding no readings yet.

Source

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

Adds a reading and returns the median of the current window.

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

The median of the readings now in the window. With a zero-length window (N is 0) the reading passes through unchanged.

Source

pub fn push(&mut self, reading: f32)

Adds a reading to the window, evicting the oldest once it is full.

§Arguments
  • reading - the latest raw reading.
Source

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

Returns the median of the readings in the window, or None if it is empty.

§Returns

The middle reading of the sorted window for an odd count, the average of the two middle readings for an even count, or None before any reading.

Source

pub fn len(&self) -> usize

Returns the number of readings currently held, at most N.

Source

pub fn is_empty(&self) -> bool

Returns true if the filter holds no readings.

Trait Implementations§

Source§

impl<const N: usize> Clone for Median<N>

Source§

fn clone(&self) -> Median<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<const N: usize> Copy for Median<N>

Source§

impl<const N: usize> Debug for Median<N>

Source§

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

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

impl<const N: usize> Default for Median<N>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Median<N>
where [f32; N]: Freeze,

§

impl<const N: usize> RefUnwindSafe for Median<N>
where [f32; N]: RefUnwindSafe,

§

impl<const N: usize> Send for Median<N>
where [f32; N]: Send,

§

impl<const N: usize> Sync for Median<N>
where [f32; N]: Sync,

§

impl<const N: usize> Unpin for Median<N>
where [f32; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for Median<N>
where [f32; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for Median<N>
where [f32; N]: 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> 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.