add dec8.aux.linlut

This commit is contained in:
Martin Sekera 2020-08-19 11:36:00 +02:00
parent 27a7ecf53d
commit f6c4029cc5

View file

@ -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"