# Modifying all the frequency components of an FFT to 0 EXCEPT the 0th, +-3rd, +-6th, etc

**URL:** <https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463>\
**Category:** New to Julia\
**Tags:** question, fftw, array\
**Created:** [October 15, 2020, 9:53pm UTC](https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463 "2020-10-15T21:53:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ash](https://discourse.julialang.org/u/ash)\
**Post date:** [October 15, 2020, 9:53pm UTC](https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463/1 "2020-10-15T21:53:21Z")

</div>

I have an array and I want to modify its FFT frequencies by an exponential factor except those that are a multiple of 3 (or more generally, M). For example, we can do this naively as:

```julia
N = 256
M = 3
ψ = rand(N)
fft!(ψ)
for i = 2:N/2+1
    i = Int(i)
    if (i - 1) % M != 0
       ψ[i] *= exp(-*abs(ψ[i])) # + component
        ψ[N - i + 2] *= exp(-abs(ψ[N - i + 2])) # - component
    end
end 
ifft!(ψ)

```

I am aware of fftshift and the FFTView.jl package, but I can’t quite figure out how to do the indexing in a more elegant way (i.e. a 1 or 2 liner). This operation is done millions or billions of times in my production code so I’d like to do it more elegantly.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 16, 2020, 5:59am UTC](https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463/2 "2020-10-16T05:59:50Z")

</div>

Don’t know anything about FFT unfortunately, but you might want to write your range as `i = 2:N÷2 + 1` which saves you the `Int(i)` call on every iteration:

```julia
help?> ÷
"÷" can be typed by \div<tab>

search: ÷

  div(x, y)
  ÷(x, y)

  The quotient from Euclidean division. Computes x/y, truncated to an integer.

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 16, 2020, 8:45am UTC](https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463/3 "2020-10-16T08:45:48Z")

</div>

Instead of looping over a range with step 1, consider looping over a range with step `M` and remove the if statement.

Is the data `ψ` real? If so, consider using `rfft` and save half the computation and memory.

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [October 16, 2020, 9:04am UTC](https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463/4 "2020-10-16T09:04:19Z")

</div>

if N and M don’t change often, you can store the indices in an array (if they do and N is big, it might be a lot of allocations)

```julia
ind = [i for i in 2:(N÷2+1) if (i-1)%M != 0]
@views ψ[ind] .*= exp.(-abs.(ψ[ind]))

```

with `@views` to avoid allocating new arrays

---

<div class="post-metadata">

**Author:** ![ash](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ash](https://discourse.julialang.org/u/ash)\
**Post date:** [October 21, 2020, 7:11am UTC](https://discourse.julialang.org/t/modifying-all-the-frequency-components-of-an-fft-to-0-except-the-0th-3rd-6th-etc/48463/5 "2020-10-21T07:11:43Z")

</div>

Thank you, I ended up with:

```julia
ind_= [i for i in 2:(N÷2+1) if (i-1)%M != 0]
ind = sort([ind_p; N.-ind.+2])
@views ψ[ind] .*= exp.(-abs.(ψ[ind]))

```
