# Problems using FFTW- SOLVED

**URL:** <https://discourse.julialang.org/t/problems-using-fftw-solved/33930>\
**Category:** General Usage\
**Tags:** fftw\
**Created:** [January 29, 2020, 12:00pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930 "2020-01-29T12:00:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![IgnacioSuarez](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@IgnacioSuarez](https://discourse.julialang.org/u/IgnacioSuarez)\
**Post date:** [January 29, 2020, 12:00pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/1 "2020-01-29T12:00:09Z")

</div>

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!! ![ft_fftw](https://global.discourse-cdn.com/julialang/original/3X/6/2/6272c085f88d55b12babcee0fcc2d4114e9b8295.png)

```julia
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.5_x^2)/(sqrt(2_pi)) whose analytical solution is exp(-2_pi_pi\*x^2). The array scaled is the analytical solution.

```julia
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")

```

---

<div class="post-metadata">

**Author:** ![Jakub\_Wronowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_wronowski/32/204030_2.png) [@Jakub\_Wronowski](https://discourse.julialang.org/u/Jakub_Wronowski)\
**Post date:** [January 29, 2020, 12:47pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/2 "2020-01-29T12:47:13Z")

</div>

Shouldn’t the array you are passing to `fft` be shifted around 0 with `fftshift`? Also your analytical solution works for infinity, so there could be some effect from those missing tails. I’m only guessing, cannot run it, please quote code with “```”

---

<div class="post-metadata">

**Author:** ![IgnacioSuarez](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@IgnacioSuarez](https://discourse.julialang.org/u/IgnacioSuarez)\
**Post date:** [January 29, 2020, 12:55pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/3 "2020-01-29T12:55:09Z")

</div>

Hi!

Thanks for your answer. When shift the array, I still obtain a complex array. I modify the post. Not sure if the code is more visible now. It is my first post 😅

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [January 29, 2020, 12:56pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/4 "2020-01-29T12:56:18Z")

</div>

Welcome!

Your function is not symmetric around the origin. Try

```julia
x=range(-5,stop=5,length=gridpoints+1)[1:end-1]
function_of_t=exp.(-0.5*x.^2)
fft(function_of_t)

```

As for the code, you should quote it within three backticks, not a single one.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 29, 2020, 12:56pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/5 "2020-01-29T12:56:56Z")

</div>

I don’t see a problem with:

```julia
using FFTW
using Plots

x = range(-5, stop=5, length=256)
function_of_t = @. exp(-0.5x^2)

fourier_trans = fftshift(fft(function_of_t))

y = function_of_t
plot(x, y, title = "Function of t", xlabel = "Time", ylabel = "f(t)")

y = fourier_trans
plot(x, abs.(y), title = "FFT by means of FFTW", xlabel = "omega", ylabel = "F(omega)")

```

Which gives:

 ![1](https://global.discourse-cdn.com/julialang/original/3X/9/8/9889dd92c8a521e4b859fdd3dc5f9f5f1c3184fe.png)

and

 ![2](https://global.discourse-cdn.com/julialang/original/3X/9/8/988578de2cf1e4725f8dc56a0a7b162ea86b1a0e.png)

---

<div class="post-metadata">

**Author:** ![IgnacioSuarez](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@IgnacioSuarez](https://discourse.julialang.org/u/IgnacioSuarez)\
**Post date:** [January 29, 2020, 1:01pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/6 "2020-01-29T13:01:09Z")

</div>

Hi!

Thanks so much for your reply.

My point is that the Fourier transform of exp(-0.5_t²) is exactly exp(-0.5_t²). When FFT is done, I do not get the same result.

---

<div class="post-metadata">

**Author:** ![IgnacioSuarez](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@IgnacioSuarez](https://discourse.julialang.org/u/IgnacioSuarez)\
**Post date:** [January 29, 2020, 1:07pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/7 "2020-01-29T13:07:35Z")

</div>

Hi!

When I do these changes, I still get the complex array: ![ft_fftw](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b2690faadefc1946df44fc84745b27b1b546e68.png)

```julia
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)
fourier_trans=fft(function_of_t)

y=fourier_trans

plot(x,y, title="FFT by means of FFTW", xlabel="omega", ylabel="F(omega)")
savefig("ft_fftw.png")

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 29, 2020, 1:11pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/8 "2020-01-29T13:11:58Z")

</div>

You should plot the magnitude of y, `plot(x, abs.(y), .. )`

---

<div class="post-metadata">

**Author:** ![IgnacioSuarez](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@IgnacioSuarez](https://discourse.julialang.org/u/IgnacioSuarez)\
**Post date:** [January 29, 2020, 1:16pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/9 "2020-01-29T13:16:59Z")

</div>

When I do that, I obtain the following plot that does not correspond with the analytical function. ![ft_fftw](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e6b965e131c00557e3c45c823f7af123e5cffe2.png)

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [January 29, 2020, 1:20pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/10 "2020-01-29T13:20:47Z")

</div>

The amplitude of the real part is of order 100, that of the imaginary part 1e-14: since a `Float64` is 16 digits, the imaginary part is effectively zero. `fft` always compute a complex transform.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [January 29, 2020, 8:08pm UTC](https://discourse.julialang.org/t/problems-using-fftw-solved/33930/11 "2020-01-29T20:08:18Z")

</div>

> [@IgnacioSuarez](#):
>
> My point is that the Fourier transform of exp(-0.5 _t²) is exactly exp(-0.5_ t²)

But you’re not calculating the FT of exp(-0.5 \*t²), but rather the DFT of a sampled version of that function, defined only for a finite amount of time.

Also, the numerical innacuracy of Floats often result in a small phase being returned by FFT, even when in theory the phase should be zero.
