在numpy数组中查找局部最大值 [英] Find local maximums in numpy array
问题描述
我正在寻找一些具有高斯平滑数据的峰.我已经看过一些可用的峰值检测方法,但它们需要一个可搜索的输入范围,我希望它比以前更自动化.这些方法也设计用于非平滑数据.由于数据已经过平滑处理,因此我需要一种更简单的方法来检索峰.我的原始数据和平滑数据在下图中.
I am looking to find the peaks in some gaussian smoothed data that I have. I have looked at some of the peak detection methods available but they require an input range over which to search and I want this to be more automated than that. These methods are also designed for non-smoothed data. As my data is already smoothed I require a much more simple way of retrieving the peaks. My raw and smoothed data is in the graph below.
从本质上讲,是否存在一种从平滑数据数组中检索最大值的有效方法,例如,像这样的数组
Essentially, is there a pythonic way of retrieving the max values from the array of smoothed data such that an array like
a = [1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1]
将返回:
r = [5,3,6]
推荐答案
There exists a bulit-in function argrelextrema
that gets this task done:
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])
这将为您提供所需的r
输出.
That gives you the desired output for r
.
从SciPy 1.1版开始,您还可以使用 find_peaks .以下是两个来自文档本身的示例.
As of SciPy version 1.1, you can also use find_peaks. Below are two examples taken from the documentation itself.
使用height
参数,可以选择高于某个阈值的所有最大值(在此示例中,所有非负最大值;如果必须处理嘈杂的基线,这可能非常有用;如果要查找最小值,只需将您的输入乘以-1
):
Using the height
argument, one can select all maxima above a certain threshold (in this example, all non-negative maxima; this can be very useful if one has to deal with a noisy baseline; if you want to find minima, just multiply you input by -1
):
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import numpy as np
x = electrocardiogram()[2000:4000]
peaks, _ = find_peaks(x, height=0)
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.plot(np.zeros_like(x), "--", color="gray")
plt.show()
另一个非常有用的参数是distance
,它定义了两个峰之间的最小距离:
Another extremely helpful argument is distance
, which defines the minimum distance between two peaks:
peaks, _ = find_peaks(x, distance=150)
# difference between peaks is >= 150
print(np.diff(peaks))
# prints [186 180 177 171 177 169 167 164 158 162 172]
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.show()
这篇关于在numpy数组中查找局部最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!