add dec8.aux.linlut
This commit is contained in:
parent
27a7ecf53d
commit
f6c4029cc5
1 changed files with 33 additions and 0 deletions
33
dec8/aux.py
33
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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue