-from scipy.optimize import brentq
def secant_opt(func, x0, limits, x1scale=0.5, tol=1e-3, maxiter=50):
"""
Find the function's zero on a bounded interval using the secant method.
a0 = abs(q0)
a1 = abs(q1)
a = abs(q)
+ #ensure we pick the correct value
if (a0 <= a1) and (a <= a1):
- #print "*** turnaround convergence ***"
- #print ''.join(['%5i ' % i for i in guesses])
- #print ''.join(['%9.2e' % i for i in results])
return p0
#else:
#print "false alarm, almost there..."
values.append(offset2signed(adc.read(), 16))
sleep(DELAY)
mv = mean(values)
- #print xi, mv
- #print '%4i'%xi, tplot(mv)
print tpx(xi), tplot(mv)
return mv
values.append(offset2signed(adc.read(), 16))
sleep(DELAY)
mv = mean(values)
- #print xi, mv
- #print tplot(mv)
- #print '%4i'%xi, tplot(mv)
print tpx(xi), tplot(mv)
return mv
values.append(offset2signed(adc.read(), 16))
sleep(DELAY)
mv = mean(values)
- #print xi, mv
- #print tplot(mv)
- #print '%4i'%xi, tplot(mv)
print tpx(xi), tplot(mv)
return mv - mux_offset
adc.triggerMode(adc.MODE_MANUAL_MANUAL)
-#from scipy.optimize import brentq as fopt
-from scipy.optimize import bisect as fopt
N_SAMPLES = 10
DELAY = 0.05
print
print 'Calibrating mux otaA'
adc.mux(4)
-#resA = fopt(mux_a_offset, -128, 127, xtol=0.01, disp=True)
-#def secant_opt(func, x0, limits, x1scale=0.5, tol=1e-3, maxiter=50):
resA = secant_opt(mux_a_offset, 0, [-128, 127])
muxA_offset = mean([offset2signed(adc.read(), 16) for i in range(1000)])
print '*** mux.otaA.offset =', muxA_offset, ' @', resA
print
print 'Calibrating mux otaB'
adc.mux(5)
- #resB = fopt(mux_b_offset, -128, 127, xtol=0.01, disp=True)
resB = secant_opt(mux_b_offset, 0, [-128, 127])
muxB_offset = mean([offset2signed(adc.read(), 16) for i in range(1000)])
print '*** mux.otaB.offset =', muxB_offset, ' @', resB
mux.otaA.mode = mux.otaA.MUX_BUF
mux.otaB.mode = mux.otaB.MUX_BUF
mux.write()
- #nos = fopt(lambda x: chain_a_offset(x, n, muxA_offset),
- #-128, 127,
- #xtol=0.01,
- #disp=True)
nos = secant_opt(lambda x: chain_a_offset(x, n, muxA_offset), 0, (-128, 127))
mean_os = mean([offset2signed(adc.read(), 16) for i in range(1000)])
print '*** chain.h[%02i].otaA.offset =' % n, mean_os, ' @', nos