# Inplace fft convolution

**URL:** <https://discourse.julialang.org/t/inplace-fft-convolution/5345>\
**Category:** General Usage\
**Created:** [August 12, 2017, 11:09am UTC](https://discourse.julialang.org/t/inplace-fft-convolution/5345 "2017-08-12T11:09:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [August 12, 2017, 11:09am UTC](https://discourse.julialang.org/t/inplace-fft-convolution/5345/1 "2017-08-12T11:09:12Z")

</div>

Dear all,

I would like to perform an inplace fft-based convolution. But somehow my inplace convolution is slower than the regular one. Can anyone give me hint why?

Thank you for your help,

Best regards

```julia
Jf(x) = 0.5exp(-abs(x))
N = 2^12;L = 100
const hx = 2L/N
const X = -L + hx*collect(0:N-1)
const wHat = fft(Jf(X))*hx

function convolution(x)
    return ifftshift(real(ifft(wHat .* fft(x))))
end

function convolution!(x)
    x_ = fft(x)
    @. x_ .= wHat .* x_
    ifft!(x_)
    @. x .= real(x_)
    x .= ifftshift(x)
end

x1 = rand(N)
    y1 = @time convolution(x1)
    @time convolution!(x1)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [August 12, 2017, 1:05pm UTC](https://discourse.julialang.org/t/inplace-fft-convolution/5345/2 "2017-08-12T13:05:16Z")

</div>

The in-place version is faster on Julia 0.7 (perhaps also on 0.6). So, update the version.  
Reminder, the FFT and friends functions have moved into FFTW package, which forgot to add some exports, so to get your example to work I had to:

```julia
Pkg.add("FFTW")
import FFTW: fft, ifft, ifftshift, ifft!

```

and then the code goes through.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [August 13, 2017, 4:33pm UTC](https://discourse.julialang.org/t/inplace-fft-convolution/5345/3 "2017-08-13T16:33:30Z")

</div>

Thank you!!
