'Find local maxima using scipy.signal
I have a 1D array (A) of 64 integer values. How can I find the 2 local maxima corresponding to the values 56 and 50 (indices 10 and 45, respectively) using the scipy.signal module?
At first I tried importing
from scipy.signal import find_peaks
A
array([ 0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.])
but before I got any further I got the error message that
"AttributeError: 'module' object has no attribute 'find_peaks'",
so then instead I tried importing
from scipy import signal
peakind = signal.find_peaks_cwt(A, widths=32)
but then I get the error message that
"TypeError: 'int' object is not subscriptable".
I still get this error message even if I first do A.astype(np.int64).
Can't I use scipy.signal_find_peaks on an array of integer values?
Solution 1:[1]
For peak detection (1d-vectors or 2d-arrays (images)), you can use the library findpeaks. This library contains multiple peak detection methods.
pip install findpeaks
A = [ 0., 1., 3., 8., 6., 16., 29., 29., 47., 42., 56.,
44., 49., 40., 34., 34., 26., 24., 25., 21., 22., 24.,
34., 17., 17., 29., 24., 26., 13., 25., 16., 19., 19.,
26., 24., 26., 41., 34., 24., 37., 37., 39., 34., 40.,
45., 50., 28., 45., 43., 46., 47., 41., 30., 23., 19.,
15., 9., 10., 3., 6., 4., 1., 1., 0.]
Topology method
from findpeaks import findpeaks
fp = findpeaks(method="topology", )
results = fp.fit(A)
results['df']
# For every datapoint, it returns whether it is a peak or valley.
labx peak valley y x
0 0 False False 0.0 0
1 1 False True 1.0 1
2 1 False False 3.0 2
3 1 False False 8.0 3
4 1 False False 6.0 4
.. ... ... ... ... ..
59 0 False False 6.0 59
60 3 True False 4.0 60
61 0 False False 1.0 61
62 0 False False 1.0 62
63 0 False False 0.0 63
'''
fp.plot()
Peakdetect method
fp = findpeaks(method="peakdetect", lookahead=10)
results = fp.fit(A)
results['df']
fp.plot()
caerus method
fp = findpeaks(method="caerus", params={'minperc':10, 'window':10})
results = fp.fit(A)
results['df']
fp.plot()
Disclaimer: I am the author of this library
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



