【问题标题】:how to find only large peaks in waves in `matplotlib`如何在`matplotlib`中仅找到波中的大峰
【发布时间】:2019-08-21 05:47:31
【问题描述】:

我试图在我的波浪中找到峰值,但我捕捉到了较小的峰值,而我只需要大的。

我尝试了一些代码来找到峰值,但不是那么准确。

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd

    # Generate a noisy AR(1) sample
    #np.random.seed(1)
    rs = np.linspace(1259,1650,4768)
    xs = [0]

    af = pd.read_csv("/home/pi/Downloads/newdata2.csv")

    sr = list(af.hex)

    for r in sr:
        xs.append(r)
    df = pd.DataFrame(xs, columns=['data'])

    # Find local peaks
    df['min'] = df.data[(df.data.shift(10) > df.data) & 
    (df.data.shift(-10) > df.data)]

    df['max'] = df.data[(df.data.shift(1) < df.data) & 
    (df.data.shift(-1) < df.data)]

    # Plot results
    #plt.scatter(df.index, df['min'], c='r') # this is valley
     plt.scatter(df.index, df['max'], c='g')
     df.data.plot()
     plt.savefig('graph.png')
     plt.show()

我只想找到大峰。

Example image

【问题讨论】:

  • 请提供您的数据样本 (minimal reproducible example)。
  • 我的数据是频率,并为一列中的 4768 个值提供动力
  • 你也可以在正弦波上给我答案

标签: python python-3.x matplotlib


【解决方案1】:

尝试检查该点是否比相邻点大 5 倍(根据需要调整此数字)。

df['max'] = df.data[(df.data.shift(10)*5 < df.data) & 
    (df.data.shift(-10)*5 < df.data)]

【讨论】:

  • 当我尝试 5x 时,峰值会增加更多。我只想要图 Arienrhod 顶部的 3 个峰值
  • 我已经修改了我的解决方案以检查更远的点。它适用于正弦波(没有 5x,正弦波 y-lim 的 b/c)。
  • 峰峦叠嶂
  • 我只需要前 3 个峰
  • 没有数据我帮不了你。
【解决方案2】:

您可以尝试使用scipy 中的find_peaks

    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.signal import find_peaks

    x = np.array(1400 - 0.002 * np.linspace(0,700_000,700_000))
    # add peaks 100 - 150 units high
    for i in range(70_000,630_000,70_000):
        x[i] += np.random.rand()*50+100

    peaks, _ = find_peaks(x, threshold=100)
    plt.plot(x)
    plt.plot(peaks, x[peaks], "x")
    plt.show()

【讨论】:

  • 谢谢你的帮助,但我想找到数据波中的峰值就像任何值或峰值一样,我可以增加 150 个单位的 Axois
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 2014-04-19
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
相关资源
最近更新 更多