DanGoodrick
DanGoodrick

Reputation: 3208

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

[]

Upvotes: 7

Views: 2457

Answers (2)

erdogant
erdogant

Reputation: 1694

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

Upvotes: 1

Pete
Pete

Reputation: 421

Depending on the more detailed usage of find_peaks, I would suggest to repeat data at the beginning and end of the array: e.g.

peak_info = find_peaks([4,5,4,3,3,2,3,2])
# correct for additional initial element in auxiliary input array
peaks = peak_info[0] - 1

If for example the minimum required peak width parameter for find_peaks is set, it might even make sense to repeat the reversed array at both beginning and end.

find_peaks will generally only identify peaks, if there are lower data values both to the left and right. It will hence not identify any peaks in [5,5,4,3,3,2,3,3] (i.e. repeating the first and last array elements, respectively) either. Repetition of the second and but last elements in the beginning and at the end of the array or inserting lower values at beginning and end would enable identifying such boundary points as peaks.

Upvotes: 0

Related Questions