a
    ˆù»jz?  ã                   @   s  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mZmZmZmZmZmZ ddlmZmZ ddlmZmZmZ d dlmZ g d¢Zd'dd	„Zi Zd
d„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&d(dd„Z'd)dd „Z(d*d"d#„Z)d+d$d%„Z*d&S ),é    )ÚasarrayÚpiÚ
zeros_likeÚarrayÚarctan2ÚtanÚonesÚarangeÚfloorÚr_Ú
atleast_1dÚsqrtÚexpÚgreaterÚcosÚaddÚsiné   )Ú	cspline2dÚsepfir2d)ÚlfilterÚsosfiltÚlfiltic)ÚBSpline)Úspline_filterÚgauss_splineÚ	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 ršt| |ƒ}t|||ƒ}| |¡}ntdƒ‚|S )	a4  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
        filtered input data

    Examples
    --------
    We can filter an multi dimensional 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© r.   ú<E:\soccer\ml_env\Lib\site-packages\scipy/signal/_bsplines.pyr      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
    >>> knots = np.array([-1.0, 0.0, -1.0])
    >>> gauss_spline(knots, 3)
    array([0.15418033, 0.6909883, 0.15418033])  # may vary

    r   g      (@é   )r   r   r   r   )ÚxÚnZsignsqr.   r.   r/   r   J   s    -r   c                 C   s>   t | td�} tjg d¢dd�}|| ƒ}d|| dk | dkB < |S )N©r'   )éþÿÿÿéÿÿÿÿr   r   r0   F©Zextrapolater   r4   r0   )r   Úfloatr   Úbasis_element©r1   Úbr-   r.   r.   r/   Ú_cubic|   s
    r;   c                 C   sB   t t| td�ƒ} tjg d¢dd�}|| ƒ}d|| dk | dkB < |S )Nr3   )ç      ø¿g      à¿g      à?ç      ø?Fr6   r   r<   r=   )Úabsr   r7   r   r8   r9   r.   r.   r/   Ú
_quadratic„   s
    r?   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   é`   é   é   é�   é0   )r   r   )ZlamÚxiZomegÚrhor.   r.   r/   Ú_coeff_smoothŒ   s
    $,rG   c                 C   s.   |t |ƒ ||   t || d  ƒ t| dƒ S )Nr   r5   )r   r   )ÚkÚcsrF   Úomegar.   r.   r/   Ú_hc”   s    "ÿrK   c                 C   s”   || d||   d||   dd| | t d| ƒ  |d   }d||  d||   t|ƒ }t| ƒ}|||  t || ƒ|t|| ƒ   S )Nr   r0   é   )r   r   r>   r   )rH   rI   rF   rJ   Zc0ÚgammaZakr.   r.   r/   Ú_hs™   s    "ÿ rN   c                 C   s"  t |ƒ\}}dd| t|ƒ  ||  }t| ƒ}t|ƒ}td|||ƒ| d  t t|d |||ƒ|  ¡ }td|||ƒ| d  td|||ƒ| d   t t|d |||ƒ|  ¡ }t|tdd| t|ƒ || f t||f ƒ}	|	 	dd¡}	t|dddd| t|ƒ || f }
|
 	dd¡}
t
|
| dd … |	d�\}}t|||f }t t||||ƒt|d |||ƒ | d d d…  ¡}t t|d |||ƒt|d |||ƒ | d d d…  ¡}t|tdd| t|ƒ || f t||f ƒ}	|	 	dd¡}	t
|
|dd d… |	d�\}}t|d d d… ||f }|S )Nr   r0   r   r4   r5   ©Úziéýÿÿÿ)rG   r   Úlenr	   rK   r   Úreducer   r   Zreshaper   rN   )ÚsignalÚlambrF   rJ   rI   ÚKrH   Zzi_2Zzi_1rP   ÚsosZypÚ_Úyr.   r.   r/   Ú_cubic_smooth_coeff¡   sB    ÿÿþ.$ÿÿÿÿ.rZ   c                 C   s$  dt dƒ }t| ƒ}|t|ƒ }|dkrZ| d |t ||  ¡  }||d  | }t|ƒS tdtd| f tt ||  ¡ƒƒ}tdƒ}td| f }t	||| |d�\}}	||d  ||d   }
t| td| f t|
ƒƒ}t
| gƒ}t	|||dd d… |d�\}}	t|d d d… |
f }|d S )Nr4   rB   r   r   rO   r5   r#   ©r   rR   r	   r   rS   r   r   r   r   r   r   ©rT   rP   rV   ZpowersZyplusÚoutputÚstater:   ÚarX   Zout_lastr.   r.   r/   Ú_cubic_coeffË   s"    $r`   c                 C   s(  ddt dƒ  }t| ƒ}|t|ƒ }|dkr^| d |t ||  ¡  }||d  | }t|ƒS tdtd| f tt ||  ¡ƒƒ}tdƒ}td| f }t	||| |d�\}}	||d  ||d   }
t| td| f t|
ƒƒ}t
| gƒ}t	|||dd d… |d�\}}	t|d d d… |
f }|d	 S )
NrQ   r0   g       @r   r   rO   r4   r5   g       @r[   r\   r.   r.   r/   Ú_quadratic_coeffí   s"    $ra   ç        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()

    rb   N)rZ   r`   ©rT   rU   r.   r.   r/   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()

    rb   z.Smoothing quadratic splines not supported yet.N)Ú
ValueErrorra   rc   r.   r.   r/   r   A  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()

    r3   r   r   r0   rL   )r   r7   r   r'   ÚsizerR   r   r
   r)   ÚintÚrangeÚclipr;   ©ÚcjZnewxZdxZx0ÚresÚNZcond1Zcond2Zcond3ÚresultZjlowerÚiZthisjZindjr.   r.   r/   r   t  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   r0   r=   rB   )r   r   re   rR   r   r
   r)   rf   rg   rh   r?   ri   r.   r.   r/   r   ¾  s*    4


r   N)r    )rb   )rb   )r!   r   )r!   r   )+Únumpyr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Z_spliner   r   Z_signaltoolsr   r   r   Zscipy.interpolater   Ú__all__r   Z_splinefunc_cacher   r;   r?   rG   rK   rN   rZ   r`   ra   r   r   r   r   r.   r.   r.   r/   Ú<module>   s&   L
82*""
2
3
J