bundles / scipy latest / scipy / signal / _peak_finding / peak_prominences
function
scipy.signal._peak_finding:peak_prominences
Signature
def peak_prominences ( x , peaks , wlen = None ) Summary
Calculate the prominence of each peak in a signal.
Extended Summary
The prominence of a peak measures how much a peak stands out from the surrounding baseline of the signal and is defined as the vertical distance between the peak and its lowest contour line.
Parameters
x: sequenceA signal with peaks.
peaks: sequenceIndices of peaks in
x.wlen: int, optionalA window length in samples that optionally limits the evaluated area for each peak to a subset of
x. The peak is always placed in the middle of the window therefore the given length is rounded up to the next odd integer. This parameter can speed up the calculation (see Notes).
Returns
prominences: ndarrayThe calculated prominences for each peak in
peaks.left_bases, right_bases: ndarrayThe peaks' bases as indices in
xto the left and right of each peak. The higher base of each pair is a peak's lowest contour line.
Raises
: ValueErrorIf a value in
peaksis an invalid index forx.
Warns
: PeakPropertyWarningFor indices in
peaksthat don't point to valid local maxima inx, the returned prominence will be 0 and this warning is raised. This also happens ifwlenis smaller than the plateau size of a peak.
Warnings
This function may return unexpected results for data containing NaNs. To avoid this, NaNs should either be removed or replaced.
Notes
Strategy to compute a peak's prominence:
Extend a horizontal line from the current peak to the left and right until the line either reaches the window border (see
wlen) or intersects the signal again at the slope of a higher peak. An intersection with a peak of the same height is ignored.On each side find the minimal signal value within the interval defined above. These points are the peak's bases.
The higher one of the two bases marks the peak's lowest contour line. The prominence can then be calculated as the vertical difference between the peaks height itself and its lowest contour line.
Searching for the peak's bases can be slow for large x with periodic behavior because large chunks or even the full signal need to be evaluated for the first algorithmic step. This evaluation area can be limited with the parameter wlen which restricts the algorithm to a window around the current peak and can shorten the calculation time if the window length is short in relation to x. However, this may stop the algorithm from finding the true global contour line if the peak's true bases are outside this window. Instead, a higher contour line is found within the restricted window leading to a smaller calculated prominence. In practice, this is only relevant for the highest set of peaks in x. This behavior may even be used intentionally to calculate "local" prominences.
Array API Standard Support
peak_prominences has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
import numpy as np from scipy.signal import find_peaks, peak_prominences import matplotlib.pyplot as plt✓
x = np.linspace(0, 6 * np.pi, 1000) x = np.sin(x) + 0.6 * np.sin(2.6 * x)✓
peaks, _ = find_peaks(x) prominences = peak_prominences(x, peaks)[0] prominences✓
contour_heights = x[peaks] - prominences
✓plt.plot(x) plt.plot(peaks, x[peaks], "x") plt.vlines(x=peaks, ymin=contour_heights, ymax=x[peaks])✗
plt.show()
✓
x = np.array([0, 1, 0, 3, 1, 3, 0, 4, 0]) peaks = np.array([5])✓
plt.plot(x) plt.plot(peaks, x[peaks], "x")✗
plt.show()
✓
peak_prominences(x, peaks) # -> (prominences, left_bases, right_bases)
✓peak_prominences(x, peaks, wlen=3.1)
✓See also
- find_peaks
Find peaks inside a signal based on peak properties.
- peak_widths
Calculate the width of peaks.
Aliases
-
scipy.signal.peak_prominences