#signal #data-science #spectrum #data-points #prominence

find_peaks

Find peaks that match criteria in 1D data

6 releases

0.1.5 May 18, 2022
0.1.4 May 2, 2022
0.1.3 Mar 6, 2022
0.1.2 Oct 8, 2020
0.1.1 Sep 24, 2020

#587 in Math

Download history 174/week @ 2024-07-21 75/week @ 2024-07-28 87/week @ 2024-08-04 89/week @ 2024-08-11 106/week @ 2024-08-18 26/week @ 2024-08-25 30/week @ 2024-09-01 45/week @ 2024-09-08 162/week @ 2024-09-15 156/week @ 2024-09-22 164/week @ 2024-09-29 438/week @ 2024-10-06 173/week @ 2024-10-13 134/week @ 2024-10-20 258/week @ 2024-10-27 289/week @ 2024-11-03

864 downloads per month
Used in 2 crates

MIT license

53KB
547 lines

Description

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:

  • prominence,
  • height,
  • the absolute value of the difference between neighboring data points,
  • number of plateau points a peak can have,
  • distance between peaks.

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).

Example

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:

No runtime deps