use crate::psychoacoustic::bark_width;
use crate::types::Rate;
pub(crate) fn deadzone_removed(loudness_ref: &[f32], loudness_deg: &[f32], out: &mut [f32]) {
for band in 0..out.len() {
let margin = 0.25 * loudness_ref[band].min(loudness_deg[band]);
let mut d = loudness_deg[band] - loudness_ref[band];
if d > margin {
d -= margin;
} else if d < -margin {
d += margin;
} else {
d = 0.0;
}
out[band] = d;
}
}
pub(crate) fn lp_norm(d: &[f32], p: f64, rate: Rate) -> f64 {
let mut weighted = 0.0f64;
let mut weight_sum = 0.0f64;
for (band, &value) in d.iter().enumerate().skip(1) {
let w = f64::from(bark_width(band, rate));
weighted += (f64::from(value.abs()) * w).powf(p);
weight_sum += w;
}
(weighted / weight_sum).powf(1.0 / p) * weight_sum
}
pub(crate) fn asymmetric_densities(
d: &[f32],
pitch_ref: &[f32],
pitch_deg: &[f32],
out: &mut [f32],
) {
for (((&d, &p_ref), &p_deg), out) in d.iter().zip(pitch_ref).zip(pitch_deg).zip(out.iter_mut())
{
let r = (f64::from(p_deg) + 50.0) / (f64::from(p_ref) + 50.0);
let mut h = r.powf(1.2).min(12.0);
if h < 3.0 {
h = 0.0;
}
*out = (f64::from(d) * h) as f32;
}
}
pub(crate) fn power_normalized(value: f32, total_power_ref: f32) -> f32 {
let h = ((f64::from(total_power_ref) + 1e5) / 1e7).powf(0.04);
(f64::from(value) / h).min(f64::from(super::DISTURBANCE_CAP)) as f32
}