use crate::{BLOCK_SIZE, traits::BufferMut}; pub fn analyse_buffer_by_stepped_windows>( threshold: u16, buf: &[u16], average: u16, peaks: &mut B, ) -> u16 { const CHUNK_SIZE: usize = BLOCK_SIZE / 32; const CHUNK_STEP: usize = CHUNK_SIZE / 2; let mut total = 0u32; let mut len = 0u32; for (i, window) in buf .array_windows::() .enumerate() .step_by(CHUNK_STEP) { let (window_total, window_diff) = window .iter() .copied() .fold((0, 0), |(total, diff), sample| { ( total + sample, diff + ((average as i16) - sample as i16).unsigned_abs(), ) }); let diff = window_diff / CHUNK_SIZE as u16; if diff > threshold { peaks.push((i, diff)); } else { total += window_total as u32; len += CHUNK_SIZE as u32; } } (total / len) as u16 }