簡體   English   中英

奇怪的FFT輸出python

[英]Strange FFT output 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')

代碼正確,您需要重新整理傅立葉理論和奈奎斯特采樣定理,並確保數字有意義。 問題出在您的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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM