### One Euro Filter Usage Example Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/one_euro.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates the basic usage of the One Euro filter for smoothing a signal. It shows how to initialize the filter and apply it to data points, asserting that the output is finite. ```rust let mut f = OneEuroFilter::new(1.0, 0.0); f.filter(1.0, 0.0); let v = f.filter(2.0, 0.0); // Should not panic or produce NaN assert!(v.is_finite()); ``` -------------------------------- ### One Euro Filter Usage Example Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/one_euro.rs.html?search= Demonstrates the basic usage of the One Euro filter for smoothing a signal. It shows how to initialize the filter and apply it to a data point, asserting that the output is finite. ```rust f.filter(1.0, 0.0); let v = f.filter(2.0, 0.0); // Should not panic or produce NaN assert!(v.is_finite()); ``` -------------------------------- ### apply_deadzone_symmetric Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone_symmetric.html?search=u32+-%3E+bool Applies a deadzone symmetrically to an f32 value. The output starts at 0 just outside the deadzone and reaches the original magnitude at full range. ```APIDOC ## Function apply_deadzone_symmetric ### Description Apply deadzone symmetrically: remap so that the output starts at 0 just outside the deadzone and reaches the original magnitude at full range. ### Signature ```rust pub fn apply_deadzone_symmetric(value: f32, deadzone: f32) -> f32 ``` ### Parameters * **value** (*f32*) - The input value to apply the deadzone to. * **deadzone** (*f32*) - The deadzone threshold. Values within `[-deadzone, deadzone]` will be clamped to 0. ### Returns * (*f32*) - The value after the deadzone has been applied. ``` -------------------------------- ### apply_deadzone_symmetric Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone_symmetric.html?search= Applies a symmetric deadzone to a floating-point value. The output is remapped so that it starts at 0 just outside the deadzone and reaches the original magnitude at full range. ```APIDOC ## Function apply_deadzone_symmetric signal_smooth::deadzone ### Description Apply deadzone symmetrically: remap so that the output starts at 0 just outside the deadzone and reaches the original magnitude at full range. ### Signature ```rust pub fn apply_deadzone_symmetric(value: f32, deadzone: f32) -> f32 ``` ### Parameters * **value** (f32) - The input floating-point value. * **deadzone** (f32) - The deadzone threshold. ### Returns (f32) - The value after applying the symmetric deadzone. ``` -------------------------------- ### apply_deadzone_symmetric Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone_symmetric.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Applies a deadzone symmetrically to a floating-point value. The output is remapped such that it starts at 0 just outside the deadzone and reaches the original magnitude at full range. ```APIDOC ## Function apply_deadzone_symmetric ### Description Apply deadzone symmetrically: remap so that the output starts at 0 just outside the deadzone and reaches the original magnitude at full range. ### Signature ```rust pub fn apply_deadzone_symmetric(value: f32, deadzone: f32) -> f32 ``` ### Parameters * **value** (f32) - The input floating-point value. * **deadzone** (f32) - The deadzone threshold. ### Returns * (f32) - The value after applying the symmetric deadzone. ``` -------------------------------- ### Apply Symmetric Deadzone Remapping Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/deadzone.rs.html?search= This function applies a deadzone symmetrically by remapping values. Output starts at 0 just outside the deadzone and reaches the original magnitude at full range. Handles negative values correctly. ```rust pub fn apply_deadzone_symmetric(value: f32, deadzone: f32) -> f32 { if deadzone <= 0.0 { return value; } let abs_val = value.abs(); if abs_val <= deadzone { 0.0 } else { // Remap [deadzone, 1.0] → [0.0, 1.0] let sign = value.signum(); let remapped = (abs_val - deadzone) / (1.0 - deadzone).max(1e-6); remapped * sign } } ``` -------------------------------- ### Apply Symmetric Deadzone Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone_symmetric.html?search= This function remaps a floating-point value by applying a symmetric deadzone. The output starts at 0 just outside the deadzone and reaches the original magnitude at full range. ```rust pub fn apply_deadzone_symmetric(value: f32, deadzone: f32) -> f32 ``` -------------------------------- ### apply_deadzone_symmetric Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/index.html?search= Applies a deadzone symmetrically. This function remaps the input value such that the output starts at 0 just outside the deadzone and reaches the original magnitude at the full range. ```APIDOC ## Function apply_deadzone_symmetric ### Description Apply deadzone symmetrically: remap so that the output starts at 0 just outside the deadzone and reaches the original magnitude at full range. ### Parameters * **value**: The input value to apply the deadzone to. * **deadzone**: The size of the deadzone. Values within this range around zero will be affected symmetrically. ### Returns The value after applying the symmetric deadzone. ### Example ```rust let value = -0.7; let deadzone = 0.2; let smoothed_value = signal_smooth::deadzone::apply_deadzone_symmetric(value, deadzone); ``` ``` -------------------------------- ### One Euro Filter Initialization and Usage Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/one_euro.rs.html?search=u32+-%3E+bool Demonstrates how to initialize and use the One Euro filter for signal smoothing. It shows applying the filter to a signal and asserting the validity of the output. ```rust let mut f = OneEuroFilter::new(1.0, 0.0, 1.0, 1.0); f.filter(1.0, 0.0); let v = f.filter(2.0, 0.0); // Should not panic or produce NaN assert!(v.is_finite()); ``` -------------------------------- ### Get Type ID of Ema Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Gets the `TypeId` of the Ema struct. This is part of blanket implementations for `Any`. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Get Current DampedSpring Velocity Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/spring/struct.DampedSpring.html?search=std%3A%3Avec Retrieves the current velocity of the DampedSpring. ```rust pub fn velocity(&self) -> f32 ``` -------------------------------- ### Get Current Ema Value Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Retrieves the current smoothed value of the EMA. ```rust pub fn value(&self) -> f32 ``` -------------------------------- ### TryFrom u32 to bool for OneEuroFilter Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search=u32+-%3E+bool Demonstrates the `try_from` method for the OneEuroFilter, specifically showing the conversion from a `u32` type to a `bool` type. This is part of blanket implementations. ```rust u32 matches **U** bool matches **T** ``` -------------------------------- ### Ema::new Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a new EMA instance. The first sample provided to the `update` method will be passed through without smoothing. ```APIDOC ## Ema::new ### Description Create a new EMA. The first sample will be passed through. ### Signature `pub fn new(initial: f32) -> Self` ### Parameters * `initial` (f32) - The initial value for the EMA. ``` -------------------------------- ### Type Conversion with u32 and bool Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone_symmetric.html?search=u32+-%3E+bool Demonstrates type conversion patterns using generic parameters U and T, where u32 is matched with U and bool with T. This is shown in the context of `try_from` methods for Ema, OneEuroFilter, and DampedSpring. ```rust u32 matches U bool matches T ``` -------------------------------- ### Type Conversion with try_from (u32 to bool) Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html?search=u32+-%3E+bool Demonstrates the use of the `try_from` method for converting a `u32` type to a `bool` type within different smoothing algorithms. This is typically used for initializing or configuring filters. ```rust method signal_smooth::ema::Ema::try_from **U** -> Result<**T** > where u32 matches **U** bool matches **T** ``` ```rust method signal_smooth::one_euro::OneEuroFilter::try_from **U** -> Result<**T** > where u32 matches **U** bool matches **T** ``` ```rust method signal_smooth::spring::DampedSpring::try_from **U** -> Result<**T** > where u32 matches **U** bool matches **T** ``` -------------------------------- ### Create New Ema Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Creates a new EMA instance. The first sample provided will be passed through directly. ```rust pub fn new(initial: f32) -> Self ``` -------------------------------- ### OneEuroFilter::with_d_cutoff Constructor Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/one_euro.rs.html Creates a new OneEuroFilter instance with explicit settings for minimum cutoff frequency, beta, and derivative cutoff. ```rust pub fn with_d_cutoff(min_cutoff: f32, beta: f32, d_cutoff: f32) -> Self { Self { min_cutoff, beta, d_cutoff, x_prev: None, dx_prev: 0.0, } } ``` -------------------------------- ### Create OneEuroFilter with Min Cutoff and Beta Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search=u32+-%3E+bool Creates a new OneEuroFilter instance with the specified minimum cutoff frequency in Hz and the beta parameter. This is the primary constructor for the filter. ```rust pub fn new(min_cutoff: f32, beta: f32) -> Self ``` -------------------------------- ### Fast Negative Exponential Approximation Tests in Rust Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html?search= Unit tests for the `fast_negexp` function, verifying its behavior at zero, for large inputs, for negative inputs, and checking its accuracy against the standard exponential function. ```Rust #[cfg(test)] mod tests { use super::*; #[test] fn test_fast_negexp_zero() { assert!((fast_negexp(0.0) - 1.0).abs() < 1e-6); } #[test] fn test_fast_negexp_large() { assert_eq!(fast_negexp(100.0), 0.0); } #[test] fn test_fast_negexp_negative() { assert!((fast_negexp(-1.0) - 1.0).abs() < 1e-6); } #[test] fn test_fast_negexp_accuracy() { // Check at x=1: e^-1 ≈ 0.3679 let result = fast_negexp(1.0); let expected = (-1.0f32).exp(); assert!( (result - expected).abs() < 0.02, "fast_negexp(1.0)={} vs exp(-1)={}", result, expected ); } } ``` -------------------------------- ### OneEuroFilter Methods Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search=u32+-%3E+bool Provides methods for creating, filtering, and managing the state of the OneEuroFilter. ```APIDOC ## OneEuroFilter ### Description 1-Euro filter for adaptive low-pass smoothing. Smooths slow movements aggressively (low cutoff) while passing through fast movements with minimal lag (high cutoff). Ideal for face tracking where jitter during stillness is undesirable but fast motions must track. Reference: Casiez et al., “1-Euro Filter: A Simple Speed-based Low-pass Filter for Noisy Input in Interactive Systems”, CHI 2012. ### Methods #### `new(min_cutoff: f32, beta: f32) -> Self` Create a new filter with the given min cutoff (Hz) and beta. #### `with_d_cutoff(min_cutoff: f32, beta: f32, d_cutoff: f32) -> Self` Create with explicit derivative cutoff. #### `filter(&mut self, x: f32, dt: f32) -> f32` Filter a new sample. `dt` is the time since the last sample in seconds. #### `value(&self) -> f32` Current filtered value, or 0.0 if no samples yet. #### `reset(&mut self)` Reset the filter state. ``` -------------------------------- ### Modules Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth?search= The signal_smooth crate is organized into several modules, each containing related functionality for signal processing. ```APIDOC ## Modules - **`deadzone`**: Contains functions for applying deadzones to signals. - **`ema`**: Provides functionality for Exponential Moving Average filters. - **`one_euro`**: Implements the One Euro filter for signal smoothing. - **`spring`**: Offers components related to damped spring systems for signal processing. - **`util`**: Includes utility functions for signal manipulation. ``` -------------------------------- ### Test Fast Negative Exponential Approximation for Negative Input Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html Verifies that the fast_negexp function returns approximately 1.0 when the input is negative (-1.0). ```Rust #[test] fn test_fast_negexp_negative() { assert!((fast_negexp(-1.0) - 1.0).abs() < 1e-6); } ``` -------------------------------- ### Ema Struct Methods Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=u32+-%3E+bool Provides documentation for the core methods of the Ema struct, including creation, updating with frame-rate independence, and value manipulation. ```APIDOC ## Struct Ema signal_smooth::ema ### Description Frame-rate independent exponential moving average. Uses a halflife parameter (in seconds) so the smoothing behaves identically regardless of frame rate. ### Methods #### `new(initial: f32) -> Self` Create a new EMA. The first sample will be passed through. #### `uninit() -> Self` Create an uninitialized EMA. The first call to `update` sets the value. #### `update(&mut self, target: f32, halflife: f32, dt: f32) -> f32` Feed a new sample. `halflife` is in seconds, `dt` is frame delta in seconds. #### `value(&self) -> f32` Current smoothed value. #### `set(&mut self, value: f32)` Hard-set the value. ``` -------------------------------- ### Update Ema with Sample Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Feeds a new sample into the EMA for smoothing. Requires the target value, halflife in seconds, and the frame delta time (dt) in seconds. ```rust pub fn update(&mut self, target: f32, halflife: f32, dt: f32) -> f32 ``` -------------------------------- ### Create OneEuroFilter with Explicit Derivative Cutoff Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search=u32+-%3E+bool Creates a new OneEuroFilter instance with explicit control over the derivative cutoff frequency, in addition to the minimum cutoff and beta parameters. ```rust pub fn with_d_cutoff(min_cutoff: f32, beta: f32, d_cutoff: f32) -> Self ``` -------------------------------- ### Test Fast Negative Exponential Approximation at Zero Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html Verifies that the fast_negexp function returns approximately 1.0 when the input is 0.0. ```Rust #[test] fn test_fast_negexp_zero() { assert!((fast_negexp(0.0) - 1.0).abs() < 1e-6); } ``` -------------------------------- ### Test: First Sample Passthrough Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/one_euro.rs.html Verifies that the first sample passed to the filter is returned directly without any smoothing. ```rust #[test] fn test_first_sample_passthrough() { let mut f = OneEuroFilter::new(1.0, 0.0); let v = f.filter(5.0, 0.016); assert!((v - 5.0).abs() < 1e-6); } ``` -------------------------------- ### signal_smooth Crate Modules and Exports Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/lib.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This snippet shows the main module declarations and public re-exports from the lib.rs file of the signal_smooth crate. It includes imports for deadzone, ema, one_euro, spring, and util modules, and re-exports key types and functions. ```rust #![no_std] pub mod deadzone; pub mod ema; pub mod one_euro; pub mod spring; pub mod util; pub use deadzone::{apply_deadzone, apply_deadzone_symmetric}; pub use ema::Ema; pub use one_euro::OneEuroFilter; pub use spring::DampedSpring; pub use util::fast_negexp; ``` -------------------------------- ### Test Fast Negative Exponential Approximation for Large Input Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html Verifies that the fast_negexp function returns 0.0 when the input is a large number (100.0). ```Rust #[test] fn test_fast_negexp_large() { assert_eq!(fast_negexp(100.0), 0.0); } ``` -------------------------------- ### Fast Negative Exponential Approximation in Rust Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Provides a fast approximation for the negative exponential function e^(-x) using a polynomial fit. It handles edge cases for large and non-positive inputs. Useful for performance-critical signal processing where high precision is not strictly required. ```Rust /// ln(2) constant. pub const LN_2: f32 = core::f32::consts::LN_2; /// Fast negative exponential approximation. /// /// Uses a degree-4 polynomial minimax fit for e^(-x) on [0, 88]. /// Max error ~0.03% in the useful range. Returns 0.0 for large x. pub fn fast_negexp(x: f32) -> f32 { if x >= 88.0 { return 0.0; } if x <= 0.0 { return 1.0; } // Polynomial approximation: 1 / (1 + x + 0.48*x^2 + 0.235*x^3) let x2 = x * x; let x3 = x2 * x; 1.0 / (1.0 + x + 0.48 * x2 + 0.235 * x3) } #[cfg(test)] mod tests { use super::*; #[test] fn test_fast_negexp_zero() { assert!((fast_negexp(0.0) - 1.0).abs() < 1e-6); } #[test] fn test_fast_negexp_large() { assert_eq!(fast_negexp(100.0), 0.0); } #[test] fn test_fast_negexp_negative() { assert!((fast_negexp(-1.0) - 1.0).abs() < 1e-6); } #[test] fn test_fast_negexp_accuracy() { // Check at x=1: e^-1 ≈ 0.3679 let result = fast_negexp(1.0); let expected = (-1.0f32).exp(); assert!( (result - expected).abs() < 0.02, "fast_negexp(1.0)={} vs exp(-1)={}", result, expected ); } } ``` -------------------------------- ### DampedSpring Methods Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/spring/struct.DampedSpring.html?search=std%3A%3Avec Provides documentation for the core methods of the DampedSpring struct, including creation, updating, and state retrieval. ```APIDOC ## DampedSpring ### Description A critically-damped spring for smooth value interpolation. Produces smooth, non-oscillatory transitions with frame-rate independence. The `halflife` parameter controls how fast the value reaches the target: smaller = snappier, larger = smoother. ### Methods #### `new(initial: f32) -> Self` Create a new spring at the given initial value with zero velocity. #### `update(&mut self, target: f32, halflife: f32, dt: f32) -> f32` Step the spring toward `target` using the given `halflife` (seconds) and `dt` (seconds). Returns the new value. #### `value(&self) -> f32` Current smoothed value. #### `velocity(&self) -> f32` Current velocity. #### `set(&mut self, value: f32)` Hard-set the value (teleport) with zero velocity. #### `set_with_velocity(&mut self, value: f32, velocity: f32)` Hard-set value and velocity. ``` -------------------------------- ### fast_negexp Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/util/fn.fast_negexp.html?search= Calculates a fast approximation of the negative exponential function e^(-x). This function is optimized for performance and uses a degree-4 polynomial minimax fit for the range [0, 88]. It returns 0.0 for large input values of x, ensuring numerical stability. ```APIDOC ## Function fast_negexp ### Description Fast negative exponential approximation. Uses a degree-4 polynomial minimax fit for e^(-x) on [0, 88]. Max error ~0.03% in the useful range. Returns 0.0 for large x. ### Signature ```rust pub fn fast_negexp(x: f32) -> f32 ``` ### Parameters * **x** (f32) - The input value for which to calculate the negative exponential approximation. ### Returns * (f32) - The approximated value of e^(-x). ``` -------------------------------- ### Fast Negative Exponential Approximation in Rust Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html?search= Provides a fast approximation for e^(-x) using a polynomial fit. Useful for scenarios requiring quick decay calculations where high precision is not critical. Handles edge cases for large and non-positive inputs. ```Rust /// ln(2) constant. pub const LN_2: f32 = core::f32::consts::LN_2; /// Fast negative exponential approximation. /// /// Uses a degree-4 polynomial minimax fit for e^(-x) on [0, 88]. /// Max error ~0.03% in the useful range. Returns 0.0 for large x. pub fn fast_negexp(x: f32) -> f32 { if x >= 88.0 { return 0.0; } if x <= 0.0 { return 1.0; } // Polynomial approximation: 1 / (1 + x + 0.48*x^2 + 0.235*x^3) let x2 = x * x; let x3 = x2 * x; 1.0 / (1.0 + x + 0.48 * x2 + 0.235 * x3) } ``` -------------------------------- ### TryInto for T Implementation Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E A blanket implementation for attempting to convert `T` into `U`. The conversion might fail, returning `Result>::Error>`. ```rust type Error = >::Error fn try_into(self) -> Result>::Error> ``` -------------------------------- ### fast_negexp Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/util/fn.fast_negexp.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Calculates a fast approximation of the negative exponential function e^(-x). It uses a degree-4 polynomial minimax fit for the range [0, 88] with a maximum error of approximately 0.03%. For large values of x, it returns 0.0. ```APIDOC ## Function fast_negexp ### Description Fast negative exponential approximation. Uses a degree-4 polynomial minimax fit for e^(-x) on [0, 88]. Max error ~0.03% in the useful range. Returns 0.0 for large x. ### Signature ```rust pub fn fast_negexp(x: f32) -> f32 ``` ### Parameters * **x** (f32) - The input value for which to calculate the negative exponential. ### Returns * (f32) - The approximated value of e^(-x). ``` -------------------------------- ### Debug Implementation for OneEuroFilter Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search=u32+-%3E+bool Enables formatting the OneEuroFilter for debugging purposes. ```APIDOC ## impl Debug for OneEuroFilter ### Methods #### `fmt(&self, f: &mut Formatter<'_>) -> Result` Formats the value using the given formatter. ``` -------------------------------- ### DampedSpring try_from Signature Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/spring.rs.html?search=u32+-%3E+bool Illustrates the generic `try_from` method signature for DampedSpring, showing its potential use with different types. ```rust method signal_smooth::spring::DampedSpring::try_from **U** -> Result<**T** > where u32 matches **U** bool matches **T** ``` -------------------------------- ### Clone Implementation for OneEuroFilter Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search=u32+-%3E+bool Allows creating a duplicate of the OneEuroFilter instance. ```APIDOC ## impl Clone for OneEuroFilter ### Methods #### `clone(&self) -> OneEuroFilter` Returns a duplicate of the value. #### `clone_from(&mut self, source: &Self)` Performs copy-assignment from `source`. ``` -------------------------------- ### OneEuroFilter::new Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/one_euro/struct.OneEuroFilter.html?search= Creates a new OneEuroFilter with the specified minimum cutoff frequency and beta parameter. The minimum cutoff (in Hz) controls the aggressiveness of smoothing for slow movements, while beta influences the filter's responsiveness to changes. ```APIDOC ## OneEuroFilter::new ### Description Creates a new filter with the given min cutoff (Hz) and beta. ### Method `pub fn new(min_cutoff: f32, beta: f32) -> Self` ### Parameters * **min_cutoff** (f32) - The minimum cutoff frequency in Hz. * **beta** (f32) - The beta parameter. ### Returns A new `OneEuroFilter` instance. ``` -------------------------------- ### TryFrom for T Implementation Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E A blanket implementation for attempting to convert `U` into `T`. The conversion might fail, returning `Result`. ```rust type Error = Infallible fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Fast Negative Exponential Approximation Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/util.rs.html Provides a fast approximation for the negative exponential function e^(-x) using a degree-4 polynomial fit. It handles edge cases for large and non-positive inputs. ```Rust /// Fast negative exponential approximation. /// /// Uses a degree-4 polynomial minimax fit for e^(-x) on [0, 88]. /// Max error ~0.03% in the useful range. Returns 0.0 for large x. pub fn fast_negexp(x: f32) -> f32 { if x >= 88.0 { return 0.0; } if x <= 0.0 { return 1.0; } // Polynomial approximation: 1 / (1 + x + 0.48*x^2 + 0.235*x^3) let x2 = x * x; let x3 = x2 * x; 1.0 / (1.0 + x + 0.48 * x2 + 0.235 * x3) } ``` -------------------------------- ### Test Spring Convergence Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/spring.rs.html?search= Verifies that the DampedSpring converges to the target value within a small tolerance after multiple updates. ```rust #[cfg(test)] mod tests { use super::*; #[test] fn test_spring_converges() { let mut s = DampedSpring::new(0.0); for _ in 0..100 { s.update(1.0, 0.1, 1.0 / 60.0); } assert!( (s.value() - 1.0).abs() < 0.01, "Spring should converge to target, got {}", s.value() ); } ``` -------------------------------- ### Generic try_from Method for Ema Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/util/fn.fast_negexp.html?search=u32+-%3E+bool Demonstrates the generic `try_from` method for the `Ema` type, where a `u32` type `U` can be converted into a `bool` type `T`. ```rust U -> Result where u32 matches U bool matches T ``` -------------------------------- ### Ema Trait Implementations Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=u32+-%3E+bool Details the trait implementations for the Ema struct, including Clone, Debug, and various auto and blanket implementations. ```APIDOC ## Trait Implementations for Ema ### `impl Clone for Ema` #### `clone(&self) -> Ema` Returns a duplicate of the value. #### `clone_from(&mut self, source: &Self)` Performs copy-assignment from `source`. ### `impl Debug for Ema` #### `fmt(&self, f: &mut Formatter<'_>) -> Result` Formats the value using the given formatter. ### Auto Trait Implementations - `Freeze` - `RefUnwindSafe` - `Send` - `Sync` - `Unpin` - `UnsafeUnpin` - `UnwindSafe` ### Blanket Implementations - `impl Any for T` - `type_id(&self) -> TypeId` - `impl Borrow for T` - `borrow(&self) -> &T` - `impl BorrowMut for T` - `borrow_mut(&mut self) -> &mut T` - `impl CloneToUninit for T` (Nightly-only experimental API) - `clone_to_uninit(&self, dest: *mut u8)` - `impl From for T` - `from(t: T) -> T` - `impl Into for T` where `U: From` - `into(self) -> U` - `impl TryFrom for T` where `U: Into` - `Error = Infallible` - `try_from(value: U) -> Result>::Error>` - `impl TryInto for T` where `U: TryFrom` - `Error = >::Error` - `try_into(self) -> Result>::Error>` ``` -------------------------------- ### DampedSpring::new - Initialize Spring Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/spring.rs.html Creates a new DampedSpring instance with a specified initial value and zero velocity. ```rust impl DampedSpring { /// Create a new spring at the given initial value with zero velocity. pub fn new(initial: f32) -> Self { Self { value: initial, velocity: 0.0, } } /// Step the spring toward `target` using the given `halflife` (seconds) and `dt` (seconds). /// /// Returns the new value. pub fn update(&mut self, target: f32, halflife: f32, dt: f32) -> f32 { if dt <= 0.0 { return self.value; } // Damping ratio for critical damping let d = halflife_to_damping(halflife); let decay = fast_negexp(d * dt); let error = self.value - target; let new_vel = (self.velocity + error * d) * decay - error * d; let new_val = (error + (self.velocity + error * d) * dt) * decay + target; self.value = new_val; self.velocity = new_vel; self.value } /// Current smoothed value. pub fn value(&self) -> f32 { self.value } /// Current velocity. pub fn velocity(&self) -> f32 { self.velocity } /// Hard-set the value (teleport) with zero velocity. pub fn set(&mut self, value: f32) { self.value = value; self.velocity = 0.0; } /// Hard-set value and velocity. pub fn set_with_velocity(&mut self, value: f32, velocity: f32) { self.value = value; self.velocity = velocity; } } ``` -------------------------------- ### Ema Struct Methods Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Provides methods for initializing and updating an Exponential Moving Average (EMA) with frame-rate independence. ```APIDOC ## Struct Ema Frame-rate independent exponential moving average. Uses a halflife parameter (in seconds) so the smoothing behaves identically regardless of frame rate. ### Methods #### `new(initial: f32) -> Self` Create a new EMA. The first sample will be passed through. #### `uninit() -> Self` Create an uninitialized EMA. The first call to `update` sets the value. #### `update(&mut self, target: f32, halflife: f32, dt: f32) -> f32` Feed a new sample. `halflife` is in seconds, `dt` is frame delta in seconds. #### `value(&self) -> f32` Current smoothed value. #### `set(&mut self, value: f32)` Hard-set the value. ``` -------------------------------- ### Ema Struct and Methods Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/ema.rs.html?search=std%3A%3Avec Provides an implementation of an Exponential Moving Average (EMA) that is independent of frame rate. It includes methods for initialization, updating with new samples, retrieving the current value, and hard-setting the value. ```APIDOC ## Ema Struct ### Description Represents a frame-rate independent exponential moving average. ### Fields - `value`: `f32` - The current smoothed value. - `initialized`: `bool` - Flag indicating if the EMA has been initialized. ## Methods ### `new(initial: f32) -> Self` #### Description Creates a new EMA with an initial value. The first sample provided to `update` will be passed through directly. #### Parameters - `initial` (f32) - The initial value for the EMA. ### `uninit() -> Self` #### Description Creates an uninitialized EMA. The first call to `update` will set the initial value. ### `update(&mut self, target: f32, halflife: f32, dt: f32) -> f32` #### Description Feeds a new sample into the EMA, updating the smoothed value. The smoothing is frame-rate independent due to the `halflife` and `dt` parameters. #### Parameters - `target` (f32) - The new sample value to incorporate. - `halflife` (f32) - The desired halflife of the EMA in seconds. A smaller value results in faster smoothing. - `dt` (f32) - The time delta (frame duration) in seconds since the last update. #### Returns - `f32` - The updated smoothed value. ### `value(&self) -> f32` #### Description Returns the current smoothed value of the EMA. #### Returns - `f32` - The current smoothed value. ### `set(&mut self, value: f32)` #### Description Hard-sets the current smoothed value of the EMA and marks it as initialized. #### Parameters - `value` (f32) - The value to set the EMA to. ``` -------------------------------- ### Test Spring Halflife Affects Speed Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/spring.rs.html?search= Compares the convergence speed of springs with different halflife values, demonstrating that a smaller halflife leads to faster convergence. ```rust #[test] fn test_spring_halflife_affects_speed() { let mut fast = DampedSpring::new(0.0); let mut slow = DampedSpring::new(0.0); for _ in 0..10 { fast.update(1.0, 0.05, 1.0 / 60.0); slow.update(1.0, 0.5, 1.0 / 60.0); } // Faster halflife should be closer to target assert!( (fast.value() - 1.0).abs() < (slow.value() - 1.0).abs(), "fast={} slow={}", fast.value(), slow.value() ); } } ``` -------------------------------- ### Deadzone Function Tests Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/deadzone.rs.html Unit tests for the `apply_deadzone` and `apply_deadzone_symmetric` functions, verifying their behavior with zero deadzone, values within and above the deadzone, and negative inputs. ```rust #[cfg(test)] mod tests { use super::*; #[test] fn test_deadzone_zero_passes_through() { assert!((apply_deadzone(0.5, 0.0) - 0.5).abs() < 1e-6); } #[test] fn test_deadzone_below_threshold() { let v = apply_deadzone(0.01, 0.1); assert!(v.abs() < 0.01, "Small value should be nearly zero, got {}", v); } #[test] fn test_deadzone_above_threshold() { let v = apply_deadzone(0.5, 0.1); assert!((v - 0.5).abs() < 1e-6, "Value above deadzone should pass through, got {}", v); } #[test] fn test_deadzone_negative() { let v = apply_deadzone(-0.01, 0.1); assert!(v.abs() < 0.01, "Small negative should be nearly zero, got {}", v); } #[test] fn test_symmetric_deadzone_in_zone() { assert_eq!(apply_deadzone_symmetric(0.05, 0.1), 0.0); } #[test] fn test_symmetric_deadzone_outside() { let v = apply_deadzone_symmetric(0.55, 0.1); assert!(v > 0.0 && v < 0.55, "Should remap, got {}", v); } #[test] fn test_symmetric_deadzone_negative() { let v = apply_deadzone_symmetric(-0.55, 0.1); assert!(v < 0.0, "Should be negative, got {}", v); } } ``` -------------------------------- ### Publicly Usable Components Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/lib.rs.html?search= The following components are publicly exposed and can be directly used by library consumers. ```APIDOC ## Public API Surface ### Description The `signal_smooth` library provides direct access to several core functionalities through its public API. ### Modules and Usable Types - **Deadzone**: Provides functions for applying deadzone to signals. - `apply_deadzone` - `apply_deadzone_symmetric` - **EMA (Exponential Moving Average)**: Offers an EMA filter implementation. - `Ema` struct - **One Euro Filter**: Implements the One Euro filter for smoothing. - `OneEuroFilter` struct - **Damped Spring**: Provides a damped spring simulation. - `DampedSpring` struct - **Utilities**: Includes utility functions. - `fast_negexp` ### Usage Example (Conceptual) ```rust // Example of using Ema (actual usage depends on Ema struct implementation) // let mut ema_filter = signal_smooth::Ema::new(0.5); // let smoothed_value = ema_filter.filter(raw_value); // Example of using OneEuroFilter (actual usage depends on OneEuroFilter struct implementation) // let mut one_euro = signal_smooth::OneEuroFilter::new(1.0, 1.0, 1.0, 1.0); // let smoothed_value = one_euro.filter(time_delta, raw_value); // Example of using apply_deadzone // let processed_value = signal_smooth::apply_deadzone(input, deadzone_size); ``` ### Note This documentation is based on the `use` statements in `lib.rs`. For detailed usage of each type and function, please refer to the specific module documentation (e.g., `ema.rs`, `one_euro.rs`). ``` -------------------------------- ### apply_deadzone Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Applies a smoothstep deadzone to a value. Values below the deadzone are smoothly faded to zero using a cubic smoothstep. Values above the deadzone ramp from 0 to their full magnitude. This prevents jitter at rest while preserving the full range of motion. ```APIDOC ## apply_deadzone ### Description Applies a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ### Signature ```rust pub fn apply_deadzone(value: f32, deadzone: f32) -> f32 ``` ### Parameters * `value` (f32) - The input value to apply the deadzone to. * `deadzone` (f32) - The threshold below which the deadzone effect is applied. ### Returns * `f32` - The value after the deadzone has been applied. ``` -------------------------------- ### apply_deadzone Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone.html?search= Applies a smoothstep deadzone to a value. Values below the deadzone are smoothly faded to zero using a cubic smoothstep. Values above the deadzone ramp from 0 to their full magnitude. This prevents jitter at rest while preserving the full range of motion. ```APIDOC ## Function apply_deadzone ### Description Applies a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ### Signature ```rust pub fn apply_deadzone(value: f32, deadzone: f32) -> f32 ``` ### Parameters * **value** (f32) - The input value to apply the deadzone to. * **deadzone** (f32) - The threshold below which the deadzone effect is applied. ### Returns * (f32) - The value after the deadzone has been applied. ``` -------------------------------- ### Apply Smoothstep Deadzone to a Value Source: https://docs.rs/signal-smooth/0.1.0/src/signal_smooth/deadzone.rs.html?search=u32+-%3E+bool Applies a smoothstep deadzone to a value. Values below the deadzone are faded to zero using a cubic smoothstep, while values above the deadzone ramp from 0 to their full magnitude. This prevents jitter at rest while preserving the full range of motion. Use when you need to eliminate small fluctuations around a central point. ```rust /// Apply a smoothstep deadzone to a value. /// /// Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. /// Values above `deadzone` ramp from 0 to their full magnitude. /// This prevents jitter at rest while preserving full range of motion. pub fn apply_deadzone(value: f32, deadzone: f32) -> f32 { if deadzone <= 0.0 { return value; } let abs_val = value.abs(); if abs_val < deadzone { // Smoothstep fade within deadzone let t = abs_val / deadzone; let smoothed = t * t * (3.0 - 2.0 * t); smoothed * value.signum() * deadzone } else { value } } ``` -------------------------------- ### apply_deadzone Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone.html Applies a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ```APIDOC ## Function apply_deadzone ### Description Applies a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ### Signature ```rust pub fn apply_deadzone(value: f32, deadzone: f32) -> f32 ``` ### Parameters * `value` (f32) - The input value to apply the deadzone to. * `deadzone` (f32) - The threshold for the deadzone. Values below this will be faded to zero. ### Returns * `f32` - The value after the deadzone has been applied. ``` -------------------------------- ### apply_deadzone Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone.html?search=std%3A%3Avec Applies a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ```APIDOC ## Function apply_deadzone ### Description Apply a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ### Signature ```rust pub fn apply_deadzone(value: f32, deadzone: f32) -> f32 ``` ### Parameters * **value** (f32) - The input value to apply the deadzone to. * **deadzone** (f32) - The threshold below which the value will be faded to zero. ``` -------------------------------- ### apply_deadzone Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone.html?search=u32+-%3E+bool Applies a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ```APIDOC ## Function apply_deadzone ### Description Apply a smoothstep deadzone to a value. Values below `deadzone` are smoothly faded to zero using a cubic smoothstep. Values above `deadzone` ramp from 0 to their full magnitude. This prevents jitter at rest while preserving full range of motion. ### Signature ```rust pub fn apply_deadzone(value: f32, deadzone: f32) -> f32 ``` ### Parameters * `value` (f32) - The input value to apply the deadzone to. * `deadzone` (f32) - The threshold for the deadzone. Values below this will be faded to zero. ``` -------------------------------- ### apply_deadzone_symmetric Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/deadzone/fn.apply_deadzone_symmetric.html Applies a symmetric deadzone to a floating-point value. Values within the deadzone are set to 0, and values outside are scaled proportionally. ```APIDOC ## apply_deadzone_symmetric ### Description Applies a symmetric deadzone to a floating-point value. Values within the deadzone are set to 0, and values outside are scaled proportionally to maintain their original magnitude at the full range. ### Function Signature ```rust pub fn apply_deadzone_symmetric(value: f32, deadzone: f32) -> f32 ``` ### Parameters * **value** (f32) - The input value to apply the deadzone to. * **deadzone** (f32) - The size of the deadzone around zero. Must be non-negative. ### Returns (f32) - The value after the deadzone has been applied. ``` -------------------------------- ### Clone Ema to Uninitialized Memory (Nightly) Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Nightly-only experimental API. Performs copy-assignment from Ema to uninitialized memory. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Clone Ema Instance Source: https://docs.rs/signal-smooth/0.1.0/signal_smooth/ema/struct.Ema.html?search=std%3A%3Avec Returns a duplicate of the EMA instance. ```rust fn clone(&self) -> Ema ```