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

Stefan van der Walt stefan at sun.ac.za
Wed Aug 30 11:41:49 CDT 2006

On Wed, Aug 30, 2006 at 12:04:22PM +0100, Andrew Jaffe wrote:
> 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)

Please produce a code snippet to demonstrate the problem.  We can then
fix the bug and use your code as a unit test.

Regards
Stéfan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fftfreq_bench.py
Type: text/x-python
Size: 2201 bytes
Desc: not available