[英]A way to get the local minima and maxima of a vector
I've been trying to retrieve the extrema of a vector that looks like this :我一直在尝试检索看起来像这样的向量的极值:
[![First case][1]][1] [![第一种情况][1]][1]
(source: noelshack.com ) (来源: noelshack.com )
or like this : [![Second case][2]][2]或像这样:[![第二种情况][2]][2]
(source: noelshack.com ) (来源: noelshack.com )
I've been trying to retrieve local maxima and minima, it works well with : (diff(sign(diff(values_right_vector))) > 0).nonzero()[0] + 1
but afterwards it is only workaround and workaround because there is always a case where my previous workaround fails..我一直在尝试检索局部最大值和最小值,它适用于: (diff(sign(diff(values_right_vector))) > 0).nonzero()[0] + 1
但之后它只是解决方法和解决方法,因为有总是我以前的解决方法失败的情况..
It has always this same pattern.它始终具有相同的模式。
Do you have any ideas how can I retrieve those maxima and minima no matter the input vector ( left
and right
) on the image.无论图像上的输入向量( left
和right
)如何,您有什么想法如何检索这些最大值和最小值。
Here is a sample :这是一个示例:
[-2.7, -2.5, -2.1, -2.1, -1.8, -1.4, -0.9, -0.2, 0.5, 1.4, 2.2, 2.9, 3.5, 3.8, 3.8, 3.3, 2.3, 1.1, -0.5, -2.1, -3.5, -4.7, -5.5, -5.8, -5.6, -5.0, -4.2, -3.3, -2.3, -1.4, -0.8, -0.3, 0.0, 0.2, 0.2, 0.2, 0.1, 0.0, 0.0, 0.0, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.2, -0.2, -0.2, -0.2, -0.2, -0.2, -0.2, -0.1, -0.1, -0.1, -0.1, -0.1, -0.2, -0.3, -0.4, -0.4, -0.5, -0.4, -0.3, -0.1, 0.2, 0.5, 0.7, 0.9, 0.9, 1.0, 0.9, 0.9, 0.9, 0.8, 0.7, 0.6, 0.3, 0.0, -0.4, -0.9, -1.3, -1.5, -1.6, -1.5, -1.1, -0.5, 0.2, 1.2, 2.1, 3.0, 3.8, 4.3, 4.3, 4.0, 3.2, 1.9, 0.4, -1.3, -3.0, -4.4, -5.4, -6.0, -6.0, -5.6, -4.8, -3.9, -2.9, -1.9, -1.2, -0.6, -0.2, 0.0, 0.1, 0.1, 0.1, 0.0, 0.0, -0.1, -0.1, -0.1, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1, -0.1, -0.1, -0.2, -0.2, -0.2, -0.2, -0.1, -0.1, 0.0, 0.0, 0.0, 0.0, -0.1, -0.3, -0.5, -0.7, -0.9, -1.1, -1.1, -1.0, -0.8, -0.4, 0.3, 1.1, 1.9, 2.8, 3.6, 4.2, 4.5, 4.5, 4.1, 3.4, 2.5, 1.5, 0.5, -0.5, -1.4, -2.1, -2.8, -3.3, -3.7, -3.9, -3.9, -3.8, -3.4, -2.9, -2.2, -1.3, -0.4, 0.7, 1.7, 2.5, 3.2, 3.6, 3.6, 3.2, 2.4, 1.3, -0.1, -1.6, -3.0, -4.1, -4.9, -5.1, -5.0, -4.4, -3.6, -2.7, -1.8, -1.1, -0.5, -0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.0, -0.1, -0.1]```
[1]: https://i.stack.imgur.com/O3er1.png
[2]: https://i.stack.imgur.com/cbqNK.png
Scipy has a find_peaks
function that you can manipulate to find the peaks you want using the distance
parameter. Scipy 有一个find_peaks
函数,您可以操作该函数以使用distance
参数找到所需的峰。 The distance
parameter tells Scipy how many spaces between samples it should look for peaks. distance
参数告诉 Scipy 它应该在样本之间寻找峰值的空间。 You can adjust this to fit your data best.您可以调整它以最适合您的数据。 Using just the sample data you provided,仅使用您提供的样本数据,
import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt
y = np.array([-2.7, -2.5, -2.1, -2.1, -1.8, -1.4, -0.9, -0.2, 0.5, 1.4, 2.2, 2.9, 3.5, 3.8, 3.8, 3.3, 2.3, 1.1, -0.5, -2.1, -3.5, -4.7, -5.5, -5.8, -5.6, -5.0, -4.2, -3.3, -2.3, -1.4, -0.8, -0.3, 0.0, 0.2, 0.2, 0.2, 0.1, 0.0, 0.0, 0.0, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.2, -0.2, -0.2, -0.2, -0.2, -0.2, -0.2, -0.1, -0.1, -0.1, -0.1, -0.1, -0.2, -0.3, -0.4, -0.4, -0.5, -0.4, -0.3, -0.1, 0.2, 0.5, 0.7, 0.9, 0.9, 1.0, 0.9, 0.9, 0.9, 0.8, 0.7, 0.6, 0.3, 0.0, -0.4, -0.9, -1.3, -1.5, -1.6, -1.5, -1.1, -0.5, 0.2, 1.2, 2.1, 3.0, 3.8, 4.3, 4.3, 4.0, 3.2, 1.9, 0.4, -1.3, -3.0, -4.4, -5.4, -6.0, -6.0, -5.6, -4.8, -3.9, -2.9, -1.9, -1.2, -0.6, -0.2, 0.0, 0.1, 0.1, 0.1, 0.0, 0.0, -0.1, -0.1, -0.1, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1, -0.1, -0.1, -0.2, -0.2, -0.2, -0.2, -0.1, -0.1, 0.0, 0.0, 0.0, 0.0, -0.1, -0.3, -0.5, -0.7, -0.9, -1.1, -1.1, -1.0, -0.8, -0.4, 0.3, 1.1, 1.9, 2.8, 3.6, 4.2, 4.5, 4.5, 4.1, 3.4, 2.5, 1.5, 0.5, -0.5, -1.4, -2.1, -2.8, -3.3, -3.7, -3.9, -3.9, -3.8, -3.4, -2.9, -2.2, -1.3, -0.4, 0.7, 1.7, 2.5, 3.2, 3.6, 3.6, 3.2, 2.4, 1.3, -0.1, -1.6, -3.0, -4.1, -4.9, -5.1, -5.0, -4.4, -3.6, -2.7, -1.8, -1.1, -0.5, -0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.0, -0.1, -0.1])
# Get the maxima and minima
maxima, _ = find_peaks(y, distance = 50)
minima, _ = find_peaks(-y, distance = 50)
find_peaks
returns the indices of the peaks, which is why we can use -y
to get the minima. find_peaks
返回峰值的索引,这就是为什么我们可以使用-y
来获取最小值。
You can also index the maxima
and minima
to select the peaks you want, by doing something like maxima[::2]
to select every other maxima.您还可以索引maxima
和minima
以选择所需的峰值,方法是执行诸如maxima[::2]
之类的操作来选择每隔一个最大值。
fig, ax = plt.subplots()
ax.plot(y)
ax.plot(maxima, y[maxima], 'x')
ax.plot(minima, y[minima], 'x')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.