scipy 计算时间序列中的峰值

0aydgbwb  于 2023-01-20  发布在  其他
关注(0)|答案(3)|浏览(169)

我正在计算一个 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]),)

此输出错误地将数据点计数为峰。

    • 理想输出**

理想输出应返回清晰峰值的数量,在本例中为11,这些峰值位于索引处:

[1,7,13,19,25,31,37,44,50,56,62]

我相信我的问题是由于argrelextrema函数的聚合性质引起的。

fdbelqdn

fdbelqdn1#

您应该在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)
print("Peaks position:", peaks[0])

# Plotting
plt.plot(a)
plt.title("Finding Peaks")

[plt.axvline(p, c='C3', linewidth=0.3) for p in peaks[0]]

plt.show()

情节

输出

# Peaks position: [ 1  7 13 19 25 31 37 44 50 56 62]
s4chpxco

s4chpxco2#

可以使用一些阈值来查找峰:

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]
ut6juiuv

ut6juiuv3#

看起来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])

相关问题