Пример #1
0
 def test_djbfft(self):
     from numpy.fft import fft as numpy_fft
     for i in range(2,14):
         n = 2**i
         x = range(n)
         y2 = numpy_fft(x)
         y1 = zeros((n,),dtype=double)
         y1[0] = y2[0].real
         y1[-1] = y2[n/2].real
         for k in range(1, int(n/2)):
             y1[2*k-1] = y2[k].real
             y1[2*k] = y2[k].imag
         y = fftpack.drfft(x)
         assert_array_almost_equal(y,y1)
Пример #2
0
 def test_djbfft(self):
     from numpy.fft import ifft as numpy_ifft
     for i in range(2,14):
         n = 2**i
         x = range(n)
         x1 = zeros((n,),dtype=cdouble)
         x1[0] = x[0]
         for k in range(1, int(n/2)):
             x1[k] = x[2*k-1]+1j*x[2*k]
             x1[n-k] = x[2*k-1]-1j*x[2*k]
         x1[n/2] = x[-1]
         y1 = numpy_ifft(x1)
         y = fftpack.drfft(x,direction=-1)
         assert_array_almost_equal(y,y1)
Пример #3
0
 def test_djbfft(self):
     from numpy.fft import ifft as numpy_ifft
     for i in range(2,14):
         n = 2**i
         x = list(range(n))
         x1 = zeros((n,),dtype=cdouble)
         x1[0] = x[0]
         for k in range(1, n//2):
             x1[k] = x[2*k-1]+1j*x[2*k]
             x1[n-k] = x[2*k-1]-1j*x[2*k]
         x1[n//2] = x[-1]
         y1 = numpy_ifft(x1)
         y = fftpack.drfft(x,direction=-1)
         assert_array_almost_equal(y,y1)
Пример #4
0
 def test_djbfft(self):
     from numpy.fft import fft as numpy_fft
     for i in range(2,14):
         n = 2**i
         x = list(range(n))
         y2 = numpy_fft(x)
         y1 = zeros((n,),dtype=double)
         y1[0] = y2[0].real
         y1[-1] = y2[n//2].real
         for k in range(1, n//2):
             y1[2*k-1] = y2[k].real
             y1[2*k] = y2[k].imag
         y = fftpack.drfft(x)
         assert_array_almost_equal(y,y1)
Пример #5
0
def fhtq(a, xsave, tdir=1):
    """Kernel routine of FFTLog.

    This is the basic FFTLog routine.

    fhtq computes a discrete version of the biased Hankel transform

             infinity
              /           q
       ã(k) = | a(r) (k r)  J  (k r) k dr
              /              mu
             0

    fhti must be called before the first call to fhtq.

    A call to fhtq with dir=1 followed by a call to fhtq with dir=-1, or vice
    versa, leaves the array a unchanged.

    Parameters
    ----------
    a : array
        Periodic array a(r) to transform: a(j) is a(r_j) at r_j = r_c
        exp[(j-jc) dlnr] where jc = (n+1)/2 = central index of array.

    xsave : array
        Working array set up by fhti.

    tdir : int, optional; {1, -1}
        -  1 for forward transform (default),
        - -1 for backward transform.
        A backward transform (dir = -1) is the same as a forward transform with
        q -> -q, for any kr if n is odd, for low-ringing kr if n is even.

    Returns
    -------
    a : array
        Transformed periodic array ã(k): a(j) is ã(k_j) at k_j = k_c exp[(j-jc)
        dlnr].

    """
    fct = a.copy()
    q = xsave[0]
    n = fct.size

    # normal FFT
    # fct = rfft(fct)
    # _raw_fft(fct, n, -1, 1, 1, _fftpack.drfft)
    fct = drfft(fct, n, 1, 0)

    m = np.arange(1, n / 2, dtype=int)  # index variable
    if q == 0:  # unbiased (q = 0) transform
        # multiply by (kr)^[- i 2 m pi/(n dlnr)] U_mu[i 2 m pi/(n dlnr)]
        ar = fct[2 * m - 1]
        ai = fct[2 * m]
        fct[2 * m - 1] = ar * xsave[2 * m + 1] - ai * xsave[2 * m + 2]
        fct[2 * m] = ar * xsave[2 * m + 2] + ai * xsave[2 * m + 1]
        # problem(2*m)atical last element, for even n
        if np.mod(n, 2) == 0:
            ar = xsave[-2]
            if (tdir == 1):  # forward transform: multiply by real part
                # Why? See http://casa.colorado.edu/~ajsh/FFTLog/index.html#ure
                fct[-1] *= ar
            elif (tdir == -1):  # backward transform: divide by real part
                # Real part ar can be zero for maximally bad choice of kr.
                # This is unlikely to happen by chance, but if it does, policy
                # is to let it happen.  For low-ringing kr, imaginary part ai
                # is zero by construction, and real part ar is guaranteed
                # nonzero.
                fct[-1] /= ar

    else:  # biased (q != 0) transform
        # multiply by (kr)^[- i 2 m pi/(n dlnr)] U_mu[q + i 2 m pi/(n dlnr)]
        # phase
        ar = fct[2 * m - 1]
        ai = fct[2 * m]
        fct[2 * m - 1] = ar * xsave[3 * m + 2] - ai * xsave[3 * m + 3]
        fct[2 * m] = ar * xsave[3 * m + 3] + ai * xsave[3 * m + 2]

        if tdir == 1:  # forward transform: multiply by amplitude
            fct[0] *= xsave[3]
            fct[2 * m - 1] *= xsave[3 * m + 1]
            fct[2 * m] *= xsave[3 * m + 1]

        elif tdir == -1:  # backward transform: divide by amplitude
            # amplitude of m=0 element
            ar = xsave[3]
            if ar == 0:
                # Amplitude of m=0 element can be zero for some mu, q
                # combinations (singular inverse); policy is to drop
                # potentially infinite constant.
                fct[0] = 0
            else:
                fct[0] /= ar

            # remaining amplitudes should never be zero
            fct[2 * m - 1] /= xsave[3 * m + 1]
            fct[2 * m] /= xsave[3 * m + 1]

        # problematical last element, for even n
        if np.mod(n, 2) == 0:
            m = int(n / 2)
            ar = xsave[3 * m + 2] * xsave[3 * m + 1]
            if tdir == 1:  # forward transform: multiply by real part
                fct[-1] *= ar
            elif (tdir == -1):  # backward transform: divide by real part
                # Real part ar can be zero for maximally bad choice of kr.
                # This is unlikely to happen by chance, but if it does, policy
                # is to let it happen.  For low-ringing kr, imaginary part ai
                # is zero by construction, and real part ar is guaranteed
                # nonzero.
                fct[-1] /= ar

    # normal FFT back
    # fct = irfft(fct)
    # _raw_fft(fct, n, -1, -1, 1, _fftpack.drfft)
    fct = drfft(fct, n, -1, 1)

    # reverse the array and at the same time undo the FFTs' multiplication by n
    # => Just reverse the array, the rest is already done in drfft.
    fct = fct[::-1]

    return fct
Пример #6
0
def fftpack_drfftb(buf, s, drfft=drfft):
	return drfft(buf, None, -1, 1, 1)
Пример #7
0
def fftpack_drfftf(buf, s, drfft=drfft):
	return drfft(buf, None, 1, 0, 1)