Hi!
I am using FFTW in Julia 1.1.
I am trying to do the Fourier transform of exp(-0.5*x^2), whose analytical solution is exactly itself but I obtain a complex array.
Does anyone know what I am doing wrong? You can find the code I use just below:
Thanks for your time!!
using FFTW
using Plots
gridpoints=256
function_of_t=zeros(gridpoints)
x=range(-5,stop=5,length=gridpoints)
for i=1:gridpoints
function_of_t[i]=exp(-0.5*x[i]*x[i])
end
fourier_trans=fftshift(fft(function_of_t))
y=function_of_t
plot(x,y, title="Function of t", xlabel="Time", ylabel="f(t)")
savefig("analytic_function.png")
y=fourier_trans
plot(x,y, title="FFT by means of FFTW", xlabel="omega", ylabel="F(omega)")
savefig("ft_fftw.png")
SOLVED. EDIT.
Hi! Thanks so much for your help. I really appreciate it.
Let’s do the Fourier Transform of exp(-0.5x^2)/(sqrt(2pi)) whose analytical solution is exp(-2pipi*x^2). The array scaled is the analytical solution.
using FFTW
using Plots
gridpoints=256
x=range(-5,stop=5,length=gridpoints+1)[1:end-1]
function_of_t=exp.(-0.5*x.^2)/(sqrt(2*pi))
function_of_t=fftshift(function_of_t)
fourier_trans=fftshift(fft(function_of_t))
inverse_fft=fftshift(ifft(fourier_trans))
y=fourier_trans
plot(x,y, title="FFT by means of FFTW", xlabel="omega", ylabel="F(omega)")
savefig("ft_fftw.png")
plot(x,abs.(y), title="FFT by means of FFTW", xlabel="omega", ylabel="F(omega)")
savefig("ft_fftw_abs.png")
z=inverse_fft
plot(x,abs.(z), title="Inverse FFT by means of FFTW", xlabel="omega", ylabel="F(omega)")
savefig("ift_fftw_abs.png")
scaled=zeros(gridpoints)
for i=1:gridpoints
scaled[i]=real(fourier_trans[i])*(x[length(x)]-x[length(1)])/gridpoints
end
y=scaled
plot(x,y, title="FFT by means of FFTW", xlabel="omega", ylabel="F(omega)")
savefig("ft_scaled_fftw_abs.png")
fft_of_analytics=exp.(-2*pi*pi*x.^2)
plot(x,y, title="FFT analytical", xlabel="omega", ylabel="F(omega)")
savefig("ft_scaled_fftw_analytical.png")