# Intended behaviour of irfft

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 ?

Bests

irfft is the inverse of 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).

1 Like

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

6 Likes