我想对 numpy 数组 Y 进行数值计算。为了进行测试,我使用高斯函数 Y = exp(-x^2)。 (符号)傅立叶变换为 Y' = 常数 * exp(-k^2/4)。
import numpy
X = numpy.arange(-100,100)
Y = numpy.exp(-(X/5.0)**2)
天真的方法失败了:
from numpy.fft import *
from matplotlib import pyplot
def plotReIm(x,y):
f = pyplot.figure()
ax = f.add_subplot(111)
ax.plot(x, numpy.real(y), 'b', label='R()')
ax.plot(x, numpy.imag(y), 'r:', label='I()')
ax.plot(x, numpy.abs(y), 'k--', label='abs()')
ax.legend()
Y_k = fftshift(fft(Y))
k = fftshift(fftfreq(len(Y)))
plotReIm(k,Y_k)
real(Y_k) 在正值和负值之间跳跃,这对应于跳跃阶段,该阶段不存在于符号结果中。这当然是不可取的。 (从技术上讲,结果是正确的,abs(Y_k) 给出了预期的幅度,ifft(Y_k) 是 Y。)
这里,函数 fftshift() 将数组 k 单调递增并相应地改变 Y_k。将此操作应用于两个向量不会改变 zip(k, Y_k) 对。
此更改似乎解决了该问题:
Y_k = fftshift(fft(ifftshift(Y)))
k = fftshift(fftfreq(len(Y)))
plotReIm(k,Y_k)
如果需要单调 Y 和 Y_k,这是使用 fft() 函数的正确方法吗?
上式的逆运算为:
Yx = fftshift(ifft(ifftshift(Y_k)))
x = fftshift(fftfreq(len(Y_k), k[1] - k[0]))
plotReIm(x,Yx)
对于这种情况,文档 http://docs.scipy.org/doc/numpy/reference/generated/numpy.fft.ifft.html?highlight=ifft#numpy.fft.ifft明确指出 Y_k 的排序必须与 fft() 和 fftfreq() 的输出兼容,我们可以通过应用 ifftshift() 来实现。
这些问题已经困扰我很长时间了: fft() 和 ifft() 的输出和输入数组是否总是这样a[0] should contain the zero frequency term, a[1:n/2+1] should contain the positive-frequency terms, and a[n/2+1:] should contain the negative-frequency terms, in order of decreasingly negative frequency
[numpy 参考],其中“频率”是自变量?
答案关于高斯的傅里叶变换不是高斯 https://stackoverflow.com/questions/5398304/fourier-transform-of-a-gaussian-is-not-a-gaussian-but-thats-wrong-python/5398901#5398901没有回答我的问题。