在numpy数组中查找局部最大值

Nat*_*mas 7 python arrays numpy distribution

我希望找到一些高斯平滑数据中的峰值.我已经研究了一些可用的峰值检测方法,但它们需要一个输入范围来搜索,我希望它比这更加自动化.这些方法也适用于非平滑数据.由于我的数据已经过平滑,我需要一种更简单的方法来检索峰值.我的原始数据和平滑数据如下图所示.

在此输入图像描述

从本质上讲,是否有一种pythonic方法从平滑数据数组中检索最大值,以便像数组一样

    a = [1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1]
Run Code Online (Sandbox Code Playgroud)

会回来:

    r = [5,3,6]
Run Code Online (Sandbox Code Playgroud)

Cle*_*leb 12

有一个bulit-in函数argrelextrema可以完成这项任务:

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])
Run Code Online (Sandbox Code Playgroud)

这为您提供了所需的输出r.

  • @NathanThomas:很高兴它有所帮助!BTW:如果你对最小值感兴趣,你可以用'np.less`替换`np.greater`. (3认同)