diff --git a/dec8/aux.py b/dec8/aux.py index ac46a9a..130a4e9 100644 --- a/dec8/aux.py +++ b/dec8/aux.py @@ -32,6 +32,39 @@ def clamp(A, low, high): # ---------------------------------------------------------------- +def linlut(data, x): + """ + Data is a list of lists. Data[x][0] must be sorted from lowest to highest. + + Finds the two records in data where the record[0] is closest to x, + and returns a linearly interpolated record. + + Out of bounds x are clamped to the range in data. + """ + + smaller = None + larger = None + + for d in data: + if d[0] <= x: + smaller = d + + elif d[0] > x: + larger = d + break + + if not smaller: + return data[0] + + if not larger: + return data[-1] + + ratio = (x - smaller[0]) / (larger[0] - smaller[0]) + + return [(Sy + ratio * (Ly - Sy)) for Sy, Ly in zip(smaller, larger)] + +# ---------------------------------------------------------------- + import random PASSPHRASE_VOWELS = "aeiuAEU"