I am resampling a real signal, and since I have at my disposal its fft from rfft
, I want to use irfft(signal, new_length)
. But I can't seem to get it working.
This is a working code snippet that resamples a signal of length 4 using complex fft:
from numpy.fft import fft,ifft
p=array([1.,2.2,4.,1.])
pk=fft(p)
pnew=ifft(pk,8)*(8./4.)
where the factor (8./4.)
rescales from the original to the new length. You can check that pnew[::2]==p
.
Now, when I try to apply the same strategy with real Fourier transform, I get the wrong result at the original points:
from numpy.fft import rfft,irfft
p=array([1.,2.2,4.,1.])
pk=rfft(p)
pnew=irfft(pk,8)*(8./4.)
and I have pnew[::2]=[ 1.45, 1.75, 4.45, 0.55]!=p
.
Does anybody have a clue of what is going on? I have tried using the routines from scipy, with the same result. The documentation itself discusses briefly how to do this, see here, bottom of the page