
     h6                     l    d dl Zd dlmZ d dlmZ d dlmZ g dZd Z	d Z
dd	ZddZd ZddZddZdS )    N)eigcomb)convolve)daubqmfcascademorletrickermorlet2cwtc           	          t           j        } dk     rt          d           dk    r$d |d          z  }t          j        ||g          S  dk    r@ |d          dz  } |d          }|t          j        d|z   d|z   d|z
  d|z
  g          z  S  dk    rd |d          z  }d |d	|z             d
z  z   d |d	           ||d	z
            z   z  d
z  z
  }t          j        |          } |d          dz  }t          j        d|z
  d|z
  z            }t          j        ||z            }dt          j        |          z  }	||z  t          j        |d|z  |	z
  d|z  d|	z  z
  dz   |d|	z  z
  dz   d|	z
  dg          z  S  dk     rP dk     r9 fdt                     D             ddd         }
t          j        |
          }n; fdt                     D             ddd         }
t          j        |
          dz  }t          j        ddg           z  }t          j        dg          }t           dz
            D ]K}||         }d |||dz
  z            z  }dd|z  z
  }||z   }t          |          dk     r||z
  }|d| gz  }L|t          j        |          z  }|t          j
        |          z   |d          z  }|j        ddd         S t          d          )aT  
    The coefficients for the FIR low-pass filter producing Daubechies wavelets.

    p>=1 gives the order of the zero at f=1/2.
    There are 2p filter coefficients.

    Parameters
    ----------
    p : int
        Order of the zero at f=1/2, can have values from 1 to 34.

    Returns
    -------
    daub : ndarray
        Return

       zp must be at least 1.            
   g      ?                    ?#   c                 @    g | ]}t          d z
  |z   |d           S )r   exactr   .0kps     R/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/scipy/signal/_wavelets.py
<listcomp>zdaub.<locals>.<listcomp>2   s0    ???a!eai!,,,???    Nc                 L    g | ] }t          d z
  |z   |d           d|z  z  !S )r   r   g      @r   r   s     r    r!   zdaub.<locals>.<listcomp>5   sI     $ $ $ a!eai!,,,sAv5 $ $ $r"      z<Polynomial factorization does not work well for p too large.)npsqrt
ValueErrorarrayconjrealrangerootspoly1dabssumc)r   r'   r1   ftmpz1z1cd0a0a1Pyjqr   yvalpartconsts   `                r    r   r   	   sS   $ 7D1uu0111AvvQKxA	
aDGGaKDGG28QUAE1q5!a%89999	
a4488m44S>>A%%dd2hhcBh.G(H1(LLgbkkDGGaKWa"fS)**WR#X_2v"a"frk1r6AF?Q3F"$q2v+/1r61"> ? ? ? 	?	
Rr66????eAhh???"EA!BB$ $ $ $(($ $ $$(DbD*A!qB Iq!fq IqcNNq1u 	 	Aa5DttDD1H-...DDLEBB1}}T\QHAA

Nq		MDDGG#s44R4y 1 2 2 	2r"   c                     t          |           dz
  }d t          |dz             D             }| ddd         t          j        |          z  S )z
    Return high-pass qmf filter from low-pass

    Parameters
    ----------
    hk : array_like
        Coefficients of high-pass filter.

    Returns
    -------
    array_like
        High-pass filter coefficients.

    r   c                 *    g | ]}d dd|dz           S )r   r#   r   r   r    )r   r   s     r    r!   zqmf.<locals>.<listcomp>^   s'    777QbMM!a% 777r"   Nr#   )lenr,   r&   r)   )hkNasgns      r    r   r   N   sM     	B!A77%A,,777Dddd8bhtnn$$r"      c                 n   t          |           dz
  }|dt          j        |dz             z
  k    rt          d          |dk     rt          d          t          j        d|d|f         \  }}t          j        d          }t          j        | df         }t          |           }t          j        |df         }t          j        d|z  |z
  d|dz             }	t          j        d|z  |z
  dz   d|dz             }
t          j	        dd||fd	          }t          j
        ||	d          |d
<   t          j
        ||
d          |d<   t          j
        ||	d          |d<   t          j
        ||
d          |d<   ||z  }t          j        d|d|z  z  t                    d|z  z  }d|z  }d|z  }t          |d
                   \  }}t          j        t          j        |dz
                      }t          j        |dd|f                   }t          j        |          }|dk     r| }| }d||z  i}t          j        |d         |d                   |d<   d|z  }|d         |dd|<   |d         |d|dz
  z  d|<   t          j        |d         |d                   |dd|<   t          j        |d         |d                   |d|dz
  z  d|<   dgt'          d|dz             D ]}fddD             }d||z
  z  }|D ]}d}t'          |          D ]}||         dk    r|d|dz
  |z
  z  z  }||dd                  }t)          |d                   }t          j        |d|f         |          }|||<   ||||z  d|<   t          j        |d|f         |          |||z  d|<   ||||fS )a  
    Return (x, phi, psi) at dyadic points ``K/2**J`` from filter coefficients.

    Parameters
    ----------
    hk : array_like
        Coefficients of low-pass filter.
    J : int, optional
        Values will be computed at grid points ``K/2**J``. Default is 7.

    Returns
    -------
    x : ndarray
        The dyadic points ``K/2**J`` for ``K=0...N * (2**J)-1`` where
        ``len(hk) = len(gk) = N+1``.
    phi : ndarray
        The scaling function ``phi(x)`` at `x`:
        ``phi(x) = sum(hk * phi(2x-k))``, where k is from 0 to N.
    psi : ndarray, optional
        The wavelet function ``psi(x)`` at `x`:
        ``phi(x) = sum(gk * phi(2x-k))``, where k is from 0 to N.
        `psi` is only returned if `gk` is not None.

    Notes
    -----
    The algorithm uses the vector cascade algorithm described by Strang and
    Nguyen in "Wavelets and Filter Banks".  It builds a dictionary of values
    and slices for quick reuse.  Then inserts vectors into final vector at the
    end.

    r      zToo many levels.zToo few levels.Nr   r   r#   d)r   r   rA   )r   r   )r   r   dtype01c                 (    g | ]}D ]	}d ||fz  
S )z%d%srB   )r   xxyyprevkeyss      r    r!   zcascade.<locals>.<listcomp>   s/    IIIII"6RH$IIIIr"   )rC   r&   log2r(   ogridr'   r_r   clipemptytakearangefloatr   argminabsoluter+   r0   dotr,   int)rD   JrE   nnkks2thkgktgkindx1indx2mxphipsilamvindsmbitdicsteplevelnewkeysfackeynumpospastphiiitemprR   s                                 @r    r	   r	   b   s   @ 	B!A	BQ+,,,	A*+++ Xbqb"1"fFB	B
%A,C	RB
%A,CGAFRKQU++EGAFRK!ORQ//E
!Q1s##Agc5!$$AdGgc5!$$AdGgc5!$$AdGgc5!$$AdGGA 		!Q!q&\///16:A
a%C
a%C 4\\FC
)BKa((
)
)C 	!!!S&	A 
B	AvvBS1r6]F&4&+..F3K6D+C$K &sCq1u&4&+..C$K "qws < <Cq1u
 uHq!a%  IIIIvIIIAI 	= 	=CCU|| 4 4s8s??A%!)c/23CSWoGSVB6!ArE(G,,DF3K#'Cc	4 #%6!ArE(G#<#<Cc	4  c3;r"         @      ?Tc                 @   t          j        | dz  t           j        z  |dz  t           j        z  |           }t          j        d|z  |z            }|r|t          j        d|dz  z            z  }|t          j        d|dz  z            t           j        dz  z  z  }|S )a)  
    Complex Morlet wavelet.

    Parameters
    ----------
    M : int
        Length of the wavelet.
    w : float, optional
        Omega0. Default is 5
    s : float, optional
        Scaling factor, windowed from ``-s*2*pi`` to ``+s*2*pi``. Default is 1.
    complete : bool, optional
        Whether to use the complete or the standard version.

    Returns
    -------
    morlet : (M,) ndarray

    See Also
    --------
    morlet2 : Implementation of Morlet wavelet, compatible with `cwt`.
    scipy.signal.gausspulse

    Notes
    -----
    The standard version::

        pi**-0.25 * exp(1j*w*x) * exp(-0.5*(x**2))

    This commonly used wavelet is often referred to simply as the
    Morlet wavelet.  Note that this simplified version can cause
    admissibility problems at low values of `w`.

    The complete version::

        pi**-0.25 * (exp(1j*w*x) - exp(-0.5*(w**2))) * exp(-0.5*(x**2))

    This version has a correction
    term to improve admissibility. For `w` greater than 5, the
    correction term is negligible.

    Note that the energy of the return wavelet is not normalised
    according to `s`.

    The fundamental frequency of this wavelet in Hz is given
    by ``f = 2*s*w*r / M`` where `r` is the sampling rate.

    Note: This function was created before `cwt` and is not compatible
    with it.

    Examples
    --------
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> M = 100
    >>> s = 4.0
    >>> w = 2.0
    >>> wavelet = signal.morlet(M, s, w)
    >>> plt.plot(wavelet)
    >>> plt.show()

    r   r               п)r&   linspacepiexp)Mwscompleteri   outputs         r    r
   r
      s    @ 	QBFRUNAEBEM155AVBFQJF ("&A'''
bfTQT]##been44FMr"   c                     dt          j        d|z            t           j        dz  z  z  }|dz  }t          j        d|           | dz
  dz  z
  }|dz  }d||z  z
  }t          j        | d|z  z            }||z  |z  }|S )a  
    Return a Ricker wavelet, also known as the "Mexican hat wavelet".

    It models the function:

        ``A * (1 - (x/a)**2) * exp(-0.5*(x/a)**2)``,

    where ``A = 2/(sqrt(3*a)*(pi**0.25))``.

    Parameters
    ----------
    points : int
        Number of points in `vector`.
        Will be centered around 0.
    a : scalar
        Width parameter of the wavelet.

    Returns
    -------
    vector : (N,) ndarray
        Array of length `points` in shape of ricker curve.

    Examples
    --------
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> points = 100
    >>> a = 4.0
    >>> vec2 = signal.ricker(points, a)
    >>> print(len(vec2))
    100
    >>> plt.plot(vec2)
    >>> plt.show()

    r   r   g      ?r   r|   r   )r&   r'   r   rY   r   )	pointsaAwsqvecxsqmodgausstotals	            r    r   r     s    J 	
RWQU^^rud{+,A
Q$C
)Av

&3,!!3
3C
q&CsSy=CFC41s7#$$EGeOELr"      c                 
   t          j        d|           | dz
  dz  z
  }||z  }t          j        d|z  |z            t          j        d|dz  z            z  t           j        dz  z  }t          j        d|z            |z  }|S )aR  
    Complex Morlet wavelet, designed to work with `cwt`.

    Returns the complete version of morlet wavelet, normalised
    according to `s`::

        exp(1j*w*x/s) * exp(-0.5*(x/s)**2) * pi**(-0.25) * sqrt(1/s)

    Parameters
    ----------
    M : int
        Length of the wavelet.
    s : float
        Width parameter of the wavelet.
    w : float, optional
        Omega0. Default is 5

    Returns
    -------
    morlet : (M,) ndarray

    See Also
    --------
    morlet : Implementation of Morlet wavelet, incompatible with `cwt`

    Notes
    -----

    .. versionadded:: 1.4.0

    This function was designed to work with `cwt`. Because `morlet2`
    returns an array of complex numbers, the `dtype` argument of `cwt`
    should be set to `complex128` for best results.

    Note the difference in implementation with `morlet`.
    The fundamental frequency of this wavelet in Hz is given by::

        f = w*fs / (2*s*np.pi)

    where ``fs`` is the sampling rate and `s` is the wavelet width parameter.
    Similarly we can get the wavelet width parameter at ``f``::

        s = w*fs / (2*f*np.pi)

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> M = 100
    >>> s = 4.0
    >>> w = 2.0
    >>> wavelet = signal.morlet2(M, s, w)
    >>> plt.plot(abs(wavelet))
    >>> plt.show()

    This example shows basic use of `morlet2` with `cwt` in time-frequency
    analysis:

    >>> t, dt = np.linspace(0, 1, 200, retstep=True)
    >>> fs = 1/dt
    >>> w = 6.
    >>> sig = np.cos(2*np.pi*(50 + 10*t)*t) + np.sin(40*np.pi*t)
    >>> freq = np.linspace(1, fs/2, 100)
    >>> widths = w*fs / (2*freq*np.pi)
    >>> cwtm = signal.cwt(sig, signal.morlet2, widths, w=w)
    >>> plt.pcolormesh(t, freq, np.abs(cwtm), cmap='viridis', shading='gouraud')
    >>> plt.show()

    r   r|   r   r   r~   r   r   )r&   rY   r   r   r'   )r   r   r   ri   waveletr   s         r    r   r   F  s~    P 		!Q1s7a-'A	AAfR!VaZ  26$A+#6#66GGWQqS\\G#FMr"   c                    |Gt          j         |d|d         fi |          j        j        dv rt           j        }nt           j        }t          j        t          |          t          |           f|          }t          |          D ]e\  }}t          j	        d|z  t          |           g          }t          j
         |||fi |ddd                   }	t          | |	d	          ||<   f|S )
a	  
    Continuous wavelet transform.

    Performs a continuous wavelet transform on `data`,
    using the `wavelet` function. A CWT performs a convolution
    with `data` using the `wavelet` function, which is characterized
    by a width parameter and length parameter. The `wavelet` function
    is allowed to be complex.

    Parameters
    ----------
    data : (N,) ndarray
        data on which to perform the transform.
    wavelet : function
        Wavelet function, which should take 2 arguments.
        The first argument is the number of points that the returned vector
        will have (len(wavelet(length,width)) == length).
        The second is a width parameter, defining the size of the wavelet
        (e.g. standard deviation of a gaussian). See `ricker`, which
        satisfies these requirements.
    widths : (M,) sequence
        Widths to use for transform.
    dtype : data-type, optional
        The desired data type of output. Defaults to ``float64`` if the
        output of `wavelet` is real and ``complex128`` if it is complex.

        .. versionadded:: 1.4.0

    kwargs
        Keyword arguments passed to wavelet function.

        .. versionadded:: 1.4.0

    Returns
    -------
    cwt: (M, N) ndarray
        Will have shape of (len(widths), len(data)).

    Notes
    -----

    .. versionadded:: 1.4.0

    For non-symmetric, complex-valued wavelets, the input signal is convolved
    with the time-reversed complex-conjugate of the wavelet data [1].

    ::

        length = min(10 * width[ii], len(data))
        cwt[ii,:] = signal.convolve(data, np.conj(wavelet(length, width[ii],
                                        **kwargs))[::-1], mode='same')

    References
    ----------
    .. [1] S. Mallat, "A Wavelet Tour of Signal Processing (3rd Edition)",
        Academic Press, 2009.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt
    >>> t = np.linspace(-1, 1, 200, endpoint=False)
    >>> sig  = np.cos(2 * np.pi * 7 * t) + signal.gausspulse(t - 0.4, fc=2)
    >>> widths = np.arange(1, 31)
    >>> cwtmatr = signal.cwt(sig, signal.ricker, widths)

    .. note:: For cwt matrix plotting it is advisable to flip the y-axis

    >>> cwtmatr_yflip = np.flipud(cwtmatr)
    >>> plt.imshow(cwtmatr_yflip, extent=[-1, 1, 1, 31], cmap='PRGn', aspect='auto',
    ...            vmax=abs(cwtmatr).max(), vmin=-abs(cwtmatr).max())
    >>> plt.show()
    Nr   r   FDGrK   r   r#   same)mode)r&   asarrayrL   char
complex128float64rW   rC   	enumerateminr*   r   )
datar   widthsrL   kwargsr   rn   widthrE   wavelet_datas
             r    r   r     s    X }:gga55f5566<AUJJMEEJEXs6{{CII.e<<<F'' @ @
UFBJD		*++wwwq%::6::44R4@AAt\???sMr"   )rG   )r{   r|   T)r   )N)numpyr&   scipy.linalgr   scipy.specialr   scipy.signalr   __all__r   r   r	   r
   r   r   r   rB   r"   r    <module>r      s                    ! ! ! ! ! !
J
J
JB2 B2 B2J% % %(g g g gTH H H HV, , ,^L L L L^W W W W W Wr"   