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
|
|
|
|
|
|
|
from artiq import *
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.attr_argument("npoints", FreeValue(100))
|
|
|
|
self.attr_argument("min_freq", FreeValue(1000))
|
|
|
|
self.attr_argument("max_freq", FreeValue(2000))
|
2015-01-13 19:12:35 +08:00
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
self.attr_argument("F0", FreeValue(1500))
|
|
|
|
self.attr_argument("noise_amplitude", FreeValue(0.1))
|
2015-01-21 14:35:37 +08:00
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
self.frequency = self.set_result("flopping_f_frequency", [], True)
|
|
|
|
self.brightness = self.set_result("flopping_f_brightness", [], True)
|
2015-01-13 19:12:35 +08:00
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
self.attr_device("scheduler")
|
2015-01-13 19:12:35 +08:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
for i in range(self.npoints):
|
|
|
|
frequency = (self.max_freq-self.min_freq)*i/(self.npoints - 1) + self.min_freq
|
2015-01-21 14:35:37 +08:00
|
|
|
brightness = model(frequency, self.F0) + self.noise_amplitude*random.random()
|
2015-01-13 19:12:35 +08:00
|
|
|
self.frequency.append(frequency)
|
|
|
|
self.brightness.append(brightness)
|
|
|
|
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-01-31 16:56:38 +08:00
|
|
|
popt, pcov = curve_fit(model_numpy,
|
2015-01-21 14:35:37 +08:00
|
|
|
self.frequency.read, self.brightness.read,
|
2015-07-14 04:08:20 +08:00
|
|
|
p0=[self.get_parameter("flopping_freq")])
|
2015-01-21 14:35:37 +08:00
|
|
|
perr = np.sqrt(np.diag(pcov))
|
|
|
|
if perr < 0.1:
|
2015-07-14 04:08:20 +08:00
|
|
|
self.set_parameter("flopping_freq", float(popt))
|