From f6c4029cc521842205e54e40fe0ab0c81d3734c2 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Wed, 19 Aug 2020 11:36:00 +0200 Subject: [PATCH] add dec8.aux.linlut --- dec8/aux.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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"