Crates.io | find_peaks |
lib.rs | find_peaks |
version | 0.1.5 |
source | src |
created_at | 2020-09-24 14:11:34.553646 |
updated_at | 2022-05-18 09:34:32.571305 |
description | Find peaks that match criteria in 1D data. |
homepage | https://github.com/tungli/find_peaks-rs.git |
repository | https://github.com/tungli/find_peaks-rs.git |
max_upload_size | |
id | 292506 |
size | 87,084 |
Find a filtered subset of local maxima in 1D slice of data.
The functionality implemented here is might be familiar to anyone using MATLAB's findpeaks
, or Python's scipy.signal.find_peaks
.
Arguably, the most useful feature in this package is filtering peaks through prominence. This parameter allows you to get the subset of local maxima that optically look like peaks even in noisy data.
Filtering conditions that can be set are:
All parameters can be specified by minimum and maximum bound.
Elements of the data slice need not be of a specific type, as long as they implement a few traits (for cloning, subtraction, comparison).
Copied from examples/spectrum.rs.
use find_peaks::PeakFinder;
use std::fs::File;
use std::io::prelude::*;
fn read_file(path: &str) -> std::io::Result<String> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn main() -> () {
let data: Vec<f64> = read_file("data/spectrum.dat").expect("File not read!").as_str()
.split_whitespace()
.map(|x| x.parse::<f64>().unwrap())
.collect();
let mut fp = PeakFinder::new(&data);
fp.with_min_prominence(200.);
fp.with_min_height(0.);
let peaks = fp.find_peaks();
for p in peaks {
println!("{} {}", p.middle_position(), p.height.unwrap());
}
}
The result visualized: