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
- Remove node at current cursor (
▶︎
) from linked list, if it exists. (by re-wiring its predecessor to its successor). - Initialize
current
andmedian
index to first node of linked list (▷
). - Walk through linked list, searching for insertion point.
- Shift median index on every other hop (thus ending up in the list’s median).
- Insert value into ring buffer and linked list respectively.
- Update index to linked list’s first node, if necessary.
- Update ring buffer’s cursor.
- Return median value.
(Based on Phil Ekstrom, Embedded Systems Programming, November 2000.)
Implementations
Returns the filter buffer’s current median value, panicking if empty.
Returns the filter buffer’s current min value, panicking if empty.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more
pub unsafe fn from_guts_unchecked(guts: <T as Guts>::Guts) -> T
pub unsafe fn from_guts_unchecked(guts: <T as Guts>::Guts) -> T
Constructs a value from its guts, without checking invariants.