Skip to main content

dspk_ops/core/
edge.rs

1//! What a stencil reads past either end of a row.
2//!
3//! Defaults follow the scipy function each kernel mirrors (`sosfiltfilt`: odd, `lfilter`: zeros,
4//! `ndimage.gaussian_filter1d`: reflect, `signal.medfilt`: zeros).
5
6/// Extension of a row `x[0..n]` beyond its ends (shown for the left end; the right end mirrors it).
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum EdgeMode {
9    /// `0 0 0 | x0 x1 x2` (scipy `constant` with `cval = 0`).
10    Zeros,
11    /// Point reflection through the edge sample: `2x0−x3 2x0−x2 2x0−x1 | x0 x1 x2` (scipy
12    /// `padtype="odd"`). Keeps the level and slope continuous, so filters start without a step.
13    Odd,
14    /// Mirror about the edge, repeating it: `x2 x1 x0 | x0 x1 x2` (scipy `reflect`).
15    Reflect,
16    /// Repeat the edge sample: `x0 x0 x0 | x0 x1 x2` (scipy `nearest`).
17    Nearest,
18}
19
20impl EdgeMode {
21    /// The comptime id kernels branch on.
22    pub const fn id(self) -> u32 {
23        match self {
24            EdgeMode::Zeros => EDGE_ZEROS,
25            EdgeMode::Odd => EDGE_ODD,
26            EdgeMode::Reflect => EDGE_REFLECT,
27            EdgeMode::Nearest => EDGE_NEAREST,
28        }
29    }
30}
31
32pub const EDGE_ZEROS: u32 = 0;
33pub const EDGE_ODD: u32 = 1;
34pub const EDGE_REFLECT: u32 = 2;
35pub const EDGE_NEAREST: u32 = 3;
36