use super::power_change::PowerOfChangeFrames;
#[derive(Debug)]
pub struct DynamicThresholds {
pub buffer: Vec<Vec<f32>>,
}
pub struct DynamicThresholdsParams {
pub threshold_time_spread: usize,
pub threshold_time_spread_factor: f32,
}
pub fn calculate_dynamic_thresholds(
params: DynamicThresholdsParams,
power_of_change_frames: &PowerOfChangeFrames,
) -> DynamicThresholds {
let DynamicThresholdsParams {
threshold_time_spread: time_spread,
threshold_time_spread_factor: beta,
} = params;
let mut result = {
let mut result = Vec::with_capacity(power_of_change_frames.len());
for _i in 0..power_of_change_frames.len() {
result.push({
let mut v = Vec::with_capacity(power_of_change_frames.bins());
v.resize(power_of_change_frames.bins(), 0.0);
v
});
}
result
};
#[allow(clippy::needless_range_loop)]
for i in 0..power_of_change_frames.len() {
for j in 0..power_of_change_frames.bins() {
let mut sum = 0.0;
{
let i = i as i32;
let time_spread = time_spread as i32;
for l in i - time_spread..i + time_spread {
if l >= 0 && l < power_of_change_frames.len() as i32 {
sum += power_of_change_frames.buffer[l as usize][j];
}
}
}
result[i][j] = beta * (sum / (2.0 * time_spread as f32 + 1.0));
}
}
DynamicThresholds { buffer: result }
}