From: Dan White Date: Fri, 15 Jun 2012 22:20:36 +0000 (-0500) Subject: add bisection function for finding offset values X-Git-Tag: calibrations~51 X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=768f0f6cc660188f7abf4ea4ba214674d4e55e69;p=430.git add bisection function for finding offset values --- diff --git a/python-lib/mpsse-test.py b/python-lib/mpsse-test.py index 33e862d..d292bcb 100755 --- a/python-lib/mpsse-test.py +++ b/python-lib/mpsse-test.py @@ -34,6 +34,45 @@ def str2hex(x): return ''.join(map(lambda h: '%02x' % ord(h), x)) +def bisect(fset, limits=[0.0, 1.0], dgmin=None): + """Given a one-input function fset and list of limits, interactively bisect + the range by sending fset(guess) and asking to go (u)p or (d)own for the + next step. Each stepsize is progressively halved and optionally limited to + dgmin if given.""" + xmin = min(limits) + xmax = max(limits) + g = (xmax - xmin) / 2 + xmin + dg = (xmax - xmin) / 4 + last = xmin + + guess = [xmin, xmax] + + quit = False + while quit == False: + print 'sending guess:', g, dg + guess.append(g) + fset(g) + + a = raw_input('q, [u,k], [d,j] ? ').lower() + if a == 'q': + quit = True + return guess[2:] + + if a in 'dj': + g = g - dg + elif a in 'uk': + g = g + dg + else: + print 'OOPS, expecting u or d only' + + dg = dg / 2 + # for ints, step size stops at 1 + if isinstance(xmin, int): + dg = max(dg, 1) + elif dgmin is not None and dg < dgmin: + dg = dgmin + + ############################################################################## # Setup FTDI serial ports