[Numpy-discussion] fftfreq very slow; rfftfreq incorrect?

Andrew Jaffe a.h.jaffe at gmail.com
Wed Aug 30 06:04:22 CDT 2006


Hi all,

the current implementation of fftfreq (which is meant to return the 
appropriate frequencies for an FFT) does the following:

     k = range(0,(n-1)/2+1)+range(-(n/2),0)
     return array(k,'d')/(n*d)

I have tried this with very long (2**24) arrays, and it is ridiculously 
slow. Should this instead use arange (or linspace?) and concatenate 
rather than converting the above list? This seems to result in 
acceptable performance, but we could also perhaps even pre-allocate the 
space.

The numpy.fft.rfftfreq seems just plain incorrect to me. It seems to 
produce lots of duplicated frequencies, contrary to the actual output of 
rfft:

def rfftfreq(n,d=1.0):
     """ rfftfreq(n, d=1.0) -> f

     DFT sample frequencies (for usage with rfft,irfft).

     The returned float array contains the frequency bins in
     cycles/unit (with zero at the start) given a window length n and a
     sample spacing d:

       f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2]/(d*n)   if n is even
       f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2,n/2]/(d*n)   if n is odd

       **** None of these should be doubled, right?

     """
     assert isinstance(n,int)
     return array(range(1,n+1),dtype=int)/2/float(n*d)

Thanks,

Andrew





More information about the Numpy-discussion mailing list