Python 如何获得信号最大振幅10%时的(x,y)坐标?
当正弦波是其最大振幅的10%时,如何提取正弦波的(x,y)坐标,如图(红点)所示?我的“x值”是数组的时间和索引号 我尝试过类似的方法,但效果不好:Python 如何获得信号最大振幅10%时的(x,y)坐标?,python,arrays,pandas,numpy,time-series,Python,Arrays,Pandas,Numpy,Time Series,当正弦波是其最大振幅的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
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)
这里有一种方法。这样做的目的是要有一个密集的x点网格,然后定义一个小的公差值。然后在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()
将numpy导入为np
将matplotlib.pyplot作为plt导入
x=np.linspace(0,10,1000)
y=np.sin(x)
平面图(x,y)
plt.axhline(0,color='k')
tol=1e-2
ind=np.argwhere(abs(y-0.1*max(y))因为您标记了熊猫,所以可以使用熊猫的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)
其中:
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)