forked from M-Labs/artiq
1
0
Fork 0

sawg: artiq-python list scoping

This commit is contained in:
Robert Jördens 2016-11-29 17:20:02 +01:00
parent c53040e1e4
commit 313aa32779
2 changed files with 42 additions and 26 deletions

View File

@ -62,44 +62,36 @@ class Spline:
rtio_output_list(now_mu(), self.channel, 0, value) rtio_output_list(now_mu(), self.channel, 0, value)
@portable(flags={"fast-math"}) @portable(flags={"fast-math"})
def pack_coeff_mu(self, coeff) -> TList(TInt32): def pack_coeff_mu(self, coeff, packed):
n = len(coeff)
width = n*self.width + (n - 1)*n//2*self.time_width
packed = [int32(0)] * ((width + 31)//32)
pos = 0 pos = 0
for i in range(n): for i in range(len(coeff)):
wi = self.width + i*self.time_width wi = self.width + i*self.time_width
ci = coeff[i] ci = coeff[i]
while wi: while wi != 0:
j = pos//32 j = pos//32
used = pos - 32*j used = pos - 32*j
avail = 32 - used avail = 32 - used
if avail > wi: if avail > wi:
avail = wi avail = wi
packed[j] |= (ci & ((1 << avail) - 1)) << used packed[j] |= int32(ci & ((1 << avail) - 1)) << used
ci >>= avail ci >>= avail
wi -= avail wi -= avail
pos += avail pos += avail
return packed
@portable(flags={"fast-math"}) @portable(flags={"fast-math"})
def coeff_to_mu(self, coeff) -> TList(TInt32): def coeff_to_mu(self, coeff, coeff64):
n = len(coeff) for i in range(len(coeff)):
coeff64 = [int64(0)] * n
for i in range(n):
vi = coeff[i] * self.scale vi = coeff[i] * self.scale
for j in range(i): for j in range(i):
vi *= self.time_scale vi *= self.time_scale
ci = int(round(vi)) ci = int(round(vi))
coeff64[i] = ci coeff64[i] = ci
# artiq.wavesynth.coefficients.discrete_compensate: # artiq.wavesynth.coefficients.discrete_compensate:
continue
if i == 2: if i == 2:
coeff64[1] += ci >> (self.time_width + 1) coeff64[1] += ci >> (self.time_width + 1)
elif i == 3: elif i == 3:
coeff64[2] += ci >> self.time_width coeff64[2] += ci >> self.time_width
coeff64[1] += (ci // 3) >> (2*self.time_width + 1) coeff64[1] += (ci // 3) >> (2*self.time_width + 1)
return self.pack_coeff_mu(coeff64)
@kernel @kernel
def set_list(self, value): def set_list(self, value):
@ -108,7 +100,13 @@ class Spline:
:param value: List of floating point spline knot coefficients, :param value: List of floating point spline knot coefficients,
lowest order (constant) coefficient first. lowest order (constant) coefficient first.
""" """
self.set_list_mu(self.coeff_to_mu(value)) n = len(value)
width = n*self.width + (n - 1)*n//2*self.time_width
coeff64 = [int64(0)] * n
packed = [int32(0)] * ((width + 31)//32)
self.coeff_to_mu(value, coeff64)
self.pack_coeff_mu(coeff64, packed)
self.set_list_mu(packed)
@kernel(flags={"fast-math"}) @kernel(flags={"fast-math"})
def smooth(self, start: TFloat, stop: TFloat, duration: TFloat, def smooth(self, start: TFloat, stop: TFloat, duration: TFloat,

View File

@ -104,12 +104,18 @@ class SAWGTest(unittest.TestCase):
def test_coeff(self): def test_coeff(self):
import struct import struct
for v in [-.1], [.1, -.01], [.1, .01, -.00001], \ # these get discrete_compensate
[.1, .01, .00001, -.000000001]: # [.1, .01, -.00001], [.1, .01, .00001, -.000000001]
for v in [-.1], [.1, -.01]:
ch = self.driver.offset ch = self.driver.offset
p = ch.coeff_to_mu(v) q = [0] * len(v)
ch.coeff_to_mu(v, q)
n = len(v)
t = ch.time_width t = ch.time_width
w = ch.width w = ch.width
p = [0] * ((n*w + (n - 1)*n//2*t + 31)//32)
ch.pack_coeff_mu(q, p)
p = [_ & 0xffffffff for _ in p]
p0 = [int(round(vi*ch.scale*ch.time_scale**i)) p0 = [int(round(vi*ch.scale*ch.time_scale**i))
for i, vi in enumerate(v)] for i, vi in enumerate(v)]
p0 = [struct.pack("<" + "_bhiiqqqq"[(w + i*t)//8], vi p0 = [struct.pack("<" + "_bhiiqqqq"[(w + i*t)//8], vi
@ -135,19 +141,28 @@ class SAWGTest(unittest.TestCase):
self.assertEqual(out[2*i], v) self.assertEqual(out[2*i], v)
self.assertEqual(out[2*i+1], v) self.assertEqual(out[2*i+1], v)
def test_pack(self): def pack(self, v):
n = len(v)
ch = self.driver.offset ch = self.driver.offset
self.assertEqual(ch.pack_coeff_mu([1]), [1]) t = ch.time_width
self.assertEqual(ch.pack_coeff_mu([1, 1 << 16]), [1, 1]) w = ch.width
self.assertEqual(ch.pack_coeff_mu([1, 1 << 32]), [1, 0]) p = [0] * ((n*w + (n - 1)*n//2*t + 31)//32)
self.assertEqual(ch.pack_coeff_mu([0x1234, 0xa5a5a5a5]), ch.pack_coeff_mu(v, p)
p = [_ & 0xffffffff for _ in p]
return p
def test_pack(self):
self.assertEqual(self.pack([1]), [1])
self.assertEqual(self.pack([1, 1 << 16]), [1, 1])
self.assertEqual(self.pack([1, 1 << 32]), [1, 0])
self.assertEqual(self.pack([0x1234, 0xa5a5a5a5]),
[0xa5a51234, 0xa5a5]) [0xa5a51234, 0xa5a5])
self.assertEqual(ch.pack_coeff_mu([1, 2, 3, 4]), self.assertEqual(self.pack([1, 2, 3, 4]),
[0x20001, 0x30000, 0, 4, 0]) [0x20001, 0x30000, 0, 4, 0])
self.assertEqual(ch.pack_coeff_mu([-1, -2, -3, -4]), self.assertEqual(self.pack([-1, -2, -3, -4]),
[0xfffeffff, 0xfffdffff, 0xffffffff, [0xfffeffff, 0xfffdffff, 0xffffffff,
0xfffffffc, 0xffffffff]) 0xfffffffc, 0xffffffff])
self.assertEqual(ch.pack_coeff_mu([0, -1, 0, -1]), self.assertEqual(self.pack([0, -1, 0, -1]),
[0xffff0000, 0x0000ffff, 0, [0xffff0000, 0x0000ffff, 0,
0xffffffff, 0xffffffff]) 0xffffffff, 0xffffffff])
@ -171,3 +186,6 @@ class SAWGTest(unittest.TestCase):
ch.set(.2) ch.set(.2)
delay_mu(1*8) delay_mu(1*8)
out = self.run_channel(self.rtio_manager.outputs) out = self.run_channel(self.rtio_manager.outputs)
# import matplotlib.pyplot as plt
# plt.plot(out)
# plt.show()