use crate::buffer::CircularBuffer;
pub fn convolution(input : & mut CircularBuffer<f64>, output : & mut CircularBuffer<f64>,
window : & [f64], next : & bool, state : & bool)
{
let mut data = 0.0;
let mut buffer = crate::buffer::PushBuffer::<f64>::new(window.len());
buffer.index = buffer.len();
while ! * state
{
for _ in 0..input.len()
{
buffer.push(input.next());
(0..window.len()).for_each(|x| data += buffer[x] * window[x]);
output.push(data);
}
if * next { continue; }
}
}
pub struct Saturation
{
ths : f64,
lim : f64,
gap : f64,
rad_pow : f64,
org : f64
}
impl Saturation
{
pub fn new(ths : f64, lim : f64) -> Self
{
let gap = lim - ths;
let side = ((gap * 2.0).powi(2) + gap.powi(2)).sqrt();
let ang = 180.0 - 2.0 * (gap * 2.0 / side).asin();
let rad = side / (2.0 * (ang/2.0).sin());
let rad_pow = rad.powi(2);
let org = lim - rad;
return Self { ths, lim, gap, rad_pow, org }
}
}
pub fn saturation(data : f64, upper : Saturation, lower : Saturation) -> f64
{
return if data > upper.lim + upper.gap { upper.lim }
else if data > upper.ths { upper.org + (upper.rad_pow - (upper.lim - data).powi(2)).sqrt() }
else if data < lower.lim - lower.gap { upper.lim }
else if data < lower.ths { lower.org - (lower.rad_pow - (lower.lim - data).powi(2)).sqrt() }
else { 0.0 }
}
pub type Process = fn(input : & CircularBuffer<f64>, output : & mut CircularBuffer<f64>, next : & bool, state : & bool);