Skip to content

Instantly share code, notes, and snippets.

@pichenettes
Created November 3, 2014 23:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pichenettes/7453b3d6e3a0e252124f to your computer and use it in GitHub Desktop.
Save pichenettes/7453b3d6e3a0e252124f to your computer and use it in GitHub Desktop.
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