Esempio n. 1
0
def fit_S21_resonance_complex(*args,**kw):
    '''
    input:
    x,y
    returns fitresults with:

    fr[0]=(A,f0,Q,s21min,slope,theta)
    '''
    #print 'no slope'
    if type(args[0])==Data:
        dat = args[0]
        xy = dat.get_data()
        x= xy[:,0]
        y= xy[:,1]
    else:
        x=args[0]
        y=args[1]
    silent = kw.pop('silent',True)
    p0i=param_estimator_complex(x,y)

    if not silent:
        print('init guess: ',p0i)
    pars = S21p0i2paramsc(x,p0i)
    fr = fit.fitxy(pars,y, fn.disp_hanger_S21_complex_sloped, ret_fit=True)   
    if not silent:
        fit.print_fitres(pars)
    fitpars=params2tuple(pars)
    fitparserr = paramserr2tuple(pars)

    output = {'fitpars':fitpars,'fitparserr':fitparserr,'fit':fr[1],'fitparsobj':pars,'parsinit':p0i}
    return output
Esempio n. 2
0
def fit_lorentzian(x, y, plot=True):
    y0 = (y[0] + y[-1]) / 2.
    peak = np.abs((np.max(y) - y0)) > np.abs((np.min(y) - y0))
    print('peak = ', peak, y0, np.max(y), np.min(y))

    if peak:
        A = np.max(y) - y0
        f0 = x[y[:].tolist().index(max(y[:]))]
        find_y2 = (y - y0) > A / 2
    else:
        A = np.min(y) - y0
        f0 = x[y[:].tolist().index(min(y[:]))]
        find_y2 = (y - y0) < A / 2
    ind_df = find_y2.tolist().index(True)
    df = 2 * np.abs(x[ind_df] - f0)
    pars = fit.Parameters()
    pars.add('f', value=x, vary=False)
    pars.add('A', value=A)
    pars.add('f0', value=f0)
    pars.add('df', value=df)
    pars.add('y0', value=y0)
    fit.print_fitres(pars)
    fit.fitxy(pars, y, fn.Lorentzian)
    fit.print_fitres(pars)
    yfit = fn.Lorentzian(pars)
    if plot:
        plt.plot(x,
                 yfit,
                 label='fit: f0 = %.4f, df = %.1f' % (pars['f0'].value, 1e3 *
                                                      (pars['df'].value)))
        plt.legend()
    return pars, fn.Lorentzian(pars)
Esempio n. 3
0
def fit_lorentzian(x,y, plot=True):
    y0 = (y[0]+y[-1])/2.
    peak = np.abs((np.max(y)-y0)) > np.abs((np.min(y)-y0))
    print('peak = ',peak, y0, np.max(y),np.min(y))
    
    if peak:
        A = np.max(y)-y0
        f0 = x[y[:].tolist().index(max(y[:]))]
        find_y2 = (y-y0)>A/2
    else:
        A = np.min(y)-y0
        f0 = x[y[:].tolist().index(min(y[:]))]
        find_y2 = (y-y0)<A/2
    ind_df = find_y2.tolist().index(True)
    df = 2*np.abs(x[ind_df] - f0)
    pars = fit.Parameters()
    pars.add('f',value = x, vary = False)
    pars.add('A',value = A)
    pars.add('f0',value = f0)
    pars.add('df',value = df)
    pars.add('y0',value = y0)
    fit.print_fitres(pars)
    fit.fitxy(pars,y,fn.Lorentzian)
    fit.print_fitres(pars)
    yfit = fn.Lorentzian(pars)
    if plot:
        plt.plot(x,yfit, label = 'fit: f0 = %.4f, df = %.1f'%(pars['f0'].value,1e3*(pars['df'].value)))
        plt.legend()
    return pars, fn.Lorentzian(pars)
Esempio n. 4
0
def fit_S21_resonance(*args, **kw):
    '''
    input:
    x,y
    returns fitresults with:

    fr[0]=(A,f0,Q,s21min,slope,theta)
    '''
    # print 'no slope'
    if type(args[0]) == Data:
        dat = args[0]
        xy = dat.get_data()
        x = xy[:, 0]
        y = xy[:, 1]

    else:
        x = args[0]
        y = np.abs(args[1])

    p0i = param_estimator(x, y)
    silent = kw.pop('silent', True)
    if not silent:
        print('init guess: ', p0i)
    pars = S21p0i2params(x, p0i)
    # print 'init_guess', p0i
    fr = fit.fitxy(pars, y, fn.S21_resonance_sloped, ret_fit=True)
    if not silent:
        fit.print_fitres(pars)
    fitpars = params2tuple(pars)
    fitparserr = paramserr2tuple(pars)
    output = {
        'fitpars': fitpars,
        'parserr': fitparserr,
        'fit': fr[1],
        'fitparsobj': pars,
        'parsinit': p0i
    }
    return output