
    `i                        d dl Z d dlZd dlmZ d dlmZ d dlmc mZ	 d dl
mZ d dl
mZ d dlmZ d dlmZmZmZmZmZmZ d dlmZmZmZmZ d0dZd0dZd1dZd2dZd3dZ d2dZ!d4dZ"d4dZ#d5dZ$d3dZ%d6dZ&d Z'd7dZ(d8dZ)d9dZ*d7dZ+d Z,d:d!Z-d9d"Z.d# Z/	 	 d;d&Z0d' Z1d( Z2d) Z3d* Z4d9d+Z5d, Z6d<d-Z7d=d.Z8d7d/Z9dS )>    N)internal)lstsq)_util)_filters)_signaltools_core)	const_exteven_extodd_extaxis_reverse
axis_sliceaxis_assign)	apply_iirapply_iir_soscompute_correction_factorscompute_correction_factors_sosfullautoc                 (    t          | |||d          S )a  Convolve two N-dimensional arrays.

    Convolve ``in1`` and ``in2``, with the output size determined by the
    ``mode`` argument.

    Args:
        in1 (cupy.ndarray): First input.
        in2 (cupy.ndarray): Second input. Should have the same number of
            dimensions as `in1`.
        mode (str): Indicates the size of the output:

            - ``'full'``: output is the full discrete linear convolution                 (default)
            - ``'valid'``: output consists only of those elements that do                 not rely on the zero-padding. Either ``in1`` or ``in2`` must                 be at least as large as the other in every dimension.
            - ``'same'``: - output is the same size as ``in1``, centered with                 respect to the ``'full'`` output

        method (str): Indicates which method to use for the computations:

            - ``'direct'``: The convolution is determined directly from sums,                 the definition of convolution
            - ``'fft'``: The Fourier Transform is used to perform the                 convolution by calling ``fftconvolve``.
            - ``'auto'``: Automatically choose direct of FFT based on an                 estimate of which is faster for the arguments (default).

    Returns:
        cupy.ndarray: the result of convolution.

    .. seealso:: :func:`cupyx.scipy.signal.choose_conv_method`
    .. seealso:: :func:`cupyx.scipy.signal.correlation`
    .. seealso:: :func:`cupyx.scipy.signal.fftconvolve`
    .. seealso:: :func:`cupyx.scipy.signal.oaconvolve`
    .. seealso:: :func:`cupyx.scipy.ndimage.convolve`
    .. seealso:: :func:`scipy.signal.convolve`
    .. note::
        By default, ``convolve`` and ``correlate`` use ``method='auto'``, which
        calls ``choose_conv_method`` to choose the fastest method using
        pre-computed values. CuPy may not choose the same method to compute
        the convolution as SciPy does given the same inputs.
    T
_correlatein1in2modemethods       s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/signal/_signaltools.pyconvolver      s    X c3fd333    c                 (    t          | |||d          S )a  Cross-correlate two N-dimensional arrays.

    Cross-correlate ``in1`` and ``in2``, with the output size determined by the
    ``mode`` argument.

    Args:
        in1 (cupy.ndarray): First input.
        in2 (cupy.ndarray): Second input. Should have the same number of
            dimensions as ``in1``.
        mode (str): Indicates the size of the output:

            - ``'full'``: output is the full discrete linear convolution                 (default)
            - ``'valid'``: output consists only of those elements that do                 not rely on the zero-padding. Either ``in1`` or ``in2`` must                 be at least as large as the other in every dimension.
            - ``'same'``: - output is the same size as ``in1``, centered with                 respect to the ``'full'`` output

        method (str): Indicates which method to use for the computations:

            - ``'direct'``: The convolution is determined directly from sums,                 the definition of convolution
            - ``'fft'``: The Fourier Transform is used to perform the                 convolution by calling ``fftconvolve``.
            - ``'auto'``: Automatically choose direct of FFT based on an                 estimate of which is faster for the arguments (default).

    Returns:
        cupy.ndarray: the result of correlation.

    .. seealso:: :func:`cupyx.scipy.signal.choose_conv_method`
    .. seealso:: :func:`cupyx.scipy.signal.convolve`
    .. seealso:: :func:`cupyx.scipy.signal.fftconvolve`
    .. seealso:: :func:`cupyx.scipy.signal.oaconvolve`
    .. seealso:: :func:`cupyx.scipy.ndimage.correlation`
    .. seealso:: :func:`scipy.signal.correlation`
    .. note::
        By default, ``convolve`` and ``correlate`` use ``method='auto'``, which
        calls ``choose_conv_method`` to choose the fastest method using
        pre-computed values. CuPy may not choose the same method to compute
        the convolution as SciPy does given the same inputs.
    Fr   r   s       r   	correlater    A   s    X c3fe444r   Fc                 (   t          j        | |||          }||S |dvrt          d          |dk    rt          | ||          }|dk    rt          j        | ||| j        |          S |s&t          j        |                                          }t          j        || j	        |j	                  }|r|| }} t          | ||          }t          j        | |          }|j        dv r|                                }|                    |d          }|S )	N)r   directfftz1acceptable methods are "auto", "direct", or "fft"r   r   r"   uiFcopy)_st_core_check_conv_inputs
ValueErrorchoose_conv_method_direct_correlatedtype_reverseconj_inputs_swap_neededshapefftconvolvecupyresult_typekindroundastype)	r   r   r   r   convolution	quick_outinputs_swappedoutr4   s	            r   r   r   p   s2   +CdKHHI...LMMM#C4888)#sD#)*57 7 	7  ,$$))++1$	39MMN S
c3
%
%C"3,,K4iikk
**[u*
-
-CJr   c                 L   t          j        | ||          }||S t          j        | ||d          \  } }fdt          t	          | j        |j                            D             }t          j        | ||d          }t          j        || j        |j        |          S )a  Convolve two N-dimensional arrays using FFT.

    Convolve ``in1`` and ``in2`` using the fast Fourier transform method, with
    the output size determined by the ``mode`` argument.

    This is generally much faster than the ``'direct'`` method of ``convolve``
    for large arrays, but can be slower when only a few output values are
    needed, and can only output float arrays (int or object array inputs will
    be cast to float).

    Args:
        in1 (cupy.ndarray): First input.
        in2 (cupy.ndarray): Second input. Should have the same number of
            dimensions as ``in1``.
        mode (str): Indicates the size of the output:

            - ``'full'``: output is the full discrete linear                           cross-correlation (default)
            - ``'valid'``: output consists only of those elements that do                            not rely on the zero-padding. Either ``in1`` or                            ``in2`` must be at least as large as the other in                            every dimension.
            - ``'same'``: output is the same size as ``in1``, centered                           with respect to the 'full' output

        axes (scalar or tuple of scalar or None): Axes over which to compute
            the convolution. The default is over all axes.

    Returns:
        cupy.ndarray: the result of convolution

    .. seealso:: :func:`cupyx.scipy.signal.choose_conv_method`
    .. seealso:: :func:`cupyx.scipy.signal.correlation`
    .. seealso:: :func:`cupyx.scipy.signal.convolve`
    .. seealso:: :func:`cupyx.scipy.signal.oaconvolve`
    .. seealso:: :func:`cupyx.scipy.ndimage.convolve`
    .. seealso:: :func:`scipy.signal.correlation`
    NFc                 T    g | ]$\  }\  }}|vrt          ||          n||z   d z
  %S    )max).0ax1x2axess       r   
<listcomp>zfftconvolve.<locals>.<listcomp>   sS     F F FHR TMMSR[[[rBw{ F F Fr   Tcalc_fast_len)r(   r)   _init_freq_conv_axes	enumeratezipr1   _freq_domain_conv_apply_conv_mode)r   r   r   rE   r;   r1   s      `  r   r2   r2      s    N 
%c3
5
5C

23T4OONCdF F F F )#ci*C*C D DF F FE

$S#tU$
O
O
OC$S#)SYdKKKr   c                 N    t           j        j                            | ||          S )aZ  Find the fastest convolution/correlation method.

    Args:
        in1 (cupy.ndarray): first input.
        in2 (cupy.ndarray): second input.
        mode (str, optional): ``'valid'``, ``'same'``, ``'full'``.

    Returns:
        str: A string indicating which convolution method is fastest,
        either ``'direct'`` or ``'fft'``.

    .. warning::
        This function currently doesn't support measure option,
        nor multidimensional inputs. It does not guarantee
        the compatibility of the return value to SciPy's one.

    .. seealso:: :func:`scipy.signal.choose_conv_method`

    )r3   _mathmisc_choose_conv_method)r   r   r   s      r   r+   r+      s     ( :?..sC>>>r   c           
         t          j        | ||          }||S | j        |j        k    rt          | ||          S t          j        | ||d          \  } }| j        |j        cst          j        | |z  |          S fdt          | j                  D             }t          | \  }}}|k    r|k    rt          | ||          S fdt          | j                  D             }	t          j	        | ||	|||          \  } }d t                    D             d D             fd	D             }
t          j        | ||
d
          t                    D ]\  }}}||         }|t          j        | g|          \  }t          j        |dg|          d         }t          j        |g|          d         }t          j        |dg|          d         }||z  }fdt          j                  D             } j        | t          d |	D                                t          j        |          S )a  Convolve two N-dimensional arrays using the overlap-add method.

    Convolve ``in1`` and ``in2`` using the overlap-add method, with the output
    size determined by the ``mode`` argument. This is generally faster than
    ``convolve`` for large arrays, and generally faster than ``fftconvolve``
    when one array is much larger than the other, but can be slower when only a
    few output values are needed or when the arrays are very similar in shape,
    and can only output float arrays (int or object array inputs will be cast
    to float).

    Args:
        in1 (cupy.ndarray): First input.
        in2 (cupy.ndarray): Second input. Should have the same number of
            dimensions as ``in1``.
        mode (str): Indicates the size of the output:

            - ``'full'``: output is the full discrete linear                           cross-correlation (default)
            - ``'valid'``: output consists only of those elements that do                            not rely on the zero-padding. Either ``in1`` or                            ``in2`` must be at least as large as the other in                            every dimension.
            - ``'same'``: output is the same size as ``in1``, centered                           with respect to the ``'full'`` output

        axes (scalar or tuple of scalar or None): Axes over which to compute
            the convolution. The default is over all axes.

    Returns:
        cupy.ndarray: the result of convolution

    .. seealso:: :func:`cupyx.scipy.signal.convolve`
    .. seealso:: :func:`cupyx.scipy.signal.fftconvolve`
    .. seealso:: :func:`cupyx.scipy.ndimage.convolve`
    .. seealso:: :func:`scipy.signal.oaconvolve`
    N)r   rE   T)sorted_axesc              3      K   | ];}|v r!t          j        |         |                   nd d |         |         fV  <dS N)r(   _calc_oa_lensrA   irE   s1s2s     r   	<genexpr>zoaconvolve.<locals>.<genexpr>  ss       F F01 >?$YYX+BqE2a5999"beRU+F F F F F Fr   c                 D    g | ]}|v r|         |         z   d z
  ndS )r?   N rX   s     r   rF   zoaconvolve.<locals>.<listcomp>  sE     - - - %&II2a5A;q==4 - - -r   c                     g | ]
\  }}||z   S r^   r^   )rA   rY   iaxs      r   rF   zoaconvolve.<locals>.<listcomp>  s     666FAs#a%666r   c                     g | ]}|d z   S r>   r^   )rA   r`   s     r   rF   zoaconvolve.<locals>.<listcomp>  s    ,,,#A,,,r   c                      g | ]
}|         S r^   r^   )rA   rY   
block_sizes     r   rF   zoaconvolve.<locals>.<listcomp>  s    ---1A---r   FrG   rV   r   r?   c                 v    g | ]5}|v|vrj         |         nj         |         j         |d z
           z  6S r>   )r1   )rA   rY   fft_axesret
split_axess     r   rF   zoaconvolve.<locals>.<listcomp>.  sZ     B B BQj-@-@ "#(!2!211ci!n,-@-@-@r   c                 ,    g | ]}t          |          S r^   )slice)rA   islices     r   rF   zoaconvolve.<locals>.<listcomp>4  s    ===vU6]]===r   )r(   r)   r1   r2   rI   rM   rangendimrK   _oa_reshape_inputsrJ   rL   r3   splitreshapetuple)r   r   r   rE   r;   optimal_sizesoverlapsin1_stepin2_stepshape_final	fft_shapeaxax_fftax_splitoverlapoverpartret_overpart	shape_retrc   re   rf   rZ   r[   rg   s      `              @@@@@@r   
oaconvolver~      s6   J 
%c3
5
5C


yCI3$T::::23T4?CE E ENCdY	FB F(S"b$EEE
F F F F F F5:38__F F FM/2M/B,J(H 2~~(b..3$T::::- - - - - -!#(OO- - -K*3T;+5x+3X? ?HC
 76ioo666J,,,,,H .------I

$S#x38: : :C !$D(J ? ? 
! 
!FH2,?
3'
F;;X:hh77:z#y&99!<z,X>>qA B B B B B B//B B BI #+y
!C e=====>>
?C$S"b$===r   fillc                 *    t          | ||||d          S )ae  Convolve two 2-dimensional arrays.

    Convolve ``in1`` and ``in2`` with output size determined by ``mode``, and
    boundary conditions determined by ``boundary`` and ``fillvalue``.

    Args:
        in1 (cupy.ndarray): First input.
        in2 (cupy.ndarray): Second input. Should have the same number of
            dimensions as ``in1``.
        mode (str): Indicates the size of the output:

            - ``'full'``: output is the full discrete linear convolution                 (default)
            - ``'valid'``: output consists only of those elements that do                 not rely on the zero-padding. Either ``in1`` or ``in2`` must                 be at least as large as the other in every dimension.
            - ``'same'``: - output is the same size as ``in1``, centered with                 respect to the ``'full'`` output

        boundary (str): Indicates how to handle boundaries:

            - ``fill``: pad input arrays with fillvalue (default)
            - ``wrap``: circular boundary conditions
            - ``symm``: symmetrical boundary conditions

        fillvalue (scalar): Value to fill pad input arrays with. Default is 0.

    Returns:
        cupy.ndarray: A 2-dimensional array containing a subset of the discrete
        linear convolution of ``in1`` with ``in2``.

    .. seealso:: :func:`cupyx.scipy.signal.convolve`
    .. seealso:: :func:`cupyx.scipy.signal.fftconvolve`
    .. seealso:: :func:`cupyx.scipy.signal.oaconvolve`
    .. seealso:: :func:`cupyx.scipy.signal.correlate2d`
    .. seealso:: :func:`cupyx.scipy.ndimage.convolve`
    .. seealso:: :func:`scipy.signal.convolve2d`
    T_correlate2dr   r   r   boundary	fillvalues        r   
convolve2dr   9  s    N S$)TBBBr   c                 *    t          | ||||d          S )a  Cross-correlate two 2-dimensional arrays.

    Cross correlate ``in1`` and ``in2`` with output size determined by
    ``mode``, and boundary conditions determined by ``boundary`` and
    ``fillvalue``.

    Args:
        in1 (cupy.ndarray): First input.
        in2 (cupy.ndarray): Second input. Should have the same number of
            dimensions as ``in1``.
        mode (str): Indicates the size of the output:

            - ``'full'``: output is the full discrete linear convolution                 (default)
            - ``'valid'``: output consists only of those elements that do                 not rely on the zero-padding. Either ``in1`` or ``in2`` must                 be at least as large as the other in every dimension.
            - ``'same'``: - output is the same size as ``in1``, centered with                 respect to the ``'full'`` output

        boundary (str): Indicates how to handle boundaries:

            - ``fill``: pad input arrays with fillvalue (default)
            - ``wrap``: circular boundary conditions
            - ``symm``: symmetrical boundary conditions

        fillvalue (scalar): Value to fill pad input arrays with. Default is 0.

    Returns:
        cupy.ndarray: A 2-dimensional array containing a subset of the discrete
        linear cross-correlation of ``in1`` with ``in2``.

    Note:
        When using ``"same"`` mode with even-length inputs, the outputs of
        ``correlate`` and ``correlate2d`` differ: There is a 1-index offset
        between them.

    .. seealso:: :func:`cupyx.scipy.signal.correlate`
    .. seealso:: :func:`cupyx.scipy.signal.convolve2d`
    .. seealso:: :func:`cupyx.scipy.ndimage.correlate`
    .. seealso:: :func:`scipy.signal.correlate2d`
    Fr   r   s        r   correlate2dr   c  s    V S$)UCCCr   c           
      V   | j         |j         cxk    rdk    s(n t          d                    |rdnd                    ddddddd}|                    |          }|t          d	          t	          j        | |||          }||S t	          j        | ||| j        ||||           S )
N   z!{} inputs must both be 2-D arraysr   r   constantwrapreflect)r   padr   circularsymm	symmetriczeAcceptable boundary flags are "fill" (or "pad"), "circular" (or "wrap"), and "symmetric" (or "symm").)rl   r*   formatgetr(   r)   r,   r-   )r   r   r   r   r   r8   _boundariesr9   s           r   r   r     s    H%%%%A%%%%<CC':LL]< < = = 	= :F	 K
 x((H 4 5 5 	5 +CdKHHI%c3ci&.	{?L L Lr   c                 z   |dk    rt          j        | dz   |           }n|dk    rVt          j        | dz   |           }|j        dz  }| dz  }| dz  dk    r|||z
  ||z            }nR|||z
  ||z   dz            }n>|dk    r8| |z
  }|dk    rt          j        |dz             }nt          j        |d          }|S )a  
    Calculates the lag / displacement indices array for 1D cross-correlation.

    Parameters
    ----------
    in1_len : int
        First input size.
    in2_len : int
        Second input size.
    mode : str {'full', 'valid', 'same'}, optional
        A string indicating the size of the output.
        See the documentation `correlate` for more information.

    Returns
    -------
    lags : array
        Returns an array containing cross-correlation lag/displacement indices.
        Indices can be indexed with the np.argmax of the correlation to return
        the lag/displacement.

    See Also
    --------
    correlate : Compute the N-dimensional cross-correlation.
    scipy.signal.correlation_lags
    r   r?   samer   r   valid)r3   arangesize)in1_lenin2_lenr   lagsmid	lag_bounds         r   correlation_lagsr     s    6 v~~ {G8a<11	 {G8a<11i1n qL	Q;!y3?;<DDy3?a*??@DD	 g%	>>;y1}--DD;y!,,DKr   c                    |d}t          j        || j        dt                    }|                     | j        j        dk    rt          j        nt          j	        d          } t          j        | |d          }t          j        | | z  |d          }|||z  z  }||                                }| |z
  }|d	||z  z
  z  }||z  }t          j        ||k     ||          S )
a  Perform a Wiener filter on an N-dimensional array.

    Apply a Wiener filter to the N-dimensional array `im`.

    Args:
        im (cupy.ndarray): An N-dimensional array.
        mysize (int or cupy.ndarray, optional): A scalar or an N-length list
            giving the size of the Wiener filter window in each dimension.
            Elements of mysize should be odd. If mysize is a scalar, then this
            scalar is used as the size in each dimension.
        noise (float, optional): The noise-power to use. If None, then noise is
            estimated as the average of the local variance of the input.

    Returns:
        cupy.ndarray: Wiener filtered result with the same shape as `im`.

    .. seealso:: :func:`scipy.signal.wiener`
    N   mysizecFr&   r   r$   r?   )r   _fix_sequence_argrl   intr7   r-   r5   r3   
complex128float64r   uniform_filtermeanwhere)imr   noise
local_mean	local_varress         r   wienerr     s    & ~$VRWhDDF	bhms&:&:4?? 
 
 
B (V*EEEJ '2vJGGGIJ&&I }   z/C1uy   C:C:i%'S999r   c                     | j         j        dv s| j         t          j        k    rt	          d          t          d |j        D                       rt	          d          t          j        | ||d          S )a  Perform an order filter on an N-D array.

    Perform an order filter on the array in. The domain argument acts as a mask
    centered over each pixel. The non-zero elements of domain are used to
    select elements surrounding each input pixel which are placed in a list.
    The list is sorted, and the output for that pixel is the element
    corresponding to rank in the sorted list.

    Args:
        a (cupy.ndarray): The N-dimensional input array.
        domain (cupy.ndarray): A mask array with the same number of dimensions
            as `a`. Each dimension should have an odd number of elements.
        rank (int): A non-negative integer which selects the element from the
            sorted list (0 corresponds to the smallest element).

    Returns:
        cupy.ndarray: The results of the order filter in an array with the same
        shape as `a`.

    .. seealso:: :func:`cupyx.scipy.ndimage.rank_filter`
    .. seealso:: :func:`scipy.signal.order_filter`
    bczdata type not supportedc              3   (   K   | ]}|d z  dk    V  dS r   r?   Nr^   )rA   xs     r   r\   zorder_filter.<locals>.<genexpr>(  s*      
,
,!1q5A:
,
,
,
,
,
,r   zIEach dimension of domain argument  should have an odd number of elements.r   )	footprintr   )	r-   r5   r3   float16r*   anyr1   r   rank_filter)rB   domainranks      r   order_filterr     s    . 	w|tqw$,662333

,
,v|
,
,
,,, D C D D 	D46
KKKKr   c                    | j         j        dk    rt          d          | j         j        dk    rt          d          t	          || j                  }| j         dk    rt          d          | j         j        dk    rt          d          t          d t          || j	                  D                       rt          j        d	           t          j        |          }t          j        | |d
z  |d          S )a  Perform a median filter on an N-dimensional array.

    Apply a median filter to the input array using a local window-size
    given by `kernel_size`. The array will automatically be zero-padded.

    Args:
        volume (cupy.ndarray): An N-dimensional input array.
        kernel_size (int or list of ints): Gives the size of the median filter
            window in each dimension. Elements of `kernel_size` should be odd.
            If `kernel_size` is a scalar, then this scalar is used as the size
            in each dimension. Default size is 3 for each dimension.

    Returns:
        cupy.ndarray: An array the same size as input containing the median
        filtered result.

    .. seealso:: :func:`cupyx.scipy.ndimage.median_filter`
    .. seealso:: :func:`scipy.signal.medfilt`
    efloat16 type not supportedbbool type not supportedFcomplex types not supportedr   c              3   (   K   | ]\  }}||k    V  d S Nr^   )rA   kss      r   r\   zmedfilt.<locals>.<genexpr>N  s*      
<
<TQ1q5
<
<
<
<
<
<r   z=kernel_size exceeds volume extent: volume will be zero-paddedr   r   r   r   )r-   charr*   r5   _get_kernel_sizerl   	TypeErrorr   rK   r1   warningswarnr   prodr   r   )volumekernel_sizer   s      r   medfiltr   .  s
   ( |C5666|C2333";<<K|s5666|C6777

<
<Sfl;;
<
<
<<< 4 3 	4 	4 	4 =%%D	%/1 1 1 1r   r   c                    | j         j        dk    rt          d          | j         j        dk    rt          d          | j        dk    rt          d          t          || j                  }| j         dk    rt          d          | j         j        d	k    rt          d          |d
         |d         z  dz  }t          j        | ||d          S )a  Median filter a 2-dimensional array.

    Apply a median filter to the `input` array using a local window-size given
    by `kernel_size` (must be odd). The array is zero-padded automatically.

    Args:
        input (cupy.ndarray): A 2-dimensional input array.
        kernel_size (int of list of ints of length 2): Gives the size of the
            median filter window in each dimension. Elements of `kernel_size`
            should be odd. If `kernel_size` is a scalar, then this scalar is
            used as the size in each dimension. Default is a kernel of size
            (3, 3).

    Returns:
        cupy.ndarray: An array the same size as input containing the median
        filtered result.

    See also
    --------
    .. seealso:: :func:`cupyx.scipy.ndimage.median_filter`
    .. seealso:: :func:`cupyx.scipy.signal.medfilt`
    .. seealso:: :func:`scipy.signal.medfilt2d`
    r   r   r   r   r   zinput must be 2dr   r   r   r   r?   r   r   )	r-   r   r*   r5   rl   r   r   r   r   )inputr   orders      r   	medfilt2dr   W  s    0 {35666{32333zQ+,,,";
;;K{c5666{36777N[^+q0Eu;Z9 9 9 9r   rV   c                     |d         }|dd          |z  }| |z  } | j         dz
  }|j         }|j        }	t          j        ||	          }|j        |         }
t          j        ||           }d}d}t          |j                  }||xx         |z  cc<   t          j        ||          }|_t          j	        |          }|dk    rt          |d||          }|dk    r,t          ||j        |         |z
  |j        |         |          }|t          ||d||          }t          ||||          }| dz  }t          j        ||          }t          j        || |d||          }|dk    r,t          ||j        |         |
z
  |j        |         |          }|j         dk    rt          j        ||          }t          j        |j                  }|j        d	k    r@t          j        |j                                                  }|                    |          }t)          |||||
          }|t          j        |j        |j                  }|dk    r@t          ||j        |         |z
  |j        |         |          }t          ||d||          }|dk    rYt          ||j        |         |z
  |j        |         |          }t          |||j        |         |z
  |j        |         |          }||fS |S )a;  
    Filter data along one-dimension with an IIR or FIR filter.

    Filter a data sequence, `x`, using a digital filter.  This works for many
    fundamental data types (including Object type).  The filter is a direct
    form II transposed implementation of the standard difference equation
    (see Notes).

    The function `sosfilt` (and filter design using ``output='sos'``) should be
    preferred over `lfilter` for most filtering tasks, as second-order sections
    have fewer numerical problems.

    Parameters
    ----------
    b : array_like
        The numerator coefficient vector in a 1-D sequence.
    a : array_like
        The denominator coefficient vector in a 1-D sequence.  If ``a[0]``
        is not 1, then both `a` and `b` are normalized by ``a[0]``.
    x : array_like
        An N-dimensional input array.
    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.
    zi : array_like, optional
        Initial conditions for the filter delays.  It is a vector
        (or array of vectors for an N-dimensional input) of length
        ``len(b) + len(a) - 2``. The first ``len(b)`` numbers correspond to the
        last elements of the previous input and the last ``len(a)`` to the last
        elements of the previous output. If `zi` is None or is not given then
        initial rest is assumed.  See `lfiltic` for more information.

        **Note**: This argument differs from dimensions from the SciPy
        implementation! However, as long as they are chained from the same
        library, the output result will be the same. Please make sure to use
        the `zi` from CuPy calls and not from SciPy. This due to the parallel
        nature of this implementation as opposed to the serial one in SciPy.

    Returns
    -------
    y : array
        The output of the digital filter.
    zf : array, optional
        If `zi` is None, this is not returned, otherwise, `zf` holds the
        final filter delay values.

    See Also
    --------
    lfiltic : Construct initial conditions for `lfilter`.
    lfilter_zi : Compute initial state (steady state of step response) for
                 `lfilter`.
    filtfilt : A forward-backward filter, to obtain a filter with zero phase.
    savgol_filter : A Savitzky-Golay filter.
    sosfilt: Filter data using cascaded second-order sections.
    sosfiltfilt: A forward-backward filter using second-order sections.

    Notes
    -----
    The filter function is implemented as a direct II transposed structure.
    This means that the filter implements::

          a[0]*y[n] = b[0]*x[n] + b[1]*x[n-1] + ... + b[M]*x[n-M]
                                - a[1]*y[n-1] - ... - a[N]*y[n-N]

    where `M` is the degree of the numerator, `N` is the degree of the
    denominator, `n` is the sample number and `L` denotes the length of the
    input.  It is implemented by computing first the FIR part and then
    computing the IIR part from it::

             a[0] * y = r(f(x, b), a)
             f(x, b)[n] = b[0]*x[n] + b[1]*x[n-1] + ... + b[M]*x[n-M]
             r(y, a)[n] = - a[1]*y[n-1] - ... - a[N]*y[n-N]

    The IIR result is computed in parallel by first dividing the input signal
    into chunks (`g_i`) of size `m`. For each chunk, the IIR recurrence
    equation is applied to each chunk (in parallel). Then the chunks are merged
    based on the last N values of the last chunk::

             nc = L/m
             x = [g_0, g_1, ..., g_nc]

             g_i = [x[i * m], ..., x[i * m + m - 1]]
             p_i = r(g_i, a)

             o_i = r(p_i, c(p_{i - 1}))   if i > 1,
                   r(p_i, zi)             otherwise

             y = [o_0, o_1, ..., o_nc]

    where `c` denotes a function that takes a chunk, slices the last `N` values
    and adjust them using a correction factor table computed using the
    (1, 2, ..., N)-fibonacci sequence. For more information see [1]_.

    The rational transfer function describing this filter in the
    z-transform domain is::

                             -1              -M
                 b[0] + b[1]z  + ... + b[M] z
         Y(z) = -------------------------------- X(z)
                             -1              -N
                 a[0] + a[1]z  + ... + a[N] z

    References
    ----------
    .. [1] Sepideh Maleki and Martin Burtscher.
           2018. Automatic Hierarchical Parallelization of Linear Recurrences.
           SIGPLAN Not. 53, 2 (February 2018), 128-138.
           `10.1145/3173162.3173168 <https://doi.org/10.1145/3173162.3173168>`_
    r   r?   Nr-   axisr   r   )r   r   originoutputu)r   zir-   )r   rl   r   _normalize_axis_indexr1   r3   r4   listzeros
atleast_1dr   r   
empty_liker   
convolve1dr-   r5   r   lowerr7   r   empty)r   rB   r   r   r   a0a_rnum_bnum_ax_ndimn	fir_dtypeprev_inprev_out	pad_shapex_fullr   r;   	iir_dtypeconst_dtypes                       r   lfilterr     s;   ^ 
1BabbE'B,C	BAFQJEHEVF)$77D	A A&&IGHQWIdOOOuOOOZ	333F	~_R  199 QD999G199!BHTNU*BHTNG G GH VWaTBBBE555FVq[F
/&	
2
2
2C

:fSJ J JC qyycio139T?NNN
x!||$Y22	j))s""*[%5%;%;%=%=>>K%%ASt	JJJ	~Z	222199 174=5(!'$-dD D DGR!U>>>B199!SYt_u,cioDJ J JHHbhtnu4bhtn  B Bw
r   c           
      0   d}| j         dz
  }|j         dz
  }||dS ||j        n|j        }t          j        ||          }t	          j        d          }||dk    rt	          j        |t	          j        t          ||j	        |         z
  d                    f|          }	t	          j
        |	t          t          |                    |          }t	          j        ||          }||dk    rt	          j        |t	          j        t          ||j	        |         z
  d                    f|          }
t	          j
        |
t          t          |                    |          }t	          j        ||          }t	          j        ||f|          }|S )a  
    Construct initial conditions for lfilter given input and output vectors.

    Given a linear filter (b, a) and initial conditions on the output `y`
    and the input `x`, return the initial conditions on the state vector zi
    which is used by `lfilter` to generate the output given the input.

    Parameters
    ----------
    b : array_like
        Linear filter term.
    a : array_like
        Linear filter term.
    y : array_like
        Initial conditions.
        If ``N = len(a) - 1``, then ``y = {y[-1], y[-2], ..., y[-N]}``.
        If `y` is too short, it is padded with zeros.
    x : array_like, optional
        Initial conditions.
        If ``M = len(b) - 1``, then ``x = {x[-1], x[-2], ..., x[-M]}``.
        If `x` is not given, its initial conditions are assumed zero.
        If `x` is too short, it is padded with zeros.
    axis: int, optional
        The axis to take the initial conditions from, if `x` and `y` are
        n-dimensional

    Returns
    -------
    zi : ndarray
        The state vector ``zi = {z_0[-1], z_1[-1], ..., z_K-1[-1]}``,
        where ``K = M + N``.

    See Also
    --------
    lfilter, lfilter_zi
    rV   r?   Nr   r   )r   rl   r   r   r3   r   concatenater   r@   r1   taker   rk   flip)r   rB   yr   r   fir_leniir_lenref_ndimr   pad_ypad_xfir_zis               r   lfilticr   0  s   P DfqjGfqjGyQYtqvvAFH)$99D	AB}1 
3w6::;;<4I I IYud5>>22>>>Yr4  }1 
3w6::;;<4I I I5$uW~~"6"6TBBB64((vrl666Ir   c                    |d         }|dd          |z  }| j         dz
  }|j         }t          j        |          }|dk    rt          j        |t          j        |          f         }t          | |t          j        |dz             |          \  }}	|d|         }
|| d         }t          j        |dk              d         }t          ||j         dz   |j                  }|dd|j         df         }|ddd|j         f         j	        }|dd|j          df         j	        }t          |          s't          j                            ||z
  ||
z
            }n-t          j                            ||z
  ||
z
  d          \  }}	}	}	t          j        |dt          j        t          j                   }t          j        ||ddd         f         }|S )a  
    Construct initial conditions for lfilter for step response steady-state.

    Compute an initial state `zi` for the `lfilter` function that corresponds
    to the steady state of the step response.

    A typical use of this function is to set the initial state so that the
    output of the filter starts at the same value as the first element of
    the signal to be filtered.

    Parameters
    ----------
    b, a : array_like (1-D)
        The IIR filter coefficients. See `lfilter` for more
        information.

    Returns
    -------
    zi : 1-D ndarray
        The initial state for the filter.

    See Also
    --------
    lfilter, lfiltic, filtfilt
    r   r?   Nr   rcondnanposinfneginfrV   )r   r3   onesr_r   r   r   r   r-   Tlenlinalgsolver   
nan_to_numinf)r   rB   r   r   r   r   r   zi_tr   _y1y2	zero_coefCC1C2y_zis                    r   
lfilter_zir  r  s   4 
1BabbE'B,CFQJEHE 
5		Bqyywr4:e,,,-q!TYuqy11d;;;1vvYvwwZJsax((+	&sCHqL#)DDaaalOqqq)38)|_qqq38)**} 9~~ 	L;$$R"Wb2g66DD !K--b2grBwd-KKMD!Qt48TXINNNWRddd^$Ir   linearc                    |dvrt          d          t          j        |           } | j        j        }|dvrd}|dv r| t          j        | |d          z
  }|S | j        }||         }t          j        t          j        t          j	        d||f                             }t          j
        ||k              rt          d	          |                                }t          |          }	|dk     r||	z   }t          j        | |d          }
|
j        }|
                    |d
          }
|s|
                                }
|
j        j        dvr|
                    |          }
t#          t          |          dz
            D ]}||dz            ||         z
  }t          j        |df|          }t          j        d|dz   |          |z  |dddf<   t)          ||         ||dz                      }t+          ||
|         d          \  }}}}|
|         ||z  z
  |
|<   |
                    |          }
t          j        |
d|          }|S )a<  
    Remove linear trend along axis from data.

    Parameters
    ----------
    data : array_like
        The input data.
    axis : int, optional
        The axis along which to detrend the data. By default this is the
        last axis (-1).
    type : {'linear', 'constant'}, optional
        The type of detrending. If ``type == 'linear'`` (default),
        the result of a linear least-squares fit to `data` is subtracted
        from `data`.
        If ``type == 'constant'``, only the mean of `data` is subtracted.
    bp : array_like of ints, optional
        A sequence of break points. If given, an individual linear fit is
        performed for each part of `data` between two break points.
        Break points are specified as indices into `data`. This parameter
        only has an effect when ``type == 'linear'``.
    overwrite_data : bool, optional
        If True, perform in place detrending and avoid a copy. Default is False

    Returns
    -------
    ret : ndarray
        The detrended input data.

    See Also
    --------
    scipy.signal.detrend


    )r  lr   r   z*Trend type must be 'linear' or 'constant'.dfDFd)r   r   T)keepdimsr   z>Breakpoints must be less than length of data along given axis.rV   r?   r   r   Nr  )r*   r3   asarrayr-   r   r   r1   sortuniquer	  r   tolistr  moveaxisro   r'   r7   rk   r  r   ri   r   )datar   typebpoverwrite_datar-   rf   dshapeNrnknewdatanewdata_shapemNptsAslcoefresidsr   r   s                       r   detrendr4    s^   F 333EFFF<DJOEF   TYtTD9999
4LYt{471b!8#455668BF 	: 9 : : :YY[[ &kk!88#:D-dA..//!R(( 	%llnnG=V++nnU++G s2ww{## 	1 	1Aa!e9r!u$D	4)U++Ak!TAXU;;;dBAaaadGr!ubQi((B$)!WR[$E$E$E!D&$!"+D0GBKK //-00mGQ--
r   c           	      D	   t          j        |           } t          j        |          }t          t          |           t          |                    dz
  }|dk    rB| d         |d         z  dz  }||z  }|t          j        g           t          j        g           fS |dk    s||j        dz
  k    rt          j        |||j        dz
            }|j        d         }|	|d|z  k    r|}	n|}	t          j        |	|f          }
t          j        |	          }d|d<   t          t          j
        d          ||          |
dddf<   t          d|          D ]}|
d| df         |
|d|f<   |
ddd         }t          | ||
ddd         d          }|ddd         }|	|k    rt          j        ||
z
  ||z
  f          }n8t          j        d|	z  d|z  f          }||
z
  |d|	d|f<   ||z
  ||	d|df<   t          | ||          }t          | ||ddddf                   ddddf         }t          | ||ddddf                   ddddf         }t          | ||          }||z
  }|	|k    r|}n1|dd|	f         }|d|	 df         }t          j        ||fd          }|j        dk    r)t           j                            ||d          d         }n|                    d|j        d                   j        }t           j                            ||d          d         j        }|                    |j        dd         |j        d         fz             }|	|k    rt          j        ||f          }n2t          j        d|	z  d|z  f          }||d|	d|f<   |||	d|df<   |                    |j                  }|}|	|k    r||z  }n>|dd|	fxx         |dd|	f         z  cc<   |d|	 dfxx         |d|	 df         z  cc<   |dd|f         }|d| df         } |dk    s||j        dz
  k    rZt          j        |||j        dz
            }t          j        | ||j        dz
            } t          j        |||j        dz
            }||| fS )	a6  Forward-backward IIR filter that uses Gustafsson's method.

    Apply the IIR filter defined by `(b,a)` to `x` twice, first forward
    then backward, using Gustafsson's initial conditions [1]_.

    Let ``y_fb`` be the result of filtering first forward and then backward,
    and let ``y_bf`` be the result of filtering first backward then forward.
    Gustafsson's method is to compute initial conditions for the forward
    pass and the backward pass such that ``y_fb == y_bf``.

    Parameters
    ----------
    b : scalar or 1-D ndarray
        Numerator coefficients of the filter.
    a : scalar or 1-D ndarray
        Denominator coefficients of the filter.
    x : ndarray
        Data to be filtered.
    axis : int, optional
        Axis of `x` to be filtered.  Default is -1.
    irlen : int or None, optional
        The length of the nonnegligible part of the impulse response.
        If `irlen` is None, or if the length of the signal is less than
        ``2 * irlen``, then no part of the impulse response is ignored.

    Returns
    -------
    y : ndarray
        The filtered data.
    x0 : ndarray
        Initial condition for the forward filter.
    x1 : ndarray
        Initial condition for the backward filter.

    Notes
    -----
    Typically the return values `x0` and `x1` are not needed by the
    caller.  The intended use of these return values is in unit tests.

    References
    ----------
    .. [1] F. Gustaffson. Determining the initial states in forward-backward
           filtering. Transactions on Signal Processing, 46(4):988-992, 1996.
    r?   r   r   rV   Nr   .r  )r3   r   r@   r  arrayrl   swapaxesr1   r   r   r  rk   hstackr   r  r   ro   r
  dot)!r   rB   r   r   irlenr   scaler   r   r.  Obsx_inr   ObsrSSrMy_fy_fby_by_bfdelta_y_bf_fbdeltastart_mend_mic_optdelta2dic_opt0Wwicy_optx0rC   s!                                    r   _filtfilt_gustrQ    s   ` 	AAAA!#Ezz1!"AI$*R..$*R..00rzzTQVaZ''M!T16A:.. 	
A}QY *aZ
 
 C:a==DDG	!a..C1I1e__ ! !1"a[ABBE

 ttt9D 	1c$$B$ia(((A 
44R4B 	AvvKc4!8,-- J!QuW~&&S"1"fuf*q!""eff*
 !Q

C1aS$$B$Y((ddd3D
!Q#ttt)
%
%c44R4i
0C1aD4KMAvvRaR(cA233h' '5!1;;;
 zQ""1e4"88; --EKO446+##Awd#;;A>@SbS!1QWR[N!BCC 	AvvKT
##J!QuW~&&"1"fuf*!""eff* **QS//C EAvvc2A2g#c2A2g,&cA233h3sQBCCx=(	VeV	B	eVWW	BrzzTQVaZ'']2tQVaZ00]2tQVaZ00eT16A:66"b=r   c                 6   | dvrt          d| z            | d}||dz  }n|}|j        |         |k    rt          d|z            | K|dk    rE| dk    rt          |||          }n.| d	k    rt          |||          }nt	          |||          }n|}||fS )
z'Helper to validate padding for filtfilt)evenoddr   NzYUnknown value '%s' given to padtype.  padtype must be 'even', 'odd', 'constant', or None.Nr   r   zJThe length of the input vector x must be greater than padlen, which is %d.rS  r   rT  )r*   r1   r	   r
   r   )padtypepadlenr   r   ntapsedgeexts          r   _validate_padrZ    s    777 H ! " " 	" ~qy 	wt} 57;< = = 	= taxx f1d...CC!T---CCAt$///CC9r   rT  r   c                    t          j        |           } t          j        |          }t          j        |          }|dvrt          d          t          j        |j                  }|j        dk    r@t          j        |j                                                  }|                    |          }|dk    rt          | ||||          \  }	}
}|	S t          ||||t          t          |          t          |                               \  }}t          | |          }dg|j        z  }|j        ||<   t          j        ||          }t#          |d|          }t%          | |||||z  	          \  }	}t#          |	d
|          }t%          | |t'          |	|          |||z  	          \  }	}t'          |	|          }	|dk    rt#          |	|| |          }	|	S )a)  
    Apply a digital filter forward and backward to a signal.

    This function applies a linear digital filter twice, once forward and
    once backwards.  The combined filter has zero phase and a filter order
    twice that of the original.

    The function provides options for handling the edges of the signal.

    The function `sosfiltfilt` (and filter design using ``output='sos'``)
    should be preferred over `filtfilt` for most filtering tasks, as
    second-order sections have fewer numerical problems.

    Parameters
    ----------
    b : (N,) array_like
        The numerator coefficient vector of the filter.
    a : (N,) array_like
        The denominator coefficient vector of the filter.  If ``a[0]``
        is not 1, then both `a` and `b` are normalized by ``a[0]``.
    x : array_like
        The array of data to be filtered.
    axis : int, optional
        The axis of `x` to which the filter is applied.
        Default is -1.
    padtype : str or None, optional
        Must be 'odd', 'even', 'constant', or None.  This determines the
        type of extension to use for the padded signal to which the filter
        is applied.  If `padtype` is None, no padding is used.  The default
        is 'odd'.
    padlen : int or None, optional
        The number of elements by which to extend `x` at both ends of
        `axis` before applying the filter.  This value must be less than
        ``x.shape[axis] - 1``.  ``padlen=0`` implies no padding.
        The default value is ``3 * max(len(a), len(b))``.
    method : str, optional
        Determines the method for handling the edges of the signal, either
        "pad" or "gust".  When `method` is "pad", the signal is padded; the
        type of padding is determined by `padtype` and `padlen`, and `irlen`
        is ignored.  When `method` is "gust", Gustafsson's method is used,
        and `padtype` and `padlen` are ignored.
    irlen : int or None, optional
        When `method` is "gust", `irlen` specifies the length of the
        impulse response of the filter.  If `irlen` is None, no part
        of the impulse response is ignored.  For a long signal, specifying
        `irlen` can significantly improve the performance of the filter.

    Returns
    -------
    y : ndarray
        The filtered output with the same shape as `x`.

    See Also
    --------
    sosfiltfilt, lfilter_zi, lfilter, lfiltic, savgol_filter, sosfilt

    Notes
    -----
    When `method` is "pad", the function pads the data along the given axis
    in one of three ways: odd, even or constant.  The odd and even extensions
    have the corresponding symmetry about the end point of the data.  The
    constant extension extends the data with the values at the end points. On
    both the forward and backward passes, the initial condition of the
    filter is found by using `lfilter_zi` and scaling it by the end point of
    the extended data.

    When `method` is "gust", Gustafsson's method [1]_ is used.  Initial
    conditions are chosen for the forward and backward passes so that the
    forward-backward filter gives the same result as the backward-forward
    filter.

    References
    ----------
    .. [1] F. Gustaffson, "Determining the initial states in forward-backward
           filtering", Transactions on Signal Processing, Vol. 46, pp. 988-992,
           1996.
    >   r   gustzmethod must be 'pad' or 'gust'.r   r\  )r   r:  rW  r?   stopr   r   r   rV   startr   r   r   rb  r_  r   )r3   r   r   r*   r-   r5   r   r   r7   rQ  rZ  r@   r  r  rl   r   ro   r   r   r   )r   rB   r   r   rU  rV  r   r:  r   r   z1z2rX  rY  r   zi_shaperP  zfy0s                      r   filtfiltri    s   ^ 	AAQA_$$:;;;*QW%%K3j!1!7!7!9!9::HH[!!"1aUCCC	2r gvq$$'AA$7$79 9 9ID# 
Aq		B
 sQV|HWHTN	b(	#	#B	Cad	+	+	+B aCdrBw777GQ 
ARd	+	+	+BaL666Tb2gNNNGQ 	QT"""AaxxqD5t<<<Hr   c                    t          j        |           }t          j        |          }|j        dk    rt          d          |j        dk    rt          d          t	          |          }t	          |          }||k    rg }|}nKt          j        ||z
  dz   t                    }d|d<   t          |||          }|t          ||d          z
  }||fS )a  Deconvolves ``divisor`` out of ``signal`` using inverse filtering.

    Returns the quotient and remainder such that
    ``signal = convolve(divisor, quotient) + remainder``

    Parameters
    ----------
    signal : (N,) array_like
        Signal data, typically a recorded signal
    divisor : (N,) array_like
        Divisor data, typically an impulse response or filter that was
        applied to the original signal

    Returns
    -------
    quotient : ndarray
        Quotient, typically the recovered original signal
    remainder : ndarray
        Remainder

    See Also
    --------
    cupy.polydiv : performs polynomial division (same operation, but
                   also accepts poly1d objects)

    Examples
    --------
    Deconvolve a signal that's been filtered:

    >>> from cupyx.scipy import signal
    >>> original = [0, 1, 0, 0, 1, 1, 0, 0]
    >>> impulse_response = [2, 1]
    >>> recorded = signal.convolve(impulse_response, original)
    >>> recorded
    array([0, 2, 1, 0, 2, 3, 1, 0, 0])
    >>> recovered, remainder = signal.deconvolve(recorded, impulse_response)
    >>> recovered
    array([ 0.,  1.,  0.,  0.,  1.,  1.,  0.,  0.])

    r?   zsignal must be 1-D.zdivisor must be 1-D.r   r   r$   )	r3   r   rl   r*   r  r   floatr   r   )	signaldivisornumdenr*  Dquotremr   s	            r   
deconvolvers  T  s    R /&
!
!C
/'
"
"C
x!||.///
x!||/000CACA1uu
1q519e,,asC''HS$V44449r   c                     | d|z  } t          j        | |dt                    } t          d | D                       rt	          d          | S )Nr   r   c              3   (   K   | ]}|d z  dk    V  dS r   r^   )rA   r   s     r   r\   z#_get_kernel_size.<locals>.<genexpr>  s*      
-
-AAEa<
-
-
-
-
-
-r   z)Each element of kernel_size should be odd)r   r   r   r   r*   )r   rl   s     r   r   r     sa    Tk)+t*7> >K

-
-
-
-
--- FDEEEr   c                 0   t          j        |           } | j        dk    rt          d          | j        \  }}|dk    rt          d          t          j        | dddf         dz
            dk                                    st          d	          | |fS )
zHelper to validate a SOS inputr   zsos array must be 2D   z'sos array must be shape (n_sections, 6)Nr   g      ?gV瞯<zsos[:, 3] should be all ones)r3   
atleast_2drl   r*   r1   absall)sos
n_sectionsr.  s      r   _validate_sosr~    s    
/#

C
x1}}/000IMJAvvBCCCHSAY_%%.3355 97888
?r   c                 b    t          j        |           } | j        dk    rt          d          | S )Nr   zx must be at least 1-D)r3   r   rl   r*   )r   s    r   _validate_xr    s.    QAv{{1222Hr   c                 f    |j         }t          j        ||          }|}t          || ||          }|S )a  
    Filter data along one dimension using cascaded second-order sections.

    Filter a data sequence, `x`, using a digital IIR filter defined by
    `sos`.

    Parameters
    ----------
    sos : array_like
        Array of second-order filter coefficients, must have shape
        ``(n_sections, 6)``. Each row corresponds to a second-order
        section, with the first three columns providing the numerator
        coefficients and the last three providing the denominator
        coefficients.
    x : array_like
        An N-dimensional input array.
    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.
    zi : array_like, optional
        Initial conditions for the cascaded filter delays.  It is a (at
        least 2D) vector of shape ``(n_sections, ..., 4, ...)``, where
        ``..., 4, ...`` denotes the shape of `x`, but with ``x.shape[axis]``
        replaced by 4.  If `zi` is None or is not given then initial rest
        (i.e. all zeros) is assumed.
        Note that these initial conditions are *not* the same as the initial
        conditions given by `lfiltic` or `lfilter_zi`.

    Returns
    -------
    y : ndarray
        The output of the digital filter.
    zf : ndarray, optional
        If `zi` is None, this is not returned, otherwise, `zf` holds the
        final filter delay values.

    See Also
    --------
    zpk2sos, sos2zpk, sosfilt_zi, sosfiltfilt, sosfreqz
    )rl   r   r   r   )r|  r   r   r   r   r;   s         r   sosfiltr    s;    T VF)$77D
C
S$
+
+CJr   c                    | j         d         }t          | d| j                  }t          j        | j         d         df| j                  }t          j        d| j                  }t          |          D ]~}t          j        ||                   }t          j        | |                   }|dd         |dddf<   t          |||          \  }}	||         }
|dd         }|dd         }|
ddddf         j	        }|
ddddf         j	        }t          j
        | |ddf         dk              d         }t          |          s't          j                            ||z
  ||z
            }n-t          j                            ||z
  ||z
  d	          \  }}	}	}	t          j        |dt          j        t          j         
          }|ddd         |dddf<   t          |||          \  }}	|S )a  
    Construct initial conditions for sosfilt for step response steady-state.

    Compute an initial state `zi` for the `sosfilt` function that corresponds
    to the steady state of the step response.

    A typical use of this function is to set the initial state so that the
    output of the filter starts at the same value as the first element of
    the signal to be filtered.

    Parameters
    ----------
    sos : array_like
        Array of second-order filter coefficients, must have shape
        ``(n_sections, 6)``. See `sosfilt` for the SOS filter format
        specification.

    Returns
    -------
    zi : ndarray
        Initial conditions suitable for use with ``sosfilt``, shape
        ``(n_sections, 4)``.

    See Also
    --------
    sosfilt, zpk2sos
    r   r      r   Nr   r  r  r  rV   )r1   r   r-   r3   r   r  rk   ry  r  r
  r   r  r  r  r   r  r  )r|  r}  r  r   x_sr   zi_ssos_sy_sr  C_sr  r  r  r  zero_iir_coefr  s                    r   
sosfilt_zir    s   8 1J&sAsy99A	SYq\1%SY	7	7	7B )ASY
'
'
'C: . .r!u%%A'' "1"gQU ---Qd!WXBQBZ\BCC[]
3q!""u:?33A6 =!! 	L;$$R"Wb2g66DD !K--b2grBwd-KKMD!Qt48TXINNN44R4jQU---QQIr   c                    t          |           \  } }t          |          }d|z  dz   }|t          | dddf         dk                                                                    | dddf         dk                                                                              z  }t          |||||          \  }}t          |           }	dg|j        z  }
d|
|<   |g|
z   |	_        t          |d|          }t          | |||	|z  	          \  }}t          |d
|          }t          | t          ||          ||	|z  	          \  }}t          ||          }|dk    rt          ||| |          }|S )a  
    A forward-backward digital filter using cascaded second-order sections.

    See `filtfilt` for more complete information about this method.

    Parameters
    ----------
    sos : array_like
        Array of second-order filter coefficients, must have shape
        ``(n_sections, 6)``. Each row corresponds to a second-order
        section, with the first three columns providing the numerator
        coefficients and the last three providing the denominator
        coefficients.
    x : array_like
        The array of data to be filtered.
    axis : int, optional
        The axis of `x` to which the filter is applied.
        Default is -1.
    padtype : str or None, optional
        Must be 'odd', 'even', 'constant', or None.  This determines the
        type of extension to use for the padded signal to which the filter
        is applied.  If `padtype` is None, no padding is used.  The default
        is 'odd'.
    padlen : int or None, optional
        The number of elements by which to extend `x` at both ends of
        `axis` before applying the filter.  This value must be less than
        ``x.shape[axis] - 1``.  ``padlen=0`` implies no padding.
        The default value is::

            3 * (2 * len(sos) + 1 - min((sos[:, 2] == 0).sum(),
                                        (sos[:, 5] == 0).sum()))

        The extra subtraction at the end attempts to compensate for poles
        and zeros at the origin (e.g. for odd-order filters) to yield
        equivalent estimates of `padlen` to those of `filtfilt` for
        second-order section filters built with `scipy.signal` functions.

    Returns
    -------
    y : ndarray
        The filtered output with the same shape as `x`.

    See Also
    --------
    filtfilt, sosfilt, sosfilt_zi, sosfreqz
    r   r?   Nr      r]  r  r^  r`  rV   ra  r   rc  )r~  r  minsumitemrZ  r  rl   r1   r   r  r   )r|  r   r   rU  rV  r}  rW  rX  rY  r   rf  x_0r   rg  y_0s                  r   sosfiltfiltr  &  s   ^ $C((OCAA 
NQE	S#aaad)q.%%'',,..QQQTa0D0D0F0F0K0K0M0MNNNEgvq$$)+ + +ID# 
CBsQV|HHTN|h&BH
Sqt
,
,
,Cc3Tb3h777GQ
Qbt
,
,
,Cc<555DR#XNNNGQQT"""AaxxqD5t<<<Hr   c                    t          j        |           rt          d          || j        |         }|dk    rt          d          t	          j        | ||          }t          j        ||j                  }|dz  dk    rdx|d<   ||dz  <   d|d|dz  <   nd|d<   d|d|dz   dz  <   | j        dk    r<t           j	        g| j        z  }t          d          ||<   |t          |                   }t	          j        ||z  |          } | S )	a  
    Compute the analytic signal, using the Hilbert transform.

    The transformation is done along the last axis by default.

    Parameters
    ----------
    x : ndarray
        Signal data.  Must be real.
    N : int, optional
        Number of Fourier components.  Default: ``x.shape[axis]``
    axis : int, optional
        Axis along which to do the transformation.  Default: -1.

    Returns
    -------
    xa : ndarray
        Analytic signal of `x`, of each 1-D array along `axis`

    Notes
    -----
    The analytic signal ``x_a(t)`` of signal ``x(t)`` is:

    .. math:: x_a = F^{-1}(F(x) 2U) = x + i y

    where `F` is the Fourier transform, `U` the unit step function,
    and `y` the Hilbert transform of `x`. [1]_

    In other words, the negative half of the frequency spectrum is zeroed
    out, turning the real-valued signal into a complex signal.  The Hilbert
    transformed signal can be obtained from ``np.imag(hilbert(x))``, and the
    original signal from ``np.real(hilbert(x))``.

    References
    ----------
    .. [1] Wikipedia, "Analytic signal".
           https://en.wikipedia.org/wiki/Analytic_signal

    See Also
    --------
    scipy.signal.hilbert

    x must be real.Nr   N must be positive.r   r   r   r?   )r3   iscomplexobjr*   r1   sp_fftr#   r   r-   rl   newaxisri   rp   ifft)r   r*  r   Xfhinds         r   hilbertr  m  s&   X  ,*+++yGDMAvv.///	Aqt	$	$	$B
1BH%%%A1uzz!qay!AF(!!QUqL.vzz|nqv%$KKD	eCjjMBF&&&AHr   c                    | j         dk     rt          j        |           } | j         dk    rt          d          t          j        |           rt          d          || j        }nit          |t                    r|dk    rt          d          ||f}n:t          |          dk    s|d         dk    s|d         dk    rt          d          t          j
        | |d	
          }t          j        |d         |j                  }t          j        |d         |j                  }||fD ]B}|j        d         }|dz  dk    rdx|d<   ||dz  <   d|d|dz  <   0d|d<   d|d|dz   dz  <   C|ddt          j        f         |t          j        ddf         z  }| j         }|dk    r!|ddt          j        f         }|dz  }|dk    !t          j        ||z  d	
          } | S )ay  
    Compute the '2-D' analytic signal of `x`

    Parameters
    ----------
    x : ndarray
        2-D signal data.
    N : int or tuple of two ints, optional
        Number of Fourier components. Default is ``x.shape``

    Returns
    -------
    xa : ndarray
        Analytic signal of `x` taken along axes (0,1).

    See Also
    --------
    scipy.signal.hilbert2

    r   zx must be 2-D.r  Nr   r  r?   z@When given as a tuple, N must hold exactly two positive integers)r   r?   )rE   r   )rl   r3   ry  r*   r  r1   
isinstancer   r  r  fft2r   r-   r  ifft2)r   r*  r  h1h2r  N1r   s           r   hilbert2r    s   * 	vzzOAvzz)*** ,*+++yG	As		 2662333F	Q11adaii 1 2 2 	2 
Q	'	'	'B	AaD	)	)	)B	AaD	)	)	)B"X # #WQZ6Q;; !!AaD1R1W:AaaiLLAaD!"AaaAo
111dl?bqqq11A	A
a%%aaao	Q a%% 	R!V&)))AHr   )r   r   )r   r   F)r   N)r   )r   r   r   )F)NNr   ru  rU   )rV   r  r   F)rV   rT  Nr   N)rV   rT  N)NrV   ):r   r3   
cupy._corer   cupy.linalgr   cupyx.scipy.fftscipyr#   r  cupyx.scipy.ndimager   r   cupyx.scipy.signalr   r(   cupyx.scipy.signal._arraytoolsr   r	   r
   r   r   r   cupyx.scipy.signal._iir_utilsr   r   r   r   r   r    r   r2   r+   r~   r   r   r   r   r   r   r   r   r   r   r  r4  rQ  rZ  ri  rs  r   r~  r  r  r  r  r  r  r^   r   r   <module>r     s                                   % % % % % % ( ( ( ( ( ( < < < < < <I I I I I I I I I I I I I I I I$ $ $ $ $ $ $ $ $ $ $ $
,4 ,4 ,4 ,4^,5 ,5 ,5 ,5^   8.L .L .L .Lb? ? ? ?.b> b> b> b>J'C 'C 'C 'CT+D +D +D +D\L L L L*: : : :z(: (: (: (:VL L L@&1 &1 &1 &1R(9 (9 (9 (9Vk k k k\? ? ? ?D9 9 9xN N N Nbo o o od     F CH} } } }@9 9 9x  
 
 
  / / / /dC C CLD D D DNA A A AH7 7 7 7 7 7r   