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

pub struct Median<T, const N: usize> { /* fields omitted */ }
Expand description

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

Implementations

Returns the window size of the filter.

Returns true if the filter’s buffer is empty, false otherwise.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

The filter’s output type.

Processes the input value, returning a corresponding output.

Constructs a value from its guts.

The type’s guts.

Destructures a value into its guts.

Returns an instance with a freshly reset internal state.

The filter’s internal state.

Returns a mutable reference to the internal state of the filter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Constructs a value from its guts, without checking invariants.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.