³ò
éeJc           @   sƒ   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Td „  Z d „  Z	 d d d	 d
 d „ Z
 e d „ Z d S(   s=   
lib/simfit.py

Simultaneous Larentzian Fit

Lev, 8 May 2009
iÿÿÿÿN(   t   *c         C   sA   t  i i |  | t i | | ƒ | | d t i |  | | ƒ S(   sw   
    Return spectrum of a Lorentzian described by (f0, A, T2, phi)
    delayed by 'delay' at given frequencies 'f'
    i   (   t   nmrfitt
   Lorentziant   lt   matht   expt   pi(   t   f0t   At   T2t   phit   delayt   f(    (    s   c:\py\lib\simfit.pyt   delayed_lorentzian   s    c   	      C   sw   |  \ } } } } g  } xX | i  ƒ  D]J } | | } t i | t | i t | | | | | | i ƒ ƒ g ƒ } q% W| S(   s)   
    Calculate residuals for the fit
    (   t   keyst   numpyt   concatenatet   abst   wR   R   (	   t   paramt   spectraR   R   R	   R
   t   resR   t   spectrum(    (    s   c:\py\lib\simfit.pyt   sim_lorentzian_residuals   s     
>i   i   g     LAg    @$Ag     ˆ³@c         C   sÌ  t  i i |  i | ƒ i d | d | d | ƒ ƒ } | i | } | i | } h  }	 xR t d | ƒ D]A }
 |  i | |
 | ƒ i d | d | d | ƒ |	 | |
 | <qc Wt  i i |	 | ƒ } | i t	 i
 | | i ƒ 9_ | i d t | i | d t | _ t i i t | i | i | i | i g d |	 f d d d	 t d
 ƒ ƒ} t  i d | d d d | d d d | d d d | d d ƒ } | d d j o t | |	 ƒ | |	 f Sn t | ƒ ‚ d S(   sB  
    Fit a lorentzian to a spectrum of 'trace'. Produce 'N' spectra using 'Npad' long
    slices of the 'trace' starting at 0, deltaT, 2*deltaT, ..., (N-1)*deltaT
    and fit simultaneously.
    
    First fit in a range 'fmin' to 'fmax' and make a guess. Then choose a frequency range
    of 'guessed f0' +- 'fwing'.
    t   Npadt   fmint   fmaxi    i   t   argst   xtolgê-™—q=t   maxfevg     jø@R   R   i   R	   R
   i   N(   R   R   t   guesst   periodt   fftR   t   ranget   fitR   R   R   R	   R
   R   t   scipyt   optimizet   leastsqR   t   intt   plot_slicest   FitError(   t   tracet   Nt   T0t   deltaTR   R   R   t   fwingt   gR   t   iR   R"   (    (    s   c:\py\lib\simfit.pyt   simfit_lorentzian   s     	3 ?'HDc         C   s·  t  ƒ  x©t | i ƒ  ƒ D]•} | | } | oy t | i | i ƒ  d d d | d ƒ\ } t | i t t |  i |  i |  i	 |  i
 | | i ƒ ƒ d d | i ƒ  ƒq t d ƒ t | i | i ƒ  d d d | d ƒ\ } t | i t t |  i |  i |  i	 |  i
 | | i ƒ ƒ d d | i ƒ  ƒt d ƒ t | i | i ƒ  d d d | d d | i ƒ  ƒt | i t t |  i |  i |  i	 |  i
 | | i ƒ ƒ d d | i ƒ  ƒq Wd  S(	   Nt   .t   labels   %d usg    €„.At   -t   coloriÓ   iÔ   (   t   figuret   sortedR   t   plotR   R   R   R   R   R	   R
   t	   get_colort   subplott   ret   realt   imt   imag(   R"   R   t   modulusR   R   t   p(    (    s   c:\py\lib\simfit.pyR'   <   s     
,M
,I
3i €  (   t   __doc__R   t   nmrR   R   t   scipy.optimizeR#   t   pylabR   R   R0   t   FalseR'   (    (    (    s   c:\py\lib\simfit.pys   <module>   s   0
		