Skip to main content

Generate C lookup-table interpolation code from Python callables

Project description

lutInterp

Generate C lookup-table interpolation code from Python callables — using integer-only arithmetic (no floating point at runtime) suitable for embedded targets (32-bit MCUs).

Complex math functions sometimes consume too much runtime on MCUs, need extra libraries and extra engineering. It's much simpler to do the math in python and produce a lookup table which can be interpolated, all with fix point arithmetic.

Installation

pip install lutinterp

What it does

Given a Python function, CInterpolator produces:

  • A C coefficient array (int8_t, int16_t, or int32_t) scaled to maximally fill the chosen integer type
  • A C function that approximates the original using only integer arithmetic and bit-shifts — no division, no floating point

The x-axis is parameterized with power-of-2 exponents so that table lookup is a single right-shift instruction.

Usage

import numpy as np
from lutinterp import CInterpolator

# Approximate sqrt over [0, 4096] with a linear LUT
lut = CInterpolator(
    np.sqrt,
    typ="linear",
    coefResExp=16,       # store coefficients as int16_t
    xSupportPointsExp=4, # 2^4 = 16 segments
    xRangeExp=12,        # input range [0, 2^12] = [0, 4096]
    xRangeSign="pos",
)

print(lut)              # prints C code to stdout
lut.print_to_file("sqrt_lut.c")  # or save to a file

Cubic spline

lut = CInterpolator(
    np.sqrt,
    typ="cubic",
    coefResExp=16,
    xSupportPointsExp=3,
    xRangeExp=12,
    xRangeSign="pos",
)

Visualise error

lut.plot_error()   # relative error vs exact function
lut.plot_func()    # overlay: linear / cubic / pchip / exact

Key parameters

Parameter Description
typ 'linear' or 'cubic'
coefResExp Bit-width of coefficients (8, 16, or 32)
xSupportPointsExp log2 of the number of LUT segments
xRangeExp log2 of the input range
xRangeSign 'pos' → [0, 2^n], 'neg' → [2^n, 0], 'center' → [-2^n, +2^n]

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lutinterp-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lutinterp-0.1.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file lutinterp-0.1.1.tar.gz.

File metadata

  • Download URL: lutinterp-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lutinterp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 69a9b8e90ee01b5c93c54e479702268a3a0630510ef7f101cf177676ad6da79b
MD5 c47136da4f197db626eaef66e816dfb7
BLAKE2b-256 424710161ca993866d68230befdf3189e85aabc520b26792ebfbdee0bfe8641b

See more details on using hashes here.

File details

Details for the file lutinterp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lutinterp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lutinterp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f63b8c43e83007ab413dddff971cd208791bf36b4cec9343115616c606bd41b6
MD5 39c0dc09a2a2f7ba8bcf9ee814345eaa
BLAKE2b-256 3be3c04f8631aa3bd891661d492a94d7cf29d466a7d9f23a8816773263fe594f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page