【问题标题】:How to find peaks in 1d array如何在一维数组中找到峰值
【发布时间】:2017-04-08 03:28:38
【问题描述】:

我正在用 python 读取一个 csv 文件并从中准备一个数据框。我有一个 Microsoft Kinect,它正在记录 Arm Abduction 练习并生成此 CSV 文件。

我有 ElbowLeft 关节 Y 坐标的 this array。你可以想象这个here。现在,我想提出一个解决方案,可以计算该数组中的峰值或局部最大值。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 试试scipy.signal.find_peaks_cwt
  • 较新的scipy.signal.find_peaks 可能效果更好,除非您确实确定需要使用小波卷积。

标签: python arrays numpy kinect


【解决方案1】:

很简单,将数据放入一维数组中,并将每个值与邻居进行比较,n-1 和 n+1 的数据都小于 n。

按照 Robert Valencia 的建议读取数据

   max_local=0
for u in range (1,len(data)-1):

if ((data[u]>data[u-1])&(data[u]>data[u+1])):
                            max_local=max_local+1

【讨论】:

  • 这个算法将失败输入像 data = [3,2,3,6,4,1,2,3,2,1,2,2,2,1]
【解决方案2】:

您可以尝试使用平滑过滤器对数据进行平滑处理,然后找到之前和之后的值小于当前值的所有值。这假设您想要序列中的所有峰值。您需要平滑滤波器的原因是避免局部最大值。所需的平滑程度取决于数据中存在的噪声。

一个简单的平滑过滤器将当前值设置为序列中当前值之前和之后的 N 个值的平均值以及正在分析的当前值。

【讨论】:

  • 我使用移动平均平滑来平滑它(使用 np.convolve)。并使用了来自 scipy.signal 的 argrelextrema。
【解决方案3】:

您可以使用 scipy.signal 模块中的 find_peaks_cwt 函数来查找一维数组中的峰值:

from scipy import signal
import numpy as np

y_coordinates = np.array(y_coordinates) # convert your 1-D array to a numpy array if it's not, otherwise omit this line
peak_widths = np.arange(1, max_peak_width)
peak_indices = signal.find_peaks_cwt(y_coordinates, peak_widths)
peak_count = len(peak_indices) # the number of peaks in the array

更多信息在这里:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks_cwt.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2020-09-25
    • 2017-09-17
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多