Python 中高斯函数的傅立叶变换
Fourier transform of a Gaussian function in Python
我想计算一些高斯函数的傅立叶变换。考虑简单的高斯 g(t) = e^{-t^2}。 g(t) 的傅立叶变换具有 simple analytical expression ,因此第 0 个频率就是根 pi。
如果我尝试在 Python 中做同样的事情:
N = 1000
t = np.linspace(-1,1,N)
g = np.exp(-t**2)
h = np.fft.fft(g) #This is the Fourier transform of expression g
很简单。现在 as per the docs h[0]
应该包含零频率项,我们从解析表达式中知道它是根 pi。但它给出了 746.444
?!
为什么解析解和计算解不一致?
不确定您为什么认为应该获得分析表达式。 NUmPy 中的 DFFT 显然是不对称的,如果您查看 Ak here 的公式,您可以清楚地看到 A0你应该得到输入的总和。此外,从 [-sigma...sigma] 区间获取高斯分布是不正确的。
这里是修改后的例子
import numpy as np
import matplotlib.pyplot as plt
N = 4001
t = np.linspace(-4.0, 4.0, N)
print((t[0], t[2000], t[4000]))
g = np.exp(-t*t)
print(np.sum(g)) # sum of input
h = np.fft.fft(g, norm=None)
print(h[0]) # should be the same as sum of input
并打印
(-4.0, 0.0, 4.0)
886.2269119018041
(886.226911901804+0j)
你可以进行逆变换并绘制它
q = np.fft.ifft(h, norm=None)
plt.plot(t, g, label = "Gauss")
plt.show()
plt.plot(t, np.abs(q), label = "dFFT Gauss")
plt.show()
f = np.fft.fftfreq(N)
plt.plot(f, np.angle(h), f, np.abs(h))
plt.show()
并获得
我想计算一些高斯函数的傅立叶变换。考虑简单的高斯 g(t) = e^{-t^2}。 g(t) 的傅立叶变换具有 simple analytical expression ,因此第 0 个频率就是根 pi。
如果我尝试在 Python 中做同样的事情:
N = 1000
t = np.linspace(-1,1,N)
g = np.exp(-t**2)
h = np.fft.fft(g) #This is the Fourier transform of expression g
很简单。现在 as per the docs h[0]
应该包含零频率项,我们从解析表达式中知道它是根 pi。但它给出了 746.444
?!
为什么解析解和计算解不一致?
不确定您为什么认为应该获得分析表达式。 NUmPy 中的 DFFT 显然是不对称的,如果您查看 Ak here 的公式,您可以清楚地看到 A0你应该得到输入的总和。此外,从 [-sigma...sigma] 区间获取高斯分布是不正确的。
这里是修改后的例子
import numpy as np
import matplotlib.pyplot as plt
N = 4001
t = np.linspace(-4.0, 4.0, N)
print((t[0], t[2000], t[4000]))
g = np.exp(-t*t)
print(np.sum(g)) # sum of input
h = np.fft.fft(g, norm=None)
print(h[0]) # should be the same as sum of input
并打印
(-4.0, 0.0, 4.0)
886.2269119018041
(886.226911901804+0j)
你可以进行逆变换并绘制它
q = np.fft.ifft(h, norm=None)
plt.plot(t, g, label = "Gauss")
plt.show()
plt.plot(t, np.abs(q), label = "dFFT Gauss")
plt.show()
f = np.fft.fftfreq(N)
plt.plot(f, np.angle(h), f, np.abs(h))
plt.show()
并获得