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 suitable for embedded targets (no floating point at runtime).

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.0.tar.gz (6.6 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.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lutinterp-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for lutinterp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 157e7fa71c18f3d469242db8f1528a54a03dacb081a81b6b6f97fe072a8cbe8f
MD5 ef21d2ce1359c666b8c016b5111cea0e
BLAKE2b-256 f216298a358f1a67437827f675937106a2b8cd71a2c36244f96b227f9a5718e0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lutinterp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for lutinterp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2efc1abb25fa184c66041a473a1a15ec58eabda6a9c9f7787868c0adc21735e3
MD5 6a2edff76c3bfea05758bc9bc10c7178
BLAKE2b-256 a8706225c857e2d13b5885c050c014f201fbf30feb044649bb09474710df904c

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