#NEWLINE# def lp_model( trace , slice = slice ( None ) , order = 8 , mode = 'f' , mirror = None , method = 'svd' , full = False ) : #NEWLINE# #INDENT# "\n    Use Linear Prediction to model 1D NMR time domain data.\n\n    Parameters\n    ----------\n    trace : 1D ndarray\n        One dimensional time domain NMR data to model.\n    slice : slice object, optional\n        Slice object which selects the region along the last axis to use in LP\n        equation.  The default, slice(None), will use all points.\n    order : int\n        Prediction order, number of LP coefficients calculated.\n    mode : {'f', 'b'}\n        Mode to generate LP filter. 'f' for forward,'b' for backward.\n    mirror : {None, '0', '180'}\n        Mode to form mirror image of data before processing.  None will\n        process the data trace as provided (no mirror image). '0' or '180'\n        forms a mirror image of the sliced trace to calculate the LP filter.\n        '0' should be used with data with no delay, '180' with data\n        with an initial half-point delay.\n    method : {'svd', 'qr', 'choleskey', 'tls'}\n        Method to use to calculate the LP filter. Choices are a SVD ('svd'), QR\n        ('qr'), or Choleskey ('choleskey') decomposition, or Hankel SVD\n        ('hsvd').\n    full : bool\n        True to return amplitudes and phases calculated by performing a least\n        squares fitting to the data after LP modeling.  False will return only\n        the damping (relaxation) factors and signal frequencies.\n\n    Returns\n    -------\n    damp : list\n        List of damping (relaxation) factors found from LP modeling.\n    freq : list\n        List of signal frequencies found from LP modeling.\n    amp : list, optional\n        List of signal amplitudes found by least squares fitting of data after\n        LP modeling, only returned when `full` parameter is True.\n    phase : list, optional.\n        List of signal phases found by least squares fitting of data after LP\n        modeling, only returned when `full` parameter is True.\n\n\n    Notes\n    -----\n    When backward LP is used the signal roots are reflected before calculating\n    model parameters.\n\n    " #NEWLINE# if ( mode not in [ 'f' , 'b' ] ) : #NEWLINE# #INDENT# raise ValueError ( "mode must be 'f' or 'b'" ) #NEWLINE# #UNINDENT# if ( method not in [ 'svd' , 'qr' , 'cholesky' , 'tls' , 'hsvd' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'Invalid method' ) #NEWLINE# #UNINDENT# if ( trace . ndim != 1 ) : #NEWLINE# #INDENT# raise ValueError ( 'trace must be a 1D array' ) #NEWLINE# #UNINDENT# x = trace [ slice ] #NEWLINE# if ( mirror is not None ) : #NEWLINE# #INDENT# x = make_mirror ( x , mirror ) #NEWLINE# #UNINDENT# if ( method in [ 'svd' , 'qr' , 'cholseky' , 'tls' ] ) : #NEWLINE# #INDENT# ( D , d ) = make_Dd ( x , order , mode ) #NEWLINE# a = find_lpc ( D , d , method ) #NEWLINE# poles = find_roots ( a , mode ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( method == 'hsvd' ) : #NEWLINE# #INDENT# poles = find_lproots_hsvd ( x , M = order , K = order , mode = mode , zmethod = 'sm' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'Invalid method' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( mode == 'b' ) : #NEWLINE# #INDENT# poles = [ ( 1.0 / pole ) for pole in poles ] #NEWLINE# #UNINDENT# damp = [ root2damp ( pole ) for pole in poles ] #NEWLINE# freq = [ root2freq ( pole ) for pole in poles ] #NEWLINE# if ( full is False ) : #NEWLINE# #INDENT# return ( damp , freq ) #NEWLINE# #UNINDENT# poles = np . array ( poles ) #NEWLINE# B = np . row_stack ( [ ( poles ** i ) for i in range ( len ( x ) ) ] ) #NEWLINE# ( z , resid , rank , s ) = np . linalg . lstsq ( B , np . array ( x ) ) #NEWLINE# amp = [ cof2amp ( cof ) for cof in z ] #NEWLINE# phase = [ cof2phase ( cof ) for cof in z ] #NEWLINE# return ( damp , freq , amp , phase )