【发布时间】:2011-12-06 07:47:10
【问题描述】:
我想在 numpy 数组 Y 上数值计算 FFT。为了测试,我使用高斯函数 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)
对于这种情况,documentation 明确指出 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 reference],其中“频率”是自变量?
Fourier Transform of a Gaussian is not a Gaussian 上的答案没有回答我的问题。
【问题讨论】: