我正在计算一个numpy数组中的波峰和波谷数.
我有一个像这样的numpy数组:
stack = np.array([0,0,5,4,1,1,1,5,1,1,5,1,1,1,5,1,1,5,1,1,5,1,1,5,1,1,5,1,1])
绘制后,此数据看起来像这样:
我正在寻找此时间序列中的峰值数量:
这是我的代码,在这样的示例中效果很好,在该示例中,时间序列表示中有明显的高峰和低谷.我的代码返回找到峰值的数组的索引.
#example
import numpy as np
from scipy.signal import argrelextrema
stack =
np.array([0,0,5,4,1,1,1,5,1,1,5,1,1,1,5,1,1,5,1,1,5,1,1,5,1,1,5,1,1])
# for local maxima
y = argrelextrema(stack, np.greater)
print(y)
结果:
(array([ 2, 7, 10, 14, 17, 20, 23, 26]),)
已找到8个清晰的峰,并且可以正确计数.
我的解决方案似乎无法与清晰程度较低且更混乱的数据配合使用.
下面的数组不能很好地工作,并且找不到我需要的峰:
array([ 0. , 5.70371806, 5.21210157, 3.71144767, 3.9020162 ,
3.87735984, 3.89030171, 6.00879918, 4.91964227, 4.37756275,
4.03048542, 4.26943028, 4.02080471, 7.54749062, 3.9150576 ,
4.08933851, 4.01794766, 4.13217794, 4.15081972, 8.11213474,
4.6561735 , 4.54128693, 3.63831552, 4.3415324 , 4.15944019,
8.55171441, 4.86579459, 4.13221943, 4.487663 , 3.95297979,
4.35334706, 9.91524674, 4.44738182, 4.32562141, 4.420753 ,
3.54525697, 4.07070637, 9.21055852, 4.87767969, 4.04429321,
4.50863677, 3.38154581, 3.73663523, 3.83690315, 6.95321174,
5.11325128, 4.50351938, 4.38070175, 3.20891173, 3.51142661,
7.80429569, 3.98677631, 3.89820773, 4.15614576, 3.47369797,
3.73355768, 8.85240649, 6.0876192 , 3.57292324, 4.43599135,
3.77887259, 3.62302175, 7.03985076, 4.91916556, 4.22246518,
3.48080777, 3.26199699, 2.89680969, 3.19251448])
绘制此数据,如下所示:
并返回相同的代码:
(array([ 1, 4, 7, 11, 13, 15, 19, 23, 25, 28, 31, 34, 37, 40, 44, 50, 53,
56, 59, 62]),)
该输出错误地将数据点计数为峰值.
理想输出
理想的输出应返回清晰峰的数量,在这种情况下,清晰峰的数量位于索引处:
[1,7,13,19,25,31,37,44,50,56,62]
我相信我的问题是由于argrelextrema函数的聚合性质而引起的.
解决方法:
您可以使用一些阈值来查找峰:
prev = stack[0] or 0.001
threshold = 0.5
peaks = []
for num, i in enumerate(stack[1:], 1):
if (i - prev) / prev > threshold:
peaks.append(num)
prev = i or 0.001
print(peaks)
# [1, 7, 13, 19, 25, 31, 37, 44, 50, 56, 62]