3
h5              	   @   s0  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mZmZ d dlmZ d dlmZ ddd	d
dddddg	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! Z)d"d# Z*d,d%dZ+d-d&dZ,d.d(dZ-d/d)dZ.d*S )0    )logical_andasarraypi
zeros_like	piecewisearrayarctan2tanzerosarangefloor)	sqrtexpgreaterlesscosaddsin
less_equalgreater_equal   )	cspline2dsepfir2d)comb)float_factorialspline_filterbsplinegauss_splinecubic	quadratic	cspline1d	qspline1dcspline1d_evalqspline1d_eval      @c       	      C   s   | j j}tdddgdd }|d
krr| jd} t| j|}t| j|}t|||}t|||}|d|  j|}n2|dkrt| |}t|||}|j|}ntd	|S )zSmoothing spline (cubic) filtering of a rank-2 array.

    Filter an input data set, `Iin`, using a (cubic) smoothing spline of
    fall-off `lmbda`.
    g      ?g      @fg      @FDy              ?dzInvalid data type for Iin)r&   r'   )r%   r(   )	dtypecharr   astyper   realimagr   	TypeError)	ZIinZlmbdaZintypeZhcolZckrZckiZoutrZoutiout r0   6/tmp/pip-build-riy7u7_k/scipy/scipy/signal/bsplines.pyr      s    

c                s   yt  S  tk
r   Y nX dd }d d }d r@d}nd}|dd|g}| x4td|d D ]"}|j|d  d   d  qfW |j|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val2r0   r1   <lambda>=   s   
z>_bspline_piecefunctions.<locals>.condfuncgen.<locals>.<lambda>   c                s
   t |  S )N)r   )r2   )r4   r0   r1   r5   @   s    c                s   t t|  t| S )N)r   r   r   )r2   )r3   r4   r0   r1   r5   B   s   
r0   )numr3   r4   r0   )r3   r4   r1   condfuncgen;   s
    z,_bspline_piecefunctions.<locals>.condfuncgenr6   g      ?g      ?r   r   g       @c                sd   d |    dk rdS fddt  d D fddt  d D  fdd}|S )	Nr6   r   c          	      s6   g | ].}d d|d   t td  |d d   qS )r   r6   )exact)floatr   ).0k)fvalorderr0   r1   
<listcomp>]   s   zA_bspline_piecefunctions.<locals>.piecefuncgen.<locals>.<listcomp>r   c                s   g | ]}  | qS r0   r0   )r;   r<   )boundr0   r1   r?   _   s    c                s:   d}x0t  d D ] }|| | |    7 }qW |S )Ng        r   )range)r2   resr<   )Mkcoeffsr>   shiftsr0   r1   thefunca   s     z>_bspline_piecefunctions.<locals>.piecefuncgen.<locals>.thefunc)rA   )r7   rF   )r@   r=   r>   )rC   rD   rE   r1   piecefuncgenY   s    z-_bspline_piecefunctions.<locals>.piecefuncgenc                s   g | ]} |qS r0   r0   )r;   r<   )rG   r0   r1   r?   h   s    z+_bspline_piecefunctions.<locals>.<listcomp>g      g      )_splinefunc_cacheKeyErrorrA   appendr   )r>   r8   lastZ
startbound	condfuncsr7   funclistr0   )r@   r=   r>   rG   r1   _bspline_piecefunctions+   s(    
rN   c                s8   t t|   t|\}} fdd|D }t ||S )zyB-spline basis function of order n.

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

    c                s   g | ]}| qS r0   r0   )r;   func)axr0   r1   r?   z   s    zbspline.<locals>.<listcomp>)absr   rN   r   )r2   nrM   rL   Zcondlistr0   )rP   r1   r   o   s    c             C   s6   |d d }dt dt |  t| d  d |  S )a:  Gaussian approximation to B-spline basis function of order n.

    Parameters
    ----------
    n : int
        The order of the spline. Must be nonnegative, i.e., n >= 0

    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
   r   g      (@r6   )r   r   r   )r2   rR   Zsignsqr0   r0   r1   r   ~   s    c             C   s   t t| }t|}t|d}|j rJ|| }dd|d  d|   ||< | t|d@ }|j r~|| }d	d| d  ||< |S )
zeA cubic B-spline.

    This is a special case of `bspline`, and equivalent to ``bspline(x, 3)``.
    r   g       @   g      ?r6      gUUUUUU?g      ?gUUUUUU?)rQ   r   r   r   any)r2   rP   rB   cond1ax1cond2ax2r0   r0   r1   r      s    
c             C   sv   t t| }t|}t|d}|j r>|| }d|d  ||< | t|d@ }|j rr|| }|d d d ||< |S )ziA quadratic B-spline.

    This is a special case of `bspline`, and equivalent to ``bspline(x, 2)``.
    g      ?g      ?r6   g      ?g       @)rQ   r   r   r   rU   )r2   rP   rB   rV   rW   rX   rY   r0   r0   r1   r      s    
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   `      rS      0   )r   r   )ZlamxiZomegrhor0   r0   r1   _coeff_smooth   s
    $,r`   c             C   s.   |t | ||   t || d   t| d S )Nr   )r   r   )r<   csr_   omegar0   r0   r1   _hc   s    "rd   c             C   s   || d||   d||   dd| | t d|   |d   }d||  d||   t| }t| }|||  t || |t||    S )Nr   r6      )r   r	   rQ   r   )r<   rb   r_   rc   Zc0gammaZakr0   r0   r1   _hs   s
    & rg   c       
      C   s
  t |\}}dd| t|  ||  }t| }t|f| jj}t|}td|||| d  tj	t|d ||||   |d< td|||| d  td|||| d   tj	t|d ||||   |d< xRt
d|D ]D}|| |  d| t| ||d    || ||d    ||< qW t|f| jj}	tj	t||||t|d ||| | d d d  |	|d < tj	t|d |||t|d ||| | d d d  |	|d < xZt
|d ddD ]F}|||  d| t| |	|d    || |	|d    |	|< qW |	S )	Nr   r6   r   rS   ra   ra   ra   ra   )r`   r   lenr
   r)   r*   r   rd   r   reducerA   rg   )
signallambr_   rc   rb   KZypr<   rR   yr0   r0   r1   _cubic_smooth_coeff   s*    "*"&,,& rn   c             C   s   dt d }t| }t|f| jj}|t| }| d |tj||    |d< x.td|D ] }| | |||d    ||< qZW t|f| j}||d  ||d   ||d < x4t|d ddD ] }|||d  ||   ||< qW |d S )	Nr6   rS   r   r   g      @ra   ra   )	r   rh   r
   r)   r*   r   r   ri   rA   )rj   zirl   ypluspowersr<   outputr0   r0   r1   _cubic_coeff   s       rt   c             C   s   ddt d  }t| }t|f| jj}|t| }| d |tj||    |d< x.td|D ] }| | |||d    ||< q^W t|f| jj}||d  ||d   ||d < x4t|d dd	D ] }|||d  ||   ||< qW |d S )
NrS   r6   g       @r   r   g       @ra   ra   )	r   rh   r
   r)   r*   r   r   ri   rA   )rj   rp   rl   rq   rr   r<   rs   r0   r0   r1   _quadratic_coeff   s       rv           c             C   s   |dkrt | |S t| S dS )aM  
    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.

    g        N)rn   rt   )rj   rk   r0   r0   r1   r      s    
c             C   s   |dkrt dnt| S dS )a  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 matplotlib.pyplot as plt
    >>> from scipy.signal import qspline1d, qspline1d_eval
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += np.random.randn(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()

    g        z.Smoothing quadratic splines not supported yet.N)
ValueErrorrv   )rj   rk   r0   r0   r1   r!   $  s    +
      ?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 jt	d }
x@t
dD ]4}|
| }|jd|d }|	| | t||  7 }	qW |	||< |S )ax  Evaluate a 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.

    )r)   r   r   r6   re   )r   r:   r   r)   sizerh   r"   r   r+   intrA   clipr   )cjnewxdxx0rB   NrV   rX   cond3resultjlowerithisjindjr0   r0   r1   r"   U  s*    


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 jtd }
x@tdD ]4}|
| }|j	d|d }|	| | t
||  7 }	qW |	||< |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 matplotlib.pyplot as plt
    >>> from scipy.signal import qspline1d, qspline1d_eval
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += np.random.randn(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   r6   g      ?rS   )r   r   rz   rh   r#   r   r+   r{   rA   r|   r   )r}   r~   r   r   rB   r   rV   rX   r   r   r   r   r   r   r0   r0   r1   r#   y  s*    2


N)r$   )rw   )rw   )ry   r   )ry   r   )/Znumpyr   r   r   r   r   r   r   r	   r
   r   r   Znumpy.core.umathr   r   r   r   r   r   r   r   r   Zspliner   r   Zscipy.specialr   Zscipy._lib._utilr   __all__r   rH   rN   r   r   r   r   r`   rd   rg   rn   rt   rv   r    r!   r"   r#   r0   r0   r0   r1   <module>   s.   4,

D

1
$