def _default_trace_plot(self): tp=Plotter(name=self.name+' trace plot') print self.freq print absolute(self.S21) print shape(self.freq) print shape(self.S21) tp.line_plot('trace_mag S21', self.freq, absolute(self.S21))#S20.0*log10(absolute(self.S21))) return tp
def _default_trace_plot(self): tp = Plotter(name=self.name + ' trace plot') print self.freq print absolute(self.S21) print shape(self.freq) print shape(self.S21) tp.line_plot('trace_mag S21', self.freq, absolute(self.S21)) #S20.0*log10(absolute(self.S21))) return tp
#return [absolute(Ga/(Ga+1.0j*w*Ct+1.0/(1.0j*w*l)))**2 for l in L] #return [absolute(Ga/(Ga+1.0j*Ba+1.0j*w*Ct+1.0/(1.0j*w*l)))**2 for l in L]#+1.0j*(VcdivV)*w*Cc) #return [c.flux_parabola[argmax(absolute(Ga/(Ga+1.0j*Ba+1.0j*w*Ct+1.0/(1.0j*w*l))), axis=0)] for l in L]#+1.0j*(VcdivV)*w*Cc) d=Fitter2() #b.scatter_plot("fluxtry", a.frequency, c.flux_parabola[argmax(array(d.R).transpose(), axis=1)]) #b.colormesh("fluxtry", a.yoko, a.frequency, array(d.R[0]).transpose()+array(d.R[1]).transpose()) b.colormesh("fluxtry2", a.yoko, a.frequency, array(d.R[0]).transpose()) #b.line_plot("fluxtry", a.frequency, d.R)#.transpose()) #b.line_plot("fluxtry", a.frequency, d.R[1])#.transpose()) if 0: from numpy import exp, pi, sqrt, sin, log10, log b.line_plot("off res", a.frequency, 10.0*log10(absolute(a.Magcom[:, 300])), linewidth=0.5) f=linspace(4.0e9, 5.0e9, 5000) class Fitter2(Operative): base_name="fitter" vf=FloatRange(3000.0, 4000.0, 3488.0).tag(tracking=True) tD=FloatRange(0.0, 2000.0, 500.0).tag(tracking=True) ZS=FloatRange(10.0, 100.0, 44.38).tag(tracking=True) epsinf=FloatRange(1.0, 10.0, 2.989).tag(tracking=True) K2=FloatRange(0.01, 0.1, 0.02458).tag(tracking=True) f0=FloatRange(4.0, 5.0, 4.447).tag(tracking=True) Cc=FloatRange(0.00001, 100.0, 26.5).tag(tracking=True) bg_off=FloatRange(-50.0, 0.0, -24.0).tag(tracking=True) bg_slope=FloatRange(-10.0, 10.0, 0.0).tag(tracking=True) apwr=Float(1.9)
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)))
@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) b3.plot_dict["G_f"].clt.set_ydata(self.G_f) b3.draw() d = Fitter3() b3.line_plot("G_f", freq, d.G_f, label="theory") #b.colormesh("magabs2", yok, frq, absolute(mag)) #b.line_plot("bg", bgf, bgmc/dB) def magdB_colormesh(): b.colormesh("magdB", a.yoko, a.frequency, a.MagdB) b.line_plot("flux_parabola", c.yoko, c.flux_parabola, color="orange", alpha=0.4) b.set_ylim(4e9, 5.85e9) b.xlabel = "Yoko (V)" b.ylabel = "Frequency (Hz)" b.title = "Reflection fluxmap"
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")
#qdt.get_member("mult").reset(qdt) #qdt.get_member("lbda0").reset(qdt) print qdt.f0, qdt.G_f0 if 0: #G_f=(1.0/sqrt(2.0))*0.5*qdt.Np*qdt.K2*qdt.f0*absolute(sinc(qdt.Np*pi*(freq-qdt.f0)/qdt.f0)) #b.line_plot("sinc", freq, G_f, label="sinc/sqrt(2)") #G_f=0.5*qdt.Np*qdt.K2*qdt.f0*absolute(sinc(qdt.Np*pi*(freq-qdt.f0)/qdt.f0)) #b.line_plot("sinc2", freq, G_f, label="sinc") Np = 9 K2 = 0.048 f0 = 2 * 5.45e9 G_f = 0.5 * Np * K2 * f0 * (sin(Np * pi * (freq - f0) / f0) / (Np * sin(pi * (freq - f0) / f0)))**2 b.line_plot("sine_sq", freq, G_f, label="sine_sq") G_f = 0.5 * Np * K2 * f0 * sinc_sq(Np * pi * (freq - f0) / f0) b.line_plot("sinc_sq", freq, G_f, label="sinc_sq") G = 1.0e9 R = 1 / (1 + (2 * (freq - f0) / G)**2) b.line_plot("Rsq", freq, 0.5 * Np * K2 * f0 * R, label="R_sq") b.vline_plot("listen", 4.475e9, alpha=0.3, color="black") b.vline_plot("listent", 4.55e9, alpha=0.3, color="black") b.vline_plot("listenb", 4.4e9, alpha=0.3, color="black") if 1: Np = 9 K2 = 0.048 f0 = 5.45e9 freq = 4.48e9
#qdt.ft="single" #qdt.get_member("mult").reset(qdt) #qdt.get_member("lbda0").reset(qdt) print qdt.f0, qdt.G_f0 if 0: #G_f=(1.0/sqrt(2.0))*0.5*qdt.Np*qdt.K2*qdt.f0*absolute(sinc(qdt.Np*pi*(freq-qdt.f0)/qdt.f0)) #b.line_plot("sinc", freq, G_f, label="sinc/sqrt(2)") #G_f=0.5*qdt.Np*qdt.K2*qdt.f0*absolute(sinc(qdt.Np*pi*(freq-qdt.f0)/qdt.f0)) #b.line_plot("sinc2", freq, G_f, label="sinc") Np=9 K2=0.048 f0=2*5.45e9 G_f=0.5*Np*K2*f0*(sin(Np*pi*(freq-f0)/f0)/(Np*sin(pi*(freq-f0)/f0)))**2 b.line_plot("sine_sq", freq, G_f, label="sine_sq") G_f=0.5*Np*K2*f0*sinc_sq(Np*pi*(freq-f0)/f0) b.line_plot("sinc_sq", freq, G_f, label="sinc_sq") G=1.0e9 R=1/(1+(2*(freq-f0)/G)**2) b.line_plot("Rsq", freq, 0.5*Np*K2*f0*R, label="R_sq") b.vline_plot("listen", 4.475e9, alpha=0.3, color="black") b.vline_plot("listent", 4.55e9, alpha=0.3, color="black") b.vline_plot("listenb", 4.4e9, alpha=0.3, color="black") if 1: Np=9 K2=0.048 f0=5.45e9 freq=4.48e9
print get_tag(qdt, "a", "unit") print qdt.latex_table() from taref.plotter.fig_format import Plotter from taref.physics.fundamentals import sinc, sinc_sq,e print e b=Plotter() from numpy import linspace, pi, absolute, sqrt freq=linspace(1e9, 10e9, 1000) #qdt.ft="single" #qdt.get_member("mult").reset(qdt) #qdt.get_member("lbda0").reset(qdt) print qdt.f0, qdt.G_f0 if 0: G_f=(1.0/sqrt(2.0))*0.5*qdt.Np*qdt.K2*qdt.f0*absolute(sinc(qdt.Np*pi*(freq-qdt.f0)/qdt.f0)) b.line_plot("sinc", freq, G_f, label="sinc/sqrt(2)") G_f=0.5*qdt.Np*qdt.K2*qdt.f0*absolute(sinc(qdt.Np*pi*(freq-qdt.f0)/qdt.f0)) b.line_plot("sinc2", freq, G_f, label="sinc") G_f=0.5*qdt.Np*qdt.K2*qdt.f0*sinc_sq(qdt.Np*pi*(freq-qdt.f0)/qdt.f0) b.line_plot("sinc_sq", freq, G_f, label="sinc_sq") b.vline_plot("listen", 4.475e9, alpha=0.3, color="black") b.vline_plot("listent", 4.55e9, alpha=0.3, color="black") b.vline_plot("listenb", 4.4e9, alpha=0.3, color="black") if 0: freq=4.475e9 f0=linspace(5e9, 6e9, 1000) G_f=(1.0/sqrt(2.0))*0.5*qdt.Np*qdt.K2*f0*absolute(sinc(qdt.Np*pi*(freq-f0)/f0)) b.line_plot("sinc", f0, G_f, label="sinc/sqrt(2)") G_f=0.5*qdt.Np*qdt.K2*f0*absolute(sinc(qdt.Np*pi*(freq-f0)/f0)) b.line_plot("sinc2", f0, G_f, label="sinc") G_f=0.5*qdt.Np*qdt.K2*f0*sinc_sq(qdt.Np*pi*(freq-f0)/f0)
err = y - lorentzian(x,p) return err #ind_bg_low = (x > min(x)) & (x < 450.0) #ind_bg_high = (x > 590.0) & (x < max(x)) #x_bg = concatenate((x[ind_bg_low],x[ind_bg_high])) #y_bg = concatenate((y[ind_bg_low],y[ind_bg_high])) #m, c = polyfit(x_bg, y_bg, 1) #background = m*x + c #y_bg_corr = y - background # initial values # p = [200e6,4.5e9, 0.02, 0.0007] # [hwhm, peak center, intensity] # # optimization # offset=0.001 pbest = leastsq(residuals,p,args=(a.MagAbs[63, 1294:1545]-offset, c.flux_parabola[1294:1545]), full_output=1) best_parameters = pbest[0] print pbest[0] # fit to data # fit = lorentzian(c.flux_parabola[800:2500], best_parameters) b.line_plot("lorentzian", c.flux_parabola[800:2500]*1e-9, fit+offset) #magabs_colormesh() #magabs_cs2() shower(b)
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)))
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")