How to find only "sharp peaks" of a signal in python?

Viewed 677

I am trying to detect level shifts in signals automatically by detecting peaks in the difference between two rolling windows.

I basically want to detect peaks which are "sharp" and don't have a wide plateau.

I want to detect the peaks in the following signals

enter image description here

But not the "wide" peak in the following image

enter image description here

I know of the functions such as find_peaks from scipy.signals but without specifying any parameter it also picks up the wide peaks in my signals.

So are there any of its parameters one should tune to only pick up "sharp" peaks? Or could I do some specific preprocessing step to the data first to make it easier to pick?

Appreciate any input!

1 Answers

Thescipy.signal.find_peaks function accepts a width parameter. If the parameter is a tuple, then the first value in the tuple is the minimum width and the second value is the maximum width. (e.g. width=(0,10)). The width specification is with respect to the "sequence" axis (i.e., list(range(len(data))), not any other axis (for instance your date axis in your second example).

Another option is scipy.signal.argrelextrema. For the comparator, you can use np.greater. The minimum peak width will be controlled by the order parameter, where min_width=order*2+1. You can calculate the width of each peak by descending the slope on either side until the data begins to rise again. You can then loop through your peaks and filter based on width requirements.

Your best bet is probably scipy.signal.find_peaks.

Related