Trait simple_moving_average::SMA[][src]

pub trait SMA<Sample, Divisor, const WINDOW_SIZE: usize> {
    fn add_sample(&mut self, new_sample: Sample);
fn get_average(&self) -> Sample;
fn get_num_samples(&self) -> usize;
fn get_sample_window_size(&self) -> usize;
fn get_sample_window_iter(&self) -> Iter<'_, Sample, WINDOW_SIZE>
Notable traits for Iter<'a, Item, CAPACITY>
impl<'a, Item, const CAPACITY: usize> Iterator for Iter<'a, Item, CAPACITY> type Item = &'a Item;
;
fn get_most_recent_sample(&self) -> Option<Sample>; }
Expand description

This trait provides an common interface for algorithms that can calculate a simple moving average.

In this crate, a simple moving average is defined as sum(window(samples, N)) / length(window(samples, N)). Here samples is a possibly infinite series of samples. The window function extracts the last N of those samples.

Implementation detail: For the purposes of this library, there is no point in keeping samples outside the sample window around, so they are discarded when newer samples push them out of the window. This allows the implementations to have constant memory requirements and be stack allocated.

Terminology:

  • Sample: A data point, a value.
  • Sample window: The subset of all samples used for average calculations.

Required methods

Adds a sample to the series of samples. If the sample window is full, this will cause the oldest sample to be dropped, i.e. no longer contribute to the average.

Returns the simple moving average value of all the samples in the sample window.

Returns the total number of samples currently in the in the sample window. This value never exceeds the sample window size.

Returns the maximum number of samples that fit in the sample window.

Returns the most recently added sample, if any.

Implementors