From 0f98ef880a0034ca65606ffe06b046560b7f0ac5 Mon Sep 17 00:00:00 2001 From: Dan White Date: Tue, 4 Sep 2012 12:25:49 -0500 Subject: [PATCH] Do not evaluate converged channels --- python-lib/calibrate.py | 44 ++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/python-lib/calibrate.py b/python-lib/calibrate.py index 676d113..48ea5f5 100644 --- a/python-lib/calibrate.py +++ b/python-lib/calibrate.py @@ -81,19 +81,19 @@ def secant_opt(func, x0, limits, x1scale=0.1, maxiter=50, k=1.0): 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 @@ -113,7 +113,7 @@ def secant_opt(func, x0, limits, x1scale=0.1, maxiter=50, k=1.0): dp *= converging p = saturate(p1 - dp) - q = feval(p) + q = feval(p, converging) # Convergence criteria # x to varies by (+1,-1) or (-1,+1) @@ -154,7 +154,7 @@ def offset2signed(v, bits): 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() @@ -167,7 +167,7 @@ def mux_a_offset(x): 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() @@ -200,35 +200,43 @@ def chain_a_offset(x, n, mux_offset): 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]) @@ -240,7 +248,7 @@ def chain_outputs(n): 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] @@ -259,7 +267,7 @@ def chain_offsets(values, mux_offset): 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] @@ -347,7 +355,7 @@ while True: 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] -- 2.25.1