ó
î&]\c           @` sÐ  d  Z  d d l m Z m Z m Z d d d d d d d	 d
 g Z d d l m Z m Z d d l Z d d l	 m
 Z
 d d l m Z d d l Z e j e
 j ƒ e j e
 j ƒ e j e
 j ƒ e j e
 j ƒ e j e
 j ƒ e j e
 j ƒ [ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z i e e j e j ƒ 6e
 j e j e j ƒ 6e e j e j  ƒ 6e
 j! e j e j" ƒ 6Z# i e e j e j ƒ 6e
 j$ e j e j ƒ 6Z% i e e j e j  ƒ 6e
 j& e j e j" ƒ 6e e j e j ƒ 6e
 j& e j e j ƒ 6Z' d „  Z( d „  Z) d „  Z* d d e, d „ Z- d d e, d „ Z. d d e, d „ Z/ d d e, d „ Z0 d „  Z1 d d e, d  „ Z2 d! „  Z3 d d e, d" „ Z4 d d& e, d$ „ Z5 d d' e, d% „ Z6 d S((   s(   
Discrete Fourier Transforms - basic.py
i    (   t   divisiont   print_functiont   absolute_importt   fftt   ifftt   fftnt   ifftnt   rfftt   irfftt   fft2t   ifft2(   t   swapaxest   zerosNi   (   t   _fftpack(   t   _init_nd_shape_and_axes_sortedc         C` s   t  |  j j | ƒ S(   N(   t
   issubclasst   dtypet   type(   t   arrt	   typeclass(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   istype   s    c         C` sC   |  | k r t  St | t j ƒ r6 t | d ƒ r6 t  S|  j d k S(   s}   
    Strict check for `arr` not sharing any data with `original`,
    under the assumption that arr = asarray(original)

    t	   __array__N(   t   Falset
   isinstancet   numpyt   ndarrayt   hasattrt   baset   None(   R   t   original(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   _datacopied   s
    "c         C` s[   t  |  ƒ }  |  d k r t Sx/ d D]' } x |  | d k rI |  | }  q, Wq# W|  |  d @S(   s·   
    Is the size of FFT such that FFTPACK can handle it in single precision
    with sufficient accuracy?

    Composite numbers of 2, 3, and 5 are accepted, as FFTPACK has those
    i    i   i   i   (   i   i   (   t   intt   True(   t   nt   c(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   _is_safe_size3   s    c         O` sH   t  | ƒ r" t j |  | | | Ž St j |  | | | Ž j t j ƒ Sd  S(   N(   R#   R   t   crfftt   zrfftt   astypeR   t	   complex64(   t   xR!   t   at   kw(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   _fake_crfftH   s    c         O` sH   t  | ƒ r" t j |  | | | Ž St j |  | | | Ž j t j ƒ Sd  S(   N(   R#   R   t   cfftt   zfftR&   R   R'   (   R(   R!   R)   R*   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt
   _fake_cfftO   s    c         O` sH   t  | ƒ r" t j |  | | | Ž St j |  | | | Ž j t j ƒ Sd  S(   N(   R#   R   R   t   drfftR&   R   t   float32(   R(   R!   R)   R*   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt
   _fake_rfftV   s    c         O` sZ   t  j t t t | ƒ ƒ ƒ r4 t j |  | | | Ž St j |  | | | Ž j t  j	 ƒ Sd  S(   N(
   R   t   allt   listt   mapR#   R   t   cfftndt   zfftndR&   R'   (   R(   t   shapeR)   R*   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   _fake_cfftnd]   s    c         C` sË   t  |  d ƒ rf |  j j t j d k rf |  j t j k rP t j |  d t j ƒSt j |  d |  j ƒSt j |  ƒ } | j t j k r t j | d t j ƒS| j j t j d k rÃ t j |  ƒ S| Sd S(   s—   Like numpy asfarray, except that it does not modify x dtype if x is
    already an array with a float dtype, and do not cast complex types to
    real.R   t   AllFloatN(	   R   R   t   charR   t	   typecodest   halft   asarrayR0   t   asfarray(   R(   t   ret(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt	   _asfarray}   s    (c         C` sÒ   t  |  j ƒ } | | | k re t d ƒ g t | ƒ } t d | ƒ | | <|  t | ƒ }  |  t f St d ƒ g t | ƒ } t d | | ƒ | | <| | | <t | |  j j	 ƒ } |  | t | ƒ <| t
 f Sd S(   s6    Internal auxiliary function for _raw_fft, _raw_fftnd.i    N(   R3   R7   t   sliceR   t   lent   tupleR   R   R   R:   R    (   R(   R!   t   axist   st   indext   z(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt
   _fix_shape”   s    

c         C` sõ   | d k r |  j | } n: | |  j | k rV t |  | | ƒ \ }  } | pP | } n  | d k  ru t d | ƒ ‚ n  | d k sš | t |  j ƒ d k rµ | |  | | d | ƒ} n< t |  | d ƒ }  | |  | | d | ƒ} t | | d ƒ } | S(   s8    Internal auxiliary function for fft, ifft, rfft, irfft.i   s1   Invalid number of FFT data points (%d) specified.iÿÿÿÿt   overwrite_xN(   R   R7   RH   t
   ValueErrorRB   R   (   R(   R!   RD   t	   directionRI   t   work_functiont	   copy_madet   r(    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   _raw_fft¥   s    %iÿÿÿÿc         C` sp  t  |  ƒ } y t | j } Wn$ t k
 rC t d | j ƒ ‚ n Xt | t j ƒ pe t | t j ƒ sq d } n  | pƒ t	 | |  ƒ } | d k r¢ | j | } n: | | j | k rÜ t | | | ƒ \ } } | pÖ | } n  | d k  rû t d | ƒ ‚ n  | d k s | t | j ƒ d k r6| | | d d | ƒ St | | d ƒ } | | | d d | ƒ } t | | d ƒ S(   s  
    Return discrete Fourier transform of real or complex sequence.

    The returned complex array contains ``y(0), y(1),..., y(n-1)`` where

    ``y(j) = (x * exp(-2*pi*sqrt(-1)*j*np.arange(n)/n)).sum()``.

    Parameters
    ----------
    x : array_like
        Array to Fourier transform.
    n : int, optional
        Length of the Fourier transform.  If ``n < x.shape[axis]``, `x` is
        truncated.  If ``n > x.shape[axis]``, `x` is zero-padded. The
        default results in ``n = x.shape[axis]``.
    axis : int, optional
        Axis along which the fft's are computed; the default is over the
        last axis (i.e., ``axis=-1``).
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed; the default is False.

    Returns
    -------
    z : complex ndarray
        with the elements::

            [y(0),y(1),..,y(n/2),y(1-n/2),...,y(-1)]        if n is even
            [y(0),y(1),..,y((n-1)/2),y(-(n-1)/2),...,y(-1)]  if n is odd

        where::

            y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n), j = 0..n-1

    See Also
    --------
    ifft : Inverse FFT
    rfft : FFT of a real sequence

    Notes
    -----
    The packing of the result is "standard": If ``A = fft(a, n)``, then
    ``A[0]`` contains the zero-frequency term, ``A[1:n/2]`` contains the
    positive-frequency terms, and ``A[n/2:]`` contains the negative-frequency
    terms, in order of decreasingly negative frequency. So for an 8-point
    transform, the frequencies of the result are [0, 1, 2, 3, -4, -3, -2, -1].
    To rearrange the fft output so that the zero-frequency component is
    centered, like [-4, -3, -2, -1,  0,  1,  2,  3], use `fftshift`.

    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    This function is most efficient when `n` is a power of two, and least
    efficient when `n` is prime.

    Note that if ``x`` is real-valued then ``A[j] == A[n-j].conjugate()``.
    If ``x`` is real-valued and ``n`` is even then ``A[n/2]`` is real.

    If the data type of `x` is real, a "real FFT" algorithm is automatically
    used, which roughly halves the computation time.  To increase efficiency
    a little further, use `rfft`, which does the same calculation, but only
    outputs half of the symmetrical spectrum.  If the data is both real and
    symmetrical, the `dct` can again double the efficiency, by generating
    half of the spectrum from half of the signal.

    Examples
    --------
    >>> from scipy.fftpack import fft, ifft
    >>> x = np.arange(5)
    >>> np.allclose(fft(ifft(x)), x, atol=1e-15)  # within numerical accuracy.
    True

    s   type %s is not supportedi   s1   Invalid number of FFT data points (%d) specified.iÿÿÿÿi    N(   R@   t   _DTYPE_TO_FFTR   t   KeyErrorRJ   R   R   R'   t
   complex128R   R   R7   RH   RB   R   (   R(   R!   RD   RI   t   tmpRL   RM   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR   º   s*    K$	%c         C` sp  t  |  ƒ } y t | j } Wn$ t k
 rC t d | j ƒ ‚ n Xt | t j ƒ pe t | t j ƒ sq d } n  | pƒ t	 | |  ƒ } | d k r¢ | j | } n: | | j | k rÜ t | | | ƒ \ } } | pÖ | } n  | d k  rû t d | ƒ ‚ n  | d k s | t | j ƒ d k r6| | | d d | ƒ St | | d ƒ } | | | d d | ƒ } t | | d ƒ S(   sw  
    Return discrete inverse Fourier transform of real or complex sequence.

    The returned complex array contains ``y(0), y(1),..., y(n-1)`` where

    ``y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean()``.

    Parameters
    ----------
    x : array_like
        Transformed data to invert.
    n : int, optional
        Length of the inverse Fourier transform.  If ``n < x.shape[axis]``,
        `x` is truncated.  If ``n > x.shape[axis]``, `x` is zero-padded.
        The default results in ``n = x.shape[axis]``.
    axis : int, optional
        Axis along which the ifft's are computed; the default is over the
        last axis (i.e., ``axis=-1``).
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed; the default is False.

    Returns
    -------
    ifft : ndarray of floats
        The inverse discrete Fourier transform.

    See Also
    --------
    fft : Forward FFT

    Notes
    -----
    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    This function is most efficient when `n` is a power of two, and least
    efficient when `n` is prime.

    If the data type of `x` is real, a "real IFFT" algorithm is automatically
    used, which roughly halves the computation time.

    Examples
    --------
    >>> from scipy.fftpack import fft, ifft
    >>> import numpy as np
    >>> x = np.arange(5)
    >>> np.allclose(ifft(fft(x)), x, atol=1e-15)  # within numerical accuracy.
    True

    s   type %s is not supportedi   s1   Invalid number of FFT data points (%d) specified.iÿÿÿÿN(   R@   RP   R   RQ   RJ   R   R   R'   RR   R   R   R7   RH   RB   R   (   R(   R!   RD   RI   RS   RL   RM   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR   #  s*    5$	%c         C` s   t  |  ƒ } t j | ƒ s* t d ƒ ‚ n  y t | j } Wn$ t k
 ra t d | j ƒ ‚ n X| pt t | |  ƒ } t	 | | | d | | ƒ S(   s£  
    Discrete Fourier transform of a real sequence.

    Parameters
    ----------
    x : array_like, real-valued
        The data to transform.
    n : int, optional
        Defines the length of the Fourier transform.  If `n` is not specified
        (the default) then ``n = x.shape[axis]``.  If ``n < x.shape[axis]``,
        `x` is truncated, if ``n > x.shape[axis]``, `x` is zero-padded.
    axis : int, optional
        The axis along which the transform is applied.  The default is the
        last axis.
    overwrite_x : bool, optional
        If set to true, the contents of `x` can be overwritten. Default is
        False.

    Returns
    -------
    z : real ndarray
        The returned real array contains::

          [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))]              if n is even
          [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))]   if n is odd

        where::

          y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k*2*pi/n)
          j = 0..n-1

    See Also
    --------
    fft, irfft, numpy.fft.rfft

    Notes
    -----
    Within numerical accuracy, ``y == rfft(irfft(y))``.

    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    To get an output with a complex datatype, consider using the related
    function `numpy.fft.rfft`.

    Examples
    --------
    >>> from scipy.fftpack import fft, rfft
    >>> a = [9, -9, 1, 3]
    >>> fft(a)
    array([  4. +0.j,   8.+12.j,  16. +0.j,   8.-12.j])
    >>> rfft(a)
    array([  4.,   8.,  12.,  16.])

    s"   1st argument must be real sequences   type %s is not supportedi   (
   R@   R   t	   isrealobjt	   TypeErrort   _DTYPE_TO_RFFTR   RQ   RJ   R   RO   (   R(   R!   RD   RI   RS   RL   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR   v  s    :c         C` s   t  |  ƒ } t j | ƒ s* t d ƒ ‚ n  y t | j } Wn$ t k
 ra t d | j ƒ ‚ n X| pt t | |  ƒ } t	 | | | d | | ƒ S(   sM  
    Return inverse discrete Fourier transform of real sequence x.

    The contents of `x` are interpreted as the output of the `rfft`
    function.

    Parameters
    ----------
    x : array_like
        Transformed data to invert.
    n : int, optional
        Length of the inverse Fourier transform.
        If n < x.shape[axis], x is truncated.
        If n > x.shape[axis], x is zero-padded.
        The default results in n = x.shape[axis].
    axis : int, optional
        Axis along which the ifft's are computed; the default is over
        the last axis (i.e., axis=-1).
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed; the default is False.

    Returns
    -------
    irfft : ndarray of floats
        The inverse discrete Fourier transform.

    See Also
    --------
    rfft, ifft, numpy.fft.irfft

    Notes
    -----
    The returned real array contains::

        [y(0),y(1),...,y(n-1)]

    where for n is even::

        y(j) = 1/n (sum[k=1..n/2-1] (x[2*k-1]+sqrt(-1)*x[2*k])
                                     * exp(sqrt(-1)*j*k* 2*pi/n)
                    + c.c. + x[0] + (-1)**(j) x[n-1])

    and for n is odd::

        y(j) = 1/n (sum[k=1..(n-1)/2] (x[2*k-1]+sqrt(-1)*x[2*k])
                                     * exp(sqrt(-1)*j*k* 2*pi/n)
                    + c.c. + x[0])

    c.c. denotes complex conjugate of preceding expression.

    For details on input parameters, see `rfft`.

    To process (conjugate-symmetric) frequency-domain data with a complex
    datatype, consider using the related function `numpy.fft.irfft`.

    Examples
    --------
    >>> from scipy.fftpack import rfft, irfft
    >>> a = [1.0, 2.0, 3.0, 4.0, 5.0]
    >>> irfft(a)
    array([ 2.6       , -3.16405192,  1.24398433, -1.14955713,  1.46962473])
    >>> irfft(rfft(a))
    array([1., 2., 3., 4., 5.])

    s"   1st argument must be real sequences   type %s is not supportediÿÿÿÿ(
   R@   R   RT   RU   RV   R   RQ   RJ   R   RO   (   R(   R!   RD   RI   RS   RL   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR   ¿  s    Bc         C` s–  | d k } t |  | | ƒ \ } } | ry x6 | D]. } t |  | | | ƒ \ }  } | p\ | } q1 W| |  | | d | ƒSx9 t d | j d ƒ D]! }	 t j |  | |	 |	 ƒ }  q Wt t |  j | j |  j ƒ ƒ }
 t j	 |  j ƒ } | | |
 <xF t t
 |
 ƒ ƒ D]2 }	 t |  | |	 |
 |	 ƒ \ }  } | p5| } qW| |  | | d | ƒ} x; t t
 | ƒ d d ƒ D]! }	 t j | |	 | |	 ƒ } qmW| S(   s.   Internal auxiliary function for fftnd, ifftnd.RI   i   i    iÿÿÿÿN(   R   R   RH   t   ranget   sizeR   R   R3   t   ndimt   onesRB   (   R(   RE   t   axesRK   RI   RL   t   noaxest   axRM   t   it   waxesR7   RN   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt
   _raw_fftnd  s&    "
 c         C` s   t  |  | | | d ƒ S(   s¡  
    Return multidimensional discrete Fourier transform.

    The returned array contains::

      y[j_1,..,j_d] = sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
         x[k_1,..,k_d] * prod[i=1..d] exp(-sqrt(-1)*2*pi/n_i * j_i * k_i)

    where d = len(x.shape) and n = x.shape.

    Parameters
    ----------
    x : array_like
        The (n-dimensional) array to transform.
    shape : int or array_like of ints or None, optional
        The shape of the result.  If both `shape` and `axes` (see below) are
        None, `shape` is ``x.shape``; if `shape` is None but `axes` is
        not None, then `shape` is ``scipy.take(x.shape, axes, axis=0)``.
        If ``shape[i] > x.shape[i]``, the i-th dimension is padded with zeros.
        If ``shape[i] < x.shape[i]``, the i-th dimension is truncated to
        length ``shape[i]``.
        If any element of `shape` is -1, the size of the corresponding
        dimension of `x` is used.
    axes : int or array_like of ints or None, optional
        The axes of `x` (`y` if `shape` is not None) along which the
        transform is applied.
        The default is over all axes.
    overwrite_x : bool, optional
        If True, the contents of `x` can be destroyed.  Default is False.

    Returns
    -------
    y : complex-valued n-dimensional numpy array
        The (n-dimensional) DFT of the input array.

    See Also
    --------
    ifftn

    Notes
    -----
    If ``x`` is real-valued, then
    ``y[..., j_i, ...] == y[..., n_i-j_i, ...].conjugate()``.

    Both single and double precision routines are implemented.  Half precision
    inputs will be converted to single precision.  Non floating-point inputs
    will be converted to double precision.  Long-double precision inputs are
    not supported.

    Examples
    --------
    >>> from scipy.fftpack import fftn, ifftn
    >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
    >>> np.allclose(y, fftn(ifftn(y)))
    True

    i   (   t   _raw_fftn_dispatch(   R(   R7   R[   RI   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR   6  s    :c         C` sŸ   t  |  ƒ } y t | j } Wn$ t k
 rC t d | j ƒ ‚ n Xt | t j ƒ pe t | t j ƒ sq d } n  | pƒ t	 | |  ƒ } t
 | | | | | | ƒ S(   Ns   type %s is not supportedi   (   R@   t   _DTYPE_TO_FFTNR   RQ   RJ   R   R   R'   RR   R   R`   (   R(   R7   R[   RI   RK   RS   RL   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyRa   s  s    $	c         C` s   t  |  | | | d ƒ S(   s¶  
    Return inverse multi-dimensional discrete Fourier transform.

    The sequence can be of an arbitrary type.

    The returned array contains::

      y[j_1,..,j_d] = 1/p * sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
         x[k_1,..,k_d] * prod[i=1..d] exp(sqrt(-1)*2*pi/n_i * j_i * k_i)

    where ``d = len(x.shape)``, ``n = x.shape``, and ``p = prod[i=1..d] n_i``.

    For description of parameters see `fftn`.

    See Also
    --------
    fftn : for detailed information.

    Examples
    --------
    >>> from scipy.fftpack import fftn, ifftn
    >>> import numpy as np
    >>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
    >>> np.allclose(y, ifftn(fftn(y)))
    True

    iÿÿÿÿ(   Ra   (   R(   R7   R[   RI   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR   ‚  s    iþÿÿÿc         C` s   t  |  | | | ƒ S(   sÂ   
    2-D discrete Fourier transform.

    Return the two-dimensional discrete Fourier transform of the 2-D argument
    `x`.

    See Also
    --------
    fftn : for detailed information.

    (   R   (   R(   R7   R[   RI   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR	   ¡  s    c         C` s   t  |  | | | ƒ S(   sÿ   
    2-D discrete inverse Fourier transform of real or complex sequence.

    Return inverse two-dimensional discrete Fourier transform of
    arbitrary type sequence x.

    See `ifft` for more information.

    See also
    --------
    fft2, ifft

    (   R   (   R(   R7   R[   RI   (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyR
   °  s    (   iþÿÿÿiÿÿÿÿ(   iþÿÿÿiÿÿÿÿ(7   t   __doc__t
   __future__R    R   R   t   __all__R   R   R   t    R   t   scipy.fftpack.helperR   t   atexitt   registert   destroy_zfft_cachet   destroy_zfftnd_cachet   destroy_drfft_cachet   destroy_cfft_cachet   destroy_cfftnd_cachet   destroy_rfft_cacheR   R   R#   R+   R.   R1   R8   R   R0   R%   t   float64R'   R-   RR   RP   R/   RV   R6   Rb   R@   RH   RO   R   R   R   R   R   R   R`   R   Ra   R   R	   R
   (    (    (    s2   lib/python2.7/site-packages/scipy/fftpack/basic.pyt   <module>   s`   										iSIP	'=	