Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mapping an uint16_t to a value between 32 and 32768 with an exponential scale; with only 64 bytes of constant data
import numpy
import pylab
# coeff = numpy.exp(numpy.arange(65536) * numpy.log(1000) / 65536.0) * 32.768
# coarse_table = numpy.round(coeff[::4096])
# fine_table = numpy.round(coeff[:4096+256:256] / 32.768 * 32768)
coarse_table = [ 33, 50, 78, 120, 184, 284, 437, 673, 1036, 1596, 2457, 3784, 5827, 8973, 13818, 21279 ]
fine_table = [ 32768, 33664, 34585, 35531, 36503, 37501, 38527, 39580, 40663, 41775, 42918, 44092, 45298, 46536, 47809, 49117, 50460 ]
def compute_coefficient(value):
coarse = coarse_table[(value >> 12) & 0xf]
fine_integral = (value >> 8) & 0xf
fine_fractional = value & 0xff
fine = fine_table[fine_integral] + ((fine_table[fine_integral + 1] - fine_table[fine_integral]) * fine_fractional >> 8)
return coarse * fine >> 15
pylab.plot([compute_coefficient(i) for i in range(65536)])
pylab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.