numpy的fft结果的幅度要乘以采样周期吗? [英] amplitude of numpy's fft results is to be multiplied by sampling period?
问题描述
我尝试通过一个示例来验证我对Numpy FFT的理解:exp(-pi*t^2)
的傅立叶变换应为exp(-pi*f^2)
,如果在直接变换上未应用任何缩放.
I try to validate my understanding of Numpy's FFT with an example: the Fourier transform of exp(-pi*t^2)
should be exp(-pi*f^2)
when no scaling is applied on the direct transform.
但是,我发现要获得此结果,我需要将FFT的结果乘以因子dt
,这是函数上两个采样点之间的时间间隔.我不明白为什么.有人可以帮忙吗?
However, I find that to obtain this result I need to multiply the result of FFT by a factor dt
, which is the time interval between two sample points on my function. I don't understand why. Can anybody help ?
这是示例代码:
# create data
N = 4097
T = 100.0
t = linspace(-T/2,T/2,N)
f = exp(-pi*t**2)
# perform FT and multiply by dt
dt = t[1]-t[0]
ft = fft(f) * dt
freq = fftfreq( N, dt )
freq = freq[:N/2+1]
# plot results
plot(freq,abs(ft[:N/2+1]),'o')
plot(freq,exp(-pi*freq**2),'r')
legend(('numpy fft * dt', 'exact solution'),loc='upper right')
xlabel('f')
ylabel('amplitude')
xlim(0,1.4)
推荐答案
请注意,您不是在计算连续时间傅立叶变换,计算机是处理离散数据的,Numpy也是如此,如果您查看 numpy.fft.fft
文档说:
Be careful, you are not computing the continuous time Fourier transform, computers work with discrete data, so does Numpy, if you take a look to numpy.fft.fft
documentation it says:
numpy.fft.fft(a,n = None,axis = -1)[源代码]
numpy.fft.fft(a, n=None, axis=-1)[source]
计算一维离散傅立叶变换.
Compute the one-dimensional discrete Fourier Transform.
此函数使用高效的快速傅立叶变换(FFT)算法计算一维n点离散傅立叶变换(DFT)
This function computes the one-dimensional n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm
这意味着您正在计算方程式定义的DFT:
That means that your are computing the DFT which is defined by equation:
连续时间傅立叶变换的定义为:
the continuous time Fourier transform is defined by:
如果您进行数学运算以查找它们之间的关系:
And if you do the maths to look for the relationship between them:
如您所见,有一个常数因子1/N
恰好是您的标度值dt
(x[n] - x[n-1]
,其中n在[0,T]区间中等于1/N
).
As you can see there is a constant factor 1/N
which is exactly your scale value dt
(x[n] - x[n-1]
where n is in [0,T] interval is equivalent to 1/N
).
仅对代码进行注释,导入所有内容from numpy import *
而不是使用以下方法不是一个好习惯:
Just a comment on your code, it is not a good practice to import everything from numpy import *
instead use:
import numpy as np
import matplotlib.pyplot as plt
# create data
N = 4097
T = 100.0
t = np.linspace(-T/2,T/2,N)
f = np.exp(-np.pi*t**2)
# perform FT and multiply by dt
dt = t[1]-t[0]
ft = np.fft.fft(f) * dt
freq = np.fft.fftfreq(N, dt)
freq = freq[:N/2+1]
# plot results
plt.plot(freq, np.abs(ft[:N/2+1]),'o')
plt.plot(freq, np.exp(-np.pi * freq**2),'r')
plt.legend(('numpy fft * dt', 'exact solution'), loc='upper right')
plt.xlabel('f')
plt.ylabel('amplitude')
plt.xlim([0, 1.4])
plt.show()
这篇关于numpy的fft结果的幅度要乘以采样周期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!