具有numpy的离散傅里叶变换

时间:2014-06-06 08:59:06

标签: math numpy matplotlib fft continuous-fourier

考虑函数f(t),如何计算连续的Fouriertransform g(w)并绘制它(使用numpy和matplotlib)?

如果傅立叶积分不存在解析解,则出现这个或反问题(g(w)给出,f(t)未知的图)。

1 个答案:

答案 0 :(得分:20)

您可以使用numpy FFT module,但必须做一些额外的工作。首先让我们看看傅里叶积分并将其离散化: 这里k,m是整数,N是f(t)的数据点数。使用这种离散化我们得到 enter image description here

最后一个表达式中的总和恰好是离散傅里叶变换(DFT)numpy使用(请参阅numpy FFT module的“实现细节”部分)。 有了这些知识,我们可以编写以下python脚本

import numpy as np
import matplotlib.pyplot as pl

#Consider function f(t)=1/(t^2+1)
#We want to compute the Fourier transform g(w)

#Discretize time t
t0=-100.
dt=0.001
t=np.arange(t0,-t0,dt)
#Define function
f=1./(t**2+1.)

#Compute Fourier transform by numpy's FFT function
g=np.fft.fft(f)
#frequency normalization factor is 2*np.pi/dt
w = np.fft.fftfreq(f.size)*2*np.pi/dt


#In order to get a discretisation of the continuous Fourier transform
#we need to multiply g by a phase factor
g*=dt*np.exp(-complex(0,1)*w*t0)/(np.sqrt(2*np.pi))

#Plot Result
pl.scatter(w,g,color="r")
#For comparison we plot the analytical solution
pl.plot(w,np.exp(-np.abs(w))*np.sqrt(np.pi/2),color="g")

pl.gca().set_xlim(-10,10)
pl.show()
pl.close()

结果图显示脚本正常工作enter image description here