a
    BCCf                     @   sX   d dl ZddlmZmZmZ ddgZg dZdd Zd	d
 Z	G dd dZ
dddZdS )    N   )_output_len_apply	mode_enumupfirdnr   )	constantwrapedgeZsmoothZ	symmetricZreflectZantisymmetricZantireflectlinec                 C   s\   t | t |  |  }t|| j}| |dt | < |d|jdddddf  }|S )a  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)

    N)lennpzerosdtypeZreshapeTZravel)hupZh_padlenZh_full r   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/signal/_upfirdn.py_pad_h.   s
    $r   c                 C   s   |   } t| }|S )N)lowerr   )modeenumr   r   r   _check_modeB   s    r   c                   @   s"   e Zd ZdZdd Zd
ddZd	S )_UpFIRDnzHelper for resampling.c                 C   s   t |}|jdks|jdkr&tdt |j|t j| _t || j}t	|| _
t	|| _| j
dk sp| jdk rxtdt|| j
| _t | j| _t|| _d S )Nr   r   z"h must be 1-D with non-zero lengthzBoth up and down must be >= 1)r   asarrayndimsize
ValueErrorZresult_typer   Zfloat32_output_typeint_up_downr   _h_trans_flipZascontiguousarrayr   _h_len_orig)selfr   Zx_dtyper   downr   r   r   __init__K   s    


z_UpFIRDn.__init__r   r   r   c              	   C   s   t | j|j| | j| j}tj|jtjd}|||< tj|| j	dd}||j
 }t|}tt|| j	| j|| j| j||| |S )z@Apply the prepared filter to the specified axis of N-D signal x.)r   C)r   order)r   r$   shaper!   r"   r   r   Zint64r   r   r   r   r   r#   )r%   xaxisr   cvalZ
output_lenZoutput_shapeoutr   r   r   apply_filterZ   s    
z_UpFIRDn.apply_filterN)r   r   r   )__name__
__module____qualname____doc__r'   r/   r   r   r   r   r   H   s   r   r   r   c                 C   s*   t |}t| |j||}|||||S )a  Upsample, FIR filter, and downsample.

    Parameters
    ----------
    h : array_like
        1-D 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.
    mode : str, optional
        The signal extension mode to use. The set
        ``{"constant", "symmetric", "reflect", "edge", "wrap"}`` correspond to
        modes provided by `numpy.pad`. ``"smooth"`` implements a smooth
        extension by extending based on the slope of the last 2 points at each
        end of the array. ``"antireflect"`` and ``"antisymmetric"`` are
        anti-symmetric versions of ``"reflect"`` and ``"symmetric"``. The mode
        `"line"` extends the signal based on a linear trend defined by the
        first and last points along the ``axis``.

        .. versionadded:: 1.4.0
    cval : float, optional
        The constant value to use when ``mode == "constant"``.

        .. versionadded:: 1.4.0

    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).

    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

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

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

    >>> import numpy as np
    >>> 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.])
    >>> 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])
    >>> 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])

    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,   r   r-   Zufdr   r   r   r   k   s    j
)r   r   r   r   r   )numpyr   Z_upfirdn_applyr   r   r   __all__Z_upfirdn_modesr   r   r   r   r   r   r   r   <module>"   s   #