Peak detection turns a sampled signal into the positions of meaningful local maxima, which helps identify pulses, beats, spikes, and repeated sensor responses. SciPy provides scipy.signal.find_peaks() for one-dimensional arrays, so the same pattern works for measured data, simulated signals, and small diagnostic samples.
find_peaks() returns the integer indexes of accepted peaks and a properties dictionary for filters that are requested. Values such as peak_heights, prominences, and widths appear only when the matching condition is supplied, which keeps a simple local-maximum search from producing extra arrays.
Choose thresholds in the same sample units as the array being searched. A distance value counts array positions, height compares the sample value at the peak, and prominence measures how far the peak rises above the surrounding baseline; replacing or removing NaN values before detection avoids misleading local comparisons.
Steps to find signal peaks with SciPy:
- Create a sample script that imports find_peaks() and defines the one-dimensional signal.
- peak_find.py
import numpy as np from scipy.signal import find_peaks signal = np.array([0.0, 1.2, 0.1, 0.8, 0.2, 3.1, 0.4, 0.7, 0.2, 2.5, 0.1]) peaks, properties = find_peaks( signal, height=1.0, distance=3, prominence=1.0, width=(None, None), ) print("peak indexes:", peaks.tolist()) print("peak values:", signal[peaks].round(2).tolist()) print("prominences:", properties["prominences"].round(2).tolist()) print("widths:", properties["widths"].round(2).tolist()) print("expected match:", np.array_equal(peaks, np.array([1, 5, 9])))
height filters by peak amplitude, distance requires spacing in samples, and prominence keeps peaks that stand above nearby baseline. width=(None, None) asks SciPy to calculate width properties without excluding peaks by width.
- Run the script to verify the expected peak indexes.
$ python3 peak_find.py peak indexes: [1, 5, 9] peak values: [1.2, 3.1, 2.5] prominences: [1.1, 3.0, 2.3] widths: [0.96, 1.07, 0.98] expected match: True
The indexes point back into the original array. In this signal, indexes 1, 5, and 9 pass the height, spacing, and prominence conditions.
- Replace the sample array with measured data and tune one condition at a time.
Start with height or prominence, then add distance when neighboring detections must be separated by a minimum number of samples.
- Remove the sample script after copying the pattern into your project.
$ rm peak_find.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.