2015-01-13 19:12:35 +08:00
|
|
|
from math import sqrt, cos, pi
|
|
|
|
import time
|
2015-01-21 14:35:37 +08:00
|
|
|
import random
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
from scipy.optimize import curve_fit
|
2015-01-13 19:12:35 +08:00
|
|
|
|
2016-01-26 07:03:01 +08:00
|
|
|
from artiq.experiment import *
|
2015-01-13 19:12:35 +08:00
|
|
|
|
|
|
|
|
2015-01-21 14:35:37 +08:00
|
|
|
def model(x, F0):
|
|
|
|
t = 0.02
|
|
|
|
tpi = 0.03
|
|
|
|
A = 80
|
|
|
|
B = 40
|
2015-01-13 19:12:35 +08:00
|
|
|
return A+(B-A)/2/(4*tpi**2*(x-F0)**2+1)*(1-cos(pi*t/tpi*sqrt(4*tpi**2*(x-F0)**2+1)))
|
|
|
|
|
|
|
|
|
2015-01-31 16:56:38 +08:00
|
|
|
def model_numpy(xdata, F0):
|
|
|
|
r = np.zeros(len(xdata))
|
|
|
|
for i, x in enumerate(xdata):
|
|
|
|
r[i] = model(x, F0)
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
class FloppingF(EnvExperiment):
|
2015-03-08 22:43:04 +08:00
|
|
|
"""Flopping F simulation"""
|
2015-02-21 05:01:34 +08:00
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
def build(self):
|
2015-10-04 00:18:21 +08:00
|
|
|
self.setattr_argument("frequency_scan", Scannable(
|
2015-07-25 00:35:48 +08:00
|
|
|
default=LinearScan(1000, 2000, 100)))
|
2015-01-13 19:12:35 +08:00
|
|
|
|
2015-10-04 00:18:21 +08:00
|
|
|
self.setattr_argument("F0", NumberValue(1500, min=1000, max=2000))
|
|
|
|
self.setattr_argument("noise_amplitude", NumberValue(0.1, min=0, max=100,
|
2015-08-06 18:43:54 +08:00
|
|
|
step=0.01))
|
2015-01-13 19:12:35 +08:00
|
|
|
|
2015-10-04 00:18:21 +08:00
|
|
|
self.setattr_device("scheduler")
|
2015-01-13 19:12:35 +08:00
|
|
|
|
|
|
|
def run(self):
|
2015-10-26 00:33:11 +08:00
|
|
|
l = len(self.frequency_scan)
|
2016-03-29 16:26:14 +08:00
|
|
|
self.set_dataset("flopping_f_frequency",
|
|
|
|
np.full(l, np.nan),
|
|
|
|
broadcast=True, save=False)
|
|
|
|
self.set_dataset("flopping_f_brightness",
|
|
|
|
np.full(l, np.nan),
|
|
|
|
broadcast=True)
|
2015-10-26 00:33:11 +08:00
|
|
|
self.set_dataset("flopping_f_fit", np.full(l, np.nan),
|
|
|
|
broadcast=True, save=False)
|
2015-08-06 18:43:54 +08:00
|
|
|
|
2015-10-26 00:33:11 +08:00
|
|
|
for i, f in enumerate(self.frequency_scan):
|
2015-08-06 18:53:14 +08:00
|
|
|
m_brightness = model(f, self.F0) + self.noise_amplitude*random.random()
|
2016-03-29 16:26:14 +08:00
|
|
|
self.mutate_dataset("flopping_f_frequency", i, f)
|
|
|
|
self.mutate_dataset("flopping_f_brightness", i, m_brightness)
|
2015-01-13 19:12:35 +08:00
|
|
|
time.sleep(0.1)
|
2015-05-17 16:11:00 +08:00
|
|
|
self.scheduler.submit(self.scheduler.pipeline_name, self.scheduler.expid,
|
2015-05-28 17:20:58 +08:00
|
|
|
self.scheduler.priority, time.time() + 20, False)
|
2015-02-20 03:10:19 +08:00
|
|
|
|
2015-01-13 19:12:35 +08:00
|
|
|
def analyze(self):
|
2015-10-12 17:18:23 +08:00
|
|
|
# Use get_dataset so that analyze can be run stand-alone.
|
|
|
|
frequency = self.get_dataset("flopping_f_frequency")
|
|
|
|
brightness = self.get_dataset("flopping_f_brightness")
|
2015-01-31 16:56:38 +08:00
|
|
|
popt, pcov = curve_fit(model_numpy,
|
2015-08-06 18:43:54 +08:00
|
|
|
frequency, brightness,
|
2015-10-26 00:33:11 +08:00
|
|
|
p0=[self.get_dataset("flopping_freq", 1500.0)])
|
2015-01-21 14:35:37 +08:00
|
|
|
perr = np.sqrt(np.diag(pcov))
|
|
|
|
if perr < 0.1:
|
2015-08-06 18:43:54 +08:00
|
|
|
F0 = float(popt)
|
2015-10-12 17:18:23 +08:00
|
|
|
self.set_dataset("flopping_freq", F0, persist=True, save=False)
|
|
|
|
self.set_dataset("flopping_f_fit",
|
2015-10-26 00:33:11 +08:00
|
|
|
np.array([model(x, F0) for x in frequency]),
|
2015-10-12 17:18:23 +08:00
|
|
|
broadcast=True, save=False)
|