return int64(around(x))
#return int(round(max(min(x, xmax), xmin)))
- def feval(x):
+ def feval(x, conv):
#print 'feval:'
#print x[0:N_CHANNELS]
- r = func(x)
+ r = func(x, conv)
guesses.append(x)
results.append(r)
return r
- q0 = feval(p0)
+ q0 = feval(p0, converging)
r = xmax - xmin
p1 = saturate(p0 - sign(q0)*x1scale*r)
- q1 = feval(p1)
+ q1 = feval(p1, converging)
for niter in range(maxiter):
iz = find(q1==q0)
q1.flat[iz] = q1.flat[iz] + 1e-6
dp *= converging
p = saturate(p1 - dp)
- q = feval(p)
+ q = feval(p, converging)
# Convergence criteria
# x to varies by (+1,-1) or (-1,+1)
return (v - 2**(bits-1)) >> 2
-def mux_a_offset(x):
+def mux_a_offset(x, c):
xi = int(x[0])
mux.otaA.offset = xi
mux.write()
print tpx(xi), tplot(mv)
return array((mv,))
-def mux_b_offset(x):
+def mux_b_offset(x, c):
xi = int(x[0])
mux.otaB.offset = xi
mux.write()
return mv
-def chain_outputs(n):
+def chain_outputs(n, converged):
adc.triggerMode(adc.MODE_IDLE)
adc.mux(4) #mux.otaA
adc.fifo(1)
adc.triggerMode(adc.MODE_MANUAL_MANUAL)
- plotter = TermPlotter([-1000, 1000], width=80)
-
outs = zeros((2, n+1))
for i in range(n+1):
+ # skip evaluating converged channel groups
+ # output values are ignored for these channels
+ if sum(converged[:,i]) == 0:
+ outs[0, i] = 0.0
+ outs[1, i] = 0.0
+ print 'skipping converged channel pair: %02i' % i
+ continue
+
mux.selA = i
mux.selB = i
mux.write()
sleep(MUX_SETTLE_DELAY)
for j in range(2):
+ # also ignore individual channel
+ # (small speedup, but it all helps...)
+ if converged[j, i] == 0:
+ outs[j, i] = 0.0
+ print 'skipping converged channel %i %s' % (i, ('A','B')[j])
+ continue
+
adc.mux(4+j)
- #sleep(0.1)
- #adc.triggerMode(adc.MODE_MANUAL_MANUAL)
- #adc.triggerMode(adc.MODE_AUTO_AUTO_SINGLE)
adc.read()
values = []
- #print
v = offset2signed(adc.read(), 16)
values.append(v)
for k in range(N_SAMPLES-1):
sleep(SAMPLE_DELAY)
v = offset2signed(adc.read(), 16)
- #print plotter(v)
values.append(v)
if N_SAMPLES >= 2:
mv = mean(values[-1*min(10, N_SAMPLES):-1])
print outs[:,0:N_CHANNELS]
return outs
-def chain_offsets(values, mux_offset):
+def chain_offsets(values, converged, mux_offset):
print
print '*** sending values: ***'
print values[0:N_CHANNELS]
chain.write()
sleep(1.0)
- outs = chain_outputs(N_CHANNELS-1)
+ outs = chain_outputs(N_CHANNELS-1, converged)
for i in range(2):
outs[i,:] -= mux_offset[i]
mux.otaA.mode = mux.otaA.MUX_BUF
mux.otaB.mode = mux.otaB.MUX_BUF
mux.write()
- chain_os, stats = secant_opt(lambda x: chain_offsets(x, (muxA_offset, muxB_offset)), resA*ones((2,N_CHANNELS), dtype=int), (-128, 127), k=0.5)
+ chain_os, stats = secant_opt(lambda x,y: chain_offsets(x, y, (muxA_offset, muxB_offset)), resA*ones((2,N_CHANNELS), dtype=int), (-128, 127), k=0.5)
print '-'*80
print 'offsets:', chain_os[:N_CHANNELS]
print 'N-evals:', stats['nevals'][:N_CHANNELS]