2015-09-07 06:08:57 +08:00
|
|
|
# Copyright (C) 2014, 2015 M-Labs Limited
|
|
|
|
# Copyright (C) 2014, 2015 Robert Jordens <jordens@gmail.com>
|
|
|
|
|
2015-03-15 23:30:07 +08:00
|
|
|
from copy import copy
|
|
|
|
from math import cos, pi
|
|
|
|
|
2015-04-05 18:42:28 +08:00
|
|
|
from artiq.wavesynth.coefficients import discrete_compensate
|
2015-04-05 17:54:02 +08:00
|
|
|
|
|
|
|
|
2015-03-15 23:30:07 +08:00
|
|
|
class Spline:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.c = [0.0]
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def set_coefficients(self, c):
|
2015-07-24 02:34:54 +08:00
|
|
|
if not c:
|
|
|
|
c = [0.]
|
2015-04-05 18:24:44 +08:00
|
|
|
self.c = copy(c)
|
|
|
|
discrete_compensate(self.c)
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def next(self):
|
|
|
|
r = self.c[0]
|
2015-04-05 15:14:19 +08:00
|
|
|
for i in range(len(self.c) - 1):
|
|
|
|
self.c[i] += self.c[i + 1]
|
2015-03-15 23:30:07 +08:00
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
class SplinePhase:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
2015-03-15 23:30:07 +08:00
|
|
|
self.c = [0.0]
|
2015-03-15 23:48:24 +08:00
|
|
|
self.c0 = 0.0
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def set_coefficients(self, c):
|
2015-07-24 02:34:54 +08:00
|
|
|
if not c:
|
|
|
|
c = [0.]
|
2015-03-16 01:05:03 +08:00
|
|
|
self.c0 = c[0]
|
2015-04-05 18:41:06 +08:00
|
|
|
c1p = c[1:]
|
|
|
|
discrete_compensate(c1p)
|
|
|
|
self.c[1:] = c1p
|
2015-03-15 23:30:07 +08:00
|
|
|
|
2015-03-15 23:48:24 +08:00
|
|
|
def clear(self):
|
|
|
|
self.c[0] = 0.0
|
|
|
|
|
2015-03-15 23:30:07 +08:00
|
|
|
def next(self):
|
2015-04-05 17:54:02 +08:00
|
|
|
r = self.c[0]
|
2015-04-05 15:14:19 +08:00
|
|
|
for i in range(len(self.c) - 1):
|
2015-04-05 17:54:02 +08:00
|
|
|
self.c[i] += self.c[i + 1]
|
|
|
|
self.c[i] %= 1.0
|
|
|
|
return r + self.c0
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DDS:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.amplitude = Spline()
|
|
|
|
self.phase = SplinePhase()
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def next(self):
|
|
|
|
return self.amplitude.next()*cos(2*pi*self.phase.next())
|
|
|
|
|
|
|
|
|
|
|
|
class Wave:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.bias = Spline()
|
|
|
|
self.dds = DDS()
|
2015-04-05 17:54:02 +08:00
|
|
|
self.v = 0.
|
2015-04-05 15:14:19 +08:00
|
|
|
self.silence = False
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def next(self):
|
2015-04-05 15:14:19 +08:00
|
|
|
v = self.bias.next() + self.dds.next()
|
|
|
|
if not self.silence:
|
2015-04-05 17:54:02 +08:00
|
|
|
self.v = v
|
|
|
|
return self.v
|
|
|
|
|
|
|
|
def set_silence(self, s):
|
|
|
|
self.silence = s
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
|
2015-03-16 01:05:03 +08:00
|
|
|
class TriggerError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Synthesizer:
|
|
|
|
def __init__(self, nchannels, program):
|
|
|
|
self.channels = [Wave() for _ in range(nchannels)]
|
2015-04-05 15:14:19 +08:00
|
|
|
self.program = program
|
2015-03-16 01:05:03 +08:00
|
|
|
# line_iter is None: "wait for segment selection" state
|
2015-04-05 17:54:02 +08:00
|
|
|
# otherwise: iterator on the current position in the frame
|
2015-03-16 01:05:03 +08:00
|
|
|
self.line_iter = None
|
|
|
|
|
2015-04-05 17:54:02 +08:00
|
|
|
def select(self, selection):
|
|
|
|
if self.line_iter is not None:
|
2015-04-18 15:12:24 +08:00
|
|
|
raise TriggerError("a frame is already selected")
|
2015-04-05 17:54:02 +08:00
|
|
|
self.line_iter = iter(self.program[selection])
|
|
|
|
self.line = next(self.line_iter)
|
2015-03-16 01:05:03 +08:00
|
|
|
|
2015-04-05 17:54:02 +08:00
|
|
|
def trigger(self):
|
|
|
|
if self.line_iter is None:
|
2015-04-18 15:12:24 +08:00
|
|
|
raise TriggerError("no frame selected")
|
2015-03-16 01:05:03 +08:00
|
|
|
|
2015-04-05 17:54:02 +08:00
|
|
|
line = self.line
|
|
|
|
if not line.get("trigger", False):
|
2015-04-18 15:12:24 +08:00
|
|
|
raise TriggerError("segment is not triggered")
|
2015-03-16 01:05:03 +08:00
|
|
|
|
2015-04-05 17:54:02 +08:00
|
|
|
r = [[] for _ in self.channels]
|
|
|
|
while True:
|
2015-03-16 01:05:03 +08:00
|
|
|
for channel, channel_data in zip(self.channels,
|
|
|
|
line["channel_data"]):
|
|
|
|
if "bias" in channel_data:
|
2015-04-05 17:54:02 +08:00
|
|
|
channel.bias.set_coefficients(
|
|
|
|
channel_data["bias"]["amplitude"])
|
2015-03-16 01:05:03 +08:00
|
|
|
if "dds" in channel_data:
|
|
|
|
channel.dds.amplitude.set_coefficients(
|
|
|
|
channel_data["dds"]["amplitude"])
|
2015-04-05 15:14:19 +08:00
|
|
|
if "phase" in channel_data["dds"]:
|
|
|
|
channel.dds.phase.set_coefficients(
|
|
|
|
channel_data["dds"]["phase"])
|
|
|
|
if channel_data["dds"].get("clear", False):
|
2015-03-16 01:05:03 +08:00
|
|
|
channel.dds.phase.clear()
|
2015-04-05 17:54:02 +08:00
|
|
|
channel.set_silence(channel_data.get("silence", False))
|
|
|
|
|
|
|
|
if line.get("dac_divider", 1) != 1:
|
|
|
|
raise NotImplementedError
|
2015-03-16 01:05:03 +08:00
|
|
|
|
|
|
|
for channel, rc in zip(self.channels, r):
|
|
|
|
for i in range(line["duration"]):
|
|
|
|
rc.append(channel.next())
|
|
|
|
|
2015-04-05 17:54:02 +08:00
|
|
|
try:
|
|
|
|
self.line = line = next(self.line_iter)
|
|
|
|
if line.get("trigger", False):
|
|
|
|
return r
|
|
|
|
except StopIteration:
|
2015-03-16 01:05:03 +08:00
|
|
|
self.line_iter = None
|
|
|
|
return r
|