How to get (x, y) coordinates of a signal at 10% of its maximum amplitude? How to get (x, y) coordinates of a signal at 10% of its maximum amplitude? numpy numpy

How to get (x, y) coordinates of a signal at 10% of its maximum amplitude?


Here is one way to do it. The idea is to have a dense mesh of x-points and then define a small tolerance value. Then look for the values in the y-array which are close to 0.1 times the maximum height (=1) within this tolerance

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 10, 1000)y = np.sin(x)plt.plot(x, y)plt.axhline(0, color='k')tol = 1e-2ind = 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()

enter image description here


Since you tagged pandas, you can do so with pandas' cumsum:

x = np.linspace(0, 10, 1000)y = np.sin(x)thresh = max(y) * 0.10s = pd.Series(y>thresh)# idx contains the jump from y<=thresh to y>thresh# except possibly the first positionidx = s.index[s.ne(s.shift())]# check the first positionif 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)

Plot:

enter image description here

Note: if as you said, the x series is y's time index, then the code above needs to change to:

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

Which gives:enter image description here