Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find troughs in a 1-D array

I've looked around StackOverflow and I noticed that a lot of the question are focused about finding peaks (not so many on finding the troughs). As of right now In order to find peaks, I'm using:

scipy.signal.find_peaks()

Which output the peaks and their index. That being said I'm wondering if there is anything similar to this function to find the troughs.

Thank you very much for your help

like image 469
Nazim Kerimbekov Avatar asked Dec 07 '22 12:12

Nazim Kerimbekov


2 Answers

Is scipy.signal.find_peaks(-x) what you need?

like image 187
Imperishable Night Avatar answered Dec 17 '22 04:12

Imperishable Night


A quick example. That expands on the sample code in https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html#scipy.signal.find_peaks

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
x = electrocardiogram()[200:300]
peaks, _= find_peaks(x)
troughs, _= find_peaks(-x)
plt.plot(x)
plt.plot(peaks,x[peaks], '^')
plt.plot(troughs,x[troughs], 'v')
plt.show()
like image 37
coremonkey Avatar answered Dec 17 '22 05:12

coremonkey