我是Python的新手,我有一个基本的理解问题。对我来说,似乎FFT的结果只是基于自己选择的空间。
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = p.linspace(0.0, N*T, N)
y = p.sin(50.0 * 2.0*p.pi*x) + 0.5*p.sin(80.0 * 2.0*p.pi*x)
yf = p.fft(y)
xf = p.linspace(0.0, 1.0/(2.0*T), N/2)
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()
以这个fft为例,我得到两个尖峰,分别为50和80 Hz。当我将xf更改为:
时xf = p.linspace(0.0, 5.0/(2.0*T), N/2)
尖峰大约在250和400 Hz之间。
这是不是意味着,我必须事先知道正确的结果(在这种情况下输入信号所包含的两个正弦波的频率),以便我以后可以调整轴的比例以适应那些结果如何?可能不是,所以如果有人能解释这个问题,我会很高兴。
答案 0 :(得分:1)
您必须根据采样率,样本数量和fft(NFFT)中使用的样本计算正确的频率。 FFT算法不知道您正在操作什么时间尺度。例如,频率轴也可以以角频率给出,在这种情况下,您只需用2 * pi来缩放轴。
因此,您必须知道输入信号的采样率和采样数,以便为FFT结果创建正确的频率轴 - 但是,您不需要了解输入信号的形状(忽略别名)问题)。
频率[Hz]可以使用以下公式计算:
dt = 0.0001 # your T
Fs = 1 / dt # sample rate
xt = np.arange (0, 10, dt)
nt = len (xt) # length of time series, your N
# make signal
st = .5 * np.sin (50 * 2 * np.pi * xt) + .5 * np.sin (80 * 2 * np.pi * xt)
# take fourier transform and shift frequency spectra
S = np.fft.fftshift(np.fft.fft (st))
## make argument array, frequency [Hz]
#
# extending from -Fs/2 to Fs/2, the Nyquist frequency, with the same
# number of samples as in the time series (xt and st).
freqs = Fs * np.linspace (-1/2, 1/2, nt)
# plot absolute value, scaled with number of samples
# ( note that it is also interesting to plot the complex angle
# using np.angle () ). together they make a bode-plot.
plt.plot (freqs, np.abs(S) / nt)
plt.xlabel ('Frequency [Hz]')
答案 1 :(得分:-1)