# Fourier transform freezes Julia

**URL:** https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771
**Category:** Signal and Image Processing
**Tags:** fftw, intervals, memory-allocation
**Created:** [May 4, 2020, 10:39pm UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771 "2020-05-04T22:39:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Martijn-R](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martijn-r/32/10335_2.png) [@Martijn-R](https://discourse.julialang.org/u/Martijn-R)
#### Post date: [May 4, 2020, 10:39pm UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771/1 "2020-05-04T22:39:31Z")

</div>

I am processing an audio sample (about 20minutes long) in small parts of several seconds.

At the moment my code freezes every now and then. The example below is a bit silly but is a minimal ‘working’ example which often stops within the first 100 iterations.

```julia
using FileIO, SampledSignals, LibSndFile, FFTW
ls = load( "sample.ogg")

for T in 10s:0.1s:1000s
  print( T)
  a = [fft( ls[ (t-0.1s)..(t-0.1s+1s)]) for t in .1s:.1s:T]
end

```

The next loop is similar except for the interval / indexing but seems to work without issues:

```julia
# this works ...
for T in 10s:0.1s:1000s
  print( T)
  a = [fft( ls[ t..(t+10s)]) for t in .1s:.1s:T]
end

```

For me it is unclear why the first loop has a problem but the second loop not. Garbage collection within the first loop seem to stabilize the code, but makes it terribly slow, of course.

I would like to make clean and efficient code, but as I am not sure what the problem is (indexing / memory / IntervalSets), I cannot manage it… Any help is appreciated!

---

<div class="post-metadata">

### Author: ![Martijn-R](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martijn-r/32/10335_2.png) [@Martijn-R](https://discourse.julialang.org/u/Martijn-R)
#### Post date: [May 27, 2020, 10:46pm UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771/2 "2020-05-27T22:46:40Z")

</div>

I checked another OS…

The above problem occurs when using Linux, often within a few minutes. With Windows, it seems that the issue does not occur.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [May 28, 2020, 1:26am UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771/3 "2020-05-28T01:26:44Z")

</div>

No idea what it might be but maybe you could try planning the FFT to see if it helps anything? You could also try in-place FFTs via `mul!`, either just do it in-place on those slices that are created in each iteration which are allocating an array anyway, or pre-allocate everything you need for the results to start, and then use views of each slice so they don’t allocate (I’m actually not sure Julia can FFT views without making a copy, but worth checking). You could also try `rfft` to get a factor of 2x in speed / memory usage, assuming those input samples are real.

[https://juliamath.github.io/AbstractFFTs.jl/stable/api/#AbstractFFTs.plan\_fft](https://juliamath.github.io/AbstractFFTs.jl/stable/api/#AbstractFFTs.plan_fft)

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [May 28, 2020, 1:31am UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771/4 "2020-05-28T01:31:22Z")

</div>

Also, if you have FFTW built with MKL, try turning that off, or if you don’t, try turning it on (MKL is generally faster, but maybe your error is tied to one or the other).

> **[GitHub - JuliaMath/FFTW.jl: Julia bindings to the FFTW library for fast...](https://github.com/JuliaMath/FFTW.jl#mkl)**
>
> Julia bindings to the FFTW library for fast Fourier transforms - GitHub - JuliaMath/FFTW.jl: Julia bindings to the FFTW library for fast Fourier transforms

---

<div class="post-metadata">

### Author: ![Martijn-R](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martijn-r/32/10335_2.png) [@Martijn-R](https://discourse.julialang.org/u/Martijn-R)
#### Post date: [May 30, 2020, 9:56pm UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771/5 "2020-05-30T21:56:22Z")

</div>

I had already tried using in-place FFT via `fft!`, but that froze the evaluation as well.

I will try to perform a planned FFT, and also see if I can toggle the MKL. Thanks for these tips. The latter could be an explanation as to why it works on Windows but not on Linux(?)

---

<div class="post-metadata">

### Author: ![Martijn-R](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martijn-r/32/10335_2.png) [@Martijn-R](https://discourse.julialang.org/u/Martijn-R)
#### Post date: [July 3, 2020, 12:56pm UTC](https://discourse.julialang.org/t/fourier-transform-freezes-julia/38771/6 "2020-07-03T12:56:30Z")

</div>

Upgrading my Linux Julia installation from v1.3.0 to v1.4.0 fixed the issue.

Thanks @marius311 for your time.
