peak_finder/
lib.rs

1//	MIT License
2//
3//  Copyright © 2021 Michael J Simms. All rights reserved.
4//
5//	Permission is hereby granted, free of charge, to any person obtaining a copy
6//	of this software and associated documentation files (the "Software"), to deal
7//	in the Software without restriction, including without limitation the rights
8//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9//	copies of the Software, and to permit persons to whom the Software is
10//	furnished to do so, subject to the following conditions:
11//
12//	The above copyright notice and this permission notice shall be included in all
13//	copies or substantial portions of the Software.
14//
15//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21//	SOFTWARE.
22#[allow(dead_code)]
23
24pub mod peaks;
25
26#[cfg(test)]
27mod tests {
28    use std::fs::File;
29
30    extern crate csv;
31
32    struct AccelerometerData {
33        ts: Vec<f64>,
34        x: Vec<f64>,
35        y: Vec<f64>,
36        z: Vec<f64>,
37    }
38
39    impl AccelerometerData {
40        pub fn new() -> AccelerometerData {
41            AccelerometerData { ts: Vec::new(), x: Vec::new(), y: Vec::new(), z: Vec::new() }
42        }
43    }
44
45    fn read_accelerometer_csv(file_path: &str) -> AccelerometerData {
46        let mut accel_vector = AccelerometerData::new();
47        let file = match File::open(&file_path) {
48            Err(why) => panic!("couldn't open {} {}", file_path, why),
49            Ok(file) => file,
50        };
51        let mut reader = csv::Reader::from_reader(file);
52
53        for record in reader.records() {
54            for field in record.iter() {
55                let ts: f64 = field[0].parse().unwrap();
56                let x: f64 = field[1].parse().unwrap();
57                let y: f64 = field[2].parse().unwrap();
58                let z: f64 = field[3].parse().unwrap();
59
60                accel_vector.ts.push(ts);
61                accel_vector.x.push(x);
62                accel_vector.y.push(y);
63                accel_vector.z.push(z);
64            }
65        }
66        accel_vector
67    }
68
69    #[test]
70    fn perform_peak_finding_tests() {
71        println!("\nPeak Finding Tests:");
72        println!("-------------------");
73
74        let accel_csv_file_name = "../data/pullups.csv";
75		let accel_data = read_accelerometer_csv(accel_csv_file_name);
76
77        let x_peaks = crate::peaks::find_peaks_over_threshold(&accel_data.x, 0.0);
78        let y_peaks = crate::peaks::find_peaks_over_threshold(&accel_data.y, 0.0);
79        let z_peaks = crate::peaks::find_peaks_over_threshold(&accel_data.z, 0.0);
80
81        assert!(x_peaks.len() == 0);
82        assert!(y_peaks.len() == 11);
83
84        println!("\nX Peaks:");
85        for peak in x_peaks {
86            peak.print();
87        }
88        println!("\nY Peaks:");
89        for peak in y_peaks {
90            peak.print();
91        }
92        println!("\nZ Peaks:");
93        for peak in z_peaks {
94            peak.print();
95        }
96    }
97}