argrelextrema 和平坦极值 [英] argrelextrema and flat extrema

查看:151
本文介绍了argrelextrema 和平坦极值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 scipy.signal 的函数 argrelextrema 不检测平坦极值.示例:

将 numpy 导入为 np从 scipy.signal 导入 argrelextrema数据 = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 1, 0 ])argrelextrema(数据,np.greater)(数组([2]),)

检测到第一个最大值 (2),未检测到第二个最大值 (3, 3).

这种行为有什么解决方法吗?谢谢.

解决方案

简短回答: 可能 argrelextrema 对您的任务不够灵活.考虑编写符合您需求的自己的函数.

<小时>

更长的答案:你一定要使用argrelextrema吗?如果是,那么您可以使用 comparatorargrelextremaorder 参数(请参阅 reference).

对于您的简单示例,选择 np.greater_equal 作为 comparator 就足够了.

<预><代码>>>>数据 = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 1, 0 ])>>>打印(argrelextrema(数据,np.greater_equal,order=1))(数组([2, 6, 7]),)

但是请注意,以这种方式

<预><代码>>>>数据 = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 4, 1, 0 ])>>>打印(argrelextrema(数据,np.greater_equal,order=1))(数组([2, 6, 8]),)

行为与您可能喜欢的不同,找到第一个 34 作为最大值,因为 argrelextrema 现在将所有内容视为最大值大于或等于它的两个最近的邻居.您现在可以使用 order 参数来决定此比较必须保留多少个邻居 - 选择 order=2 会将我上面的示例更改为仅找到 4 为最大值.

<预><代码>>>>打印(argrelextrema(数据,np.greater_equal,order=2))(数组([2, 8]),)

但是,这有一个缺点 - 让我们再次更改数据:

<预><代码>>>>数据 = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 4, 1, 5 ])>>>打印(argrelextrema(数据,np.greater_equal,order=2))(数组([ 2, 10]),)

添加另一个峰值作为最后一个值可防止您在 4 处找到峰值,因为 argrelextrema 现在看到大于 4 的第二个邻居(这对嘈杂的数据很有用,但不一定是所有情况下预期的行为).

<小时>

使用argrelextrema,您将始终受限于固定数量的邻居之间的二元运算.但是请注意,如果 data[n] > 在上面的示例中,所有 argrelextrema 所做的就是返回 n.数据[n-1] 和数据[n] >数据[n+1].您可以自己轻松实现这一点,然后细化规则,例如检查第二个邻居,以防第一个邻居具有相同的值.

<小时>

为了完整起见,scipy.signalfind_peaks_cwt.但是我没有使用它的经验,因此无法为您提供更多详细信息.

Function argrelextrema from scipy.signal does not detect flat extrema. Example:

import numpy as np
from scipy.signal import argrelextrema
data = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 1, 0 ])
argrelextrema(data, np.greater)
(array([2]),)

the first max (2) is detected, the second max (3, 3) is not detected.

Any workaround for this behaviour? Thanks.

解决方案

Short answer: Probably argrelextrema will not be flexible enough for your task. Consider writing your own function matching your needs.


Longer answer: Are you bound to use argrelextrema? If yes, then you can play around with the comparator and the order arguments of argrelextrema (see the reference).

For your easy example, it would be enough to chose np.greater_equal as comparator.

>>> data = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 1, 0 ])
>>> print(argrelextrema(data, np.greater_equal,order=1))
(array([2, 6, 7]),)

Note however that in this way

>>> data = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 4, 1, 0 ])
>>> print(argrelextrema(data, np.greater_equal,order=1))
(array([2, 6, 8]),)

behaves differently that you would probably like, finding the first 3 and the 4 as maxima, since argrelextrema now sees everything as a maximum that is greater or equal to its two nearest neighbors. You can now use the order argument to decide to how many neighbors this comparison must hold - choosing order=2 would change my upper example to only find 4 as a maximum.

>>> print(argrelextrema(data, np.greater_equal,order=2))
(array([2, 8]),)

There is, however, a downside to this - let's change the data once more:

>>> data = np.array([ 0, 1, 2, 1, 0, 1, 3, 3, 4, 1, 5 ])
>>> print(argrelextrema(data, np.greater_equal,order=2))
(array([ 2, 10]),)

Adding another peak as a last value keeps you from finding your peak at 4, as argrelextrema is now seeing a second-neighbor that is greater than 4 (which can be useful for noisy data, but not necessarily the behavior expected in all cases).


Using argrelextrema, you will always be limited to binary operations between a fixed number of neighbors. Note, however, that all argrelextrema is doing in your example above is to return n, if data[n] > data[n-1] and data[n] > data[n+1]. You could easily implement this yourself, and then refine the rules, for example by checking the second neighbor in case that the first neighbor has the same value.


For the sake of completeness, there seems to be a more elaborate function in scipy.signal, find_peaks_cwt. I have however no experience using it and can therefore not give you more details about it.

这篇关于argrelextrema 和平坦极值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆