Ejemplo n.º 1
0
 def flux_par(self):
     flux_over_flux0 = qdt.call_func("flux_over_flux0",
                                     voltage=self.yoko,
                                     offset=self.offset,
                                     flux_factor=self.flux_factor)
     Ej = qdt.call_func("Ej", flux_over_flux0=flux_over_flux0)
     return qdt._get_fq(Ej, qdt.Ec)
Ejemplo n.º 2
0
 def flux_parabola(self):
    flux_over_flux0=qdt.call_func("flux_over_flux0", voltage=self.yoko, offset=self.offset, flux_factor=self.flux_factor)
    Ej=qdt.call_func("Ej", flux_over_flux0=flux_over_flux0)
    return qdt._get_fq(Ej, qdt.Ec)
Ejemplo n.º 3
0
    def magabs_cs_fit():
        def lorentzian(x, p):
            return p[2] * (1.0 - 1.0 / (1.0 + ((x - p[1]) / p[0])**2)) + p[3]

        def fano(x, p):
            return p[2] * (((p[4] * p[0] + x - p[1])**2) /
                           (p[0]**2 + (x - p[1])**2)) + p[3]

        def refl_fano(x, p):
            return p[2] * (1.0 - ((p[4] * p[0] + x - p[1])**2) /
                           (p[0]**2 + (x - p[1])**2)) + p[3]

        def onebounce(x, p):
            w = 2 * pi * x
            k = 2.0 * pi * x / 3488.0
            Cc = 0.0
            D = 500.0e-6
            D1 = 300.0e-6
            D2 = 200.0e-6
            S12q = ((p[4] * p[0] / 6.28 + (x - p[1]) * 2 * pi)**
                    2) / (p[0]**2 + (p[4] * p[0] / 6.28 + x - p[1])**2)
            S11q = 1.0 / (p[0]**2 + (p[4] * p[0] / 6.28 + x - p[1])**2)
            S11 = p[5]
            return p[2] * absolute(
                exp(1.0j * k * D) * S12q *
                (1 + exp(2.0j * k * D1) * S11q * S11 + exp(2.0j * k * D2) *
                 S11q * S11 + exp(2.0j * k * D) * S11**2 * S12q**2) +
                2.0j * w * Cc * 50.0)**2 + p[3]

        def allbounces(x, p):
            w = 2 * pi * x
            k = 2.0 * pi * x / 3488.0
            Cc = 0.0
            D = 500.0e-6
            D1 = 300.0e-6
            D2 = 200.0e-6

            return p[2] * absolute(
                exp(1.0j * k * D) * S12q *
                (-2.0 + 1.0 / (1.0 - exp(2.0j * k * D1) * S11q * S11) + 1.0 /
                 (1.0 - exp(2.0j * k * D2) * S11q * S11) + 1.0 /
                 (1 - exp(2.0j * k * D) * S11**2 * S12q**2)) +
                2.0j * w * Cc * 50.0)

        def residuals(p, y, x):
            err = y - lorentzian(x, p)
            return err

        def residuals2(p, y, x):
            return y - fano(x, p)

        def residuals3(p, y, x):
            return y - refl_fano(x, p)

        p = [200e6, 4.5e9, 0.002, 0.022, 0.1, 0.1]

        indices = [
            range(81, 120 + 1),
            range(137, 260 + 1),
            range(269, 320 + 1),
            range(411, 449 + 1)
        ]  #, [490]]#, [186]]
        indices = [range(len(a.frequency))]
        widths = []
        freqs = []
        freq_diffs = []
        fanof = []
        filt = []
        for n in range(len(yok)):
            myifft = fft.ifft(mag[:, n])
            #b.line_plot("ifft", absolute(myifft))
            myifft[50:-50] = 0.0
            #myifft[:20]=0.0
            #myifft[-20:]=0.0
            filt.append(absolute(fft.fft(myifft))**2)
        filt = array(filt).transpose()
        for ind_list in indices:
            for n in ind_list:
                pbest = leastsq(residuals3,
                                p,
                                args=(a.MagAbs[n, :], c.flux_parabola[:]),
                                full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:  #n % 8==0:
                    bb.scatter_plot("magabs_flux",
                                    c.flux_parabola[:] * 1e-9,
                                    a.MagAbs[n, :],
                                    label="{}".format(n),
                                    linewidth=0.2,
                                    marker_size=0.8)
                    bb.line_plot("lorentzian",
                                 c.flux_parabola * 1e-9,
                                 refl_fano(c.flux_parabola, best_parameters),
                                 label="fit {}".format(n),
                                 linewidth=0.5)
                if 1:  #absolute(best_parameters[1]-a.frequency[n])<2e8:
                    freqs.append(a.frequency[n])
                    freq_diffs.append(
                        absolute(best_parameters[1] - a.frequency[n]))
                    widths.append(absolute(best_parameters[0]))
                    fanof.append(absolute(best_parameters[4]))
        if 1:
            widths2 = []
            freqs2 = []
            freq_diffs2 = []
            fano2 = []
            flux_over_flux0 = qdt.call_func("flux_over_flux0",
                                            voltage=yok,
                                            offset=-0.037,
                                            flux_factor=0.2925)
            Ej = qdt.call_func("Ej", flux_over_flux0=flux_over_flux0)
            flux_par = qdt._get_fq(Ej, qdt.Ec)
            magabs = absolute(mag)**2
            for n in range(len(frq)):
                pbest = leastsq(residuals2,
                                p,
                                args=(filt[n, :], flux_par[:]),
                                full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:  #n==539 or n==554:#n % 10:
                    b.line_plot("magabs_flux",
                                flux_par * 1e-9,
                                (magabs[n, :] - best_parameters[3]) /
                                best_parameters[2],
                                label="{}".format(n),
                                linewidth=0.2)
                    b.line_plot("lorentzian",
                                flux_par * 1e-9,
                                fano(flux_par, best_parameters),
                                label="fit {}".format(n),
                                linewidth=0.5)
                if 1:  #absolute(best_parameters[1]-frq[n])<1.5e8:
                    freqs2.append(frq[n])
                    freq_diffs2.append(absolute(best_parameters[1] - frq[n]))
                    widths2.append(absolute(best_parameters[0]))
                    fano2.append(absolute(best_parameters[4]))

        b.line_plot("widths", freqs, widths, label="-110 dBm")
        b.scatter_plot("widths2",
                       freqs2,
                       widths2,
                       color="red",
                       label="-130 dBm")
        vf = 3488.0
        p = [1.0001, 0.5, 0.3, 1.0e-15, 0.001]
        Np = 9
        K2 = 0.048
        f0 = 5.348e9

        def fourier(x, p):
            w = 2 * pi * x
            k = 2.0 * pi * x / 3488.0
            D = 500.0e-6
            D1 = 300.0e-6
            D2 = 200.0e-6
            G_f = 0.5 * Np * K2 * f0 * (sin(Np * pi * (x - f0) / f0) /
                                        (Np * pi * (x - f0) / f0))**2

            return G_f * absolute(
                exp(1.0j * k * D) * p[0] *
                (1 + exp(2.0j * k * D1) *
                 (p[1] + 1.0j * p[2]) + exp(2.0j * k * D2) *
                 (p[1] + 1.0j * p[2])) + 2.0j * w * p[3] * 50.0) + p[4]
            #exp(2.0j*k*D)*(p[1]+1.0j*p[2]))+

        def resid(p, y, x):
            #return y - onebounce(x,p)
            return y - fourier(x, p)

        #pbest=leastsq(resid, p, args=(absolute(widths2[318:876]), frq[318:876]), full_output=1)
        #print pbest[0]
        #b.line_plot("fourier", frq, fourier(frq, pbest[0]))
        #pi*vf/2*x=D
        from scipy.signal import lombscargle
        #lombscargle(freqs2[318:876], widths2[318:876])
        #bb.line_plot("fft", #frq[318:876]*fft.fftfreq(len(frq[318:876]), d=frq[1]-frq[0]),
        #absolute(fft.fft(widths2[318:876])))
        frqdiffs = linspace(0.01, 500e6, 1000)
        #bb.line_plot("ls", frqdiffs, lombscargle(array(freqs2[285:828]), array(widths2[285:828]), frqdiffs ))
        #bb.line_plot("ls", frqdiffs, lombscargle(array(freqs[318:876]), array(widths[318:876]), frqdiffs))

        bb.line_plot("fft2", absolute(fft.ifft(widths2[285:828])))
        bb.line_plot("fft", absolute(fft.ifft(widths[285:828])))

        bbb = Plotter()
        #bbb.scatter_plot("wid", freqs2, widths2)
        myifft = fft.ifft(widths2[285:828])
        myifft[12:-12] = 0.0
        bbb.line_plot("ff", freqs2[285:828], absolute(fft.fft(myifft)))
Ejemplo n.º 4
0
    def magabs_cs_fit():
        def lorentzian(x, p):
            return p[2] * (1.0 - 1.0 / (1.0 + ((x - p[1]) / p[0])**2)) + p[3]

        def fano(x, p):
            return p[2] * (((p[4] * p[0] + x - p[1])**2) /
                           (p[0]**2 + (x - p[1])**2)) + p[3]

        def refl_fano(x, p):
            return p[2] * (1.0 - ((p[4] * p[0] + x - p[1])**2) /
                           (p[0]**2 + (x - p[1])**2)) + p[3]

        def onebounce(x, p):
            w = 2 * pi * x
            k = 2.0 * pi * x / 3488.0
            Cc = 0.0
            D = 500.0e-6
            D1 = 300.0e-6
            D2 = 200.0e-6
            S12q = ((p[4] * p[0] / 6.28 + (x - p[1]) * 2 * pi)**
                    2) / (p[0]**2 + (p[4] * p[0] / 6.28 + x - p[1])**2)
            S11q = 1.0 / (p[0]**2 + (p[4] * p[0] / 6.28 + x - p[1])**2)
            S11 = p[5]
            return p[2] * absolute(
                exp(1.0j * k * D) * S12q *
                (1 + exp(2.0j * k * D1) * S11q * S11 + exp(2.0j * k * D2) *
                 S11q * S11 + exp(2.0j * k * D) * S11**2 * S12q**2) +
                2.0j * w * Cc * 50.0)**2 + p[3]

        def allbounces(x, p):
            w = 2 * pi * x
            k = 2.0 * pi * x / 3488.0
            Cc = 0.0
            D = 500.0e-6
            D1 = 300.0e-6
            D2 = 200.0e-6

            return p[2] * absolute(
                exp(1.0j * k * D) * S12q *
                (-2.0 + 1.0 / (1.0 - exp(2.0j * k * D1) * S11q * S11) + 1.0 /
                 (1.0 - exp(2.0j * k * D2) * S11q * S11) + 1.0 /
                 (1 - exp(2.0j * k * D) * S11**2 * S12q**2)) +
                2.0j * w * Cc * 50.0)

        def residuals(p, y, x):
            err = y - lorentzian(x, p)
            return err

        def residuals2(p, y, x):
            return y - fano(x, p)

        def residuals3(p, y, x):
            return y - refl_fano(x, p)

        p = [200e6, 4.5e9, 0.002, 0.022, 0.1, 0.1]

        indices = [
            range(81, 120 + 1),
            range(137, 260 + 1),
            range(269, 320 + 1),
            range(411, 449 + 1)
        ]  #, [490]]#, [186]]
        indices = [range(len(a.frequency))]
        widths = []
        freqs = []
        freq_diffs = []
        fanof = []
        for ind_list in indices:
            for n in ind_list:
                pbest = leastsq(residuals3,
                                p,
                                args=(a.MagAbs[n, :], c.flux_parabola[:]),
                                full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:  #n % 8==0:
                    bb.scatter_plot("magabs_flux",
                                    c.flux_parabola[:] * 1e-9,
                                    a.MagAbs[n, :],
                                    label="{}".format(n),
                                    linewidth=0.2,
                                    marker_size=0.8)
                    bb.line_plot("lorentzian",
                                 c.flux_parabola * 1e-9,
                                 refl_fano(c.flux_parabola, best_parameters),
                                 label="fit {}".format(n),
                                 linewidth=0.5)
                if 1:  #absolute(best_parameters[1]-a.frequency[n])<2e8:
                    freqs.append(a.frequency[n])
                    freq_diffs.append(
                        absolute(best_parameters[1] - a.frequency[n]))
                    widths.append(absolute(best_parameters[0]))
                    fanof.append(absolute(best_parameters[4]))
        if 1:
            widths2 = []
            freqs2 = []
            freq_diffs2 = []
            fano2 = []
            flux_over_flux0 = qdt.call_func("flux_over_flux0",
                                            voltage=yok,
                                            offset=-0.037,
                                            flux_factor=0.2925)
            Ej = qdt.call_func("Ej", flux_over_flux0=flux_over_flux0)
            flux_par = qdt._get_fq(Ej, qdt.Ec)
            magabs = absolute(mag)**2
            for n in range(len(frq)):
                pbest = leastsq(residuals2,
                                p,
                                args=(magabs[n, :], flux_par[:]),
                                full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:  #n==539 or n==554:#n % 10:
                    b.line_plot("magabs_flux",
                                flux_par * 1e-9,
                                (magabs[n, :] - best_parameters[3]) /
                                best_parameters[2],
                                label="{}".format(n),
                                linewidth=0.2)
                    b.line_plot("lorentzian",
                                flux_par * 1e-9,
                                fano(flux_par, best_parameters),
                                label="fit {}".format(n),
                                linewidth=0.5)
                if 1:  #absolute(best_parameters[1]-frq[n])<1.5e8:
                    freqs2.append(frq[n])
                    freq_diffs2.append(absolute(best_parameters[1] - frq[n]))
                    widths2.append(absolute(best_parameters[0]))
                    fano2.append(absolute(best_parameters[4]))

        b.line_plot("widths", freqs, widths, label="-110 dBm")
        b.scatter_plot("widths2",
                       freqs2,
                       widths2,
                       color="red",
                       label="-130 dBm")
        vf = 3488.0
        p = [1.0001, 0.5, 0.3, 1.0e-15, 0.001]
        Np = 9
        K2 = 0.048
        f0 = 5.348e9

        def fourier(x, p):
            w = 2 * pi * x
            k = 2.0 * pi * x / 3488.0
            D = 500.0e-6
            D1 = 300.0e-6
            D2 = 200.0e-6
            G_f = 0.5 * Np * K2 * f0 * (sin(Np * pi * (x - f0) / f0) /
                                        (Np * pi * (x - f0) / f0))**2

            return G_f * absolute(
                exp(1.0j * k * D) * p[0] *
                (1 + exp(2.0j * k * D1) *
                 (p[1] + 1.0j * p[2]) + exp(2.0j * k * D2) *
                 (p[1] + 1.0j * p[2])) + 2.0j * w * p[3] * 50.0) + p[4]
            #exp(2.0j*k*D)*(p[1]+1.0j*p[2]))+

        def resid(p, y, x):
            #return y - onebounce(x,p)
            return y - fourier(x, p)

        #pbest=leastsq(resid, p, args=(absolute(widths2[318:876]), frq[318:876]), full_output=1)
        #print pbest[0]
        #b.line_plot("fourier", frq, fourier(frq, pbest[0]))
        #pi*vf/2*x=D
        from scipy.signal import lombscargle
        #lombscargle(freqs2[318:876], widths2[318:876])
        #bb.line_plot("fft", #frq[318:876]*fft.fftfreq(len(frq[318:876]), d=frq[1]-frq[0]),
        #absolute(fft.fft(widths2[318:876])))
        frqdiffs = linspace(0.01, 500e6, 1000)
        #bb.line_plot("ls", frqdiffs, lombscargle(array(freqs2[285:828]), array(widths2[285:828]), frqdiffs ))
        #bb.line_plot("ls", frqdiffs, lombscargle(array(freqs[318:876]), array(widths[318:876]), frqdiffs))

        bb.line_plot("fft2", absolute(fft.ifft(widths2[285:828])))
        bb.line_plot("fft", absolute(fft.ifft(widths[285:828])))

        bbb = Plotter()
        #bbb.scatter_plot("wid", freqs2, widths2)
        myifft = fft.ifft(widths2[285:828])
        myifft[12:-12] = 0.0
        bbb.line_plot("ff", freqs2[285:828], absolute(fft.fft(myifft)))
        b.line_plot("ff", freqs2[285:828], absolute(fft.fft(myifft)))

        #bb.line_plot("fft2", absolute(fft.fft(widths[52:155])))

        #b.line_plot("fano", freqs, fano)
        #b.line_plot("fano", freqs2, fano2)

        #f0=5.37e9
        freq = linspace(4e9, 5e9, 1000)

        #G_f=0.5*Np*K2*f0*(sin(Np*pi*(freq-f0)/f0)/(Np*sin(pi*(freq-f0)/f0)))**2
        #b.scatter_plot("freq_test", freqs, freq_diffs)

        class Fitter3(Operative):
            base_name = "fitter"
            mult = FloatRange(0.001, 5.0, 0.82).tag(tracking=True)
            f0 = FloatRange(4.0, 6.0, 5.348).tag(tracking=True)
            offset = FloatRange(0.0, 100.0, 18.0).tag(tracking=True)

            @tag_Property(plot=True, private=True)
            def G_f(self):
                f0 = self.f0 * 1.0e9
                return self.offset * 1e6 + self.mult * 0.5 * Np * K2 * f0 * (
                    sin(Np * pi * (freq - f0) / f0) / (Np * pi *
                                                       (freq - f0) / f0))**2

            @observe("f0", "mult", "offset")
            def update_plot(self, change):
                if change["type"] == "update":
                    self.get_member("G_f").reset(self)
                    b.plot_dict["G_f"].clt.set_ydata(self.G_f)
                    b.draw()

        d = Fitter3()
        b.line_plot("G_f", freq, d.G_f, label="theory")
    def magabs_cs_fit():
        def lorentzian(x,p):
            return p[2]*(1.0-1.0/(1.0+((x-p[1])/p[0])**2))+p[3]

        def lorentzian2(x, p):
            return p[2]*(((p[4]*p[0]+x-p[1])**2)/(p[0]**2+(x-p[1])**2))+p[3]

        def lorentzian3(x, p):
            return p[2]*(((p[4]*p[0]/6.28+(x-p[1])*2*pi)**2)/(p[0]**2+(p[4]*p[0]/6.28+x-p[1])**2))+p[3]
            #return p[2]*(((p[4]*p[0]/6.28+(x-p[1])*2*pi)**2)/(p[0]**2+(p[4]*p[0]/6.28+x-p[1])**2))+p[3]
        def onebounce(x,p):
            w=2*pi*x
            k=2.0*pi*x/3488.0
            Cc=0.0
            D=500.0e-6
            D1=300.0e-6
            D2=200.0e-6
            S12q=((p[4]*p[0]/6.28+(x-p[1])*2*pi)**2)/(p[0]**2+(p[4]*p[0]/6.28+x-p[1])**2)
            S11q=1.0/(p[0]**2+(p[4]*p[0]/6.28+x-p[1])**2)
            S11=p[5]
            return p[2]*absolute(exp(1.0j*k*D)*S12q*(1+exp(2.0j*k*D1)*S11q*S11+exp(2.0j*k*D2)*S11q*S11+
            exp(2.0j*k*D)*S11**2*S12q**2)+2.0j*w*Cc*50.0)**2+p[3]

        def allbounces(x,p):
            w=2*pi*x
            k=2.0*pi*x/3488.0
            Cc=0.0
            D=500.0e-6
            D1=300.0e-6
            D2=200.0e-6

            return p[2]*absolute(exp(1.0j*k*D)*S12q*(-2.0
                +1.0/(1.0-exp(2.0j*k*D1)*S11q*S11) 
                +1.0/(1.0- exp(2.0j*k*D2)*S11q*S11)
                +1.0/(1- exp(2.0j*k*D)*S11**2*S12q**2))
                +2.0j*w*Cc*50.0)

        def residuals(p,y,x):
            err = y - lorentzian(x,p)
            return err

        def residuals2(p,y,x):
            return y - lorentzian2(x,p)

        def residuals3(p,y,x):
            #return y - onebounce(x,p)
            return y - lorentzian3(x,p)

        p = [200e6,4.5e9, 0.002, 0.022, 0.1, 0.1]

        indices=[range(81, 120+1), range(137, 260+1), range(269, 320+1), range(411, 449+1)]#, [490]]#, [186]]
        widths=[]
        freqs=[]
        freq_diffs=[]
        fano=[]
        for ind_list in indices:
            for n in ind_list:
                pbest = leastsq(residuals3, p, args=(a.MagAbs[n, :], c.flux_parabola[:]), full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0: #n % 10==0:
                    b.scatter_plot("magabs_flux", c.flux_parabola[:]*1e-9, a.MagAbs[n, :], label="{}".format(n), linewidth=0.2, marker_size=0.8)
                    b.line_plot("lorentzian", c.flux_parabola*1e-9, lorentzian3(c.flux_parabola,best_parameters), label="fit {}".format(n), linewidth=0.5)
                if 1:#absolute(best_parameters[1]-a.frequency[n])<2e8:
                    freqs.append(a.frequency[n])
                    freq_diffs.append(absolute(best_parameters[1]-a.frequency[n]))
                    widths.append(absolute(best_parameters[0]))
                    fano.append(absolute(best_parameters[4]))
        if 1:
            widths2=[]
            freqs2=[]
            freq_diffs2=[]
            fano2=[]
            flux_over_flux0=qdt.call_func("flux_over_flux0", voltage=yok, offset=-0.037, flux_factor=0.2925)
            Ej=qdt.call_func("Ej", flux_over_flux0=flux_over_flux0)
            flux_par=qdt._get_fq(Ej, qdt.Ec)
            magabs=absolute(mag)**2
            for n in range(len(frq)):
                pbest = leastsq(residuals3,p,args=(magabs[n, :], flux_par[:]), full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:#n==539 or n==554:#n % 10:
                    b.line_plot("magabs_flux", flux_par*1e-9, (magabs[n, :]-best_parameters[3])/best_parameters[2], label="{}".format(n), linewidth=0.2)
                    #b.line_plot("lorentzian", flux_par*1e-9, lorentzian3(flux_par,best_parameters), label="fit {}".format(n), linewidth=0.5)
                if 1:#absolute(best_parameters[1]-frq[n])<1.5e8:
                    freqs2.append(frq[n])
                    freq_diffs2.append(absolute(best_parameters[1]-frq[n]))
                    widths2.append(absolute(best_parameters[0]))
                    fano2.append(absolute(best_parameters[4]))

        b.scatter_plot("widths", freqs, widths, label="-110 dBm")
        b.scatter_plot("widths2", freqs2, widths2, color="red", label="-130 dBm")
        vf=3488.0
        p=[1.0001, 0.5, 0.3, 1.0e-15, 0.001]
        Np=9
        K2=0.048
        f0=5.348e9
        def fourier(x, p):
            w=2*pi*x
            k=2.0*pi*x/3488.0
            D=500.0e-6
            D1=300.0e-6
            D2=200.0e-6
            G_f=0.5*Np*K2*f0*(sin(Np*pi*(x-f0)/f0)/(Np*pi*(x-f0)/f0))**2

            return G_f*absolute(exp(1.0j*k*D)*p[0]*(1+exp(2.0j*k*D1)*(p[1]+1.0j*p[2])+exp(2.0j*k*D2)*(p[1]+1.0j*p[2]))
            +2.0j*w*p[3]*50.0)+p[4]
            #exp(2.0j*k*D)*(p[1]+1.0j*p[2]))+

           
        def resid(p,y,x):
            #return y - onebounce(x,p)
            return y - fourier(x,p)
        pbest=leastsq(resid, p, args=(absolute(widths2[318:876]), frq[318:876]), full_output=1)
        print pbest[0]
        b.line_plot("fourier", frq, fourier(frq, pbest[0]))
        #pi*vf/2*x=D
        #b.line_plot("fft", #frq[318:876]*fft.fftfreq(len(frq[318:876]), d=frq[1]-frq[0]),
        #absolute(fft.fft(widths2[318:876])))
        #b.line_plot("fano", freqs, fano)
        #b.line_plot("fano", freqs2, fano2)

        #f0=5.37e9
        freq=linspace(4e9, 5e9, 1000)
        #G_f=0.5*Np*K2*f0*(sin(Np*pi*(freq-f0)/f0)/(Np*sin(pi*(freq-f0)/f0)))**2
        #b.scatter_plot("freq_test", freqs, freq_diffs)

        class Fitter3(Operative):
            base_name="fitter"
            mult=FloatRange(0.001, 5.0, 0.82).tag(tracking=True)
            f0=FloatRange(4.0, 6.0, 5.348).tag(tracking=True)
            offset=FloatRange(0.0, 100.0, 18.0).tag(tracking=True)

            @tag_Property(plot=True, private=True)
            def G_f(self):
                f0=self.f0*1.0e9
                return self.offset*1e6+self.mult*0.5*Np*K2*f0*(sin(Np*pi*(freq-f0)/f0)/(Np*pi*(freq-f0)/f0))**2

            @observe("f0", "mult", "offset")
            def update_plot(self, change):
                if change["type"]=="update":
                    self.get_member("G_f").reset(self)
                    b.plot_dict["G_f"].clt.set_ydata(self.G_f)
                    b.draw()

        d=Fitter3()
        b.line_plot("G_f", freq, d.G_f, label="theory")
    def magabs_cs_fit():
        def lorentzian(x,p):
            return p[2]*(1.0-1.0/(1.0+((x-p[1])/p[0])**2))+p[3]

        def fano(x, p):
            return p[2]*(((p[4]*p[0]+x-p[1])**2)/(p[0]**2+(x-p[1])**2))+p[3]

        def refl_fano(x, p):
            return p[2]*(1.0-((p[4]*p[0]+x-p[1])**2)/(p[0]**2+(x-p[1])**2))+p[3]

        def onebounce(x,p):
            w=2*pi*x
            k=2.0*pi*x/3488.0
            Cc=0.0
            D=500.0e-6
            D1=300.0e-6
            D2=200.0e-6
            S12q=((p[4]*p[0]/6.28+(x-p[1])*2*pi)**2)/(p[0]**2+(p[4]*p[0]/6.28+x-p[1])**2)
            S11q=1.0/(p[0]**2+(p[4]*p[0]/6.28+x-p[1])**2)
            S11=p[5]
            return p[2]*absolute(exp(1.0j*k*D)*S12q*(1+exp(2.0j*k*D1)*S11q*S11+exp(2.0j*k*D2)*S11q*S11+
            exp(2.0j*k*D)*S11**2*S12q**2)+2.0j*w*Cc*50.0)**2+p[3]

        def allbounces(x,p):
            w=2*pi*x
            k=2.0*pi*x/3488.0
            Cc=0.0
            D=500.0e-6
            D1=300.0e-6
            D2=200.0e-6

            return p[2]*absolute(exp(1.0j*k*D)*S12q*(-2.0
                +1.0/(1.0-exp(2.0j*k*D1)*S11q*S11)
                +1.0/(1.0- exp(2.0j*k*D2)*S11q*S11)
                +1.0/(1- exp(2.0j*k*D)*S11**2*S12q**2))
                +2.0j*w*Cc*50.0)

        def residuals(p,y,x):
            err = y - lorentzian(x,p)
            return err

        def residuals2(p,y,x):
            return y - fano(x,p)

        def residuals3(p,y,x):
            return y - refl_fano(x,p)

        p = [200e6,4.5e9, 0.002, 0.022, 0.1, 0.1]

        indices=[range(81, 120+1), range(137, 260+1), range(269, 320+1), range(411, 449+1)]#, [490]]#, [186]]
        indices=[range(len(a.frequency))]
        widths=[]
        freqs=[]
        freq_diffs=[]
        fanof=[]
        filt=[]
        for n in range(len(yok)):
            myifft=fft.ifft(mag[:,n])
                #b.line_plot("ifft", absolute(myifft))
            myifft[50:-50]=0.0
                #myifft[:20]=0.0
                #myifft[-20:]=0.0
            filt.append(absolute(fft.fft(myifft))**2)
        filt=array(filt).transpose()
        for ind_list in indices:
            for n in ind_list:
                pbest = leastsq(residuals3, p, args=(a.MagAbs[n, :], c.flux_parabola[:]), full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:#n % 8==0:
                    bb.scatter_plot("magabs_flux", c.flux_parabola[:]*1e-9, a.MagAbs[n, :], label="{}".format(n), linewidth=0.2, marker_size=0.8)
                    bb.line_plot("lorentzian", c.flux_parabola*1e-9, refl_fano(c.flux_parabola,best_parameters), label="fit {}".format(n), linewidth=0.5)
                if 1:#absolute(best_parameters[1]-a.frequency[n])<2e8:
                    freqs.append(a.frequency[n])
                    freq_diffs.append(absolute(best_parameters[1]-a.frequency[n]))
                    widths.append(absolute(best_parameters[0]))
                    fanof.append(absolute(best_parameters[4]))
        if 1:
            widths2=[]
            freqs2=[]
            freq_diffs2=[]
            fano2=[]
            flux_over_flux0=qdt.call_func("flux_over_flux0", voltage=yok, offset=-0.037, flux_factor=0.2925)
            Ej=qdt.call_func("Ej", flux_over_flux0=flux_over_flux0)
            flux_par=qdt._get_fq(Ej, qdt.Ec)
            magabs=absolute(mag)**2
            for n in range(len(frq)):
                pbest = leastsq(residuals2,p,args=(filt[n, :], flux_par[:]), full_output=1)
                best_parameters = pbest[0]
                print best_parameters
                if 0:#n==539 or n==554:#n % 10:
                    b.line_plot("magabs_flux", flux_par*1e-9, (magabs[n, :]-best_parameters[3])/best_parameters[2], label="{}".format(n), linewidth=0.2)
                    b.line_plot("lorentzian", flux_par*1e-9, fano(flux_par,best_parameters), label="fit {}".format(n), linewidth=0.5)
                if 1:#absolute(best_parameters[1]-frq[n])<1.5e8:
                    freqs2.append(frq[n])
                    freq_diffs2.append(absolute(best_parameters[1]-frq[n]))
                    widths2.append(absolute(best_parameters[0]))
                    fano2.append(absolute(best_parameters[4]))

        b.line_plot("widths", freqs, widths, label="-110 dBm")
        b.scatter_plot("widths2", freqs2, widths2, color="red", label="-130 dBm")
        vf=3488.0
        p=[1.0001, 0.5, 0.3, 1.0e-15, 0.001]
        Np=9
        K2=0.048
        f0=5.348e9
        def fourier(x, p):
            w=2*pi*x
            k=2.0*pi*x/3488.0
            D=500.0e-6
            D1=300.0e-6
            D2=200.0e-6
            G_f=0.5*Np*K2*f0*(sin(Np*pi*(x-f0)/f0)/(Np*pi*(x-f0)/f0))**2

            return G_f*absolute(exp(1.0j*k*D)*p[0]*(1+exp(2.0j*k*D1)*(p[1]+1.0j*p[2])+exp(2.0j*k*D2)*(p[1]+1.0j*p[2]))
            +2.0j*w*p[3]*50.0)+p[4]
            #exp(2.0j*k*D)*(p[1]+1.0j*p[2]))+


        def resid(p,y,x):
            #return y - onebounce(x,p)
            return y - fourier(x,p)
        #pbest=leastsq(resid, p, args=(absolute(widths2[318:876]), frq[318:876]), full_output=1)
        #print pbest[0]
        #b.line_plot("fourier", frq, fourier(frq, pbest[0]))
        #pi*vf/2*x=D
        from scipy.signal import lombscargle
        #lombscargle(freqs2[318:876], widths2[318:876])
        #bb.line_plot("fft", #frq[318:876]*fft.fftfreq(len(frq[318:876]), d=frq[1]-frq[0]),
        #absolute(fft.fft(widths2[318:876])))
        frqdiffs=linspace(0.01, 500e6, 1000)
        #bb.line_plot("ls", frqdiffs, lombscargle(array(freqs2[285:828]), array(widths2[285:828]), frqdiffs ))
        #bb.line_plot("ls", frqdiffs, lombscargle(array(freqs[318:876]), array(widths[318:876]), frqdiffs))

        bb.line_plot("fft2", absolute(fft.ifft(widths2[285:828])))
        bb.line_plot("fft", absolute(fft.ifft(widths[285:828])))

        bbb=Plotter()
        #bbb.scatter_plot("wid", freqs2, widths2)
        myifft=fft.ifft(widths2[285:828])
        myifft[12:-12]=0.0
        bbb.line_plot("ff", freqs2[285:828], absolute(fft.fft(myifft)))