def fit(f, xdata, ydata, p0=None, bounds=(-np.inf, np.inf), tfit=None, name=''): fit_data = munch.Munch() fit_data.popt, fit_data.pcov = optimize.curve_fit(f, xdata, ydata, p0=p0, bounds=bounds) fit_data.x = f(xdata, *fit_data.popt) fit_data.resid = ydata - fit_data.x if tfit is None: tfit = xdata fs = 1.0 / np.mean(np.diff(tfit)) li = lockin.LockIn(tfit, fit_data.x, fs) li.lock2() li.phase(tf=0) fit_data.li = li fit_data.name = name fit_data.li.name = name return fit_data
def signal_average_parab_list(gr_list, ti, tf, invert=True, align_voltage=False): """Utility function to signal average a group from an HDF5 file.""" b = munch.Munch() xs = [] ts = [] if invert: scale = -1 else: scale = 1 for ds in tqdm.tqdm(gr_list): # Move the zero in time to the initial voltage pulse x = ds['cantilever-nm'][:] dt = ds['dt [s]'].value if align_voltage: t1 = ds.attrs['Abrupt BNC565 CantClk.t1 [s]'] t0 = -t1 - ds["half periods [s]"][0] t = np.arange(x.size) * dt + t0 else: t = pk.gr2t(ds) m = (t > ti) & (t < tf) ts.append(t[m]) x = ds['cantilever-nm'][:] xs.append(scale * x[m]) ts = np.array(ts) b.t = np.mean(ts, axis=0) xs = np.array(xs) # Do proper signal averaging, fitting data to a parabola at each point x_err = np.zeros(xs.shape[1]) x_bf = np.zeros(xs.shape[1]) for i, (tr, xr) in tqdm.tqdm(enumerate(zip(ts.T, xs.T))): x, err = avg_xt(tr, xr) x_err[i] = err x_bf[i] = x b.x = x_bf b.x_err = x_err m2 = b.t < 0.0 b.x0neg = b.x[m2].mean() b.x = b.x - b.x0neg b.t_ms = b.t * 1e3 b.t_us = b.t * 1e6 b.t5 = b.t[500:] b.x5 = b.x[500:] b.t5ms = b.t5 * 1e3 b.t5us = b.t5 * 1e6 b.li = lockin.LockIn(b.t, b.x, 1e6) b.li.lock2() b.li.phase(tf=0) b.li.name = 'data' return b
def signal_average_gr(gr, ti, tf): """Utility function to signal average a group from an HDF5 file.""" b = munch.Munch() xs = [] ts = [] for ds in tqdm.tqdm(gr.values()): t = pk.gr2t(ds) t1 = ds.attrs['Abrupt BNC565 CantClk.t1 [s]'] t0 = -t1 - ds["half periods [s]"][0] m = (t > ti) & (t < tf) ts.append(t[m]) x = ds['cantilever-nm'][:] xs.append(x[m]) ts = np.array(ts) b.t = np.mean(ts, axis=0) x_array = np.array(xs) b.x = np.mean(x_array, axis=0) m2 = b.t < 0.0 b.x = b.x - b.x[m2].mean() b.t_ms = b.t * 1e3 b.t_us = b.t * 1e6 b.t5 = b.t[500:] b.x5 = b.x[500:] b.t5ms = b.t5 * 1e3 b.t5us = b.t5 * 1e6 b.li = lockin.LockIn(b.t, b.x, 1e6) b.li.lock2() b.li.phase(tf=0) b.li.name = 'data' return b
def gr2locks(top_gr): locks = [] for gr in tqdm(top_gr.values()): half_periods = gr["half periods [s]"][:] N2even = gr.attrs['Calc BNC565 CantClk.N2 (even)'] t1 = gr.attrs['Abrupt BNC565 CantClk.t1 [s]'] t2 = np.sum(gr["half periods [s]"][:N2even + 1]) t0 = -(t1 + t2) x = gr['cantilever-nm'][:] dt = gr['dt [s]'].value t = np.arange(x.size) * dt + t0 lock = lockin.LockIn(t, x, 1 / dt) lock.lock2(fp=5000, fc=15000, print_response=False) lock.phase(ti=3e-3, tf=7e-3) locks.append(lock) return locks
def delay_dA_dphi(top_gr, tp, tLi, tLf, tRi, tRf): dAs = [] dPhis = [] delays = [] for ds_name, gr in tqdm(top_gr.items()): half_periods = gr["half periods [s]"][:] N2even = gr.attrs['Calc BNC565 CantClk.N2 (even)'] t1 = gr.attrs['Abrupt BNC565 CantClk.t1 [s]'] t2 = np.sum(gr["half periods [s]"][:N2even + 1]) t0 = -(t1 + t2) x = gr['cantilever-nm'][:] dt = gr['dt [s]'].value t = np.arange(x.size) * dt + t0 li = lockin.LockIn(t, x, 1 / dt) li.lock2(fp=5000, fc=15000, print_response=False) li.phase(ti=3e-3, tf=7e-3) dA, dphi = measure_dA_dphi(li, tp, tLi, tLf, tRi, tRf) dAs.append(dA) dPhis.append(dphi) delays.append(gr.attrs['Abrupt BNC565 CantClk.D tip [s]']) return delays, dAs, dPhis
df.loc[idx[200, :], 'dPhi'] = dPhis # In[4]: locks = [] fir = lockin.lock2(62000, 5000, 15000, 1e6) for gr in tqdm(fh800['data'].values()): half_periods = gr["half periods [s]"][:] N2even = gr.attrs['Calc BNC565 CantClk.N2 (even)'] t1 = gr.attrs['Abrupt BNC565 CantClk.t1 [s]'] t2 = np.sum(gr["half periods [s]"][:N2even + 1]) t0 = -(t1 + t2) x = gr['cantilever-nm'][:] dt = gr['dt [s]'].value t = np.arange(x.size) * dt + t0 lock = lockin.LockIn(t, x, 1 / dt) lock.lock2(fp=5000, fc=15000, print_response=False) lock.phase(ti=3e-3, tf=7e-3) locks.append(lock) m0 = phasekick.masklh(t, -5e-6, 5e-6) # In[5]: with mpl.rc_context(rcParams): fig, ax2 = plt.subplots(figsize=(1.4, 0.9)) lock = locks[48] m2 = phasekick.masklh(lock.t, -1e-6, 8e-6) m = phasekick.masklh(lock.t, -3e-3, 4.6e-3) t_many = np.linspace(lock.t[m2][0], lock.t[m2][-1], 3001) * 1e6 ti = 16 / 4.0