import numpy as np
array = np.array([1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1])
# Check that it is bigger than either of it's neighbors exluding edges:
max = (array[1:-1] > array[:-2]) & (array[1:-1] > array[2:])
# Print these values
print(array[1:-1][max])
# Locations of the maxima
print(np.arange(1, array.size-1)[max])
import numpy as np
from scipy.signal import argrelextrema
a = np.array([1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1])
# determine the indices of the local maxima
maxInd = argrelextrema(a, np.greater)
# get the actual values using these indices
r = a[maxInd] # array([5, 3, 6])
如果原始数据是有噪声的,那么最好使用统计方法,因为并非所有的峰值都是显著的。对于您的
a
数组,可能的解决方案是使用双微分:如果可以排除数组边缘的maxima,则始终可以通过检查以下内容来检查一个元素是否大于其每个相邻元素:
函数^{} 中存在一个bulit,它完成了此任务:
这将为
r
提供所需的输出。从SciPy版本1.1开始,您还可以使用find_peaks。下面是从文档本身获取的两个示例。
使用
height
参数,可以选择高于某个阈值的所有最大值(在本例中,所有非负最大值;如果必须处理噪声基线,这非常有用;如果要找到最小值,只需将输入乘以-1
):另一个非常有用的参数是
distance
,它定义了两个峰值之间的最小距离:相关问题 更多 >
编程相关推荐