ó
Ë½÷Xc           @   sc   d  d l  Z d d l m Z m Z d d g Z d „  Z d e f d „  ƒ  YZ d d d  d	 „ Z	 d S(
   iÿÿÿÿNi   (   t   _output_lent   _applyt   upfirdnR    c         C   sx   t  |  ƒ t  |  ƒ | } t j | |  j ƒ } |  | t  |  ƒ *| j d | ƒ j d d … d d d … f j ƒ  } | S(   s´  Store coefficients in a transposed, flipped arrangement.

    For example, suppose upRate is 3, and the
    input number of coefficients is 10, represented as h[0], ..., h[9].

    Then the internal buffer will look like this::

       h[9], h[6], h[3], h[0],   // flipped phase 0 coefs
       0,    h[7], h[4], h[1],   // flipped phase 1 coefs (zero-padded)
       0,    h[8], h[5], h[2],   // flipped phase 2 coefs (zero-padded)

    iÿÿÿÿN(   t   lent   npt   zerost   dtypet   reshapet   Tt   ravel(   t   ht   upt   h_padlent   h_full(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_upfirdn.pyt   _pad_h)   s
    4t   _UpFIRDnc           B   s   e  Z d  „  Z d d „ Z RS(   c         C   sè   t  j | ƒ } | j d k s- | j d k r< t d ƒ ‚ n  t  j | j | t  j ƒ |  _ t  j | |  j ƒ } t	 | ƒ |  _
 t	 | ƒ |  _ |  j
 d k  s« |  j d k  rº t d ƒ ‚ n  t | |  j
 ƒ |  _ t  j |  j ƒ |  _ d S(   s   Helper for resamplingi   i    s!   h must be 1D with non-zero lengths   Both up and down must be >= 1N(   R   t   asarrayt   ndimt   sizet
   ValueErrort   result_typeR   t   float32t   _output_typet   intt   _upt   _downR   t   _h_trans_flipt   ascontiguousarray(   t   selfR
   t   x_dtypeR   t   down(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_upfirdn.pyt   __init__>   s    iÿÿÿÿc         C   s§   t  t |  j ƒ | j | |  j |  j ƒ } t j | j ƒ } | | | <t j | d |  j	 d d ƒ} | | j
 } t t j | |  j	 ƒ |  j | |  j |  j | ƒ | S(   s@   Apply the prepared filter to the specified axis of a nD signal xR   t   ordert   C(   R    R   R   t   shapeR   R   R   R   R   R   R   R   (   R   t   xt   axist
   output_lent   output_shapet   out(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_upfirdn.pyt   apply_filterM   s    
	(   t   __name__t
   __module__R   R(   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_upfirdn.pyR   =   s   	c         C   s7   t  j | ƒ } t |  | j | | ƒ } | j | | ƒ S(   s©
  Upsample, FIR filter, and downsample

    Parameters
    ----------
    h : array_like
        1-dimensional FIR (finite-impulse response) filter coefficients.
    x : array_like
        Input signal array.
    up : int, optional
        Upsampling rate. Default is 1.
    down : int, optional
        Downsampling rate. Default is 1.
    axis : int, optional
        The axis of the input data array along which to apply the
        linear filter. The filter is applied to each subarray along
        this axis. Default is -1.

    Returns
    -------
    y : ndarray
        The output signal array. Dimensions will be the same as `x` except
        for along `axis`, which will change size according to the `h`,
        `up`,  and `down` parameters.

    Notes
    -----
    The algorithm is an implementation of the block diagram shown on page 129
    of the Vaidyanathan text [1]_ (Figure 4.3-8d).

    .. [1] P. P. Vaidyanathan, Multirate Systems and Filter Banks,
       Prentice Hall, 1993.

    The direct approach of upsampling by factor of P with zero insertion,
    FIR filtering of length ``N``, and downsampling by factor of Q is
    O(N*Q) per output sample. The polyphase implementation used here is
    O(N/P).

    .. versionadded:: 0.18

    Examples
    --------
    Simple operations:

    >>> from scipy.signal import upfirdn
    >>> upfirdn([1, 1, 1], [1, 1, 1])   # FIR filter
    array([ 1.,  2.,  3.,  2.,  1.])
    >>> upfirdn([1], [1, 2, 3], 3)  # upsampling with zeros insertion
    array([ 1.,  0.,  0.,  2.,  0.,  0.,  3.,  0.,  0.])
    >>> upfirdn([1, 1, 1], [1, 2, 3], 3)  # upsampling with sample-and-hold
    array([ 1.,  1.,  1.,  2.,  2.,  2.,  3.,  3.,  3.])
    >>> upfirdn([.5, 1, .5], [1, 1, 1], 2)  # linear interpolation
    array([ 0.5,  1. ,  1. ,  1. ,  1. ,  1. ,  0.5,  0. ])
    >>> upfirdn([1], np.arange(10), 1, 3)  # decimation by 3
    array([ 0.,  3.,  6.,  9.])
    >>> upfirdn([.5, 1, .5], np.arange(10), 2, 3)  # linear interp, rate 2/3
    array([ 0. ,  1. ,  2.5,  4. ,  5.5,  7. ,  8.5,  0. ])

    Apply a single filter to multiple signals:

    >>> x = np.reshape(np.arange(8), (4, 2))
    >>> x
    array([[0, 1],
           [2, 3],
           [4, 5],
           [6, 7]])

    Apply along the last dimension of ``x``:

    >>> h = [1, 1]
    >>> upfirdn(h, x, 2)
    array([[ 0.,  0.,  1.,  1.],
           [ 2.,  2.,  3.,  3.],
           [ 4.,  4.,  5.,  5.],
           [ 6.,  6.,  7.,  7.]])

    Apply along the 0th dimension of ``x``:

    >>> upfirdn(h, x, 2, axis=0)
    array([[ 0.,  1.],
           [ 0.,  1.],
           [ 2.,  3.],
           [ 2.,  3.],
           [ 4.,  5.],
           [ 4.,  5.],
           [ 6.,  7.],
           [ 6.,  7.]])

    (   R   R   R   R   R(   (   R
   R#   R   R   R$   t   ufd(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_upfirdn.pyR   [   s    Y(
   t   numpyR   t   _upfirdn_applyR    R   t   __all__R   t   objectR   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_upfirdn.pyt   <module>"   s
   	