[英]How to get (x, y) coordinates of a signal at 10% of its maximum amplitude?
How do I extract the (x, y) coordinates of a sinewave, when sinewave is 10% of itss maximum amplitude, as seen in the figure (red dots)? 当正弦波为最大振幅的10%时,如何提取正弦波的(x,y)坐标,如图所示(红点)? My 'x-values' is the time and the index number of the array.
我的'x-values'是数组的时间和索引号。
I have tried something like this, it is not working properly: 我尝试过类似的东西,它运行不正常:
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)
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.
我们的想法是拥有一个密集的x点网格,然后定义一个小的公差值。 Then look for the values in the y-array which are close to 0.1 times the maximum height (=1) within this tolerance
然后查找y数组中的值,该值接近此容差范围内最大高度(= 1)的0.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()
Since you tagged pandas, you can do so with pandas' cumsum
: 由于您标记了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)
Plot: 情节:
Note : if as you said, the x
series is y
's time index, then the code above needs to change to: 注意 :如果你说的是,
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.