【问题标题】:How to get (x, y) coordinates of a signal at 10% of its maximum amplitude?如何获得信号在其最大幅度的 10% 处的 (x, y) 坐标?
【发布时间】:2019-10-11 18:21:07
【问题描述】:

当正弦波是其最大幅度的 10% 时,如何提取正弦波的 (x, y) 坐标,如图所示(红点)?我的“x 值”是数组的时间和索引号。

我尝试过类似的方法,但无法正常工作:

sinewave_max = sinewave[0:argmax(sinewave)]
for i,val in enumerate(sinewave_max):
                if i == int(0.1*(len(sinewave_max))):
                    y = sinewave_max[i]
                    x = index(y)  (#Pseudo-Code line)             

【问题讨论】:

    标签: python arrays pandas numpy time-series


    【解决方案1】:

    由于您标记了 pandas,因此您可以使用 pandas 的cumsum

    x = np.linspace(0, 10, 1000)
    y = np.sin(x)
    thresh = max(y) * 0.10
    
    s = pd.Series(y>thresh)
    
    # idx contains the jump from y<=thresh to y>thresh
    # except possibly the first position
    idx = s.index[s.ne(s.shift())]
    
    # check the first position
    if y[0] < thresh: idx = idx[1:]
    
    # plot:
    plt.figure(figsize=(10,6))
    plt.plot(x,y)
    plt.scatter(x[idx],y[idx], c='r', s=100)
    plt.grid(True)
    

    剧情:

    注意:如果如你所说,x系列是y的时间索引,那么上面的代码需要改为:

    s = pd.Series(y>thresh)
    
    # idx contains the jump from y<=thresh to y>thresh
    # except possibly the first position
    idx = s.index[s.ne(s.shift())]
    
    # check the first position
    if y.iloc < thresh: idx = idx[1:]
    
    plt.figure(figsize=(10,6))
    plt.plot(x,y)
    
    # we only need to plot y[idx] against idx now
    plt.scatter(idx,y[idx], c='r', s=100)
    plt.grid(True)
    

    这给出了:

    【讨论】:

      【解决方案2】:

      这是一种方法。这个想法是有一个 x 点的密集网格,然后定义一个小的容差值。然后在 y 数组中寻找接近 0.1 倍最大高度 (=1) 在此容差范围内的值

      import numpy as np
      import matplotlib.pyplot as plt
      
      x = np.linspace(0, 10, 1000)
      y = np.sin(x)
      
      plt.plot(x, y)
      plt.axhline(0, color='k')
      tol = 1e-2
      ind = np.argwhere(abs(y-0.1*max(y))<=tol)
      
      plt.scatter(x[ind], y[ind], c='r', s=100, zorder=3)
      plt.xlabel('Time')
      plt.ylabel('Amplitude = sin(time)')
      plt.title('Sine wave')
      plt.grid()
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2012-07-22
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多