shravan
shravan

Reputation: 35

Lower peaks in python

I have a column with values ranging from 20 to 45 and the average value of 35. I want to identify upper and lower peaks with a threshold. I am able to find upper peaks, but not lower peaks. I have tried the below code:

from scipy.signal import find_peaks
vector=Agitator['AGITATOR AMPS'].values
peaks, _ = find_peaks(vector, height=53)
plt.figure(figsize=(15,5))
plt.plot(vector)
plt.plot(peaks,vector[peaks],"o")
plt.show()

How can I improve upon this?

Upvotes: 1

Views: 2728

Answers (1)

ThomaS
ThomaS

Reputation: 895

find_peaks only look for local maxima (and not minima)

You can use find peaks on the negative of your data -> find_peaks(-vector)

    Array = np.random.uniform(20,45,size=(50))
    peaks, _ = find_peaks(Array, height=20)
    peaks2, _ = find_peaks(-Array)
    plt.plot(Array)
    plt.plot(peaks,Array[peaks],"o")
    plt.plot(peaks2,Array[peaks2],"o")

result_graph

It seems to create complication when using the height argument with negative value though. But if you just add the maximal value like find_peaks(-vector+max(vector)) it should work fine:

    Array = np.random.uniform(20,45,size=(50))
    peaks, _ = find_peaks(Array, height=20)
    peaks2, _ = find_peaks(-Array+45, height=20)
    plt.plot(Array)
    plt.plot(peaks,Array[peaks],"o")
    plt.plot(peaks2,Array[peaks2],"o")

result_graph

Also I think your height at 53 is wrong with range from 20 to 45, because 45-20 < 53.

Upvotes: 3

Related Questions