使用numpy从电压和时间数据通过FFT估算频率

时间:2019-01-24 06:14:15

标签: python numpy jupyter-notebook fft

我想通过基于电压和时间数据执行傅立叶变换来估计频率(请参见下面的链接) 当我用Jupyter笔记本电脑执行fft时。

电压和时间之间的关系是下图的第一张图。就此而言,峰值似乎在2 Hz左右,但不会达到预期。 我认为代码有问题,但是据我所知我还不知道,所以我想告诉你。

test_data_middle = pd.read_csv("data.CSV")
x = test_data_middle["time"]
y = test_data_middle["volt"]
N = len(y)
dt = x[1]-x[0]

t = np.arange(0,N*dt,dt)
#minus Y.mean() to remove DC offset
Y = np.array(y)
Y_block_offset = Y - Y.mean()


F = np.fft.fft(Y_block_offset)
Amp = np.abs(F)
freq = np.fft.fftfreq(N,d=dt)

#PLot
plt.figure(figsize=(18,3),facecolor="white")
plt.plot(x,Y_block_offset,'b-', linewidth=1)
plt.xlabel('Time')
plt.ylabel('Volt')
plt.grid(True)

#FFT graph
fig, ax = plt.subplots(nrows=4, sharex=True, figsize=(6,6))
ax[0].plot(F.real, label="Real part")
ax[0].legend()
ax[1].plot(F.imag, label="Imaginary part")
ax[1].legend()
ax[2].plot(freq, label="Frequency")
ax[2].legend()
ax[3].plot(Amp, label="Amp")
ax[3].legend()
ax[3].set_xlabel("Number of data")

plt.show()

image of research data

0 个答案:

没有答案
相关问题