
    tKgs=                         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Zi Zd Zd	 Zd
 Z d Z!d Z"d Z#d Z$d Z%d Z&ddZ'ddZ(ddZ)ddZ*y)    )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_evalc                    | j                   j                  }t        g dd      dz  }|dv rp| j                  d      } t	        | j
                  |      }t	        | j                  |      }t        |||      }t        |||      }|d|z  z   j                  |      }|S |dv r,t	        | |      }t        |||      }|j                  |      }|S t        d      )	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)	Iinlmbdaintypehcolckrckioutroutiouts	            Z/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/scipy/signal/_bsplines.pyr   r      s    L YY^^F#&,Djjo%(%(T4(T4(b4i''/ J 
:	U#sD$'jj  J 344    c                     t        |       } |dz   dz  }dt        dt        z  |z        z  t        | dz   dz  |z        z  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nsignsqs      r7   r   r   J   sL    Z 	
A!et^FtAFVO$$sAF7Q;+?'@@@r8   c                     t        | t              } t        j                  g dd      } ||       }d|| dk  | dkD  z  <   |S )Nr(   )r   r   r:   Fextrapolater   r@   r:   )r   floatr   basis_elementr;   br6   s      r7   _cubicrH   |   sF    A/UCA
A$CCRAEJr8   c                     t        t        | t                    } t        j                  g dd      } ||       }d|| dk  | dkD  z  <   |S )Nr?   )      g      g      ?      ?FrB   r   rJ   rK   )absr   rD   r   rE   rF   s      r7   
_quadraticrM      sK    GAU#$A4%HA
A$C"#CTa#gJr8   c           
         dd| z  z
  d| z  t        dd| z  z         z  z   }t        t        d| z  dz
        t        |            }d| z  dz
  t        |      z
  d| z  z  }|t        d| z  d| z  t        dd| z  z         z  z   |z        z  }||fS )Nr   `            0   )r   r   )lamxiomegrhos       r7   _coeff_smoothrX      s    	
R#XS4C#I#66	6B4c	A&R1D8a<$r("rCx
0C
b3hcDS3Y,?!??2EF
FC9r8   c                 h    |t        |      z  || z  z  t        || dz   z        z  t        | d      z  S )Nr   rA   )r   r   )kcsrW   omegas       r7   _hcr]      s;    UOsax(3uA+??ArN r8   c                    ||z  d||z  z   z  d||z  z
  z  dd|z  |z  t        d|z        z  z
  |dz  z   z  }d||z  z
  d||z  z   z  t        |      z  }t        |       }|||z  z  t        ||z        |t        ||z        z  z   z  S )Nr   r:      )r   r   rL   r   )rZ   r[   rW   r\   c0gammaaks          r7   _hsrc      s    
r'Qs]
#q39}
5q3w}s1u9~--q8:Bs]q39}-E
:E	QBr	>S_us52:/FFGGr8   c           	         t        |      \  }}dd|z  t        |      z  z
  ||z  z   }t        |       }t        |      }t	        d|||      | d   z  t        j                  t	        |dz   |||      | z        z   }t	        d|||      | d   z  t	        d|||      | d   z  z   t        j                  t	        |dz   |||      | z        z   }t        |t        dd|z  t        |      z  ||z  f   t        ||f         }	|	j                  dd      }	t        |dddd|z  t        |      z  ||z  f   }
|
j                  dd      }
t        |
| dd  |	      \  }}t        |||f   }t        j                  t        ||||      t        |dz   |||      z   | d d d   z        }t        j                  t        |dz
  |||      t        |dz   |||      z   | d d d   z        }t        |t        dd|z  t        |      z  ||z  f   t        ||f         }	|	j                  dd      }	t        |
|dd d   |	      \  }}t        |d d d   ||f   }|S )Nr   r:   r   r@   rA   zi)rX   r   lenr
   r]   r   reducer   r   reshaper   rc   )signallambrW   r\   r[   KrZ   zi_2zi_1rf   sosyp_ys                 r7   _cubic_smooth_coeffrt      s   t$JC	
QWs5z!	!C#I	-BFAq	A2sE"VAY.JJs1q5"c51F:;<D2sE"VAY.2sE"VAY./JJs1q5"c51F:;<D 
R28c%j0#);<btn	MB	Ar	B
RAq"s(SZ/s:
;C
++a
CC+EB	D$N	B ::s1b#u-1q5"c5125;DbD\B CD::s1q5"c511q5"c5125;DbD\B CD 
R28c%j0#);<btn	MB	Ar	B326r6
r*DAq
1TrT7D$AHr8   c           
      `   dt        d      z   }t        |       }|t        |      z  }|dk(  r7| d   |t        j                  || z        z  z   }||dz
  z  |z  }t        |      S t        dt        d| f   t        t        j                  || z                    }t        d      }t        d| f   }t        ||| |      \  }}	||dz
  z  ||dz
     z  }
t        | t        d| f   t        |
            }t        | g      }t        |||dd d   |      \  }}	t        |d d d   |
f   }|dz  S )Nr@   rQ   r   r   re   rA   r$   r   rh   r
   r   ri   r   r   r   r	   r   r   rk   rf   rm   powersyplusoutputstaterG   arr   out_lasts              r7   _cubic_coeffr~      sH   	d1gBFA6!9_FAvq	BFVO!<<<rAv&&!! Ar!bS&z:cjj&.I#JKEQA
1rc6
Aq!V.HE1 R!V}uQU|+HRCAsFZ%9:E"A1eBFFm6IFAtth&'FC<r8   c           
      f   ddt        d      z  z   }t        |       }|t        |      z  }|dk(  r7| d   |t        j                  || z        z  z   }||dz
  z  |z  }t        |      S t        dt        d| f   t        t        j                  || z                    }t        d      }t        d| f   }t        ||| |      \  }}	||dz
  z  ||dz
     z  }
t        | t        d| f   t        |
            }t        | g      }t        |||dd d   |      \  }}	t        |d d d   |
f   }|d	z  S )
Nrg   r:   g       @r   r   re   r@   rA   g       @rv   rw   s              r7   _quadratic_coeffr      sM   	a$s)m	BFA6!9_FAvq	BFVO!<<<rAv&&!! Ar!bS&z:cjj&.I#JKEQA
1rc6
Aq!V.HE1 R!V}uQU|+HRCAsFZ%9:E"A1eBFFm6IFAtth&'FC<r8   c                 :    |dk7  rt        | |      S t        |       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()

            )rt   r~   rk   rl   s     r7   r   r     s$    X s{"6400F##r8   c                 8    |dk7  rt        d      t        |       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()

    r   z.Smoothing quadratic splines not supported yet.)
ValueErrorr   r   s     r7   r   r   A  s#    Z s{IJJ''r8   c                 P   t        |      |z
  t        |      z  }t        || j                        }|j                  dk(  r|S t        |       }|dk  }||dz
  kD  }||z   }t        | ||          ||<   t        | d|dz
  z  ||   z
        ||<   ||   }|j                  dk(  r|S t        || j                        }	t        |dz
        j                  t              dz   }
t        d      D ]3  }|
|z   }|j                  d|dz
        }|	| |   t        ||z
        z  z  }	5 |	||<   |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   rD   r   r(   sizerh   r   r   r*   intrangecliprH   cjnewxdxx0resNcond1cond2cond3resultjlowerithisjindjs                 r7   r   r   t  s;   d DMB%)+D
T
*C
xx1}
BA1HEAENEemET%[L1CJAQK$u+$=>CJ;DyyA~
BHH-F4!8_##C(1,F1X
zz!QU#"T(VD5L111  CJJr8   c                    t        |      |z
  |z  }t        |      }|j                  dk(  r|S t        |       }|dk  }||dz
  kD  }||z   }t	        | ||          ||<   t	        | d|dz
  z  ||   z
        ||<   ||   }|j                  dk(  r|S t        |      }	t        |dz
        j                  t              dz   }
t        d      D ]3  }|
|z   }|j                  d|dz
        }|	| |   t        ||z
        z  z  }	5 |	||<   |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:   rK   rQ   )r   r   r   rh   r    r   r*   r   r   r   rM   r   s                 r7   r    r      s,   h DMB"$D
T
C
xx1}
BA1HEAENEemET%[L1CJAQK$u+$=>CJ;DyyA~
F4#:%%c*Q.F1X
zz!QU#"T(Zu555  CJJr8   N)g      @)r   )r"   r   )+numpyr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   _spliner   r   _signaltoolsr   r   r   scipy.interpolater   __all__r   _splinefunc_cacher   rH   rM   rX   r]   rc   rt   r~   r   r   r   r   r     r8   r7   <module>r      s   F F F F F
 ) 3 3 %I5p  /Ad
H'TDD/$d0(fGTIr8   