use crate::buffer::PushBuffer;
pub struct Convolution<T>
{
buffer : PushBuffer<T>,
window : Box<[T]>
}
impl Convolution<f32>
{
pub fn new(data : & [f32]) -> Self
{
let mut window = Self
{
buffer : PushBuffer::<f32>::new(data.len()),
window : Box::<[f32]>::from(data),
};
window.buffer.index = window.buffer.len();
return window;
}
pub fn run(& mut self, input : & Box<[f32]>, output : & mut Box<[f32]>)
{
let mut data = 0.0;
for x in 0..input.len()
{
self.buffer.push(input[x]);
(0..self.window.len()).for_each(|x| data += self.buffer[x] * self.window[x]);
output[x] = data;
}
}
}
impl Convolution<f64>
{
pub fn new(data : & [f64]) -> Self
{
let mut window = Self
{
buffer : PushBuffer::<f64>::new(data.len()),
window : Box::<[f64]>::from(data),
};
window.buffer.index = window.buffer.len();
return window;
}
pub fn run(& mut self, input : & Box<[f64]>, output : & mut Box<[f64]>)
{
let mut data = 0.0;
for x in 0..input.len()
{
self.buffer.push(input[x]);
(0..self.window.len()).for_each(|x| data += self.buffer[x] * self.window[x]);
output[x] = data;
}
}
}
pub struct Saturation<T>
{
ths : T,
lim : T,
gap : T,
rad_pow : T,
org : T
}
impl Saturation<f32>
{
pub fn new(ths : f32, lim : f32) -> 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 run(data : f32, upper : Saturation<f32>, lower : Saturation<f32>) -> f32
{
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 }
}
}
impl Saturation<f64>
{
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<f64>, lower : Saturation<f64>) -> 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 : & Box<[f64]>, output : & mut Box<[f64]>, next : & bool, state : & bool);