U
    `»{j>
  ã                   @   s8   d Z ddlZddlmZ ddlZddd„Zddd„ZdS )	zBaseline estimation algorithms.é    Nc                 C   sÖ   |dkrd}|dkrd}|dkr$d}|d }t  |¡}t t| ƒ ¡ d| ¡}t  d|| j¡}|  ¡ }t  	||¡}	t
 |	¡}
t|ƒD ]L}t  |
| ¡}t
 || ¡t
 |¡ |k r´ qÒ|}t  |	|¡}t  | |¡} q„|S )a  
    Computes the baseline of a given data.

    Iteratively performs a polynomial fitting in the data to detect its
    baseline. At every iteration, the fitting weights on the regions with
    peaks are reduced to identify the baseline only.

    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int (default: 3)
        Degree of the polynomial that will estimate the data baseline. A low
        degree may fail to detect all the baseline present, while a high
        degree may make the data too oscillatory, especially at the edges.
    max_it : int (default: 100)
        Maximum number of iterations to perform.
    tol : float (default: 1e-3)
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration. The iteration
        procedure will stop when the difference between them is lower than
        *tol*.

    Returns
    -------
    ndarray
        Array with the baseline amplitude for every original point in *y*
    Né   éd   gü©ñÒMbP?é   g      ð?g        )ÚnpZonesÚmathÚpowÚabsÚmaxZlinspaceÚsizeÚcopyÚvanderÚLAZpinvÚrangeÚdotZnormÚminimum)ÚyÚdegÚmax_itÚtolÚorderZcoeffsZcondÚxÚbaser   Zvander_pinvÚ_Z
coeffs_new© r   úQ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/peakutils/baseline.pyÚbaseline   s*       

r   c                 C   s   |   ¡ t|   ¡ |  |||ƒ S )ad  
    Computes the upper envelope of a given data.
    It is implemented in terms of the `baseline` function.
    
    Parameters
    ----------
    y : ndarray
        Data to detect the baseline.
    deg : int
        Degree of the polynomial that will estimate the envelope.
    max_it : int
        Maximum number of iterations to perform.
    tol : float
        Tolerance to use when comparing the difference between the current
        fit coefficients and the ones from the last iteration.

    Returns
    -------
    ndarray
        Array with the envelope amplitude for every original point in *y*
    )r	   r   )r   r   r   r   r   r   r   ÚenvelopeA   s    r   )NNN)NNN)	Ú__doc__Únumpyr   Zscipy.linalgZlinalgr   r   r   r   r   r   r   r   Ú<module>   s
   
9