【问题标题】:Strange FFT output python奇怪的FFT输出python
【发布时间】:2015-03-24 08:07:31
【问题描述】:

我正在尝试获取 FFT 并绘制它。问题是,我的代码适用于小频率(如 50),但不适用于我需要的更大频率。我的代码怎么了?!我希望在我输入的正弦波的频率上看到一个尖峰,但根据我使用的样本间距,尖峰的频率不同。

bins = 600
ss = 2048
freq = 44100
centerfreq = freq*bins/ss
# Number of samplepoints
N = ss
# sample spacing
T = 1 / 800.
x = np.linspace(0.0, N*T, N)
y = sin(2*np.pi*centerfreq*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]), 'r')

【问题讨论】:

  • “它不起作用”是什么意思?输入是什么,您期望看到什么,实际发生了什么?
  • 输入如我发布的代码所示(bins,ss,freq);输出是图中的一个尖峰,作为与上面代码中的“centerfreq”无关的频率。似乎当我更改“T”的值时,它会移动尖峰。

标签: python fft


【解决方案1】:

代码是正确的,您需要复习傅立叶理论和奈奎斯特采样定理,并确保数字有意义。问题在于您的 x 轴刻度。 plot 函数绘制 x 中的第一项和 y 中的第一项,如果 x 没有按比例放大到您的预期,您会感到惊讶。如果您绘制正弦信号(正弦波)并期望“度数”并且例如得到弧度,您也会看到这一点。您有责任很好地扩展它,使其符合您的期望。

请参阅此 SO 答案 https://stackoverflow.com/a/25735436/2061422

from scipy import *
from numpy import *
from pylab import *  # imports for me to get going

bins = 600
ss = 2048
freq = 44100
centerfreq = freq*bins/ss
print centerfreq
# Number of samplepoints
N = ss
# sample spacing
T = 1. / freq   # i have decreased the spacing considerably
x = np.linspace(0.0, N*T, N)
sample_spacing = x[1] - x[0]  # but this is the real sample spacing
y = sin(2*np.pi*centerfreq*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
freqs = np.fft.fftfreq(len(y), sample_spacing) # read the manual on this fella.
plt.plot(freqs[:N/2], 1.0/N * np.abs(yf[0:N/2]), 'r')
plt.grid()
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多