I am testing the validity of the FFT and IFFT functions in Matlab.
I can compare the outputs of these functions to a well-known mathematical fact: the Fourier transform of an even, real function (like a Gaussian centered at 0), is another even, real function (FFT[real, 0-centered Gaussian] = real, 0-centered Gaussian). This fact should hold both for FFT and IFFT.
First I make my grid:
nx = 256; % grid total pixel count
X = 500; % grid size (um)
dx = X/nx; % grid spacing (um)
x = linspace(-nx/2,nx/2-1,nx)*dx; % x grid (um)
df = 1/(nx*dx); % spectral grid spacing (1/um)
f = linspace(-nx/2,nx/2-1,nx)*df; % f grid (1/um)
And I make my Gaussian:
A = 1; % magnitude (arbitrary units)
x_fwhm = 7; % Full width at half maximum diameter (um)
x0 = x_fwhm/sqrt(2*log(2)); % 1/e^2 radius (um)
y = A*exp(-2*x.^2./(x0)^2); % Gaussian (arbitrary units)
And apply the Fourier transform, with FFT:
yFFT = fftshift(fft(fftshift(y)));
Alternatively, with IFFT:
yIFFT = fftshift(ifft(fftshift(y)));
The IFFT does a perfect job: yIFFT is a purely real Gaussian. However, FFT yields a complex number: a very small imaginary part exists. This is fine, since an error should be expected in the fourier transform algorithms, and it's negligible anyway. What confuses me is why there is no error at all in IFFT? Are the FFT and IFFT algorithms that much different?
*** Note: fftshift and ifftshift are equivalent here, since my array has an even number of elements.