ó
Ë½÷Xc           @   s¿  d  Z  d d l Z d d l m Z d g Z i d g d 6d g d 6d g d 6d g d	 6d	 g d
 6d
 g d 6d d
 d g d 6d	 g d 6d g d 6d g d 6d d d g d 6d d d g d 6d d d g d 6d g d 6d d d g d 6d g d 6d g d 6d d d g d 6d g d 6d g d 6d g d 6d g d 6d d d g d 6d g d 6d d d g d 6d d d g d 6d g d  6d g d! 6d! d  d g d" 6d  g d# 6d# d" d g d$ 6Z d d d d% „ Z d S(&   s   Tools for MLS generationiÿÿÿÿNi   (   t   _max_len_seq_innert   max_len_seqi   i   i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i    c      	   C   s%  | d k rt |  t k rX t j t t j ƒ  ƒ ƒ } t d | j ƒ  | j ƒ  f ƒ ‚ n  t j t |  t j	 ƒ } n‚ t j
 t j | t j	 ƒ ƒ d d d … } t j | d k  ƒ sØ t j | |  k ƒ sØ | j d k  rç t d ƒ ‚ n  t j | ƒ } d |  d } | d k r| } n' t | ƒ } | d k  r@t d ƒ ‚ n  | d k rmt j |  d	 t j d
 d ƒ} n' t j | d	 t d
 d ƒj t j ƒ } | j d k s²| j |  k rÁt d ƒ ‚ n  t j | d k ƒ råt d ƒ ‚ n  t j | d	 t j d
 d ƒ} t | | |  | | ƒ } | | f S(   sD
  
    Maximum length sequence (MLS) generator.

    Parameters
    ----------
    nbits : int
        Number of bits to use. Length of the resulting sequence will
        be ``(2**nbits) - 1``. Note that generating long sequences
        (e.g., greater than ``nbits == 16``) can take a long time.
    state : array_like, optional
        If array, must be of length ``nbits``, and will be cast to binary
        (bool) representation. If None, a seed of ones will be used,
        producing a repeatable representation. If ``state`` is all
        zeros, an error is raised as this is invalid. Default: None.
    length : int, optional
        Number of samples to compute. If None, the entire length
        ``(2**nbits) - 1`` is computed.
    taps : array_like, optional
        Polynomial taps to use (e.g., ``[7, 6, 1]`` for an 8-bit sequence).
        If None, taps will be automatically selected (for up to
        ``nbits == 32``).

    Returns
    -------
    seq : array
        Resulting MLS sequence of 0's and 1's.
    state : array
        The final state of the shift register.

    Notes
    -----
    The algorithm for MLS generation is generically described in:

        https://en.wikipedia.org/wiki/Maximum_length_sequence

    The default values for taps are specifically taken from the first
    option listed for each value of ``nbits`` in:

        http://www.newwaveinstruments.com/resources/articles/
            m_sequence_linear_feedback_shift_register_lfsr.htm

    .. versionadded:: 0.15.0

    Examples
    --------
    MLS uses binary convention:

    >>> from scipy.signal import max_len_seq
    >>> max_len_seq(4)[0]
    array([1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0], dtype=int8)

    MLS has a white spectrum (except for DC):

    >>> import matplotlib.pyplot as plt
    >>> from numpy.fft import fft, ifft, fftshift, fftfreq
    >>> seq = max_len_seq(6)[0]*2-1  # +1 and -1
    >>> spec = fft(seq)
    >>> N = len(seq)
    >>> plt.plot(fftshift(fftfreq(N)), fftshift(np.abs(spec)), '.-')
    >>> plt.margins(0.1, 0.1)
    >>> plt.grid(True)
    >>> plt.show()

    Circular autocorrelation of MLS is an impulse:

    >>> acorrcirc = ifft(spec * np.conj(spec)).real
    >>> plt.figure()
    >>> plt.plot(np.arange(-N/2+1, N/2+1), fftshift(acorrcirc), '.-')
    >>> plt.margins(0.1, 0.1)
    >>> plt.grid(True)
    >>> plt.show()

    Linear autocorrelation of MLS is approximately an impulse:

    >>> acorr = np.correlate(seq, seq, 'full')
    >>> plt.figure()
    >>> plt.plot(np.arange(-N+1, N), acorr, '.-')
    >>> plt.margins(0.1, 0.1)
    >>> plt.grid(True)
    >>> plt.show()

    s/   nbits must be between %s and %s if taps is NoneNiÿÿÿÿi    i   sE   taps must be non-empty with values between zero and nbits (inclusive)i   s)   length must be greater than or equal to 0t   dtypet   ordert   cs1   state must be a 1-dimensional array of size nbitss   state must not be all zeros(   t   Nonet	   _mls_tapst   npt   arrayt   listt   keyst
   ValueErrort   mint   maxt   intpt   uniquet   anyt   sizet   ascontiguousarrayt   intt   onest   int8t   boolt   astypet   ndimt   allt   emptyR    (   t   nbitst   statet   lengtht   tapst
   known_tapst   n_maxt   seq(    (    s8   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_max_len_seq.pyR      s4    S+9	!'(   t   __doc__t   numpyR   R    t   __all__R   R   R   (    (    (    s8   /tmp/pip-build-7oUkmx/scipy/scipy/signal/_max_len_seq.pyt   <module>   s   	O>4844