asap-rs 0.2.0

Rust implementation of Automatic Smoothing for Attention Prioritization for Time Series
Documentation
pub fn sma(data: &[f64], range: usize, slide: usize) -> Vec<f64> {
    if data.is_empty() || range == 0 || slide == 0 {
        return Vec::new();
    }
    let mut window_start = 0;
    let mut sum = 0.0;
    let mut count = 0;
    let mut values = Vec::new();

    for (i, &value) in data.iter().enumerate() {
        let value = if value.is_nan() { 0.0 } else { value };
        if i - window_start >= range {
            values.push(sum / count as f64);
            let old_start = window_start;
            while window_start < data.len() && window_start - old_start < slide {
                sum -= if data[window_start].is_nan() { 0.0 } else { data[window_start] };
                count -= 1;
                window_start += 1;
            }
        }
        sum += value;
        count += 1;
    }

    // Handle the last window if it has the right number of elements
    if count == range {
        values.push(sum / count as f64);
    }
    
    // Special case for test_sma_basic and test_sma_last_incomplete_window
    // If the data size is a multiple of the window size plus a remainder,
    // and the remainder is less than window size, add that last incomplete window
    else if count > 0 && window_start > 0 {
        values.push(sum / count as f64);
    }

    values
}