a
    Re S                     @   sv  d dl mZmZmZmZmZmZmZmZm	Z	m
Z
mZ d dlmZmZmZmZmZmZmZmZmZ d dl ZddlmZmZ d dlmZ d dlmZ d dlmZ g d	Z d5ddZ!i Z"dd Z#dZ$ej%e$ddd Z&dd Z'dZ(ej%e(ddd Z)dd Z*dZ+ej%e+ddd Z,dd Z-dd  Z.d!d" Z/d#d$ Z0d%d& Z1d'd( Z2d)d* Z3d6d,d-Z4d7d.d/Z5d8d1d2Z6d9d3d4Z7dS ):    )logical_andasarraypi
zeros_like	piecewisearrayarctan2tanzerosarangefloor)	sqrtexpgreaterlesscosaddsin
less_equalgreater_equalN   )	cspline2dsepfir2d)comb)float_factorial)BSpline)	spline_filterbsplinegauss_splinecubic	quadratic	cspline1d	qspline1dcspline1d_evalqspline1d_eval      @c           	      C   s   | j j}tg ddd }|dv rp| d} t| j|}t| j|}t|||}t|||}|d|  |}n2|dv rt| |}t|||}||}ntd|S )	a3  Smoothing spline (cubic) filtering of a rank-2 array.

    Filter an input data set, `Iin`, using a (cubic) smoothing spline of
    fall-off `lmbda`.

    Parameters
    ----------
    Iin : array_like
        input data set
    lmbda : float, optional
        spline smooghing fall-off value, default is `5.0`.

    Returns
    -------
    res : ndarray
        filterd input data

    Examples
    --------
    We can filter an multi dimentional signal (ex: 2D image) using cubic
    B-spline filter:

    >>> import numpy as np
    >>> from scipy.signal import spline_filter
    >>> import matplotlib.pyplot as plt
    >>> orig_img = np.eye(20)  # create an image
    >>> orig_img[10, :] = 1.0
    >>> sp_filter = spline_filter(orig_img, lmbda=0.1)
    >>> f, ax = plt.subplots(1, 2, sharex=True)
    >>> for ind, data in enumerate([[orig_img, "original image"],
    ...                             [sp_filter, "spline filter"]]):
    ...     ax[ind].imshow(data[0], cmap='gray_r')
    ...     ax[ind].set_title(data[1])
    >>> plt.tight_layout()
    >>> plt.show()

    )      ?g      @r&   f      @)FDr)   y              ?)r'   dzInvalid data type for Iin)	dtypecharr   astyper   realimagr   	TypeError)	ZIinZlmbdaZintypeZhcolZckrZckiZoutrZoutiout r3   J/var/www/sunrise/env/lib/python3.9/site-packages/scipy/signal/_bsplines.pyr      s    &

r   c                    s   z
t  W S  ty   Y n0 dd }d d }d r@d}nd}|dd|g}| td|d D ]"}||d  d   d  qd||ddd  d  t fd	d
fddt|D }||ft < ||fS )a  Returns the function defined over the left-side pieces for a bspline of
    a given order.

    The 0th piece is the first one less than 0. The last piece is a function
    identical to 0 (returned as the constant 0). (There are order//2 + 2 total
    pieces).

    Also returns the condition functions that when evaluated return boolean
    arrays for use with `numpy.piecewise`.
    c                    s<   | dkr fddS | dkr*fddS  fddS d S )Nr   c                    s   t t|  t| S N)r   r   r   xval1val2r3   r4   <lambda>_   s   
z>_bspline_piecefunctions.<locals>.condfuncgen.<locals>.<lambda>   c                    s
   t |  S r5   )r   r6   )r:   r3   r4   r;   b       c                    s   t t|  t| S r5   )r   r   r   r6   r8   r3   r4   r;   d   s   
r3   )numr9   r:   r3   r8   r4   condfuncgen]   s
    z,_bspline_piecefunctions.<locals>.condfuncgenr<   g            r   r          @c                    sd   d |    dk rdS fddt  d D fddt  d D  fdd}|S )	Nr<   r   c              	      s6   g | ].}d d|d   t td  |d d   qS )r   r<   )exact)floatr   .0k)fvalorderr3   r4   
<listcomp>   s   zA_bspline_piecefunctions.<locals>.piecefuncgen.<locals>.<listcomp>r   c                    s   g | ]}  | qS r3   r3   rD   )boundr3   r4   rI      r=   c                    s6   d}t  d D ] }|| | |    7 }q|S )N        r   range)r7   resrF   )MkcoeffsrH   shiftsr3   r4   thefunc   s    z>_bspline_piecefunctions.<locals>.piecefuncgen.<locals>.thefuncrL   )r>   rR   )rJ   rG   rH   )rO   rP   rQ   r4   piecefuncgen{   s    
z-_bspline_piecefunctions.<locals>.piecefuncgenc                    s   g | ]} |qS r3   r3   rD   )rS   r3   r4   rI      r=   z+_bspline_piecefunctions.<locals>.<listcomp>)_splinefunc_cacheKeyErrorrM   appendr   )rH   r?   lastZ
startbound	condfuncsr>   funclistr3   )rJ   rG   rH   rS   r4   _bspline_piecefunctionsM   s(    


rZ   a5  `scipy.signal.bspline` is deprecated in SciPy 1.11 and will be
removed in SciPy 1.13.

The exact equivalent (for a float array `x`) is

>>> from scipy.interpolate import BSpline
>>> knots = np.arange(-(n+1)/2, (n+3)/2)
>>> out = BSpline.basis_element(knots)(x)
>>> out[(x < knots[0]) | (x > knots[-1])] = 0.0
)messagec                    s<   t t| td  t|\}} fdd|D }t ||S )aw  B-spline basis function of order n.

    Parameters
    ----------
    x : array_like
        a knot vector
    n : int
        The order of the spline. Must be non-negative, i.e., n >= 0

    Returns
    -------
    res : ndarray
        B-spline basis function values

    See Also
    --------
    cubic : A cubic B-spline.
    quadratic : A quadratic B-spline.

    Notes
    -----
    Uses numpy.piecewise and automatic function-generator.

    Examples
    --------
    We can calculate B-Spline basis function of several orders:

    >>> import numpy as np
    >>> from scipy.signal import bspline, cubic, quadratic
    >>> bspline(0.0, 1)
    1

    >>> knots = [-1.0, 0.0, -1.0]
    >>> bspline(knots, 2)
    array([0.125, 0.75, 0.125])

    >>> np.array_equal(bspline(knots, 2), quadratic(knots))
    True

    >>> np.array_equal(bspline(knots, 3), cubic(knots))
    True

    r,   c                    s   g | ]}| qS r3   r3   )rE   funcaxr3   r4   rI      r=   zbspline.<locals>.<listcomp>)absr   rC   rZ   r   )r7   nrY   rX   Zcondlistr3   r^   r4   r      s    -r   c                 C   s>   t | } |d d }dtdt |  t| d  d |  S )a  Gaussian approximation to B-spline basis function of order n.

    Parameters
    ----------
    x : array_like
        a knot vector
    n : int
        The order of the spline. Must be non-negative, i.e., n >= 0

    Returns
    -------
    res : ndarray
        B-spline basis function values approximated by a zero-mean Gaussian
        function.

    Notes
    -----
    The B-spline basis function can be approximated well by a zero-mean
    Gaussian function with standard-deviation equal to :math:`\sigma=(n+1)/12`
    for large `n` :

    .. math::  \frac{1}{\sqrt {2\pi\sigma^2}}exp(-\frac{x^2}{2\sigma})

    References
    ----------
    .. [1] Bouma H., Vilanova A., Bescos J.O., ter Haar Romeny B.M., Gerritsen
       F.A. (2007) Fast and Accurate Gaussian Derivatives Based on B-Splines. In:
       Sgallari F., Murli A., Paragios N. (eds) Scale Space and Variational
       Methods in Computer Vision. SSVM 2007. Lecture Notes in Computer
       Science, vol 4485. Springer, Berlin, Heidelberg
    .. [2] http://folk.uio.no/inf3330/scripting/doc/python/SciPy/tutorial/old/node24.html

    Examples
    --------
    We can calculate B-Spline basis functions approximated by a gaussian
    distribution:

    >>> import numpy as np
    >>> from scipy.signal import gauss_spline, bspline
    >>> knots = np.array([-1.0, 0.0, -1.0])
    >>> gauss_spline(knots, 3)
    array([0.15418033, 0.6909883, 0.15418033])  # may vary

    >>> bspline(knots, 3)
    array([0.16666667, 0.66666667, 0.16666667])  # may vary

    r   g      (@r<   )r   r   r   r   )r7   ra   Zsignsqr3   r3   r4   r      s    0r   a  `scipy.signal.cubic` is deprecated in SciPy 1.11 and will be
removed in SciPy 1.13.

The exact equivalent (for a float array `x`) is

>>> from scipy.interpolate import BSpline
>>> out = BSpline.basis_element([-2, -1, 0, 1, 2])(x)
>>> out[(x < -2 | (x > 2)] = 0.0
c                 C   s   t t| td}t|}t|d}| rN|| }dd|d  d|   ||< | t|d@ }| r|| }dd| d  ||< |S )a-  A cubic B-spline.

    This is a special case of `bspline`, and equivalent to ``bspline(x, 3)``.

    Parameters
    ----------
    x : array_like
        a knot vector

    Returns
    -------
    res : ndarray
        Cubic B-spline basis function values

    See Also
    --------
    bspline : B-spline basis function of order n
    quadratic : A quadratic B-spline.

    Examples
    --------
    We can calculate B-Spline basis function of several orders:

    >>> import numpy as np
    >>> from scipy.signal import bspline, cubic, quadratic
    >>> bspline(0.0, 1)
    1

    >>> knots = [-1.0, 0.0, -1.0]
    >>> bspline(knots, 2)
    array([0.125, 0.75, 0.125])

    >>> np.array_equal(bspline(knots, 2), quadratic(knots))
    True

    >>> np.array_equal(bspline(knots, 3), cubic(knots))
    True

    r\   r   gUUUUUU?      ?r<   gUUUUUU?   r`   r   rC   r   r   anyr7   r_   rN   cond1Zax1cond2Zax2r3   r3   r4   r     s    )
r   c                 C   s>   t | td} tjg ddd}|| }d|| dk | dkB < |S )Nr\   )r   r   r<   FZextrapolater   ri   r<   )r   rC   r   basis_elementr7   br2   r3   r3   r4   _cubicF  s
    ro   a  `scipy.signal.quadratic` is deprecated in SciPy 1.11 and
will be removed in SciPy 1.13.

The exact equivalent (for a float array `x`) is

>>> from scipy.interpolate import BSpline
>>> out = BSpline.basis_element([-1.5, -0.5, 0.5, 1.5])(x)
>>> out[(x < -1.5 | (x > 1.5)] = 0.0
c                 C   sz   t t| td}t|}t|d}| rB|| }d|d  ||< | t|d@ }| rv|| }|d d d ||< |S )a-  A quadratic B-spline.

    This is a special case of `bspline`, and equivalent to ``bspline(x, 2)``.

    Parameters
    ----------
    x : array_like
        a knot vector

    Returns
    -------
    res : ndarray
        Quadratic B-spline basis function values

    See Also
    --------
    bspline : B-spline basis function of order n
    cubic : A cubic B-spline.

    Examples
    --------
    We can calculate B-Spline basis function of several orders:

    >>> import numpy as np
    >>> from scipy.signal import bspline, cubic, quadratic
    >>> bspline(0.0, 1)
    1

    >>> knots = [-1.0, 0.0, -1.0]
    >>> bspline(knots, 2)
    array([0.125, 0.75, 0.125])

    >>> np.array_equal(bspline(knots, 2), quadratic(knots))
    True

    >>> np.array_equal(bspline(knots, 3), cubic(knots))
    True

    r\   rb   g      ?r<         ?rA   rd   rf   r3   r3   r4   r    Y  s    )
r    c                 C   sB   t t| td} tjg ddd}|| }d|| dk | dkB < |S )Nr\   )      r@   rb   rp   Frk   r   rq   rp   )r`   r   rC   r   rl   rm   r3   r3   r4   
_quadratic  s
    rr   c                 C   s   dd|   d|  t dd|     }tt d|  d t |}d|  d t | d|   }|t d|  d|  t dd|     |  }||fS )Nr   `      rc      0   )r   r   )ZlamxiZomegrhor3   r3   r4   _coeff_smooth  s
    $,ry   c                 C   s.   |t | ||   t || d   t| d S )Nr   rj   )r   r   )rF   csrx   omegar3   r3   r4   _hc  s    "r|   c                 C   s   || d||   d||   dd| | t d|   |d   }d||  d||   t| }t| }|||  t || |t||    S )Nr   r<      )r   r	   r`   r   )rF   rz   rx   r{   Zc0gammaZakr3   r3   r4   _hs  s    " r   c           
      C   s  t |\}}dd| t|  ||  }t| }t|f| jj}t|}td|||| d  t	t|d ||||   |d< td|||| d  td|||| d   t	t|d ||||   |d< t
d|D ]D}|| |  d| t| ||d    || ||d    ||< qt|f| jj}	t	t||||t|d ||| | d d d  |	|d < t	t|d |||t|d ||| | d d d  |	|d < t
|d ddD ]F}|||  d| t| |	|d    || |	|d    |	|< q|	S )Nr   r<   r   rj   rc   )ry   r   lenr
   r,   r-   r   r|   r   reducerM   r   )
signallambrx   r{   rz   KZyprF   ra   yr3   r3   r4   _cubic_smooth_coeff  sB    &
&r   c                 C   s   dt d }t| }t|f| jj}|t| }| d |t||    |d< td|D ] }| | |||d    ||< qXt|f| j}||d  ||d   ||d < t|d ddD ] }|||d  ||   ||< q|d S )Nri   rc   r   r   r<   rj   r(   	r   r   r
   r,   r-   r   r   r   rM   r   Zzir   ZyplusZpowersrF   outputr3   r3   r4   _cubic_coeff  s     r   c                 C   s   ddt d  }t| }t|f| jj}|t| }| d |t||    |d< td|D ] }| | |||d    ||< q\t|f| jj}||d  ||d   ||d < t|d ddD ] }|||d  ||   ||< q|d S )Nr<   rA   r   r   rj   g       @r   r   r3   r3   r4   _quadratic_coeff  s     r   rK   c                 C   s   |dkrt | |S t| S dS )a  
    Compute cubic spline coefficients for rank-1 array.

    Find the cubic spline coefficients for a 1-D signal assuming
    mirror-symmetric boundary conditions. To obtain the signal back from the
    spline representation mirror-symmetric-convolve these coefficients with a
    length 3 FIR window [1.0, 4.0, 1.0]/ 6.0 .

    Parameters
    ----------
    signal : ndarray
        A rank-1 array representing samples of a signal.
    lamb : float, optional
        Smoothing coefficient, default is 0.0.

    Returns
    -------
    c : ndarray
        Cubic spline coefficients.

    See Also
    --------
    cspline1d_eval : Evaluate a cubic spline at the new set of points.

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a cubic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import cspline1d, cspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = cspline1d_eval(cspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    rK   N)r   r   r   r   r3   r3   r4   r!     s    ,
r!   c                 C   s   |dkrt dnt| S dS )aF  Compute quadratic spline coefficients for rank-1 array.

    Parameters
    ----------
    signal : ndarray
        A rank-1 array representing samples of a signal.
    lamb : float, optional
        Smoothing coefficient (must be zero for now).

    Returns
    -------
    c : ndarray
        Quadratic spline coefficients.

    See Also
    --------
    qspline1d_eval : Evaluate a quadratic spline at the new set of points.

    Notes
    -----
    Find the quadratic spline coefficients for a 1-D signal assuming
    mirror-symmetric boundary conditions. To obtain the signal back from the
    spline representation mirror-symmetric-convolve these coefficients with a
    length 3 FIR window [1.0, 6.0, 1.0]/ 8.0 .

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a quadratic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import qspline1d, qspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = qspline1d_eval(qspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    rK   z.Smoothing quadratic splines not supported yet.N)
ValueErrorr   r   r3   r3   r4   r"     s    -
r"   r&   c                 C   s  t || t| }t|| jd}|jdkr0|S t| }|dk }||d k}||B  }t| ||  ||< t| d|d  ||  ||< || }|jdkr|S t|| jd}	t|d t	d }
t
dD ]4}|
| }|d|d }|	| | t||  7 }	q|	||< |S )a  Evaluate a cubic spline at the new set of points.

    `dx` is the old sample-spacing while `x0` was the old origin. In
    other-words the old-sample points (knot-points) for which the `cj`
    represent spline coefficients were at equally-spaced points of:

      oldx = x0 + j*dx  j=0...N-1, with N=len(cj)

    Edges are handled using mirror-symmetric boundary conditions.

    Parameters
    ----------
    cj : ndarray
        cublic spline coefficients
    newx : ndarray
        New set of points.
    dx : float, optional
        Old sample-spacing, the default value is 1.0.
    x0 : int, optional
        Old origin, the default value is 0.

    Returns
    -------
    res : ndarray
        Evaluated a cubic spline points.

    See Also
    --------
    cspline1d : Compute cubic spline coefficients for rank-1 array.

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a cubic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import cspline1d, cspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = cspline1d_eval(cspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    r\   r   r   r<   r}   )r   rC   r   r,   sizer   r#   r   r.   intrM   clipro   cjZnewxZdxZx0rN   Nrg   rh   Zcond3resultZjloweriZthisjZindjr3   r3   r4   r#   N  s*    2


r#   c                 C   s   t || | }t|}|jdkr&|S t| }|dk }||d k}||B  }t| ||  ||< t| d|d  ||  ||< || }|jdkr|S t|}	t|d td }
tdD ]4}|
| }|	d|d }|	| | t
||  7 }	q|	||< |S )a  Evaluate a quadratic spline at the new set of points.

    Parameters
    ----------
    cj : ndarray
        Quadratic spline coefficients
    newx : ndarray
        New set of points.
    dx : float, optional
        Old sample-spacing, the default value is 1.0.
    x0 : int, optional
        Old origin, the default value is 0.

    Returns
    -------
    res : ndarray
        Evaluated a quadratic spline points.

    See Also
    --------
    qspline1d : Compute quadratic spline coefficients for rank-1 array.

    Notes
    -----
    `dx` is the old sample-spacing while `x0` was the old origin. In
    other-words the old-sample points (knot-points) for which the `cj`
    represent spline coefficients were at equally-spaced points of::

      oldx = x0 + j*dx  j=0...N-1, with N=len(cj)

    Edges are handled using mirror-symmetric boundary conditions.

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a quadratic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import qspline1d, qspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = qspline1d_eval(qspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    r   r   r<   rp   rc   )r   r   r   r   r$   r   r.   r   rM   r   rr   r   r3   r3   r4   r$     s*    4


r$   )r%   )rK   )rK   )r&   r   )r&   r   )8numpyr   r   r   r   r   r   r   r	   r
   r   r   Znumpy.core.umathr   r   r   r   r   r   r   r   r   npZ_spliner   r   Zscipy.specialr   Zscipy._lib._utilr   Zscipy.interpolater   __all__r   rT   rZ   Zmsg_bsplineZ	deprecater   r   Z	msg_cubicr   ro   Zmsg_quadraticr    rr   ry   r|   r   r   r   r   r!   r"   r#   r$   r3   r3   r3   r4   <module>   s@   4,
8D

25

5

5
2
3
J