Skip to content

Instantly share code, notes, and snippets.

@ggggggggg
Last active August 29, 2015 14:14
Show Gist options
  • Save ggggggggg/06a33f11b27762653e9a to your computer and use it in GitHub Desktop.
Save ggggggggg/06a33f11b27762653e9a to your computer and use it in GitHub Desktop.
sampling scope time zero
import numpy as np
import pylab as plt
from scipy.optimize import curve_fit
import gpib_instrument
scope = gpib_instrument.Gpib_Instrument(pad=3)
scope.ask("OUT TRA1")
def ask(s):
scope.write(s)
r = ""
while '\x00' not in r:
r+=scope.read()
return r[:r.find('\x00')-1]
def getxinfo():
r = ask("WFMP?")
r2=r.split(",",10000)
d={c.split(":")[0]:c.split(":")[1] for c in r2}
xincr = float(d["XINCR"])
ymult = float(d["YMULT"])
return xincr, ymult
def askcurv():
d = ask("CURV?")
d2=d.split(",",1000000)
curveid = d2.pop(0)
print(curveid)
d3 = [int(q) for q in d2]
d3 = d3[:-1]
xincr,ymult = getxinfo()
return np.arange(len(d3))*xincr, np.array(d3)*ymult
def model(x,baseline, amp, t0, tau1, tau2):
# model the turn on as an exponential
t=x-t0
# following http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1485918
return baseline+amp*(np.exp(t/tau1)+tau2/tau1*(np.exp(t/tau1)-np.exp(t/tau2)))*np.greater(t,0)
def model_simple(x, baseline, amp, t0, tau):
t = x-t0
return baseline*np.less_equal(x,0)+(amp+(baseline-amp)*np.exp(-t/tau))*np.greater(t,0)
def dofit(x,y, guesses=[1,10, 30, 40,50]):
p_guess = np.array(guesses, dtype="float64")
firstnotuse = plt.find(y>np.amax(y)*0.5)[0]
useinds = np.arange(firstnotuse)
xfit = x[useinds]
yfit = y[useinds]
p_fit, p_covar =curve_fit(model, xfit, yfit, p0=p_guess)
p_names = ["baseline", "amplitude", "t0", "tau1", "tau2"]
if np.shape(p_covar)==():
p_covar = np.eye(len(p_fit))*np.inf
for i in xrange(len(p_fit)):
print("%s %g +/- %g"%(p_names[i], p_fit[i], np.sqrt(p_covar[i,i])))
plt.figure()
plt.plot(x,y,label="trace")
plt.plot(x[useinds], y[useinds],"o",label="used for fit")
plt.plot(x[useinds], model(x[useinds],p_fit[0],p_fit[1],p_fit[2],p_fit[3],p_fit[4]),"s",label="fit")
plt.plot([p_fit[2],p_fit[2]], plt.ylim())
plt.xlim(x[0],x[-1])
plt.legend()
plt.show()
plt.ion()
data = np.loadtxt("apdrise")
x=data[0,:]
y=data[1,:]
y-=y[0]
plt.plot(x,y)
firstnotuse = plt.find(y>np.amax(y)*0.5)[0]
plt.plot(x[:firstnotuse],model(x[:firstnotuse], 0, 0.0002, 0.4e-9,50e-12,100e-12))
plt.plot(x[:firstnotuse],model_simple(x[:firstnotuse], 0, 0.0075, 0.4e-9,50e-12))
dofit(x,y,[0, 0.0002, 0.4e-9,50e-12,100e-12])
tau_rc = 35e-12
ymodel=model(x, 0,0.000217, 396e-12, 59.5e-12, 144e-12 )
yft = np.fft.rfft(ymodel)
f = np.fft.rfftfreq(len(y), x[1]-x[0])
cf = np.sqrt(1/(1+(f*tau_rc)**2))
ysm = np.fft.irfft(cf*yft,len(y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment