高斯的傅立叶变换不是高斯,但是那是错误的! - Python [英] Fourier transform of a Gaussian is not a Gaussian, but thats wrong! - Python
问题描述
我正在尝试使用Numpy的fft函数,但是当我给该函数一个简单的高斯函数时,该高斯函数的fft不是高斯,而是接近但减半,因此每一半都位于x轴的两端
I am trying to utilize Numpy's fft function, however when I give the function a simple gausian function the fft of that gausian function is not a gausian, its close but its halved so that each half is at either end of the x axis.
我正在计算的高斯函数是 y = exp(-x ^ 2)
The Gaussian function I'm calculating is y = exp(-x^2)
这是我的代码:
from cmath import *
from numpy import multiply
from numpy.fft import fft
from pylab import plot, show
""" Basically the standard range() function but with float support """
def frange (min_value, max_value, step):
value = float(min_value)
array = []
while value < float(max_value):
array.append(value)
value += float(step)
return array
N = 256.0 # number of steps
y = []
x = frange(-5, 5, 10/N)
# fill array y with values of the Gaussian function
cache = -multiply(x, x)
for i in cache: y.append(exp(i))
Y = fft(y)
# plot the fft of the gausian function
plot(x, abs(Y))
show()
结果不太正确,因为高斯函数的FFT应该是高斯函数本身...
The result is not quite right, cause the FFT of a Gaussian function should be a Gaussian function itself...
推荐答案
np.fft.fft
returns a result in so-called "standard order": (from the docs)
如果
A = fft(a, n)
,则A[0]
包含零频项( 信号的均值),通常 对于真实输入而言,完全是真实的.然后A[1:n/2]
包含 正频率项,以及A[n/2+1:]
包含 负频率项,按 负频率递减.
If
A = fft(a, n)
, thenA[0]
contains the zero-frequency term (the mean of the signal), which is always purely real for real inputs. ThenA[1:n/2]
contains the positive-frequency terms, andA[n/2+1:]
contains the negative-frequency terms, in order of decreasingly negative frequency.
函数np.fft.fftshift
将结果重新排列为大多数人期望的顺序(这对于绘制很有用):
The function np.fft.fftshift
rearranges the result into the order most humans expect (and which is good for plotting):
例程
np.fft.fftshift(A)
转变变换及其 归零频率 中间的组件...
The routine
np.fft.fftshift(A)
shifts transforms and their frequencies to put the zero-frequency components in the middle...
因此使用np.fft.fftshift
:
import matplotlib.pyplot as plt
import numpy as np
N = 128
x = np.arange(-5, 5, 10./(2 * N))
y = np.exp(-x * x)
y_fft = np.fft.fftshift(np.abs(np.fft.fft(y))) / np.sqrt(len(y))
plt.plot(x,y)
plt.plot(x,y_fft)
plt.show()
这篇关于高斯的傅立叶变换不是高斯,但是那是错误的! - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!