# Python

Two Python packages let a host program call the same C functions a firmware build links, for prototyping and for checking results against NumPy, SciPy or PyWavelets. They model what a function computes; they do not model execution time on a target or the behaviour of a particular Helium kernel.

| Package | Built from | Provides |
| --- | --- | --- |
| `cmsisdsp` | `third_party/CMSIS-DSP`, Arm's Python wrapper, unchanged | CMSIS-DSP's `arm_*` functions |
| `heliadsp` | `helia/python` | heliaDSP's `helia_*` operations, one module per family, such as `heliadsp.dwt` |

Both are built and tested on Linux with CPython only, and neither is published by a heliaDSP release.

## Build and install

From the repository root of a checkout with the submodule (see [What to download](https://ambiqai.github.io/helia-dsp/getting-started/#what-to-download)), with a C compiler, the CPython headers and the `build` package:

```sh
python -m build --wheel third_party/CMSIS-DSP -o dist
python -m build --wheel helia/python -o dist
pip install dist/cmsisdsp-*.whl dist/heliadsp-*.whl
```

`cmsisdsp` also needs CMake. `heliadsp` needs NumPy and nothing else at run time. Its build compiles C sources and headers from `helia/include`, `helia/src` and `third_party/CMSIS-DSP/Include` in place; an sdist built with `python -m build --sdist helia/python` carries copies of them and builds on its own.

The `heliadsp` tests are in `helia/python/tests`. They need the `test` extra and run against the installed wheel from the repository root:

```sh
pip install "$(ls dist/heliadsp-*.whl)[test]"
python -m pytest helia/python/tests
```

## Calling a heliaDSP operation

Each C entry point a host caller uses is one Python function, named as in C without the `helia_` prefix: `helia_dwt_forward_f32` is `heliadsp.dwt.dwt_forward_f32`. A family's general entry point and its reference entry point are separate functions; no keyword selects among implementations.

- Array arguments are NumPy arrays of the C element type (`float32` for `float32_t`, `int16` for `q15_t` and `int16_t`, `int32` for `q31_t` and `int32_t`), in native byte order, C-contiguous, aligned to the element size, with the shape the C contract states. Each family's reference page gives the dtype and shape of every argument.
- An argument outside those rules raises `ValueError` that names the C parameter and what the contract requires; nothing is converted or copied. An object that is not a NumPy array raises `TypeError`.
- Outputs are new arrays. The binding allocates every output and workspace the C function needs, so no buffer overlaps another.
- An initialized instance, and storage that the C contract requires to persist between calls, is a Python object returned by the binding of the C initializer. It owns its C storage; it cannot be constructed directly. An initializer that returns `ARM_MATH_ARGUMENT_ERROR` raises `ValueError` with the supported configurations.
- The binding adds no arithmetic.

`heliadsp` and `cmsisdsp` are independent distributions. Neither imports the other's native module, and an instance object of one cannot be passed to the other; code that uses both exchanges NumPy arrays.

## What the host build runs

The host build compiles each family's optimized scalar and reference sources and no Helium source, without fast-math and without floating-point contraction, as the library compiles them for a target. The general entry point therefore runs the optimized scalar implementation, which is what a target build without floating-point MVE, or with `AUTOVECTORIZE=ON`, runs. How that result relates to the Helium implementation's is the family's numerical contract, stated on its reference page; for the [wavelet transform](https://ambiqai.github.io/helia-dsp/reference/dwt/), the two are equal bit for bit on the test corpus.

## Example

A 320-sample frame through a six-level Coif5 transform, with the finest detail band removed, compared with the input using CMSIS-DSP's RMS:

```python
import numpy as np
import cmsisdsp as dsp
from heliadsp import dwt

S = dwt.dwt_init_f32(320, 6, dwt.DWT_COIF5_PERIODIC)
t = np.arange(320, dtype=np.float32) / 320
x = (np.sin(2 * np.pi * 3 * t) + 0.1 * np.sin(2 * np.pi * 140 * t)).astype(np.float32)

coeffs = dwt.dwt_forward_f32(S, x)          # [A_6 | D_6 | ... | D_1], 320 values
coeffs[160:] = 0                            # D_1 is the last N/2 values
y = dwt.dwt_inverse_f32(S, coeffs)

print(dsp.arm_rms_f32(x - y))               # 0.0707107: 0.1 / sqrt(2), the removed 140-cycle component
```

`dwt.dwt_reference_forward_f32(S, x)` returns the reference implementation's coefficients, equal to `coeffs` bit for bit before the band was removed.
