I want to calculate the FFT numerically on the set Y. For testing, I use the Gauss function Y = exp (-x ^ 2). The symbolic Fourier transform is Y '= constant * exp (-k ^ 2/4).
import numpy X = numpy.arange(-100,100) Y = numpy.exp(-(X/5.0)**2)
The naive approach fails:
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) jumps between positive and negative values, which correspond to the phase of the jump, which is absent in the symbolic result. This, of course, is undesirable. (The result is technically correct in the sense that abs (Y_k) gives the amplitudes as expected if if (Y_k) equals Y.)
Here the function fftshift () displays the array k monotonically increasing and accordingly changes Y_k. The pair zip (k, Y_k) does not change, applying this operation to both vectors.
These changes fix the problem:
Y_k = fftshift(fft(ifftshift(Y))) k = fftshift(fftfreq(len(Y))) plotReIm(k,Y_k)
Is it correct to use the fft () function if monotone Y and Y_k are required?
Reverse operation above:
Yx = fftshift(ifft(ifftshift(Y_k))) x = fftshift(fftfreq(len(Y_k), k[1] - k[0])) plotReIm(x,Yx)
In this case, the document clearly indicates that Y_k must be sorted compatible with the outputs fft () and fftfreq (), which we can achieve by using ifftshift ().
These questions have bothered me for a long time: Are the output and input arrays of both fft () and ifft () always such that 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], where "frequency" is an independent variable?
The answer to the Fourier transform of a Gaussian is not Gaussian does not answer my question.