Skip to content

Overview of the peaks dectection algorithms available in Python

License

Notifications You must be signed in to change notification settings

nivertech/py-findpeaks

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Overview

Algorithm Integration Filters MatLab findpeaks-like?
scipy.signal.find_peaks_cwt Included in Scipy ?
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 peak distance
Octave-Forge findpeaks Requires an Octave-Forge distribution
+ PyPI package oct2py
Depends on Scipy
Minimum distance
Minimum height
Minimum peak width

How to make your choice?

When you're selecting an algorithm, you might consider:

  • The function interface. You may want the function to work natively with Numpy arrays or may search something similar to other platform algorithms, like the MatLab findpeaks.
  • The dependencies. Does it require extra dependency? Does is it easy to make it run on a fresh box?
  • The filtering support. Does the algorithm allows to define multiple filters? Which ones do you need?

## scipy.signal.find_peaks_cwt

import numpy as np
from vector import vector, plot_peaks
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))

Documentation. Sample code.

The peak detection algorithm from the Scipy signal processing package. It appears like the obvious choice when you already work with Scipy, but may in fact not be as it uses a wavelet convolution approach.

Thus this function requires to understand wavelets to be well used, which is less trivial and direct than other algorithms. However this can a good choice on noisy data.

detect_peaks from Marcos Duarte

import numpy as np
from vector import vector, plot_peaks
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.

peakutils.peak.indexes

import numpy as np
from vector import vector, plot_peaks
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.

peakdetect from sixtenbe

import numpy as np
from vector import vector, plot_peaks
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).

Octave-Forge findpeaks

import numpy as np
from vector import vector, plot_peaks
from oct2py import octave
# Load the Octage-Forge signal package.
octave.eval("pkg load signal")
print('Detect peaks with minimum height and distance filters.')
(pks, indexes) = octave.findpeaks(np.array(vector), 'DoubleSided',
    'MinPeakHeight', 6, 'MinPeakDistance', 2, 'MinPeakWidth', 0)
# The results are in a 2D array and in floats: get back to 1D array and convert
# peak indexes to integer. Also this is MatLab-style indexation (one-based),
# so we must substract one to get back to Python indexation (zero-based).
indexes = indexes[0].astype(int) - 1
print('Peaks are: %s' % (indexes))

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 maximam 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.


Contribute

Feel free to open a new ticket or submit a PR to improve this overview.

Happy processing!

About

Overview of the peaks dectection algorithms available in Python

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%