Struct signalo_filters::filter::median::Median[][src]

pub struct Median<T, N> where
    N: ArrayLength<ListNode<T>>, 
{ /* fields omitted */ }

An implementation of a median filter of fixed width with linear complexity.

While the common naïve implementation of a median filter has a worst-case complexity of O(n^2) (due to having to sort the sliding window) the use of a combination of linked list and ring buffer allows for a worst-case complexity of O(n).

Implementation

The algorithm makes use of a ring buffer of the same size as its filter window. Inserting values into the ring buffer appends them to a linked list that is embedded inside said ring buffer (using relative integer jump offsets as links).

Example

Given a sequence of values [3, 2, 4, 6, 5, 1] and a buffer of size 5, the buffer would be filled like this:

new(5)  consume(3)  consume(2)  consume(4)  consume(6)  consume(5)  consume(1)
▶︎[ ]      ▷[3]       ┌→[3]       ┌→[3]─┐     ┌→[3]─┐    ▶︎┌→[3]─┐      ▷[1]─┐
 [ ]      ▶︎[ ]      ▷└─[2]      ▷└─[2] │    ▷└─[2] │    ▷└─[2] │    ▶︎┌─[2]←┘
 [ ]       [ ]        ▶︎[ ]         [4]←┘     ┌─[4]←┘     ┌─[4]←┘     └→[4]─┐
 [ ]       [ ]         [ ]        ▶︎[ ]       └→[6]       │ [6]←┐     ┌→[6] │
 [ ]       [ ]         [ ]         [ ]        ▶︎[ ]       └→[5]─┘     └─[5]←┘

Algorithm

  1. Remove node at current cursor (▶︎) from linked list, if it exists. (by re-wiring its predecessor to its successor).
  2. Initialize current and median index to first node of linked list ().
  3. Walk through linked list, searching for insertion point.
  4. Shift median index on every other hop (thus ending up in the list's median).
  5. Insert value into ring buffer and linked list respectively.
  6. Update index to linked list's first node, if necessary.
  7. Update ring buffer's cursor.
  8. Return median value.

(Based on Phil Ekstrom, Embedded Systems Programming, November 2000.)

Methods

impl<T, N> Median<T, N> where
    T: Clone + PartialOrd,
    N: ArrayLength<ListNode<T>>, 
[src]

Creates a new median filter with a given window size.

Returns the window size of the filter.

Returns the filter buffer's current median value, panicking if empty.

Returns the filter buffer's current min value, panicking if empty.

Returns the filter buffer's current max value, panicking if empty.

Trait Implementations

impl<T, N> Clone for Median<T, N> where
    T: Clone,
    N: ArrayLength<ListNode<T>>, 
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T, N> Debug for Median<T, N> where
    T: Debug,
    N: ArrayLength<ListNode<T>>, 
[src]

Formats the value using the given formatter. Read more

impl<T, N> Filter<T> for Median<T, N> where
    T: Clone + PartialOrd,
    N: ArrayLength<ListNode<T>>, 
[src]

The filter's output type.

Processes the input value, returning a corresponding output.

Auto Trait Implementations

impl<T, N> Send for Median<T, N> where
    T: Send

impl<T, N> Sync for Median<T, N> where
    T: Sync