我正在计算一个numpy数组中的波峰和波谷数。
我有一个类似的numpy数组:
doLast
绘制该数据,看起来像这样:
我正在寻找该时间序列中的峰数:
这是我的代码,在类似这样的示例中效果很好,在该示例中,时间序列表示中有明显的高峰和低谷。我的代码返回找到峰的数组的索引。
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)
已经找到8个清晰的峰,并且可以正确计数。
我的解决方案似乎无法与清晰程度较低且更混乱的数据配合使用。
下面的数组不能很好地工作,并且找不到我需要的峰:
(array([ 2, 7, 10, 14, 17, 20, 23, 26]),)
绘制,此数据如下:
相同的代码返回:
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]),)
我相信我的问题是由于argrelextrema函数的聚合性质引起的。
答案 0 :(得分:1)
似乎argrelextrema
会为您提供大部分帮助。它具有您想要的所有峰,但也有一些额外的峰。您需要提出适合您情况的标准,并过滤掉不需要的峰。
例如,如果您不希望峰小于5,则可以执行以下操作:
In [17]: result = argrelextrema(a, np.greater)
In [18]: result
Out[18]:
(array([ 1, 4, 7, 11, 13, 15, 19, 23, 25, 28, 31, 34, 37, 40, 44, 50, 53,
56, 59, 62]),)
In [19]: result[0][a[result[0]] > 5]
Out[19]: array([ 1, 7, 13, 19, 25, 31, 37, 44, 50, 56, 62])
答案 1 :(得分:1)
您应该在 scipy.signal 模块中尝试 find_peaks
import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt
a = np.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])
# Here you should fine tune parameters to get what you want
peaks = find_peaks(a, prominence=1.5)
# Plotting
plt.plot(a)
plt.title("Finding Peaks")
[plt.axvline(p, c='C3', linewidth=0.3) for p in peaks[0]]
plt.show()