Repo for designs & driver for a TA7642 powered lightning detector
at main 42 lines 1.1 kB view raw
1use crate::{BLOCK_SIZE, traits::BufferMut}; 2 3pub fn analyse_buffer_by_stepped_windows<B: BufferMut<(usize, u16)>>( 4 threshold: u16, 5 buf: &[u16], 6 average: u16, 7 peaks: &mut B, 8) -> u16 { 9 const CHUNK_SIZE: usize = BLOCK_SIZE / 32; 10 const CHUNK_STEP: usize = CHUNK_SIZE / 2; 11 12 let mut total = 0u32; 13 let mut len = 0u32; 14 15 for (i, window) in buf 16 .array_windows::<CHUNK_SIZE>() 17 .enumerate() 18 .step_by(CHUNK_STEP) 19 { 20 let (window_total, window_diff) = 21 window 22 .iter() 23 .copied() 24 .fold((0, 0), |(total, diff), sample| { 25 ( 26 total + sample, 27 diff + ((average as i16) - sample as i16).unsigned_abs(), 28 ) 29 }); 30 31 let diff = window_diff / CHUNK_SIZE as u16; 32 33 if diff > threshold { 34 peaks.push((i, diff)); 35 } else { 36 total += window_total as u32; 37 len += CHUNK_SIZE as u32; 38 } 39 } 40 41 (total / len) as u16 42}