ó
Ë½÷Xc           @` s*  d  d l  m Z m Z m Z d  d l Z 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 d  d l m Z d d d d d	 d
 g Z d d „ Z d d „ Z d d d d e e d „ Z d d  e d „ Z d e d „ Z d  d „ Z d „  Z d e! d „ Z" d S(   i    (   t   divisiont   print_functiont   absolute_importN(   t   asarrayt   zerost   placet   nant   modt   pit   extractt   logt   sqrtt   expt   cost   sint   polyvalt   polyint(   t   string_typest   sawtootht   squaret
   gausspulset   chirpt
   sweep_polyt   unit_impulsei   c         C` se  t  |  ƒ t  | ƒ }  } t  | |  |  ƒ } t  |  | | ƒ }  |  j j d k rb |  j j } n d } t |  j | ƒ } | d k | d k  B} t | | t ƒ t |  d t ƒ } d | | | d t k  @} t	 | | ƒ } t	 | | ƒ }	 t | | | t |	 d ƒ d | d | @}
 t	 |
 | ƒ } t	 |
 | ƒ }	 t | |
 t |	 d | t d |	 ƒ | S(   s™  
    Return a periodic sawtooth or triangle waveform.

    The sawtooth waveform has a period ``2*pi``, rises from -1 to 1 on the
    interval 0 to ``width*2*pi``, then drops from 1 to -1 on the interval
    ``width*2*pi`` to ``2*pi``. `width` must be in the interval [0, 1].

    Note that this is not band-limited.  It produces an infinite number
    of harmonics, which are aliased back and forth across the frequency
    spectrum.

    Parameters
    ----------
    t : array_like
        Time.
    width : array_like, optional
        Width of the rising ramp as a proportion of the total cycle.
        Default is 1, producing a rising ramp, while 0 produces a falling
        ramp.  `width` = 0.5 produces a triangle wave.
        If an array, causes wave shape to change over time, and must be the
        same length as t.

    Returns
    -------
    y : ndarray
        Output array containing the sawtooth waveform.

    Examples
    --------
    A 5 Hz waveform sampled at 500 Hz for 1 second:

    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt
    >>> t = np.linspace(0, 1, 500)
    >>> plt.plot(t, signal.sawtooth(2 * np.pi * 5 * t))

    t   fFdDt   di   i    i   (   R   (
   R   t   dtypet   charR   t   shapeR   R   R   R   R	   (   t   tt   widtht   wt   ytypet   yt   mask1t   tmodt   mask2t   tsubt   wsubt   mask3(    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR      s&    &(g      à?c   	      C` s  t  |  ƒ t  | ƒ }  } t  | |  |  ƒ } t  |  | | ƒ }  |  j j d k rb |  j j } n d } t |  j | ƒ } | d k | d k  B} t | | t ƒ t |  d t ƒ } d | | | d t k  @} t | | d ƒ d | d | @} t | | d ƒ | S(   s'  
    Return a periodic square-wave waveform.

    The square wave has a period ``2*pi``, has value +1 from 0 to
    ``2*pi*duty`` and -1 from ``2*pi*duty`` to ``2*pi``. `duty` must be in
    the interval [0,1].

    Note that this is not band-limited.  It produces an infinite number
    of harmonics, which are aliased back and forth across the frequency
    spectrum.

    Parameters
    ----------
    t : array_like
        The input time array.
    duty : array_like, optional
        Duty cycle.  Default is 0.5 (50% duty cycle).
        If an array, causes wave shape to change over time, and must be the
        same length as t.

    Returns
    -------
    y : ndarray
        Output array containing the square waveform.

    Examples
    --------
    A 5 Hz waveform sampled at 500 Hz for 1 second:

    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt
    >>> t = np.linspace(0, 1, 500, endpoint=False)
    >>> plt.plot(t, signal.square(2 * np.pi * 5 * t))
    >>> plt.ylim(-2, 2)

    A pulse-width modulated sine wave:

    >>> plt.figure()
    >>> sig = np.sin(2 * np.pi * t)
    >>> pwm = signal.square(2 * np.pi * 30 * t, duty=(sig + 1)/2)
    >>> plt.subplot(2, 1, 1)
    >>> plt.plot(t, sig)
    >>> plt.subplot(2, 1, 2)
    >>> plt.plot(t, pwm)
    >>> plt.ylim(-1.5, 1.5)

    R   R   i   i    i   iÿÿÿÿ(   s   fFdD(	   R   R   R   R   R   R   R   R   R   (	   R   t   dutyR   R    R!   R"   R#   R$   R'   (    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR   [   s    0iè  iúÿÿÿiÄÿÿÿc         C` s¨  | d k  r t  d | ƒ ‚ n  | d k r> t  d | ƒ ‚ n  | d k r] t  d | ƒ ‚ n  t d | d ƒ } t | | d d t | ƒ } t |  t ƒ rþ |  d	 k rï | d k rÇ t  d
 ƒ ‚ n  t d | d ƒ }	 t t |	 ƒ | ƒ St  d ƒ ‚ n  t | |  |  ƒ }
 |
 t d t | |  ƒ } |
 t	 d t | |  ƒ } | r]| r]| S| rt| rt| |
 f S| r‹| r‹| | f S| r¤| r¤| | |
 f Sd S(   sD  
    Return a Gaussian modulated sinusoid:

        ``exp(-a t^2) exp(1j*2*pi*fc*t).``

    If `retquad` is True, then return the real and imaginary parts
    (in-phase and quadrature).
    If `retenv` is True, then return the envelope (unmodulated signal).
    Otherwise, return the real part of the modulated sinusoid.

    Parameters
    ----------
    t : ndarray or the string 'cutoff'
        Input array.
    fc : int, optional
        Center frequency (e.g. Hz).  Default is 1000.
    bw : float, optional
        Fractional bandwidth in frequency domain of pulse (e.g. Hz).
        Default is 0.5.
    bwr : float, optional
        Reference level at which fractional bandwidth is calculated (dB).
        Default is -6.
    tpr : float, optional
        If `t` is 'cutoff', then the function returns the cutoff
        time for when the pulse amplitude falls below `tpr` (in dB).
        Default is -60.
    retquad : bool, optional
        If True, return the quadrature (imaginary) as well as the real part
        of the signal.  Default is False.
    retenv : bool, optional
        If True, return the envelope of the signal.  Default is False.

    Returns
    -------
    yI : ndarray
        Real part of signal.  Always returned.
    yQ : ndarray
        Imaginary part of signal.  Only returned if `retquad` is True.
    yenv : ndarray
        Envelope of signal.  Only returned if `retenv` is True.

    See Also
    --------
    scipy.signal.morlet

    Examples
    --------
    Plot real component, imaginary component, and envelope for a 5 Hz pulse,
    sampled at 100 Hz for 2 seconds:

    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt
    >>> t = np.linspace(-1, 1, 2 * 100, endpoint=False)
    >>> i, q, e = signal.gausspulse(t, fc=5, retquad=True, retenv=True)
    >>> plt.plot(t, i, t, q, t, e, '--')

    i    s'   Center frequency (fc=%.2f) must be >=0.s+   Fractional bandwidth (bw=%.2f) must be > 0.s7   Reference level for bandwidth (bwr=%.2f) must be < 0 dBg      $@g      4@i   g      @t   cutoffs.   Reference level for time cutoff must be < 0 dBs'   If `t` is a string, it must be 'cutoff'N(
   t
   ValueErrort   powR   R
   t
   isinstanceR   R   R   R   R   (   R   t   fct   bwt   bwrt   tprt   retquadt   retenvt   reft   at   treft   yenvt   yIt   yQ(    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR   ¥   s6    ;!

t   linearc         C` s7   t  |  | | | | | ƒ } | t d 9} t | | ƒ S(   s\
  Frequency-swept cosine generator.

    In the following, 'Hz' should be interpreted as 'cycles per unit';
    there is no requirement here that the unit is one second.  The
    important distinction is that the units of rotation are cycles, not
    radians. Likewise, `t` could be a measurement of space instead of time.

    Parameters
    ----------
    t : array_like
        Times at which to evaluate the waveform.
    f0 : float
        Frequency (e.g. Hz) at time t=0.
    t1 : float
        Time at which `f1` is specified.
    f1 : float
        Frequency (e.g. Hz) of the waveform at time `t1`.
    method : {'linear', 'quadratic', 'logarithmic', 'hyperbolic'}, optional
        Kind of frequency sweep.  If not given, `linear` is assumed.  See
        Notes below for more details.
    phi : float, optional
        Phase offset, in degrees. Default is 0.
    vertex_zero : bool, optional
        This parameter is only used when `method` is 'quadratic'.
        It determines whether the vertex of the parabola that is the graph
        of the frequency is at t=0 or t=t1.

    Returns
    -------
    y : ndarray
        A numpy array containing the signal evaluated at `t` with the
        requested time-varying frequency.  More precisely, the function
        returns ``cos(phase + (pi/180)*phi)`` where `phase` is the integral
        (from 0 to `t`) of ``2*pi*f(t)``. ``f(t)`` is defined below.

    See Also
    --------
    sweep_poly

    Notes
    -----
    There are four options for the `method`.  The following formulas give
    the instantaneous frequency (in Hz) of the signal generated by
    `chirp()`.  For convenience, the shorter names shown below may also be
    used.

    linear, lin, li:

        ``f(t) = f0 + (f1 - f0) * t / t1``

    quadratic, quad, q:

        The graph of the frequency f(t) is a parabola through (0, f0) and
        (t1, f1).  By default, the vertex of the parabola is at (0, f0).
        If `vertex_zero` is False, then the vertex is at (t1, f1).  The
        formula is:

        if vertex_zero is True:

            ``f(t) = f0 + (f1 - f0) * t**2 / t1**2``

        else:

            ``f(t) = f1 - (f1 - f0) * (t1 - t)**2 / t1**2``

        To use a more general quadratic function, or an arbitrary
        polynomial, use the function `scipy.signal.waveforms.sweep_poly`.

    logarithmic, log, lo:

        ``f(t) = f0 * (f1/f0)**(t/t1)``

        f0 and f1 must be nonzero and have the same sign.

        This signal is also known as a geometric or exponential chirp.

    hyperbolic, hyp:

        ``f(t) = f0*f1*t1 / ((f0 - f1)*t + f1*t1)``

        f0 and f1 must be nonzero.

    i´   (   t   _chirp_phaseR   R   (   R   t   f0t   t1t   f1t   methodt   phit   vertex_zerot   phase(    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR     s    Uc   	      C` s#  t  |  ƒ }  t | ƒ } t | ƒ } t | ƒ } | d k ro | | | } d t | |  d | |  |  } n°| d k ré | | | d } | r¸ d t | |  | |  d	 d	 } qd t | |  | | |  d	 | d	 d	 } n6| d k rw| | d k rt d ƒ ‚ n  | | k r5d t | |  } q| t | | ƒ } d t | | t | | |  | ƒ d } n¨ | d k r| d k s›| d k rªt d ƒ ‚ n  | | k rËd t | |  } q| | | | } d t | | t t j d |  | ƒ ƒ } n t d | ƒ ‚ | S(   s…   
    Calculate the phase used by chirp_phase to generate its output.

    See `chirp_phase` for a description of the arguments.

    R9   t   lint   lii   g      à?t	   quadratict   quadt   qi   t   logarithmicR
   t   log        sJ   For a logarithmic chirp, f0 and f1 must be nonzero and have the same sign.g      ð?t
   hyperbolict   hypi    s2   For a hyperbolic chirp, f0 and f1 must be nonzero.i   sb   method must be 'linear', 'quadratic', 'logarithmic', or 'hyperbolic', but a value of %r was given.(   R9   RB   RC   (   RD   RE   RF   (   RG   s   logs   lo(   RI   RJ   (   R   t   floatR   R*   R
   R+   t   npt   abs(	   R   R;   R<   R=   R>   R@   t   betaRA   t   sing(    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR:   c  s:    %%1.1
c         C` s+   t  |  | ƒ } | t d 9} t | | ƒ S(   s@  
    Frequency-swept cosine generator, with a time-dependent frequency.

    This function generates a sinusoidal function whose instantaneous
    frequency varies with time.  The frequency at time `t` is given by
    the polynomial `poly`.

    Parameters
    ----------
    t : ndarray
        Times at which to evaluate the waveform.
    poly : 1-D array_like or instance of numpy.poly1d
        The desired frequency expressed as a polynomial.  If `poly` is
        a list or ndarray of length n, then the elements of `poly` are
        the coefficients of the polynomial, and the instantaneous
        frequency is

          ``f(t) = poly[0]*t**(n-1) + poly[1]*t**(n-2) + ... + poly[n-1]``

        If `poly` is an instance of numpy.poly1d, then the
        instantaneous frequency is

          ``f(t) = poly(t)``

    phi : float, optional
        Phase offset, in degrees, Default: 0.

    Returns
    -------
    sweep_poly : ndarray
        A numpy array containing the signal evaluated at `t` with the
        requested time-varying frequency.  More precisely, the function
        returns ``cos(phase + (pi/180)*phi)``, where `phase` is the integral
        (from 0 to t) of ``2 * pi * f(t)``; ``f(t)`` is defined above.

    See Also
    --------
    chirp

    Notes
    -----
    .. versionadded:: 0.8.0

    If `poly` is a list or ndarray of length `n`, then the elements of
    `poly` are the coefficients of the polynomial, and the instantaneous
    frequency is:

        ``f(t) = poly[0]*t**(n-1) + poly[1]*t**(n-2) + ... + poly[n-1]``

    If `poly` is an instance of `numpy.poly1d`, then the instantaneous
    frequency is:

          ``f(t) = poly(t)``

    Finally, the output `s` is:

        ``cos(phase + (pi/180)*phi)``

    where `phase` is the integral from 0 to `t` of ``2 * pi * f(t)``,
    ``f(t)`` as defined above.

    i´   (   t   _sweep_poly_phaseR   R   (   R   t   polyR?   RA   (    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR   ˜  s    @c         C` s'   t  | ƒ } d t t | |  ƒ } | S(   sƒ   
    Calculate the phase used by sweep_poly to generate its output.

    See `sweep_poly` for a description of the arguments.

    i   (   R   R   R   (   R   RQ   t   intpolyRA   (    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyRP   Þ  s    c         C` s   t  |  | ƒ } t j |  ƒ }  | d k r= d t |  ƒ } nD | d k r\ t |  d ƒ } n% t | d ƒ s | f t |  ƒ } n  d | | <| S(   s#  
    Unit impulse signal (discrete delta function) or unit basis vector.

    Parameters
    ----------
    shape : int or tuple of int
        Number of samples in the output (1-D), or a tuple that represents the
        shape of the output (N-D).
    idx : None or int or tuple of int or 'mid', optional
        Index at which the value is 1.  If None, defaults to the 0th element.
        If ``idx='mid'``, the impulse will be centered at ``shape // 2`` in
        all dimensions.  If an int, the impulse will be at `idx` in all
        dimensions.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.

    Returns
    -------
    y : ndarray
        Output array containing an impulse signal.

    Notes
    -----
    The 1D case is also known as the Kronecker delta.

    .. versionadded:: 0.19.0

    Examples
    --------
    An impulse at the 0th element (:math:`\delta[n]`):

    >>> from scipy import signal
    >>> signal.unit_impulse(8)
    array([ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

    Impulse offset by 2 samples (:math:`\delta[n-2]`):

    >>> signal.unit_impulse(7, 2)
    array([ 0.,  0.,  1.,  0.,  0.,  0.,  0.])

    2-dimensional impulse, centered:

    >>> signal.unit_impulse((3, 3), 'mid')
    array([[ 0.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  0.]])

    Impulse at (2, 2), using broadcasting:

    >>> signal.unit_impulse((4, 4), 2)
    array([[ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  0.],
           [ 0.,  0.,  0.,  0.]])

    Plot the impulse response of a 4th-order Butterworth lowpass filter:

    >>> imp = signal.unit_impulse(100, 'mid')
    >>> b, a = signal.butter(4, 0.2)
    >>> response = signal.lfilter(b, a, imp)

    >>> import matplotlib.pyplot as plt
    >>> plt.plot(np.arange(-50, 50), imp)
    >>> plt.plot(np.arange(-50, 50), response)
    >>> plt.margins(0.1, 0.1)
    >>> plt.xlabel('Time [samples]')
    >>> plt.ylabel('Amplitude')
    >>> plt.grid(True)
    >>> plt.show()

    i    t   midi   t   __iter__i   N(   i    (   R   RL   t
   atleast_1dt   Nonet   lent   tuplet   hasattr(   R   t   idxR   t   out(    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyR   ë  s    I
(#   t
   __future__R    R   R   t   numpyRL   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   t   scipy._lib.sixR   t   __all__R   R   t   FalseR   t   TrueR   R:   R   RP   RV   RK   R   (    (    (    s5   /tmp/pip-build-7oUkmx/scipy/scipy/signal/waveforms.pyt   <module>   s   ^	GJb[5F	