Overview of the peaks dectection algorithms available in Python
Overview of the peaks dectection algorithms available in Python
This is an overview of all the ready-to-use algorithms I've found to perform peak detection in Python. I've also written a blog post on the subject.
| Algorithm | Integration | Filters | MatLab findpeaks-like? |
|---|---|---|---|
| scipy.signal.find_peaks_cwt | Included in Scipy | ? | ✘ |
| scipy.signal.argrelextrema | Included in Scipy 0.11+ | Minimum distance | ✘ |
| scipy.signal.find_peaks | Included in Scipy 1.1+ | Amplitude | |
| Threshold | |||
| Distance | |||
| Prominence | |||
| Width | ✔ | ||
| detect_peaks | Single file source | ||
| Depends on Numpy | Minimum distance | ||
| Minimum height | |||
| Relative threshold | ✔ | ||
| peakutils.peak.indexes | PyPI package PeakUtils | ||
| Depends on Scipy | Amplitude threshold | ||
| Minimum distance | ✔ | ||
| peakdetect | Single file source | ||
| Depends on Scipy | Minimum distance | ✘ | |
| Octave-Forge findpeaks | Requires an Octave-Forge distribution |
When you're selecting an algorithm, you might consider:
findpeaks.import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
import scipy.signal
print('Detect peaks without any filters.')
indexes = scipy.signal.find_peaks_cwt(vector, np.arange(1, 4),
max_distances=np.arange(1, 4)*2)
indexes = np.array(indexes) - 1
print('Peaks are: %s' % (indexes))The first historical peak detection algorithm from the Scipy signal processing package. Its name appears to make it an obvious choice (when you already work with Scipy), but it may actually not be, as it uses a wavelet convolution approach.
This function requires to understand wavelets to be properly used. This is less trivial and direct than other algorithms. However the wavelet approach can make it a good choice on noisy data.
import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
import scipy.signal
print('Detect peaks with order (distance) filter.')
indexes = scipy.signal.argrelextrema(
np.array(vector),
comparator=np.greater,order=2
)
print('Peaks are: %s' % (indexes[0]))New peak detection algorithm from Scipy since version 0.11.0. Its usage is really trivial, but it misses out of the box filtering capacities.
It includes an order parameter that can serve as a kind of minimum distance filter.
The filtering behavior is customizable through the comparator parameter, which
can make it a good choice for building your own filtering algorithm over it.
See also related functions argrelmin and argrelmax.
import numpy as np
import scipy.signal
vector = np.array([0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8,
13, 8, 10, 3, 1, 20, 7, 3, 0])
print('Detect peaks with minimum height and distance filters.')
indexes, _ = scipy.signal.find_peaks(vector, height=7, distance=2.1)
print('Peaks are: %s' % (indexes))This function was added to SciPy in version 1.1.0 and is comparable to findpeaks provided in Matlab's Signal Processing Toolbox.
scipy.signal.find_peaks searches for peaks (local maxima) based on simple value comparison of neighbouring samples and returns those peaks whose properties match optionally specified conditions (minimum and / or maximum) for their height, prominence, width, threshold and distance to each other.
On the prominence parameter, see this explanation.
import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
from libs import detect_peaks
print('Detect peaks with minimum height and distance filters.')
indexes = detect_peaks.detect_peaks(vector, mph=7, mpd=2)
print('Peaks are: %s' % (indexes))Documentation. Source. Sample code.
This algorithm comes from a notebook written by Marcos Duarte and is pretty trivial to use.
The function has an interface very similar and consistent results with the MatLab Signal Processing Toolbox findpeaks, yet with less complete filtering and tuning support.
import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
import peakutils.peak
print('Detect peaks with minimum height and distance filters.')
indexes = peakutils.peak.indexes(np.array(vector),
thres=7.0/max(vector), min_dist=2)
print('Peaks are: %s' % (indexes))Documentation. Package. Sample code.
This algorithm can be used as an equivalent of the MatLab findpeaks and will give easily give consistent results if you only need minimal distance and height filtering.
import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
from libs import peakdetect
print('Detect peaks with distance filters.')
peaks = peakdetect.peakdetect(np.array(vector), lookahead=2, delta=2)
# peakdetect returns two lists, respectively positive and negative peaks,
# with for each peak a tuple of (indexes, values).
indexes = []
for posOrNegPeaks in peaks:
for peak in posOrNegPeaks:
indexes.append(peak[0])
print('Peaks are: %s' % (indexes))Source and documentation. Sample code.
The algorithm was written by sixtenbe based on the previous work of endolith and Eli Billauer.
Easy to setup as it comes in a single source file, but the lookahead parameter make it hard to use on low-sampled signals or short samples. May miss filtering capacities (only minimum peak distance with the delta parameter).
…Documentation. oct2py package. Sample code.
Use findpeaks from the Octave-Forge signal package through the oct2py bridge. This algorithm allows to make a double sided detection, which means it will detect both local maxima and minima in a single run.
Requires a rather complicated and not very efficient setup to be called from Python code. Of course, you will need an up-to-date distribution of Octave, with the signal package installed from Octave-Forge.
Although the function have an interface close to the MatLab findpeaks, it is harder to have the exact same results that with detect_peaks or peakutils.peak.indexes.
import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
from libs.findpeaks import findpeaks
indexes = findpeaks(np.array(vector), spacing=2, limit=7)
print('Peaks are: %s' % (indexes))Documentation. Source. Sample code.
Small and fast peak detection algorithm, with minimum distance and height filtering support. Comes as an handy single function, depending only on Numpy.
Contrary to the MatLab findpeaks-like distance filters, the Janko Slavic findpeaks spacing param requires that all points within the specified width to be lower than the peak. If you work on very low sampled signal, the minimum distance filter may miss fine granularity tuning.
import numpy as np
vector = [
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ]
from libs.tony_beltramelli_detect_peaks import detect_peaks
print('Detect peaks with height threshold.')
indexes = detect_peaks(vector, 1.5)
print('Peaks are: %s' % (indexes))Source and documentation. Sample code.
Straightforward, simple and lightweight peak detection algorithm, with minimum distance filtering support.
No minimum peak height filtering support.
import numpy as np
import scipy.signal
vector = [0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8,
13, 8, 10, 3, 1, 20, 7, 3, 0]
print('Detect peaks with minimum distance filter.')
indexes = mlpy.findpeaks_dist(vector, mindist=2.1)
print('Peaks are: %s' % (indexes))Find peaks, with a minimum distance filter between peaks. Code written by Davide Albanese.
import numpy as np
import scipy.signal
vector = [0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8,
13, 8, 10, 3, 1, 20, 7, 3, 0]
print('Detect peaks with sliding window of 5.')
indexes = mlpy.findpeaks_win(vector, span=5)
print('Peaks are: %s' % (indexes))Find peaks, with a sliding window of specified width. Code written by Davide Albanese.
Most algorithms detect only local maximas. You may want to detect both minimas and maximas.
One solution is to invert the signal before feeding it to the algorithm for detecting lows, as suggested
No open issues yet, or sync has not completed.