In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import os
Load file
In [31]:
filepath = r'C:\Users\ChildressLab\Desktop\Rasmus notes\Measurements'
filename = '2017-08-14_141938_full_sweep_data'
delimiter = '\t'
with open(os.path.join(filepath, filename), 'rb') as file:
data = np.loadtxt(file, delimiter=delimiter)
times = data[0:4,2000:]
volts = data[4:8,2000:]
In [32]:
np.argmin(volts[1])
volts[1]
Out[32]:
In [34]:
total_trace = 2 # sec
ramp_period = 1.5 #sec
period_index = len(times[0])* ramp_period / total_trace
ramp_mid = np.argmin(volts[1])
low_index = ramp_mid - int(period_index/2)
high_index = ramp_mid + int(period_index/2)
print(ramp_mid, low_index, high_index)
volts_trim = volts[:,low_index:high_index]
times_trim = times[:,low_index:high_index]
plt.plot(times_trim[3],volts_trim[3]/np.mean(volts_trim[3]))
plt.plot(times_trim[1],volts_trim[1])
plt.plot(times_trim[0],volts_trim[0]/np.mean(volts_trim[0]))
plt.show()
Fit scan
In [35]:
# Initial guess
freq0 = 2/3
start0 = 0.0
stop0 = -3.0
phase0 = 0.0
#Fitting setup
parameter_guess = [start0, stop0, freq0, phase0]
func = mynicard.sweep_function
xdata = times_trim[1]
ydata = volts_trim[1]
#Actual fitting
popt, pcov = curve_fit(func, xdata, ydata, parameter_guess)
In [ ]:
# Guess plot
ramp = mynicard.sweep_function(times[1], start0, stop0, freq0, phase0)
plt.plot(times_trim[1],volts_trim[1])
plt.plot(times[1],ramp)
plt.show()
# fit plot
plt.plot(times_trim[1],volts_trim[1])
plt.plot(xdata, func(xdata, *popt), 'r-', linewidth = 3, label='fit')
plt.show()
print(popt)
In [ ]: