Skip to main content

Trend

Struct Trend 

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

Tracks the linear trend of the most recent N readings.

Is a value rising or falling, and how fast? A Trend fits a least-squares straight line to the last N readings, taken as evenly spaced in time, and reports its slope in units per sample: positive when rising, negative when falling, near zero when flat. Because the fit uses the whole window, a single noisy reading does not masquerade as a trend the way a bare difference between two samples can. The slope is the ordinary least-squares estimate, the sum of (x - mean_x) * (y - mean_y) over the sum of (x - mean_x) squared, with x taken as the sample index 0, 1, 2, and so on.

§Examples

use pamoja_kit::Trend;

// A tank level falling two units per reading.
let mut level = Trend::<4>::new();
for reading in [40.0, 38.0, 36.0, 34.0] {
    level.push(reading);
}
assert!((level.slope().unwrap() + 2.0).abs() < 1e-4);

Implementations§

Source§

impl<const N: usize> Trend<N>

Source

pub fn new() -> Self

Creates an empty trend tracker over a window of N readings.

§Returns

A tracker holding no readings yet.

Source

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

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

§Arguments
  • reading - the latest reading, taken one sample interval after the previous one.
Source

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

Returns the trend slope in units per sample, or None with fewer than two readings.

§Returns

The least-squares slope of the window: positive when rising, negative when falling. None until at least two readings are present, since a single point has no slope.

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 tracker holds no readings.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Trend<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 Trend<N>

Source§

impl<const N: usize> Debug for Trend<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 Trend<N>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Trend<N>

§

impl<const N: usize> RefUnwindSafe for Trend<N>

§

impl<const N: usize> Send for Trend<N>

§

impl<const N: usize> Sync for Trend<N>

§

impl<const N: usize> Unpin for Trend<N>

§

impl<const N: usize> UnsafeUnpin for Trend<N>

§

impl<const N: usize> UnwindSafe for Trend<N>

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 = 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.