奇怪的FFT输出python

2024-04-27 05:44:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用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')

Tags: 代码fftnp绘制尖峰ss频率freq
1条回答
网友
1楼 · 发布于 2024-04-27 05:44:42

代码是正确的,你需要复习你的傅立叶理论和奈奎斯特抽样定理,并确保这些数字是有意义的。问题是你的x轴刻度。plot函数将x中的第一个项目与y中的第一个项目绘制在一起,如果x没有按照您的期望进行缩放,您将获得一个惊喜。如果你绘制一个正弦信号(正弦波)并期望“度”,然后得到弧度,你也会看到这一点。你有责任把它放大,使之符合你的期望。在

参考这个,所以回答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()

相关问题 更多 >