Last active
September 5, 2019 06:46
-
-
Save superjax/625d2242ea605057e685c3fbaf0722c7 to your computer and use it in GitHub Desktop.
Python lookup table generation script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from math import sin, pi, asin, tan, atan | |
def f(x): | |
return ((1.0 - pow(x / 101325.0, 0.190284)) * 145366.45) * 0.3048 | |
def finv(x): | |
return 101325.0 * (1 - 2.25577 * 10 ** -5 * x) ** 5.25588 | |
max_alt = 3048 # 10,000 feet | |
min_alt = -430 # -1400 feet (dead sea) | |
max = finv(min_alt) | |
min = finv(max_alt) | |
scale = 32767/f(min) | |
num_entries = 200 | |
function_name = "pressure" | |
x = np.arange(min, max, (max - min) / num_entries) | |
lookup_table = [round(scale*f(i)) for i in x] | |
# Make sure we aren't going to overflow | |
for i in lookup_table: | |
if abs(i) > 32767: | |
print "going to overflow" | |
def fast_f(x): | |
dx = (max - min) / num_entries | |
global lookup_table | |
t = (x - min) / (max - min) * num_entries | |
index = int(t) | |
dp = t - index | |
if index >= num_entries: | |
out = min | |
elif index < num_entries - 1: | |
out = lookup_table[index]/scale + dp * (lookup_table[index + 1] - lookup_table[index])/scale | |
else: | |
out = lookup_table[index]/scale + dp * (lookup_table[index] - lookup_table[index - 1])/scale | |
return out | |
def fast_f_no_interp(x): | |
dx = (max - min) / num_entries | |
global lookup_table | |
t = (x - min) / (max - min) * num_entries | |
index = int(t) | |
return lookup_table[index] | |
y = np.arange(min, max, (max - min)/(num_entries*1000)) | |
fast = [fast_f(i) for i in y] | |
truth = [f(i) for i in y] | |
error = [abs(i - j) for i, j in zip(fast, truth)] | |
file = open('table.txt', 'w') | |
count = 0 | |
file.write("static const float %s_max_x = %f;\n" % (function_name , max)) | |
file.write("static const float %s_min_x = %f;\n" % (function_name , min)) | |
file.write("static const float %s_scale_factor = %f;\n" % (function_name , scale)) | |
file.write("static const int16_t %s_num_entries = %d;\n" % (function_name , num_entries)) | |
file.write("static const int16_t %s_lookup_table[%s] = {\n" % (function_name , int(len(lookup_table)))) | |
for i in lookup_table: | |
count += 1 | |
if count == 10: | |
file.write("%s,\n" % int(i)) | |
count = 0 | |
else: | |
file.write("%s,\t" % int(i)) | |
file.write("};") | |
plt.figure(1) | |
# plt.plot(y, [fast_alt_no_interp(i) for i in y]) | |
plt.plot(y, [fast_f(i) for i in y], "o") | |
plt.plot(y, [f(i) for i in y]) | |
plt.figure(2) | |
plt.plot(y, error) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also added a few lines so that a comma isnt appended onto the final value in the array. This isn't a huge deal since it wasnt causing build errors before, more of an aesthetic thing I guess?
https://gist.github.com/len0rd/194cb419349d7025afba5bce53ff376a