Thanks for the code and the discussion - nice and helpful.
A book I'm reading says it should be
I'm confused. Could you explain where to use ifftshift when N=odd?
(The book is "Numerical Simulation of Optical Wave Propagation with examples in Matlab")
You're welcome. ifftshift is the same as fftshift for even length sequence, but different for odd length. So if one uses fftshift(fft(ifftshift(...))) things work well. By the way, scaling by dt and df to correct for scaling introduced by FFT algorithm is neat trick.
There is a problem with above recipe. It fails when you have sequence of odd length. Correct recipe is:
fftshift(fft(ifftshift(sig))) or fftshift(ifft(ifftshift(spectrum))).
A description of this can be found on my submission on fftshift, ifftshift.
To observe that above is true, run the following code with fftshift and ifftshift on inner call for computing Xfinal.
Bx = 50;
A = sqrt(log(2))/(2*pi*Bx);
fs = 500; %sampling frequency
dt = 1/fs; %time step
T=1; %total time window
t = -T/2:dt:T/2; %time grids
df = 1/T; %freq step
Fmax = 1/2/dt; %freq window
f=-Fmax:df:Fmax; %freq grids, not used in our examples, could be used by plot(f, X)
x = exp(-t.^2/(2*A^2));
Xan = A*sqrt(2*pi)*exp(-2*pi^2*f.^2*A^2); %X(f), analytical Fourier transform of x(t), real
Xfft = dt * fft(x); %directly using fft()
Xfftshift = dt * fft(fftshift(x)); %using fftshift() before fft()
Xfinal = dt * fftshift(fft(ifftshift(x))); %identical with analytical X(f), also note dt