³ò
/SKc           @   s‘   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d	 „  Z d S(
   s‡   
nmr.py - a library of nmr routines

classes: nmr.Trace, nmr.Spectrum, nmr.SignalMismatchError
function: nmr.rfftfreq

7 Dec 2009, Lev
iÿÿÿÿNt   SignalMismatchErrorc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s’   
    Exception raised when incompatible signals are combined.
    If not None, 'self.signal1' and 'self.signal2' are the signals in question.
    c         C   s   | |  _  | |  _ d S(   ss   
        Exception constructor. Two arguments can be specified,
        containing two signals in question
        N(   t   signal1t   signal2(   t   selfR   R   (    (    s   c:\py\lib\nmr.pyt   __init__2   s    	c         C   s   d S(   Ns   Signal mismatch(    (   R   (    (    s   c:\py\lib\nmr.pyt   __str__:   s    N(   t   __name__t
   __module__t   __doc__t   NoneR   R   (    (    (    s   c:\py\lib\nmr.pyR    ,   s   t   Tracec           B   s*  e  Z d  Z d0 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e Z e Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d d d d d d „ Z  d „  Z! d0 d0 d „ Z" d d0 d „ Z# e$ d d  „ ƒ Z% e$ d d! „ ƒ Z& e$ d d" „ ƒ Z' e$ d# „  ƒ Z( e$ d$ „  ƒ Z) e$ d% „  ƒ Z* d0 d& „ Z+ d' e, i- d( „ Z. d0 d0 d0 d0 d) „ Z/ d0 d0 d* „ Z0 d0 d0 d+ „ Z1 d, „  Z2 d- „  Z3 d. „  Z4 d0 d0 d d/ „ Z5 RS(1   sW  
    Time domain signal trace.
    
    An array of samples is stored at 's', 'dt' is the sampling interval.
    Timeline is returned by 't()'.
    
    Arithmetic operations +,-,*,/ are supported. Other signals or numbers can be
    added and subtracted. A signal can be multiplied or divided by a number.
    
    '&' appends samples to a trace. An array of samples can be concatenated,
    or another trace. The latter must the same 'dt' as the trace being extended.
    Also samples can be added to the beginning of a trace.
    
    '[t]' returns a sample at the moment 't', time is rounded down. Negative 't'
    is interpreted as time relative to the end of the trace. IndexError is raised
    if t is out of range.
    
    'info', set by default to 'None' is intended for metadata related to the
    trace, such as a record from a scope log.
    c         C   s  | d j oX | d | d t | ƒ d } t i t i | d d … d f ƒ d t i ƒ|  _ nˆ t i t i | ƒ d t i ƒ|  _ t i | ƒ o
 | } nJ t | ƒ t | ƒ j o t d ƒ ‚ n | d | d t | ƒ d } t i t i | ƒ ƒ |  _	 d |  _
 d S(	   sU  
        Create a time domain signal trace. Takes 1 or 2 arguments.
        
        If a single argument is supplied, it is expected to be a two-column
        array, first column - timeline, second column - samples.

        If two arguments are supplied, the first is a timeline if it is an
        array or a timebase if it is a scalar, the second argument is an array
        of samples.
        
        All supplied arrays are copied (so a modification of .s will not affect
        an array used to create a Trace. If complex numbers are given,
        imaginary part is discared.
        iÿÿÿÿi    i   Nt   dtypes.   time and sample arrays are of different lenght(   iÿÿÿÿi    (   i    i    (   R	   t   lent   numpyt   asarrayt   realt   float64t   st   isscalart
   ValueErrort   dtt   info(   R   t   arg1t   arg2R   (    (    s   c:\py\lib\nmr.pyR   R   s     8$
 c         C   s   t  |  i ƒ S(   s   Return number of samples(   R   R   (   R   (    (    s   c:\py\lib\nmr.pyt   __len__o   s    c         C   s   |  i  t |  ƒ S(   sÇ   
        Return time over which samples span [s].
        This includes one 'dt' after the last sample, or one might think of it
        as 'dt/2' before the first and after the last sample.
        (   R   R   (   R   (    (    s   c:\py\lib\nmr.pyt   timespans   s    c         C   s   t  i t |  i ƒ ƒ |  i S(   s   Return timeline array(   R   t   arangeR   R   R   (   R   (    (    s   c:\py\lib\nmr.pyt   t{   s    c         C   s   d |  i  S(   s   Return sampling rate [Hz]g      ð?(   R   (   R   (    (    s   c:\py\lib\nmr.pyt   srate   s    c         C   s9   t  | t ƒ o) t |  ƒ t | ƒ j o |  i | i j S(   s>   Return True if two traces have same timelines, False otherwise(   t
   isinstanceR
   R   R   (   R   t   other(    (    s   c:\py\lib\nmr.pyt
   compatibleƒ   s    c         C   sA   t  | t ƒ p
 t ‚ n |  i | ƒ p t |  | ƒ ‚ n d S(   s~   
        Raise a ValueError, if other is not a Trace, a SignalMismatchError if
        two traces are not compatible.
        N(   R   R
   R   R   R    (   R   R   (    (    s   c:\py\lib\nmr.pyt   check‡   s    
c         C   s   t  |  i |  i i ƒ  ƒ S(   s   Return a copy of the trace(   R
   R   R   t   copy(   R   (    (    s   c:\py\lib\nmr.pyR!   ‘   s    c         C   s&   |  i  | ƒ o t |  i | i j ƒ S(   N(   R   t   allR   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __eq__“   s    c         C   s   |  | j S(   N(    (   R   R   (    (    s   c:\py\lib\nmr.pyt   __ne__”   s    c         C   s   t  |  i |  i ƒ S(   N(   R
   R   R   (   R   (    (    s   c:\py\lib\nmr.pyt   __neg__–   s    c         C   s   |  S(   N(    (   R   (    (    s   c:\py\lib\nmr.pyt   __pos__—   s    c         C   sR   t  i | ƒ o t |  i |  i | ƒ Sn |  i | ƒ t |  i |  i | i ƒ S(   N(   R   R   R
   R   R   R    (   R   R   (    (    s   c:\py\lib\nmr.pyt   __add__™   s    c         C   sR   t  i | ƒ o t |  i |  i | ƒ Sn |  i | ƒ t |  i |  i | i ƒ S(   N(   R   R   R
   R   R   R    (   R   R   (    (    s   c:\py\lib\nmr.pyt   __sub__Ÿ   s    c         C   s;   t  i | ƒ o t |  i |  i | ƒ Sn t d ƒ ‚ d  S(   Ns*   a trace can be only multiplied by a number(   R   R   R
   R   R   R   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __mul__¥   s    c         C   s;   t  i | ƒ o t |  i |  i | ƒ Sn t d ƒ ‚ d  S(   Ns'   a trace can be only divided by a number(   R   R   R
   R   R   R   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __truediv__ª   s    c         C   s5   t  i | ƒ o t |  i | |  i ƒ Sn t ‚ d  S(   N(   R   R   R
   R   R   R   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __rsub__µ   s    c         C   s“   t  | t ƒ oF |  i | i j o
 t ‚ n t |  i t i |  i | i f ƒ ƒ Sn t |  i t i |  i t i t i | ƒ d t i	 ƒf ƒ ƒ S(   NR   (
   R   R
   R   R    R   t   concatenateR   t   arrayR   R   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __and__º   s
    
)c         C   sR   | d  j o |  Sn t |  i t i t i t i | ƒ d t i ƒ|  i f ƒ ƒ S(   NR   (	   R	   R
   R   R   R,   R-   R   R   R   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __rand__Á   s    c         C   sD   d |  i  t |  ƒ |  i |  i d  j o d n d t |  i ƒ f S(   Ns   Trace(dt=%.3es,%d points: %s)%st    t    (   R   R   R   R   R	   t   repr(   R   (    (    s   c:\py\lib\nmr.pyt   __repr__Æ   s    c         C   s   t  |  i ƒ S(   N(   t   hashR   (   R   (    (    s   c:\py\lib\nmr.pyt   __hash__Ç   s    c         C   s!   |  i  t t i | |  i ƒ ƒ S(   N(   R   t   intt   matht   floorR   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __getitem__É   s    c         C   s'   | |  i  t t i | |  i ƒ ƒ <d  S(   N(   R   R6   R7   R8   R   (   R   R   t   value(    (    s   c:\py\lib\nmr.pyt   __setitem__Ê   s    c         C   s(   | d j o | |  i  t |  ƒ d j S(   Ni    i   (   R   R   (   R   R   (    (    s   c:\py\lib\nmr.pyt   __contains__Ë   s    c         C   s   t  |  i ƒ  ƒ S(   N(   t   iterR   (   R   (    (    s   c:\py\lib\nmr.pyt   __iter__Ì   s    i    c         C   s  |  i  i ƒ  } |  i ƒ  }	 | d j  p | t | ƒ j o t | ƒ } n | d j  o
 d } n | d j  o- t d t i t i | | d ƒ ƒ ƒ } n | d j o) | t i	 t i
 |	 | | ƒ |	 ƒ 8} n | d j o | t i |	 | ƒ 9} n t i i | | | !| ƒ |  i }
 t | |  i ƒ } | d j o t | ƒ } n | | j | | j @} t | | |
 | ƒ } ~
 ~ ~ ~ ~	 | S(   s[  
        (fast) Fourier transform a signal.
        
        Nstart, Nstop - subset of samples to use for FFT. Nstart defaults to 0, Nstop to number of samples.
        fmin, fmax    - frequency range to return FFT in
        bgpower       - power of a polynom to background subtract. Subtraction is not carried if bgpower is less than 0.
        i   i    i   (   R   R!   R   R   R6   R7   t   ceilt   logR   t   polyvalt   polyfitt   expt   fftt   rfftR   t   rfftfreqt   maxt   Spectrum(   R   t   Nstartt   Nstopt   Npadt   fmint   fmaxt   bgpowert   T2filterR   R   t   wwt   fft   iit   X(    (    s   c:\py\lib\nmr.pyRD   Î   s(     
-)#c         C   s   t  |  i |  i | ƒ S(   s=   Return a subset of the trace based upon 'condition' argument.(   R
   R   R   (   R   t	   condition(    (    s   c:\py\lib\nmr.pyt   subsetù   s    c         C   s]   | d j o
 d } n | d j o |  i ƒ  } n |  i ƒ  } |  i | | j | | j @ƒ S(   s®   
        Return a subset of the trace happening between start and stop
        (in seconds), start defaults to the beginning of the trace,
        stop - to the end.
        i    N(   R	   R   R   RU   (   R   t   startt   stopR   (    (    s   c:\py\lib\nmr.pyt   periodý   s    
c         C   sF   | d j o t |  i |  i | ƒ Sn t |  i |  i | | !ƒ Sd S(   s¼   
        Return a subset of the trace starting with 'Nstart'th sample
        and up to 'Nstop'-1 sample. If 'Nstop' is not specified, it
        defaults to the end of the trace.
        N(   R	   R
   R   R   (   R   RI   RJ   (    (    s   c:\py\lib\nmr.pyt   range  s    t   channel0c         C   s3   t  i |  d t ƒ\ } } } t d | | | ƒ S(   sG  
        Load a trace from a binary file. Sampling rate is specified explicitely.
        If the file is stored in binary format 2 or higher, 'channel' can be
        used to determine which section of the file to use. Defaults
        to 'channel0', which is the only section available in the earlier
        formats.
        t   fullg      ð?(   t   flibt
   loadbinaryt   TrueR
   (   t   filenamet   samplingratet   channelt   vt   ht   d(    (    s   c:\py\lib\nmr.pyR]     s    	c         C   s2   t  i g  } | D] } | |  | q ~ | | ƒ S(   sU  
        Load traces from binary files and average them. Sampling rate is specified explicitely
        If the file is stored in binary format 2 or higher, 'channel' can be
        used to determine which section of the file to use. Defaults
        to 'channel0', which is the only section available in the earlier
        formats.
        (   R
   t   loadallbinaries(   t   filenameTemplatet   indicesR`   Ra   t   _[1]t   i(    (    s   c:\py\lib\nmr.pyt   loadbinaries#  s    	c         C   s…   t  |  t t f ƒ o t i |  ƒ }  n t |  ƒ d j  o d Sn d } x$ |  D] } | t i | | ƒ 7} qQ W| t |  ƒ :} | S(   s  
        Load traces from binary files and average them. Sampling rate is specified explicitely.
        If 'filenames' is a string it is treated as a wildcard (see fnmatch), otherwise it is supposed
        to be a list of strings, each pointing to an existing filename.
        
        If the file is stored in binary format 2 or higher, 'channel' can be
        used to determine which section of the file to use. Defaults
        to 'channel0', which is the only section available in the earlier
        formats.
        i   i    N(   R   t   strt   unicodet   globR   R	   R
   R]   (   t	   filenamesR`   Ra   t   dataR_   (    (    s   c:\py\lib\nmr.pyRe   .  s     c         C   s   t  t i |  d d ƒƒ S(   sa   
        Load trace in ASCII format:
        (1) Time (sec)
        (2) Signal amplitude
        t   usecolsi    i   (   i    i   (   R
   R\   t	   loadascii(   R_   (    (    s   c:\py\lib\nmr.pyRq   F  s    c         C   s,   t  i g  } | D] } | |  | q ~ ƒ S(   so   
        Load traces from ascii files and average them.
        Sampling rate is specified explicitely
        (   R
   t   loadallasciis(   Rf   Rg   Rh   Ri   (    (    s   c:\py\lib\nmr.pyt
   loadasciisO  s    c         C   s‚   t  |  t t f ƒ o t i |  ƒ }  n t |  ƒ d j  o d Sn d } x! |  D] } | t i | ƒ 7} qQ W| t |  ƒ :} | S(   s  
        Load traces from binary files and average them. Sampling rate is specified explicitely.
        If 'filenames' is a string it is treated as a wildcard (see fnmatch), otherwise it is supposed
        to be a list of strings, each pointing to an existing filename.
        i   i    N(   R   Rk   Rl   Rm   R   R	   R
   Rq   (   Rn   Ro   R_   (    (    s   c:\py\lib\nmr.pyRr   W  s     c      	   C   s5   t  i | |  i ƒ  |  i f d d d | d t ƒd S(   st   
        Save trace in ASCII format: (all in %.15e, tab-delimited)
        (1) Timebase
        (2) Samples
        t   fmts   %.15et   headert   packN(   R\   t	   saveasciiR   R   R^   (   R   R_   Ru   (    (    s   c:\py\lib\nmr.pyRw   j  s    R0   c         C   s>   t  i | h  t | ƒ d <t i |  i d | ƒd <ƒ d S(   s<   Save a trace in a binary format, sampling rate is not saved.Ru   R   RZ   N(   R\   t
   savebinaryR2   R   R   R   (   R   R_   Ru   R   (    (    s   c:\py\lib\nmr.pyRx   r  s    c         C   s  | d j o | d j o t d ƒ ‚ nO | d j	 o | d j	 o t d ƒ ‚ n% | d j o t | |  i ƒ } n t |  ƒ } | | j o
 | } n | d j o | d j o
 | } nO | d j	 o | d j	 o t d ƒ ‚ n% | d j o t | |  i ƒ } n | d j  o
 d } n t i | | ƒ } | d } | | } x¢ t t | ƒ ƒ D]Ž }	 |	 | | }
 |	 | | } |
 d j  o d }
 | } n" | | j o | | }
 | } n t |  i	 |
 | !ƒ t
 |  i	 |
 | !ƒ | |	 <q^Wt |  i | | ƒ S(   s•  
        Return an envelope of the trace, as an nmr.Trace object.
        Envelope contains the peak-to-peak amplitude of a moving window 'Nwindow'
        points or 'Twindow' seconds long. Either 'Nwindow' or 'Twindow' must be
        specified, but not both.
        
        In a similar way 'Nstep' or 'Tstep' determines a step by which the
        window moves, it defaults to window length.
        s1   'Nwindow' or 'Twindow' argument must be specifieds>   Only one of 'Nwindow' and 'Twindow' arguments can be specifieds:   Only one of 'Nstep' and 'Tstep' arguments can be specifiedi   i   i    N(   R	   R   R6   R   R   R   t   zerosRY   RG   R   t   minR
   (   R   t   Nwindowt   Twindowt   Nstept   Tstept   Nt   et   Nleftt   Nrightt   nRI   RJ   (    (    s   c:\py\lib\nmr.pyt   envelopev  s@    




 


2c   	      C   s  | d j o | d j o
 d } n | d j o | |  i } n | d j  o
 d } n | t |  ƒ d j o t |  ƒ d } n t i |  i |  ƒ } | d |  i } t i |  i | ƒ } |  i ƒ  | d |  i } | | | | } | | | } t |  i | | |  i ƒ  ƒ S(   so  
        Return an nmr.Trace object containing a straight line best fittin through first 'window' and
        last 'window' points (or first 'Twindow' and last 'Twindow' seconds). This can be subtracted
        from the signal to avoid ripple due to start and end level mismatch. If neither 'window'
        nor 'Twindow' is specified 'window' defaults to 1.
        i   i   g       @N(	   R	   R   R   R   t   meanR   R   R
   R   (	   R   t   windowR|   RV   t   tstartt   endt   tendt   kt   b(    (    s   c:\py\lib\nmr.pyt   linearbg¨  s    

c         C   s   |  |  i  | | ƒ S(   sl   
        Return a difference between the trace and its linear background,
        see 'linearbg()'.
        (   RŒ   (   R   R†   R|   (    (    s   c:\py\lib\nmr.pyt   subtract_linearbgÂ  s    c         C   s   t  |  i t i |  i ƒ ƒ S(   s>   
        Take a logarithm of every point of the trace
        (   R
   R   R   R@   R   (   R   (    (    s   c:\py\lib\nmr.pyR@   É  s    c         O   s9   t  i |  i ƒ } t i |  i ƒ  | |  i | | | Ž S(   s4   
        Plot the trace, only finite points
        (   R   t   isfiniteR   t   pylabt   plotR   (   R   t   argst   vargst   cond(    (    s   c:\py\lib\nmr.pyR   Ï  s    c         O   sF   t  i |  i ƒ |  i d j @} t i |  i ƒ  | |  i | | | Ž S(   s9   
        Plot the trace with log scale on y axis
        i    (   R   RŽ   R   R   t   semilogyR   (   R   R‘   R’   R“   (    (    s   c:\py\lib\nmr.pyt   logplotÖ  s    c         C   sh   | d j o7 | d j o t d ƒ ‚ n t i | |  i ƒ } n t |  i | |  i | d | … ƒ S(   sK   
        Return a subset of the trace at a different sampling rate
        s   Downsampling is not specifiedN(   R	   R   R   t   roundR   R
   R   (   R   R}   t   newDTRI   (    (    s   c:\py\lib\nmr.pyt
   downsampleÝ  s
    N(6   R   R   R   R	   R   R   R   R   R   R   R    R!   R#   R$   R%   R&   R'   R(   R)   R*   t   __div__t   __radd__t   __rmul__R+   R.   R/   R3   R5   R9   R;   R<   R>   RD   RU   RX   RY   t   staticmethodR]   Rj   Re   Rq   Rs   Rr   Rw   R   t   float32Rx   R„   RŒ   R   R@   R   R•   R˜   (    (    (    s   c:\py\lib\nmr.pyR
   <   sf   						
																		+	
	2			RH   c           B   s  e  Z d  Z d6 d „ Z d „  Z d „  Z d6 d6 d „ Z d6 d6 d „ Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e Z  e Z! e Z" d „  Z# d „  Z$ d „  Z% d  „  Z& d! „  Z' d" „  Z( d# „  Z) e* d$ „  ƒ Z+ d6 d% „ Z, e* d& „  ƒ Z- d' e. i/ d( „ Z0 d) „  Z1 d* d+ „ Z2 d* d6 d* d, „ Z3 d- „  Z4 d. „  Z5 d/ e6 d6 d6 d0 „ Z7 d1 „  Z8 d2 d3 „ Z9 d4 „  Z: d5 „  Z; RS(7   s>  
    A Fourier Transform of a signal. A spectrum can contain only a subset of frequencies
    to reduce amount of data. Frequencies are stored as 'f', complex spectral densities as 'w'.
    
    Spectra with identical sets of frequency can be added to, subtracted from,
    multiplied by and divided by each other using '+-*/'.
    
    'self[f]' is the amplitude at frequency 'f'. IndexError is raised if f is not present
    in the spectrum.

    '&' can be used to combine two spectra without overlapping frequencies. ValueError is raised
    if there is an overlap.
    c         C   sÜ   | d j oc t i t i | d d … d f ƒ d t i ƒ|  _ t i | d d … d f d t i ƒ|  _ ni t | ƒ t | ƒ j o t	 d ƒ ‚ n t i t i | ƒ d t i ƒ|  _ t i | d t i ƒ|  _ d S(   sø   
        Creates a Fourier Transform
        f - array of frequencies, only real component is used
        w - array of complex spectral densities.
        If a single argument is given, f[:,0] is frequencies, f[:,1] is spectral densities.
        Ni    R   i   s2   frequency and spectrum arrays are different length(
   R	   R   R   R   R   t   ft
   complex128t   wR   R   (   R   Rž   R    (    (    s   c:\py\lib\nmr.pyR   ÷  s    4/$c         C   s   t  |  i i ƒ  |  i i ƒ  ƒ S(   N(   RH   Rž   R!   R    (   R   (    (    s   c:\py\lib\nmr.pyR!     s    c         C   s   t  |  i | |  i | ƒ S(   sR   
        Return a subset of the Spectrum based upon 'condition' argument.
        (   RH   Rž   R    (   R   RT   (    (    s   c:\py\lib\nmr.pyRU   
  s    c         C   s]   | d j o |  i ƒ  } n | d j o |  i ƒ  } n |  i |  i | j |  i | j @ƒ S(   s÷   
        Return a spectrum in given frequency range.
        If fmin is not specified or out of range, minimum frequency of the spectrum is used.
        If fmax is not specified or out of range, maximum frequency of the spectrum is used.
        N(   R	   RL   RM   RU   Rž   (   R   RL   RM   (    (    s   c:\py\lib\nmr.pyt   frange  s
      c         C   s]   | d j o |  i ƒ  } n | d j o |  i ƒ  } n |  i |  i | j  |  i | j Bƒ S(   sé   
        Return a subset of the spectrum excluding a frequency range limited
        by [fmin, fmax], boundaries being discared.
        fmin defaults to the minimum frequency,
        fmax defaults to the maximum frequency.
        N(   R	   RL   RM   RU   Rž   (   R   RL   RM   (    (    s   c:\py\lib\nmr.pyt   nofrange  s
      c         C   sš   t  | ƒ d j  o |  Sn |  i |  i j } x] | D]U } t i | ƒ o | |  i | j M} q4 | |  i | d j  |  i | d j BM} q4 W|  i | ƒ S(   s  
        Return a subset of a trace, lacking frequencies mentioned in 'freq_blacklist'.
        The list should contain numbers - the frequencies to discard, and (fmin, fmax)
        two-number pairs, which are interpreted as frequency range boundaries.
        i   i    (   R   Rž   R   R   RU   (   R   t   freq_blacklistR“   Rž   (    (    s   c:\py\lib\nmr.pyt   comb%  s     ,c         C   s#   | |  i  ƒ  j o | |  i ƒ  j S(   sj   
        Return True if f is in the range of frequencies of the spectrum,
        False otherwise
        (   RL   RM   (   R   Rž   (    (    s   c:\py\lib\nmr.pyt	   isinrange8  s    c         C   s   t  |  i ƒ S(   s   minumum frequency(   Rz   Rž   (   R   (    (    s   c:\py\lib\nmr.pyRL   ?  s    c         C   s   t  |  i ƒ S(   s   maximum frequency(   RG   Rž   (   R   (    (    s   c:\py\lib\nmr.pyRM   @  s    c         C   s"   |  i  ƒ  |  i ƒ  t |  ƒ d S(   s   frequency stepi   (   RM   RL   R   (   R   (    (    s   c:\py\lib\nmr.pyt   dfA  s    c         C   s   t  i |  i ƒ S(   s   real part of spectrum(   R   R   R    (   R   (    (    s   c:\py\lib\nmr.pyt   reC  s    c         C   s   t  i |  i ƒ S(   s   imaginary part of spectrum(   R   t   imagR    (   R   (    (    s   c:\py\lib\nmr.pyt   imD  s    c         C   s   t  |  i ƒ S(   s   absolute value of spectrum(   t   absR    (   R   (    (    s   c:\py\lib\nmr.pyRª   E  s    c         C   s   t  |  i ƒ S(   N(   R   Rž   (   R   (    (    s   c:\py\lib\nmr.pyR   G  s    c         C   s&   t  | t ƒ o t |  i | i j ƒ S(   sZ   
        Return True if two spectrums have same frequency arrays, False otherwise
        (   R   RH   R"   Rž   (   R   R   (    (    s   c:\py\lib\nmr.pyR   I  s    c         C   s'   |  i  | ƒ p t |  | ƒ ‚ n d S(   sR   
        Raise a SignalMismatchError if two spectrums are not compatible.
        N(   R   R    (   R   R   (    (    s   c:\py\lib\nmr.pyR    O  s    c         C   s>   |  i  ƒ  t |  i  ƒ  ƒ j } |  i | d |  i | d f S(   s½   
        Return a tuple containing the frequency and complex amplitude of the
        peak, defined as a point in the spectrum with maximum absolute value
        of the amplitude.
        i    (   Rª   RG   Rž   R    (   R   RR   (    (    s   c:\py\lib\nmr.pyt   findpeakV  s    c         C   s   |  i  ƒ  d S(   s4   Return the frequency of a peak found by 'findpeak()'i    (   R«   (   R   (    (    s   c:\py\lib\nmr.pyt   peakf_  s    c         C   s   |  i  ƒ  d S(   s>   Return the complex amplitude of the peak found by 'findpeak()'i   (   R«   (   R   (    (    s   c:\py\lib\nmr.pyt   peak`  s    c         C   s>   |  i  ƒ  t |  i  ƒ  ƒ j } |  i | d |  i | d f S(   s½   
        Return a tuple containing the frequency and complex amplitude of the
        peak, defined as a point in the spectrum with minimum absolute value
        of the amplitude.
        i    (   Rª   Rz   Rž   R    (   R   RR   (    (    s   c:\py\lib\nmr.pyt   findminb  s    c         C   s?   |  i  | ƒ o/ t |  i | i j ƒ o t |  i | i j ƒ S(   N(   R   R"   Rž   R    (   R   R   (    (    s   c:\py\lib\nmr.pyR#   k  s    c         C   s   |  | j S(   N(    (   R   R   (    (    s   c:\py\lib\nmr.pyR$   l  s    c         C   s   t  |  i |  i ƒ S(   N(   RH   Rž   R    (   R   (    (    s   c:\py\lib\nmr.pyR%   n  s    c         C   s   |  S(   N(    (   R   (    (    s   c:\py\lib\nmr.pyR&   o  s    c         C   sR   t  i | ƒ o t |  i |  i | ƒ Sn |  i | ƒ t |  i |  i | i ƒ S(   N(   R   R   RH   Rž   R    R    (   R   R   (    (    s   c:\py\lib\nmr.pyR'   q  s    c         C   sR   t  i | ƒ o t |  i |  i | ƒ Sn |  i | ƒ t |  i |  i | i ƒ S(   N(   R   R   RH   Rž   R    R    (   R   R   (    (    s   c:\py\lib\nmr.pyR(   w  s    c         C   sR   t  i | ƒ o t |  i |  i | ƒ Sn |  i | ƒ t |  i |  i | i ƒ S(   N(   R   R   RH   Rž   R    R    (   R   R   (    (    s   c:\py\lib\nmr.pyR)   }  s    c         C   sR   t  i | ƒ o t |  i |  i | ƒ Sn |  i | ƒ t |  i |  i | i ƒ S(   N(   R   R   RH   Rž   R    R    (   R   R   (    (    s   c:\py\lib\nmr.pyR*   „  s    c         C   s5   t  i | ƒ o t |  i | |  i ƒ Sn t ‚ d  S(   N(   R   R   RH   Rž   R    R   (   R   R   (    (    s   c:\py\lib\nmr.pyR+   ‘  s    c         C   sr   t  | t ƒ o^ t |  i | i j ƒ o t d ƒ ‚ n t t i |  i | i ƒ t i |  i | i ƒ ƒ Sn t S(   Ns:   Can't to merge two spectra with overlapping frequency sets(   R   RH   t   anyRž   R   R   R,   R    (   R   R   (    (    s   c:\py\lib\nmr.pyR.   –  s
    5c         C   sB   t  |  ƒ d j o' d t  |  ƒ |  i ƒ  |  i ƒ  f Sn d Sd  S(   Ni    s&   Spectrum [%d points in %g-%g Hz range]s   Empty Spectrum(   R   RL   RM   (   R   (    (    s   c:\py\lib\nmr.pyR3     s    'c         C   s5   | |  j o t  d ƒ ‚ n |  i |  i | j d S(   Ns(   frequency is not present in the spectrumi    (   t
   IndexErrorR    Rž   (   R   Rž   (    (    s   c:\py\lib\nmr.pyR9   £  s    c         C   s7   | |  j o t  d ƒ ‚ n | |  i |  i | j <d  S(   Ns(   frequency is not present in the spectrum(   R°   R    Rž   (   R   Rž   R:   (    (    s   c:\py\lib\nmr.pyR;   ¨  s    c         C   s   | |  i  j S(   N(   Rž   (   R   Rž   (    (    s   c:\py\lib\nmr.pyR<   ­  s    c         C   s   t  |  i ƒ S(   N(   R=   Rž   (   R   (    (    s   c:\py\lib\nmr.pyR>   ®  s    c         C   s?   t  i |  d d d t d t ƒ\ } } } t | | d | ƒ S(	   sÁ   
        Load trace in ASCII format:
        (1) Frequency [Hz]
        (2) Abs amplitude *** disregarded ***
        (3) Real part of amplitude
        (4) Imaginary part of amplitude
        Rp   i    i   i   t   unpackt   keep2Dy              ð?(   i    i   i   (   R\   Rq   R^   RH   (   R_   Rž   t   rRi   (    (    s   c:\py\lib\nmr.pyRq   °  s    	*c      	   C   sG   t  i | |  i |  i ƒ  |  i ƒ  |  i ƒ  f d d d | d t ƒd S(   sÎ   
        Save spectrum in ASCII format: (all in %.15e, tab-delimited)
        (1) Frequency [Hz]
        (2) Abs amplitude
        (3) Real part of amplitude
        (4) Imaginary part of amplitude
        Rt   s   %.15eRu   Rv   N(   R\   Rw   Rž   Rª   R§   R©   R^   (   R   R_   Ru   (    (    s   c:\py\lib\nmr.pyRw   ¼  s    c         C   s  t  i |  d t ƒ\ } } } | d } t | ƒ d } t | |  | | d | !d | d | d | !ƒ } | d j o3 y t | h  t i d <ƒ } Wq³ h  } q³ Xn h  } ~ xE | i ƒ  D]7 \ } } y | i	 | ƒ WqÃ | i
 | | ƒ qÃ XqÃ W| S(   sÒ   
        Load a spectrum from a binary file.
        The dataset is assumed to be 3*N long, first N points - frequencies,
        next go the real parts and finally the imaginary parts of the spectrum.
        R[   RZ   i   i   y              ð?i    t   nan(   R\   R]   R^   R   RH   t   evalR   R´   t   itemst   __getattribute__t   __setattr__(   R_   Rb   Rc   Rd   R   R   RŠ   (    (    s   c:\py\lib\nmr.pyR]   Æ  s$    
5 R0   c         C   se   t  i t  i |  i |  i ƒ  |  i ƒ  g ƒ d t  i ƒ} t i | h  t	 | ƒ d <| d <ƒ d S(   sX  
        Save the spectrum into a binary file.
        First all frequencies, then real and finally imaginary parts are stored
        in a single section of the file called 'channel0'.
        
        Optinally a header is saved (a string)
        
        Data format can be specified by 'dtype' argument, defaults to 4 byte floats.
        R   Ru   RZ   N(
   R   R   R,   Rž   R§   R©   R   R\   Rx   R2   (   R   R_   Ru   R   Ro   (    (    s   c:\py\lib\nmr.pyRx   à  s    
9c         O   sL   | i  d ƒ o | i d ƒ } n d } t i |  i |  i ƒ  | | | Ž S(   su   
        Plot absolute amplitudes of the spectrum.
        Optional 'y0' argument shifts spectrum vertically
        t   y0i    (   t   has_keyt   popR   R   Rž   Rª   (   R   R‘   R’   R¹   (    (    s   c:\py\lib\nmr.pyt   plotabsí  s    i    c         O   sO   |  i  t i d | ƒ } t i d ƒ t i t i | ƒ t i | ƒ | | Ž S(   s„   
        Plot a hodograph of the spectrum - real on x and imaginary on y.
        Axis is set to proportional aspect ratio.
        y              ð?t   equal(   R    R   RC   R   t   axisR   R   R¨   (   R   t   phaseR‘   R’   t   W(    (    s   c:\py\lib\nmr.pyt   plothodoø  s    c         K   sÓ   | d j o
 d } n |  i t i d | ƒ } t i |  i t i | ƒ | | d |  } | d j o | d i ƒ  } n | i	 d ƒ o | i
 d ƒ n t i |  i t i | ƒ | | d |  } | | S(   sˆ  
        Plot real (solid) and imaginary (dash-dot) parts of the spectrum.
        Both are plotted in the same colour, either specified with 'color'
        argument, or picked by pylab automatically.
        Optional 'phase' argument rotates the spectrum
        Optional 'y0' argument shifts the spectrum vertically
        If 'label' is specified, only the real part is labelled.
        R0   y              ð?t   -i    t   labels   --N(   R	   R    R   RC   R   R   Rž   R   t	   get_colorRº   R»   R¨   (   R   R¿   t   colorR¹   R’   RÀ   t   pret   pim(    (    s   c:\py\lib\nmr.pyt   plotri  s    
 
,  ,c         C   sS   t  i |  i t | | ƒ j ƒ o' t | t  i i |  i | | ƒ ƒ Sn t ‚ d S(   sç   
        Inverse FFT. Return nmr.Trace object.
        Only works if the spectrum is complete from 0 Hz to Nyquist Frequency,
        as if was produced by FFT of an 'N'-point trace with a sampling
        interval of 'dt'
        N(	   R   R"   Rž   RF   R
   RD   t   irfftR    t   NotImplementedError(   R   R   R   (    (    s   c:\py\lib\nmr.pyt   ifft  s    "'c         C   s   t  |  i d d |  i ƒ S(   sÄ   
        Shift frequencies to focus on 1-1.1MHz range
        In a returned spectrum 1 MHz is subtracted from all frequencies and
        frequencies are expressed in kHz, rather than Hz.
        g    €„.Ag     @@(   RH   Rž   R    (   R   (    (    s   c:\py\lib\nmr.pyt   mhz'  s    i   c         C   s™   | d j o |  i ƒ  } n | d j o |  i ƒ  } n |  i t i |  i | ƒ | t i t i |  i t	 ƒ d ƒ d j } | | | j | | j @S(   sÛ  
        Find peak frequencies. Peaks are those points which differ from
        neighbourgs by more than alpha times the rms absolute difference
        between the points. If 'singlepeaks' is True, a point is considered
        a peak only if it is higher that its both neighbours, otherwise
        points on sides of wider peaks are included as well.
        
        If 'minf'/'maxf' are specified, peaks are looked for only in a given
        frequency window.
        i   g      à?N(
   R	   RL   RM   Rž   R\   t   absdiffR    R   R…   R^   (   R   t   alphat   singlepeakst   minft   maxft   peaks(    (    s   c:\py\lib\nmr.pyt	   findpeaks/  s      Gc         K   s&   |  i  t i |  i |  i |   ƒ ƒ S(   s§   
        Return a subset of the spectrum excluding single point peaks. For the
        peak search algorithm and argument meaning see nmr.Spectrum.findpeaks()
        (   RU   R   t   setmember1dRž   RÓ   (   R   R’   (    (    s   c:\py\lib\nmr.pyt   removepeaks@  s    i
   c         C   s1   t  t i t i |  i |  i f ƒ i ƒ  | ƒ ƒ S(   sg   
        Thin the spectrum down by averaging every 'Nwindow' neighbour points
        together
        (   RH   R\   t   averageR   R-   Rž   R    t	   transpose(   R   R{   (    (    s   c:\py\lib\nmr.pyRÖ   G  s    c         C   s"   t  |  i t i |  i |  i ƒ ƒ S(   s¾   
        Evaluate an integral of the spectrum between the minimum frequency
        and every frequency present in the spectrum. Return the result as an
        nmr.Spectrum object.
        (   RH   Rž   R\   t	   integrateR    (   R   (    (    s   c:\py\lib\nmr.pyRØ   N  s    c         C   s'   y |  i  |  i SWn t i Sn Xd S(   s‡   
        return the time domain trace voltage swing, if this information was
        saved into the spectrum or NaN otherwise.
        N(   t	   trace_maxt	   trace_minR   R´   (   R   (    (    s   c:\py\lib\nmr.pyt   trace_swing_VppV  s    N(<   R   R   R   R	   R   R!   RU   R¡   R¢   R¤   R¥   RL   RM   R¦   R§   R©   Rª   R   R   R    R«   R¬   R­   R®   R#   R$   R%   R&   R'   R(   R)   R*   R™   Rš   R›   R+   R.   R3   R9   R;   R<   R>   Rœ   Rq   Rw   R]   R   R   Rx   R¼   RÁ   RÈ   RË   RÌ   R^   RÓ   RÕ   RÖ   RØ   RÛ   (    (    (    s   c:\py\lib\nmr.pyRH   è  sj   		
																																
						c         C   s    t  i d |  d d ƒ |  | S(   sÊ   
    Return an array of frequencies for Real Fast Fourier Transform (only
    positive frequencies) N - number of points in the original time trace
    dt - sampling rate of the original time trace
    i    i   (   R   R   (   R   R   (    (    s   c:\py\lib\nmr.pyRF   `  s    (   R   R   R\   R   R7   Rm   t	   ExceptionR    t   objectR
   RH   RF   (    (    (    s   c:\py\lib\nmr.pys   <module>   s   ÿ ­ÿ y