Find all local Maxima and Minima when x and y values are given as numpy arrays

时间:2015-06-26 10:12:49

标签: python numpy derivative

I have two arrays x and y as :

x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3, 5, 3, 9, 8, 10, 7])

I am finding index of local minima and maxima as follows:

sortId = np.argsort(x)
x = x[sortId]
y = y[sortId]
minm = np.array([])
maxm = np.array([])
while i < y.size-1:
   while(y[i+1] >= y[i]):
      i = i + 1

   maxm = np.insert(maxm, 0, i)
   i++
   while(y[i+1] <= y[i]):
      i = i + 1

   minm = np.insert(minm, 0, i)
   i++

What is the problem in this code? The answer should be index of minima = [2, 5, 7] and that of maxima = [1, 3, 6].

3 个答案:

答案 0 :(得分:19)

根本不需要这个while循环。下面的代码将为您提供所需的输出;它会找到所有本地最小值和所有本地最大值,并分别将它们存储在minmmaxm中。请注意:将其应用于大型数据集时,请务必先将信号平滑;否则你最终会陷入极端的浪潮。

import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt

x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3 ,5 ,3 ,9 ,8, 10, 7])

# sort the data in x and rearrange y accordingly
sortId = np.argsort(x)
x = x[sortId]
y = y[sortId]

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.show()
maxm = argrelextrema(y, np.greater)  # (array([1, 3, 6]),)
minm = argrelextrema(y, np.less)  # (array([2, 5, 7]),)

这应该比上面的while循环更有效。

情节看起来像这样;我移动了x值,使它们与minmmaxm中返回的索引相对应:

enter image description here

从SciPy 1.1版开始,您还可以使用find_peaks

from scipy.signal import find_peaks

peaks, _ = find_peaks(y)

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.plot(peaks, y[peaks], "x")
plt.show()

产生

enter image description here

好消息是,您现在也可以轻松设置最小峰高(例如8):

peaks, _ = find_peaks(y, height=8)

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.plot(peaks, y[peaks], "x")
plt.show() 

enter image description here

请注意,现在排除第一个峰值,因为它的高度低于8.

此外,您还可以设置峰之间的最小距离(例如5):

peaks, _ = find_peaks(y, distance=5)

# this way the x-axis corresponds to the index of x
plt.plot(x-1, y)
plt.plot(peaks, y[peaks], "x")
plt.show()

enter image description here

现在排除中间峰值,因为它与其他两个峰值的距离小于5.

答案 1 :(得分:1)

x=np.array([6,3,5,2,1,4,9,7,8])
y=np.array([2,1,3,5,7,9,8,10,7])

sort_idx = np.argsort(x)
y=y[sort_idx]
x=x[sort_idx]
minm=np.array([],dtype=int)
maxm=np.array([],dtype=int)
length = y.size
i=0

while i < length-1:
    if i < length - 1:
        while i < length-1 and y[i+1] >= y[i]:
            i+=1

        if i != 0 and i < length-1:
            maxm = np.append(maxm,i)

        i+=1

    if i < length - 1:
        while i < length-1 and y[i+1] <= y[i]:
            i+=1

        if i < length-1:
            minm = np.append(minm,i)
        i+=1


print minm
print maxm

minmmaxm分别包含最小值和最大值的索引。

答案 2 :(得分:0)

This will work fine.

Python uses += instead of ++.

Before you use i in a while loop you have to assign some value - in this case 0 - , this way initializing it to avoid error.

import numpy as np

x=np.array([6,3,5,2,1,4,9,7,8])
y=np.array([2,1,3,5,3,9,8,10,7])


sortId=np.argsort(x)
x=x[sortId]
y=y[sortId]
minm = np.array([])
maxm = np.array([])
i = 0
while i < y.size-1:
   while(y[i+1] >= y[i]):
      i+=1

   maxm=np.insert(maxm,0,i)
   i+=1
   while(y[i+1] <= y[i]):
      i+=1

   minm=np.insert(minm,0,i)
   i+=1

print minm, maxm