【发布时间】:2020-06-10 02:33:05
【问题描述】:
我有一组要对其执行 FFT 的模拟数据。我正在使用 matplotlib 来做到这一点。但是,FFT 看起来很奇怪,所以我不知道我的代码中是否遗漏了一些东西。将不胜感激。
原始数据:
FFT:
FFT 计算代码:
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack as fftpack
data = pd.read_csv('table.txt',header=0,sep="\t")
fig, ax = plt.subplots()
mz_res=data[['mz ()']].to_numpy()
time=data[['# t (s)']].to_numpy()
ax.plot(time[:300],mz_res[:300])
ax.set_title("Time-varying mz component")
ax.set_xlabel('time')
ax.set_ylabel('mz amplitude')
fft_res=fftpack.fft(mz_res[:300])
power=np.abs(fft_res)
frequencies=fftpack.fftfreq(fft_res.size)
fig2, ax_fft=plt.subplots()
ax_fft.plot(frequencies[:150],power[:150]) // taking just half of the frequency range
我只是绘制前 300 个数据点,因为其余的并不重要。
我在这里做错了吗?我期待单频峰值不是我得到的。谢谢!
输入文件的链接:
编辑
原来错误在于将数据帧转换为 numpy 数组。出于我尚未理解的原因,如果我将数据帧转换为 numpy 数组,它将被转换为数组数组,即结果数组的每个元素本身就是单个元素的数组。当我将代码更改为:
mz_res=data['mz ()'].to_numpy()
所以它是从 pandas series 到 numpy 数组的转换,然后 FFT 的行为与预期一样,我从 FFT 中得到单个频率峰值。
所以我只是把它放在这里以防其他人发现它有用。经验教训:从 pandas 系列转换为 numpy 数组产生的结果与从 pandas 数据帧转换产生的结果不同。
【问题讨论】:
-
您能否将您的解决方案放入答案中并接受它以便我们关闭问题?
-
@AhmedFasih 完成
标签: matplotlib fft