pyfftw rfft/irfft not symmetric for odd number of samples?

234 Views Asked by At

I'm trying to call forward and inverse rfft using the pyfftw module.
For even number of samples, irfft(rfft) = identity as I was expecting,
But this is not true for an odd number of samples, why?

example below :

import numpy as np
from pyfftw.builders import rfft as rfft_builder, irfft as irfft_builder
from pyfftw import empty_aligned

n = 5  # number of samples

# >>>>> forward
a = empty_aligned((n, ), dtype="float32")
rfft = rfft_builder(a)

a[:] = np.arange(n)
b = rfft()
print(a, "=>", b)

# <<<<< backward
B = empty_aligned((len(b), ), dtype=b.dtype)
irfft = irfft_builder(B)

B[:] = b[:]
A = irfft()
print(B, "=>", A)

For n=4 => ok

[0. 1. 2. 3.] => [ 6.+0.j -2.+2.j -2.+0.j]
[ 6.+0.j -2.+2.j -2.+0.j] => [0. 1. 2. 3.]

For n=5 => ???

[0. 1. 2. 3. 4.] => [10. +0.j        -2.5+3.440955j  -2.5+0.8122992j]
[10. +0.j        -2.5+3.440955j  -2.5+0.8122992j] => [0.625     1.4045225 3.125     4.8454776]
1

There are 1 best solutions below

1
On

This matches the behavior of np.fft.irfft(), which should be called as np.fft.irfft(b, len(a)) to get the original a back. The numpy docs go into more detail in the Notes section.

For pyFFTW, building the inverse transform with irfft = irfft_builder(B, n) should fix it.