# Intended behaviour of irfft

**URL:** <https://discourse.julialang.org/t/intended-behaviour-of-irfft/11945>\
**Category:** New to Julia\
**Created:** [June 25, 2018, 7:18pm UTC](https://discourse.julialang.org/t/intended-behaviour-of-irfft/11945 "2018-06-25T19:18:06Z")\
**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:** [June 25, 2018, 7:18pm UTC](https://discourse.julialang.org/t/intended-behaviour-of-irfft/11945/1 "2018-06-25T19:18:06Z")

</div>

Hi,

I am trying to get a hold on `irfft` but there is no much docs / examples about it. I tried some python code but the equivalent failed. Basically (and I aopologize for this), I can’t make sense of

`irfft(fft(rand(16)))`

which throws an error

Has anyone used this ?

Thank you for your help,

Bests

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [June 25, 2018, 7:36pm UTC](https://discourse.julialang.org/t/intended-behaviour-of-irfft/11945/2 "2018-06-25T19:36:55Z")

</div>

[`irfft`](https://docs.julialang.org/en/stable/stdlib/math/#Base.DFT.irfft) is the inverse of [`rfft`](https://docs.julialang.org/en/stable/stdlib/math/#Base.DFT.rfft). Also, you need to supply an extra argument for the size of first dimension (since it is not uniquely determined by the output of `rfft`).

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [June 25, 2018, 7:58pm UTC](https://discourse.julialang.org/t/intended-behaviour-of-irfft/11945/3 "2018-06-25T19:58:59Z")

</div>

To give a little more context, the Discrete Fourier Transform (DFT) is in general a complex-to-complex transform - if you give it a vector of `N` complex numbers in the time domain, you get `N` complex numbers back representing the spectra of your signal. The Fast Fourier Transform (FFT) is a fast algorithm for computing the DFT, but it’s ubiquitous enough that people commonly use “FFT” for the transform in general.

In the special case where you signal is real-valued (the imaginary part is zero for all your samples), it turns out that the spectrum is conjugate-symmetric (positive frequencies are the complex conjugates of negative frequencies), so you only need to compute half of it. `rfft` exists to do that optimization, and only gives you the positive half of the spectrum. So in that case `rfft(x)` will give you a vector of length `length(x) ÷ 2 + 1` (note `÷` is truncating integer division).

So if `x` is length `16`, `rfft(x)` will be of length `9`, but if `x` is length `17`, `rfft(x)` will _also_ be length `9`. That’s why when you invert the transform with `irfft` you need to specify the original length, as it’s lost in the `rfft` process.

If you’re dealing with real-valued signals you can use `rfft` and `irfft`, and if you have complex signals you should use `fft` and `ifft`
