PeakFinder Classes#
Base Class#
Moving-average Class#
- class SlidingWindowPeakLocator(window_size, max_number_of_peaks=5, padding_value=-1, window_step=-1, compute_width=False, compute_area=False, threshold=0.5)[source]#
Bases:
BasePeakLocator
- SlidingWindowPeakLocator(window_size, window_step=-1, max_number_of_peaks=5, padding_value=-1,
compute_width=False, compute_area=False, threshold=0.5)
A sliding-window-based peak detection utility for 1D signals. This class segments the input signal into fixed-size windows (which can be overlapping if window_step is less than window_size) and identifies the local maximum in each window. Optionally, it computes additional metrics for each peak: - Width: the number of contiguous samples above a specified fraction of the peak’s height. - Area: the sum of signal values under the peak within its window.
The results are returned as a dictionary containing fixed-length arrays. If fewer peaks are detected than max_number_of_peaks, the arrays are padded with padding_value (for indices) or NaN (for numeric values).
- Parameters:
window_size (int) – The size of the sliding window used for local peak detection.
window_step (int, optional) – The step size between consecutive windows. If not provided or set to -1, defaults to window_size (i.e., non-overlapping windows). To create overlapping windows, specify a value less than window_size.
max_number_of_peaks (int, optional) – The maximum number of peaks to report. If fewer peaks are detected, the results are padded. Default is 5.
padding_value (int, optional) – The value used to pad the output array for indices when fewer than max_number_of_peaks peaks are found. Default is -1.
compute_width (bool, optional) – If True, compute and return the width of each detected peak (in samples). Default is False.
compute_area (bool, optional) – If True, compute and return the area (sum of signal values) under each detected peak. Default is False.
threshold (float, optional) – The fraction of the peak’s height used to determine the boundaries for width and area calculations. For example, a threshold of 0.5 uses 50% of the peak height as the cutoff. Default is 0.5.
- Returns:
- A dictionary containing the following keys:
”Index”: A fixed-length array of detected peak indices, padded as necessary.
”Height”: A fixed-length array of the corresponding peak values.
”Width”: (optional) A fixed-length array of computed peak widths (if compute_width is True).
”Area”: (optional) A fixed-length array of computed peak areas (if compute_area is True).
- Return type:
dict
Examples
>>> import numpy as np >>> # Example signal >>> data = np.array([1, 3, 2, 5, 4, 2, 8, 1], dtype=float) >>> # Create a peak locator without additional metrics (non-overlapping windows). >>> locator = SlidingWindowPeakLocator(window_size=2, max_number_of_peaks=4, padding_value=-1) >>> result = locator(data) >>> print("Indices:", result["Index"]) [1, 3, 4, 6] >>> print("Heights:", result["Height"]) [3.0, 5.0, 4.0, 8.0] >>> # Create a peak locator with overlapping windows and with width and area computations. >>> locator_metrics = SlidingWindowPeakLocator(window_size=4, window_step=2, max_number_of_peaks=4, ... padding_value=-1, compute_width=True, compute_area=True, threshold=0.5) >>> result = locator_metrics(data) >>> print("Peak indices:", result["Index"]) >>> print("Heights:", result["Height"]) >>> print("Widths:", result["Width"]) >>> print("Areas:", result["Area"])
Derivative based Class#
- class DerivativePeakLocator(max_number_of_peaks=5, padding_value=-1, compute_width=False, compute_area=False, threshold=0.5)[source]#
Bases:
BasePeakLocator
A peak detection utility based on the zero-crossing of the first derivative.
This class detects peaks by computing the first and second derivatives of each row of a 2D array. A peak is defined where the first derivative changes sign from positive to negative and the second derivative is negative. Optionally, for each detected peak, the width (number of samples above a threshold) and area (sum of values over the peak region) can be computed using a simple threshold-based approach.
- Parameters:
max_number_of_peaks (int, optional) – Maximum number of peaks to return per row. If fewer peaks are detected, missing values are padded with padding_value. Default is 5.
padding_value (object, optional) – Value to pad missing peaks in output. Default is -1.
compute_width (bool, optional) – If True, compute the width (in samples) of each detected peak. Default is False.
compute_area (bool, optional) – If True, compute the area (sum of values) under each detected peak. Default is False.
threshold (float, optional) – Fraction of the peak height used to determine the boundaries for width and area calculations. Default is 0.5.
Examples
>>> import numpy as np >>> # Without additional metrics: >>> peak_locator = DerivativePeakLocator(max_number_of_peaks=3) >>> data = np.array([[0, 100, 500, 200, 50, 1000], ... [200, 400, 100, 900, 500, 700]]) >>> result = peak_locator(data) >>> print(result["peak_index"]) [[2. 5. nan] [1. 3. 5.]]
>>> # With width and area computations enabled: >>> peak_locator_metrics = DerivativePeakLocator(max_number_of_peaks=3, compute_width=True, compute_area=True, threshold=0.5) >>> result = peak_locator_metrics(data) >>> print("Peak indices:\n", result["peak_index"]) >>> print("Widths:\n", result["width"]) >>> print("Areas:\n", result["area"])