torch_ecg.utils.detect_peaks¶
- torch_ecg.utils.detect_peaks(x: Sequence, mph: Real | None = None, mpd: int = 1, threshold: Real = 0, left_threshold: Real = 0, right_threshold: Real = 0, prominence: Real | None = None, prominence_wlen: int | None = None, edge: str | None = 'rising', kpsh: bool = False, valley: bool = False, show: bool = False, ax=None, verbose: int = 0) ndarray [source]¶
Detect peaks in data based on their amplitude and other features.
- Parameters:
x (array_like) – 1D array of data.
mph (positive number, optional) – abbr. for maximum (minimum) peak height, detect peaks that are greater than minimum peak height (if parameter valley is False), or peaks that are smaller than maximum peak height (if parameter valley is True)
mpd (positive integer, default 1) – abbr. for minimum peak distance, detect peaks that are at least separated by minimum peak distance (in number of samples)
threshold (positive number, default 0) – detect peaks (valleys) that are greater (smaller) than threshold, in relation to their neighbors within the range of mpd
left_threshold (positive number, default 0) – threshold that is restricted to the left
right_threshold (positive number, default 0) – threshold that is restricted to the left
prominence (positive number, optional) – threshold of prominence of the detected peaks (valleys)
prominence_wlen (positive int, optional) – the wlen parameter of the function scipy.signal.peak_prominences
edge (str or None, default "rising") – can also be “falling”, “both”, for a flat peak, keep only the rising edge (“rising”), only the falling edge (“falling”), both edges (“both”), or don’t detect a flat peak (None)
kpsh (bool, default False) – keep peaks with same height even if they are closer than mpd
valley (bool, default False) – if True (1), detect valleys (local minima) instead of peaks
show (bool, default False) – if True (1), plot data in matplotlib figure
ax (a matplotlib.axes.Axes instance, optional) –
- Returns:
ind – Indeces of the peaks in x.
- Return type:
array_like
Note
The detection of valleys instead of peaks is performed internally by simply negating the data:
ind_valleys = detect_peaks(-x)
.The function can handle NaN’s.
See this IPython Notebook [1].
References
Examples
x = np.random.randn(100) x[60:81] = np.nan # detect all peaks and plot data ind = detect_peaks(x, show=True) print(ind) x = np.sin(2*np.pi*5*np.linspace(0, 1, 200)) + np.random.randn(200)/5 # set minimum peak height = 0 and minimum peak distance = 20 detect_peaks(x, mph=0, mpd=20, show=True) x = [0, 1, 0, 2, 0, 3, 0, 2, 0, 1, 0] # set minimum peak distance = 2 detect_peaks(x, mpd=2, show=True) x = np.sin(2*np.pi*5*np.linspace(0, 1, 200)) + np.random.randn(200)/5 # detection of valleys instead of peaks detect_peaks(x, mph=-1.2, mpd=20, valley=True, show=True) x = [0, 1, 1, 0, 1, 1, 0] # detect both edges detect_peaks(x, edge="both", show=True) x = [-2, 1, -2, 2, 1, 1, 3, 0] # set threshold = 2 detect_peaks(x, threshold = 2, show=True)
Version history
- “1.0.5”:
The sign of mph is inverted if parameter valley is True