ó
 nTMc           @   s   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l	 Z	 d d d „  ƒ  YZ
 d	 e
 f d
 „  ƒ  YZ d „  Z d „  Z e d k rd d l Z d „  Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d Z e e j d ƒ Z e j e ƒ e j d e ƒ d e e  e ƒ ƒ d Z! e! d e e  e ƒ ƒ d Z! e
 d e! e ƒ Z" e" j# GHe" j$ ƒ  GHe" j% d ƒ Z& e e f e e! ƒ ƒ d  Z' e" j( d ƒ Z) e" j* e j d d d ƒ ƒ Z+ e" j, ƒ  Ge" j- ƒ  d ƒ Ge) j. ƒ  GHe e) ƒ n  d S(    s0   
Auto-regressive model for stochastic processes
iÿÿÿÿ(   t   InterpolatingFunction(   t
   Polynomial(   t   RationalFunction(   t   NNt   AutoRegressiveModelc           B   st   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z RS(   s’   Auto-regressive model for stochastic process

    This implementation uses the Burg algorithm to obtain the
    coefficients of the AR model.
    i   c         C   s9   | |  _  | |  _ d |  _ |  j | ƒ |  j | ƒ d S(   s  
        @param order: the order of the model
        @type order: C{int}
        @param data: the time series
        @type data: sequence of C{float} or C{complex}
        @param delta_t: the sampling interval for the time series
        @type delta_t: C{float}
        N(   t   ordert   delta_tt   Nonet   _polest   _findCoefficientst   _setTrajectory(   t   selfR   t   dataR   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   __init__   s
    				c         C   s£  | } | } t  j d g ƒ } g  } t  j j t | ƒ d ƒ t | ƒ } | |  _ xt |  j ƒ D]ñ } | d } | d  }	 d t  j j | t  j	 |	 ƒ ƒ t  j j | t  j	 | ƒ |	 t  j	 |	 ƒ ƒ }
 | j
 |
 ƒ | |
 |	 } |	 t  j	 |
 ƒ | } t  j | d g f ƒ } | |
 t  j	 | d  d  d … ƒ } | d t |
 ƒ d } qc W| |  j d d … |  _ t  j | ƒ |  _ | |  _ t  j | ƒ |  _ d  S(   Ng      ð?i   i   iÿÿÿÿg       @g        i    (   R   t   arrayt   addt   reducet   abst   lent   variancet   rangeR   t	   conjugatet   appendt   concatenatet   coefft   parcort   sigsqt   sqrtt   sigma(   R   R   t   et   bt   aR   R   t   rt   ert   brt   g(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR	   (   s*    &	

 1$	c         C   s   t  j  | |  j ƒ |  _ d  S(   N(   t   copyR   t
   trajectory(   R   R   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR
   A   s    c         C   sA   t  j j |  j |  j ƒ } |  j d |  j d *| |  j d <| S(   s`  
        Calculates the linear prediction of the next step in the series.
        This step is appended internally  to the current
        trajectory, making it possible to call this method repeatedly
        in order to obtain a sequence of predicted steps.
        
        @returns: the predicted step
        @rtype: C{float} or C{complex}
        i   iÿÿÿÿ(   R   R   R   R   R%   (   R   t   next(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   predictStepD   s    
c         C   s“   d } xR t  d t |  j ƒ d ƒ D]4 } | |  j | t j d | |  j | ƒ } q# Wd |  j |  j | t j | ƒ j } t	 | f | ƒ S(   sÿ   
        @param omega: the angular frequencies at which the spectrum
        is to be evaluated
        @type omega: C{Numeric.array} of C{float}
        @returns: the frequency spectrum of the process
        @rtype: C{Numeric.array} of C{float}
        g      ð?i   y              ð¿g      à?(
   R   R   R   R   t   expR   R   R   t   realR    (   R   t   omegat   sumt   it   s(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   spectrumS   s
    #2(c         C   s¿   |  j  d k r¸ d d l m } t |  j ƒ } | d k rI |  j |  _  q¸ t j | | f |  j ƒ } t j | d ƒ | d d … d d … f <|  j | d d … d f <| | ƒ |  _  n  |  j  S(   s{   
        @returns: the poles of the model in the complex M{z}-plane
        @rtype: C{Numeric.array} of C{complex}
        iÿÿÿÿ(   t   eigenvaluesi   N(	   R   R   t   Scientific.LAR/   R   R   R   t   zeros_stt   identity(   R   R/   t   nR   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   polesa   s    )c         C   s±  |  j  ƒ  } t j | ƒ } d } t j |  j d | |  j d ƒ } x	t t | ƒ ƒ D]õ } | | } t j j | | |  ƒ t j j | | | d ƒ t j j | d | ƒ } y | | | | } WqX t	 k
 rLt j
 | j t j ƒ }	 xD t t | ƒ ƒ D]0 } y | | | |	 | <Wqt k
 r6qXqW| |	 | } qX XqX W|  j | t j |  j d ƒ }
 t |  j ƒ s‘t |
 ƒ }
 n  t |  j t j | ƒ f |
 ƒ S(   sI  
        @param nsteps: the number of time steps for which the autocorrelation
        function is to be evaluated
        @type nsteps: C{int}
        @returns: the autocorrelation function of the process as estimated
        from the AR model
        @rtype: L{Scientific.Functions.Interpolation.InterpolatingFunction}
        g        i   g      ð?i    (   R4   R   R   t   arangeR   R   R   t   multiplyR   t   OverflowErrort   zerost   shapet   Complext
   ValueErrorR   R   t
   _isComplext	   _realPartR    R   (   R   t   nstepsR4   t   cpolest   xt	   exponentsR,   t   polet   factort   powert   cf(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   correlationr   s,    	$
3"c   	      C   s§  |  j  ƒ  } t j | ƒ } t j |  j d ƒ } t j |  j f t j ƒ } x• t |  j ƒ D]„ } | | } |  j | |  j d | t j	 j
 | | |  ƒ t j	 j
 | | | d ƒ t j	 j
 | d | ƒ |  j | | <q\ W| t j | ƒ } d } x< t |  j ƒ D]+ } | t | | g | | d g ƒ } qWd | t d d g ƒ |  j d } t |  j ƒ s£t | j j ƒ | j _ t | j j ƒ | j _ n  | S(   s   
        @returns: the M{z}-transform of the process' memory function
        @rtype: L{Scientific.Function.Rational.RationalFunction}
        i    i   g      ð?g        g      ð¿i   (   R4   R   R   R   R8   R   R:   R   R   R6   R   R   R+   R   R   R   R<   R=   t	   numeratort   denominator(	   R   R4   R?   t   coeff0t   betaR,   RB   R+   t   mz(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   memoryFunctionZ”   s$    
K)%c         C   sã  |  j  ƒ  } t j | ƒ } t j |  j d ƒ } t j |  j f t j ƒ } x• t |  j ƒ D]„ } | | } |  j | |  j d | t j	 j
 | | |  ƒ t j	 j
 | | | d ƒ t j	 j
 | d | ƒ |  j | | <q\ W| t j | ƒ } g  } x[ t | ƒ D]M } d }	 x1 t |  j ƒ D]  }
 |	 | |
 | |
 | 7}	 q&W| j |	 ƒ q
W| j ƒ  t | d g d g | ƒ t d d g ƒ |  j d } t |  j ƒ sßt | j j ƒ | j _ t | j j ƒ | j _ n  | S(   s7  
        @param den_order: 
        @type den_order: C{int}
        @returns: an approximation to the M{z}-transform of the process'
        memory function that correponds to an expansion of the
        denominator up to order den_order
        @rtype: L{Scientific.Function.Rational.RationalFunction}
        i    i   g      ð?g        g      ð¿i   (   R4   R   R   R   R8   R   R:   R   R   R6   R   R   R+   R   t   reverseR   R   R   R<   R=   RG   RH   (   R   t	   den_orderR4   R?   RI   RJ   R,   RB   t	   den_coeffR+   t   jRK   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   memoryFunctionZapprox®   s.    	
K
c         C   sž   |  j  | |  j ƒ } | j | d ƒ d j d d d … } t | ƒ | d k r` | d } n  d t | d ƒ | d <|  j t j | ƒ } t	 | f | ƒ S(   s7  
        @param nsteps: the number of time steps for which the memory
        function is to be evaluated
        @type nsteps: C{int}
        @returns: the memory function of the process as estimated
        from the AR model
        @rtype: L{Scientific.Functions.Interpolation.InterpolatingFunction}
        i   i    Niÿÿÿÿg       @(
   RQ   R   t   divideR   R   R=   R   R   R5   R    (   R   R>   RK   t   memt   time(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   memoryFunctionÑ   s    	'c         C   s^  |  j  ƒ  } t j | ƒ } t j |  j d ƒ } t j |  j f t j ƒ } x• t |  j ƒ D]„ } | | } |  j | |  j d | t j	 j
 | | |  ƒ t j	 j
 | | | d ƒ t j	 j
 | d | ƒ |  j | | <q\ W| t j | ƒ } d } x1 t |  j ƒ D]  } | | | d | | } qWt |  j ƒ sOt | ƒ } n  d | |  j S(   st   
        @returns: the friction constant of the process, i.e. the
        integral over the memory function
        i    i   g      ð?g        (   R4   R   R   R   R8   R   R:   R   R   R6   R   R   R+   R<   R=   R   (   R   R4   R?   RI   RJ   R,   RB   R+   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   frictionConstantâ   s     
K(   t   __name__t
   __module__t   __doc__R   R	   R
   R'   R.   R4   RF   RL   RQ   RU   RV   (    (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR      s   						"		#	t   AveragedAutoRegressiveModelc           B   s#   e  Z d  Z d „  Z d d „ Z RS(   s  Averaged auto-regressive model for stochastic process

    An averaged model is constructed by averaging the model
    coefficients of several auto-regressive models of the same
    order. An averaged model is created empty, then individual
    models are added.
    c         C   s^   | |  _  | |  _ d |  _ t j | f t j ƒ |  _ d |  _ d |  _ d |  _	 d |  _ d S(   s´   
        @param order: the order of the model
        @type order: C{int}
        @param delta_t: the sampling interval for the time series
        @type delta_t: C{float}
        g        N(   R   R   t   weightR   R8   t   FloatR   R   R   R   R   R   (   R   R   R   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR     s    						i   c         C   s¿   |  j  | j  k r! t d ƒ ‚ n  |  j | } |  j |  j | | j | |  _ |  j |  j | | j | |  _ t j |  j ƒ |  _ |  j |  j | | j | |  _ | |  _ d |  _
 d S(   su  
        Adds the coefficients of an autoregressive model to the average.

        @param model: an autoregressive model
        @type model: L{AutoRegressiveModel}
        @param weight: the weight of the model in the average
        @type weight: C{float}
        @raise ValueError: if the order of the model does not match the
        order of the average model
        s   model orders not equalN(   R   R;   R[   R   R   R   R   R   R   R   R   (   R   t   modelR[   t   nw(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR     s    """	(   RW   RX   RY   R   R   (    (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyRZ   û   s   	c         C   s.   y |  j  d SWn t t f k
 r) d SXd  S(   Ni   i    (   t   imagt   AttributeErrorR;   (   R@   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR<   ,  s
    c         C   s*   y |  j  SWn t t f k
 r% |  SXd  S(   N(   R)   R`   R;   (   R@   (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyR=   4  s    t   __main__c         C   s~   d t  |  ƒ } t j |  | d ƒ } | t j | ƒ } t j | t  | ƒ d ƒ } | t  |  ƒ  t  |  ƒ t j t  |  ƒ ƒ S(   Ni   i    (   R   t   FFTt   fftR   R   t   inverse_fftR5   (   t   seriesR3   t	   FFTSeries(    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   AutoCorrelationFunction=  s
    (   t   gaussian(   t   mean(   t	   readArray(   t   plot(   t   randomg      ð?iô  g      @gš™™™™™¹?g      à?y        š™™™™™¹?i   iÈ   g        g      @g{®Gáz„?(    (/   RY   t"   Scientific.Functions.InterpolationR    t   Scientific.Functions.PolynomialR   t   Scientific.Functions.RationalR   t
   ScientificR   R$   R   RZ   R<   R=   RW   Rb   Rg   t   MMTK.RandomRh   t   Scientific.StatisticsRi   t   Scientific.IO.ArrayIORj   t   GnuplotRk   t   RandomArrayRl   t   dtR5   t   tt   sint   cosR   R   R]   R   R4   RF   t   ct   crefRU   t   mR.   R-   RV   RL   t   definiteIntegral(    (    (    s:   C:\Python27\Lib\site-packages\Scientific\Signals\Models.pyt   <module>	   s<   ê1			8%