Program to solve Yule-Walker equations for AutoRegressive Models
    
    :author: XAVI LLORT (llort(at)grahi.upc.edu)
    :created: MAY 2007
    :changes: adapted to python by R.Dussurget

    :parameter acf: AutoCorrelation Function
    :parameter orden: Order of the AutoRegressive Model
    :return:
      * parameters : Parameters
      * sigma_e : Standard deviation of the noise term

def yule_walker(acf, orden):
    """
    Program to solve Yule-Walker equations for AutoRegressive Models
    
    :author: XAVI LLORT (llort(at)grahi.upc.edu)
    :created: MAY 2007
    :changes: adapted to python by R.Dussurget

    :parameter acf: AutoCorrelation Function
    :parameter orden: Order of the AutoRegressive Model
    :return:
      * parameters : Parameters
      * sigma_e : Standard deviation of the noise term
    """
    
    if len(acf) + 1 <= orden : raise Exception('ACF too short for the solicited order!')
    
    bb = acf[1:orden+1]
    aa = np.zeros((orden, orden))
    
    for ii in np.arange(0, orden):
        for jj in np.arange(0, orden):
            aa[ii, jj] = acf[ np.int(np.abs(ii - jj)) ]
    
    aa_1 = np.linalg.inv(aa)
    
    parameters = np.ma.dot(bb,aa_1) #Compliant with IDL aa_1#bb
    
    import warnings
    warnings.filterwarnings('error')
    try:
        s = acf[0] - np.sum(parameters * bb)
        sigma_e = np.sqrt(s)
    except RuntimeWarning:
        print "Warning : bad parameters - spectrum not computed"
        sigma_e = np.nan
        
    
    return parameters, sigma_e