find_peaks 未识别数组开头的峰
find_peaks does not identify a peak at the start of the array
我正在尝试找到一种矢量化方法来找到数组中第一个位置,其中的值不高于前面 n 个数字的最大值。我想过用scipy.signal的find_peaks方法求局部最大值。我认为如果您将距离定义为 10 n 就是 10,那么它就是这样做的。但不幸的是,距离的条件必须在两个方向上都满足 - 先前和即将到来的数字。有没有其他方法或方法可以找到这样的东西?
示例:
arr1 = np.array([1. , 0.73381293, 0.75649351, 0.77693474, 0.77884614,
0.81055903, 0.81402439, 0.78798586, 0.78839588, 0.82967961,
0.8448 , 0.83276451, 0.82539684, 0.81762916, 0.82722515,
0.82101804, 0.82871127, 0.82825041, 0.82086957, 0.8347826 ,
0.82666665, 0.82352942, 0.81270903, 0.81191224, 0.83180428,
0.84975767, 0.84044236, 0.85057473, 0.8394649 , 0.80000001,
0.83870965, 0.83962262, 0.85039371, 0.83359748, 0.84019768,
0.83281732, 0.83660132])
from scipy.signal import find_peaks
peaks, _ = find_peaks(arr1, distance=10)
在这种情况下,它找到位置 10 和 27。但位置 0 也有 10 个不更高的后续元素。我怎样才能找到这些?
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
def get_peaks(arr, window):
maxss = np.argmax(rolling_window(arr1, window), axis=1)
return np.where(maxss == 0)[0]
>>> arr1 = np.array([1. , 0.73381293, 0.75649351, 0.77693474, 0.77884614,
0.81055903, 0.81402439, 0.78798586, 0.78839588, 0.82967961,
0.8448 , 0.83276451, 0.82539684, 0.81762916, 0.82722515,
0.82101804, 0.82871127, 0.82825041, 0.82086957, 0.8347826 ,
0.82666665, 0.82352942, 0.81270903, 0.81191224, 0.83180428,
0.84975767, 0.84044236, 0.85057473, 0.8394649 , 0.80000001,
0.83870965, 0.83962262, 0.85039371, 0.83359748, 0.84019768,
0.83281732, 0.83660132])
>>> get_peaks(arr1, 10)
array([ 0, 10, 27])
滚动学分 window 功能:Rolling window for 1D arrays in Numpy?
很遗憾,find_peaks()
works by comparing neighboring values - so will not identify peaks that occur at the beginning or end of the array. One workaround is to use np.concatenate()
要在首尾插入数组的最小值,然后peaks变量减1:
>>> import numpy as np
>>> peaks, _ = find_peaks(np.concatenate(([min(arr1)],arr1,[min(arr1)])), distance=10)
>>> peaks-1
array([ 0, 10, 27], dtype=int64)
我们可以使用1D sliding-windowed max filter from SciPy
。此外,您似乎正在与 n
之前的元素进行比较。因为,第一个元素不会有任何前面的元素,我们需要让它忽略。
因此,我们会像这样实现 -
from scipy.ndimage.filters import maximum_filter1d
def peaks_previousN(a, n):
W = (n-1)//2
return np.flatnonzero(a[1:]>=maximum_filter1d(a, n, origin=W)[:-1])+1
具有给定样本数组的样本 运行 -
In [107]: peaks_previousN(arr1, n=10)
Out[107]: array([25, 27])
我正在尝试找到一种矢量化方法来找到数组中第一个位置,其中的值不高于前面 n 个数字的最大值。我想过用scipy.signal的find_peaks方法求局部最大值。我认为如果您将距离定义为 10 n 就是 10,那么它就是这样做的。但不幸的是,距离的条件必须在两个方向上都满足 - 先前和即将到来的数字。有没有其他方法或方法可以找到这样的东西?
示例:
arr1 = np.array([1. , 0.73381293, 0.75649351, 0.77693474, 0.77884614,
0.81055903, 0.81402439, 0.78798586, 0.78839588, 0.82967961,
0.8448 , 0.83276451, 0.82539684, 0.81762916, 0.82722515,
0.82101804, 0.82871127, 0.82825041, 0.82086957, 0.8347826 ,
0.82666665, 0.82352942, 0.81270903, 0.81191224, 0.83180428,
0.84975767, 0.84044236, 0.85057473, 0.8394649 , 0.80000001,
0.83870965, 0.83962262, 0.85039371, 0.83359748, 0.84019768,
0.83281732, 0.83660132])
from scipy.signal import find_peaks
peaks, _ = find_peaks(arr1, distance=10)
在这种情况下,它找到位置 10 和 27。但位置 0 也有 10 个不更高的后续元素。我怎样才能找到这些?
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
def get_peaks(arr, window):
maxss = np.argmax(rolling_window(arr1, window), axis=1)
return np.where(maxss == 0)[0]
>>> arr1 = np.array([1. , 0.73381293, 0.75649351, 0.77693474, 0.77884614,
0.81055903, 0.81402439, 0.78798586, 0.78839588, 0.82967961,
0.8448 , 0.83276451, 0.82539684, 0.81762916, 0.82722515,
0.82101804, 0.82871127, 0.82825041, 0.82086957, 0.8347826 ,
0.82666665, 0.82352942, 0.81270903, 0.81191224, 0.83180428,
0.84975767, 0.84044236, 0.85057473, 0.8394649 , 0.80000001,
0.83870965, 0.83962262, 0.85039371, 0.83359748, 0.84019768,
0.83281732, 0.83660132])
>>> get_peaks(arr1, 10)
array([ 0, 10, 27])
滚动学分 window 功能:Rolling window for 1D arrays in Numpy?
很遗憾,find_peaks()
works by comparing neighboring values - so will not identify peaks that occur at the beginning or end of the array. One workaround is to use np.concatenate()
要在首尾插入数组的最小值,然后peaks变量减1:
>>> import numpy as np
>>> peaks, _ = find_peaks(np.concatenate(([min(arr1)],arr1,[min(arr1)])), distance=10)
>>> peaks-1
array([ 0, 10, 27], dtype=int64)
我们可以使用1D sliding-windowed max filter from SciPy
。此外,您似乎正在与 n
之前的元素进行比较。因为,第一个元素不会有任何前面的元素,我们需要让它忽略。
因此,我们会像这样实现 -
from scipy.ndimage.filters import maximum_filter1d
def peaks_previousN(a, n):
W = (n-1)//2
return np.flatnonzero(a[1:]>=maximum_filter1d(a, n, origin=W)[:-1])+1
具有给定样本数组的样本 运行 -
In [107]: peaks_previousN(arr1, n=10)
Out[107]: array([25, 27])