use crate::PeqModel;
use crate::optim::compute::compute_smoothness_penalty;
use crate::optim::misc::{apply_audibility_deadband, maybe_smooth_error};
use crate::x2peq::x2spl;
use ndarray::Array1;
#[derive(Debug, Clone, Copy)]
pub struct ObjectiveContext<'a> {
pub freqs: &'a Array1<f64>,
pub target: &'a Array1<f64>,
pub deviation: &'a Array1<f64>,
pub srate: f64,
pub peq_model: PeqModel,
pub min_freq: f64,
pub max_freq: f64,
pub smooth: bool,
pub smooth_n: usize,
pub audibility_deadband: Option<&'a crate::roomeq::AudibilityDeadbandConfig>,
pub smoothness_penalty: Option<&'a crate::optim::SmoothnessPenaltyConfig>,
}
impl ObjectiveContext<'_> {
pub fn peq_spl(&self, x: &[f64]) -> Array1<f64> {
x2spl(self.freqs, x, self.srate, self.peq_model)
}
pub fn smooth_error(&self, error: Array1<f64>) -> Array1<f64> {
maybe_smooth_error(self.freqs, error, self.smooth, self.smooth_n)
}
pub fn apply_deadband(&self, error: &Array1<f64>) -> Array1<f64> {
apply_audibility_deadband(
self.freqs,
error,
self.min_freq,
self.max_freq,
self.audibility_deadband,
)
}
pub fn smoothness_penalty(&self, peq_spl: &Array1<f64>) -> f64 {
self.smoothness_penalty
.map(|cfg| {
compute_smoothness_penalty(peq_spl, self.freqs, self.min_freq, self.max_freq, cfg)
})
.unwrap_or(0.0)
}
}