Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding peaks at data borders

I would like to use scipy.signal.find_peaks to find peaks in some 2D data but the function defaults to ignore all edge peaks (peaks that occur on the left or right border of the array). Is there a way to include edge peaks in the results?

For example, I would like this code:

find_peaks([5,4,3,3,2,3])

to produce

[0,5]

but instead it produces

[]
like image 879
DanGoodrick Avatar asked Nov 06 '22 16:11

DanGoodrick


1 Answers

The findpeaks library can detect the edges.

pip install findpeaks

Example:

X = [5,4,3,2,1,2,3,3,2,3,2,4,5,6]
# initialize with smoothing parameter
fp = findpeaks(lookahead=1, interpolate=5)
# fit
results=fp.fit(X)
# Plot
fp.plot()

# Print the xy coordinates.
# The first and last one in the array are the edges.
print(results['df'])

# x  y  labx    valley  peak    labx_topology   valley_topology peak_topology
# 0  5  1.0 True    False   1.0 True    True
# 1  4  1.0 False   False   1.0 False   False
# 2  3  1.0 False   False   1.0 False   False
# 3  2  1.0 False   False   1.0 False   False
# 4  1  1.0 True    False   1.0 True    False
# 5  2  2.0 False   False   2.0 False   False
# 6  3  2.0 False   True    2.0 False   True
# 7  3  2.0 False   False   2.0 False   False
# 8  2  2.0 True    False   2.0 True    False
# 9  3  3.0 False   True    3.0 False   True
# 10 2  3.0 True    False   3.0 True    False
# 11 4  4.0 False   False   4.0 False   False
# 12 5  4.0 True    False   4.0 True    False
# 13 6  4.0 False   False   4.0 False   True

Fit is done on a smoothed vector: smoothed vector

Final results are projected back to the original vector: input image

like image 56
erdogant Avatar answered Nov 13 '22 21:11

erdogant