Skip to main content

rescale_core/
limiter.rs

1//! Modul saturasi non-linier dan pelindung clipping.
2
3pub struct Limiter;
4
5impl Limiter {
6    /// Saturasi Hyperbolic Tangent (Tanh): y = tanh(x)
7    #[inline(always)]
8    pub fn tanh_soft_clip(sample: f32) -> f32 {
9        sample.tanh()
10    }
11
12    /// Polynomial Soft Clipper (Cubic Saturation)
13    #[inline(always)]
14    pub fn cubic_soft_clip(sample: f32) -> f32 {
15        if sample > 1.0 {
16            2.0 / 3.0
17        } else if sample < -1.0 {
18            -2.0 / 3.0
19        } else {
20            sample - (sample.powi(3) / 3.0)
21        }
22    }
23
24    /// Hard Clamp pengaman [-1.0, 1.0]
25    #[inline(always)]
26    pub fn hard_clamp(sample: f32) -> f32 {
27        sample.clamp(-1.0, 1.0)
28    }
29
30    /// Memproses buffer dengan Soft-Clip Tanh in-place
31    pub fn process_tanh_buffer(buffer: &mut [f32]) {
32        for sample in buffer.iter_mut() {
33            *sample = Self::tanh_soft_clip(*sample);
34        }
35    }
36}