# Discrete wavelet transform (helia_dwt_*)

`helia/include/helia/helia_dwt.h` declares a multi-level forward and inverse discrete wavelet transform (DWT) of one frame of f32 samples with periodic boundary extension. Its output equals PyWavelets' `wavedec` and its input layout equals `waverec`, both with `mode="periodization"`, up to f32 rounding. The header is the contract; this page summarizes it.

## Using it

The operation was added in heliaDSP 2.1.0; the `helia_dsp` target defines `HELIA_DSP_VERSION_MAJOR`, `HELIA_DSP_VERSION_MINOR` and `HELIA_DSP_VERSION_PATCH` for compile-time checks. Include `helia/helia_dwt.h` and link `helia_dsp` (alias `nsx::helia_dsp`; under Zephyr, the `helia-dsp` module with `CONFIG_HELIA_DSP=y`), which supplies the CMSIS-DSP provider for `arm_math_types.h`. The entry points call `memcpy` and `memset` from the application's C library. The declarations and the instance layout depend on no build option.

## Wavelets and configurations

| `helia_dwt_wavelet_t` | PyWavelets name | Filter taps |
| --- | --- | --- |
| `HELIA_DWT_BIOR44_PERIODIC` | `bior4.4` | 10 |
| `HELIA_DWT_COIF5_PERIODIC` | `coif5` | 30 |

The configurations it was admitted for are those of compressionKIT's PPG codec (Coif5, 320 samples, six levels) and ECG codec (bior4.4, 512 samples, six levels).

`helia_dwt_init_f32()` accepts a frame of N samples and L levels when 1 ≤ L ≤ 30, N is a nonzero multiple of 2^L, and N ≤ UINT32_MAX / 2. Stages shorter than the filter wrap periodically.

## Entry points

Each direction has three entry points with the same contract:

| Forward | Inverse | Implementation |
| --- | --- | --- |
| `helia_dwt_forward_f32` | `helia_dwt_inverse_f32` | Helium (MVE) when the library is compiled for a target with floating-point MVE and without `AUTOVECTORIZE=ON` (which defines `ARM_MATH_AUTOVECTORIZE`), otherwise the optimized scalar implementation |
| `helia_dwt_forward_scalar_f32` | `helia_dwt_inverse_scalar_f32` | Optimized scalar, on every target |
| `helia_dwt_reference_forward_f32` | `helia_dwt_reference_inverse_f32` | Reference: the definition written for review, against which the others are tested |

The preprocessor fixes the implementation behind `helia_dwt_forward_f32` and `helia_dwt_inverse_f32` when the library is compiled. Each entry point is in its own object file, so a program that calls one entry point links one implementation; `tools/qualification/build.py` checks this for `helia_dwt_forward_f32`.

## Buffers

| Buffer | Elements | Notes |
| --- | --- | --- |
| Instance | `helia_dwt_instance_f32` | Written by `helia_dwt_init_f32()`, read-only afterwards; its layout does not depend on build options |
| Source | N | Not modified |
| Destination | N | Forward: coefficients packed as `[A_L \| D_L \| ... \| D_1]`; band D_l has N/2^l values and starts at element N/2^l |
| Workspace | `helia_dwt_workspace_size_f32()` | max(2N, N + 10) for bior4.4, max(2N, N + 30) for Coif5; contents on entry ignored |

All buffers need 4-byte alignment and no padding, and must not overlap one another or the instance. They may be in any Normal memory; no memory region is required, and cycle counts depend on the placement. No entry point allocates memory or keeps a pointer after it returns. The instance is the only persistent storage and must stay unmodified while it is in use; no state is carried from one call to the next, and the filter coefficients are constant tables in the library's read-only data.

Every function returning `arm_status` returns `ARM_MATH_SUCCESS` or `ARM_MATH_ARGUMENT_ERROR` and no other value. A NULL pointer or a short workspace returns `ARM_MATH_ARGUMENT_ERROR` before any buffer is written; the instance's fields are not checked on each call.

## Stack

Bytes of stack one call uses. The process entry points were measured on the Apollo510 EVB by painting below the stack pointer (the figure includes the measuring call's overhead), over frames of 8 to 2048 samples at one, three, four and six levels and both wavelets, library built at -O3; the other three functions call no function and their figures are the compilers' static frame sizes. No entry point recurses or allocates stack in proportion to N.

| Function | ATfE 22.1.0 | GCC 14.2.1 |
| --- | ---: | ---: |
| `helia_dwt_forward_f32` | 124 to 128 | 136 |
| `helia_dwt_forward_scalar_f32` | 136 | 108 |
| `helia_dwt_reference_forward_f32` | 136 | 112 |
| `helia_dwt_inverse_f32` | 124 | 140 |
| `helia_dwt_inverse_scalar_f32` | 124 | 116 |
| `helia_dwt_reference_inverse_f32` | 124 | 108 |
| `helia_dwt_init_f32` | 8 | 4 |
| `helia_dwt_packed_size_f32` | 0 | 0 |
| `helia_dwt_workspace_size_f32` | 8 | 8 |

## Numerical behaviour

Every implementation forms each output from the same products, added in the same order, without fused multiply-add (a forward output adds tap times sample in tap order; an inverse output adds, for each input in increasing order and each tap, the approximation and detail products of that tap summed first), and the library compiles the DWT sources without fast-math or floating-point contraction whatever the application's flags. On the test corpus the optimized and Helium implementations return the reference's result bit for bit; [`helia/REFERENCE_POLICY.md`](https://github.com/AmbiqAI/helia-dsp/blob/4a7b7f054eafd96ae50c12050ad3ef3ec1ebaea1/helia/REFERENCE_POLICY.md) states the policy behind that gate.

The qualified domain is inputs for which every input, product, partial sum and output is zero or a normal f32 number. The entry points do not check for NaN or infinity, and no result is specified for such inputs. Helium floating-point instructions flush subnormal operands and results to zero whatever FPSCR.FZ selects, while the scalar implementations keep subnormals unless FPSCR.FZ is set, so outside the domain the Helium result can differ from the reference. In the test corpus this happens once: Coif5, 2048 samples, six levels, impulse input, where the finest coefficients reach 1.3e-39. The domain also requires FPSCR.RMode at round to nearest, its reset value: Helium floating-point instructions round to nearest whatever FPSCR.RMode selects, while the scalar implementations follow it, so under another rounding mode the Helium result can differ from the reference on ordinary inputs. In the domain, every zero output is +0.0f and an input of -0.0f gives the same result as an input of +0.0f.

A forward transform followed by the inverse returns the input with a largest error, relative to the largest input magnitude, of 3.1e-7 (bior4.4) and 7.2e-7 (Coif5) over the test corpus.

## Performance

Cycles, flash and stack on the Apollo510 EVB for every entry point, under ATfE 22.1.0 and GCC 14.2.1, are in [Measurements](https://ambiqai.github.io/helia-dsp/guide/measurements/#wavelet-transform).

## Tests

`helia/tests/dwt/` holds the tests, run by `python tools/qualification/build.py --toolchain host` and, on the Corstone-300 model, by its `--fvp` form:

- 1,862 configurations (every even N from 2 to 130 at every valid depth up to 7, and 64, 256, 320, 1024 and 2048 samples at six levels; both wavelets; seven input patterns), each entry point compared bit for bit with the reference, forward and inverse, with poisoned outputs and workspace;
- argument errors and the absence of writes on error;
- signed zero: sawtooth, impulse and all-zero inputs at 2, 16, 64, 320 and 1024 samples, one level and the deepest up to six, run with each zero sample as +0.0f and as -0.0f; every entry point must give the same result for both and no -0.0f output, forward and inverse;
- PyWavelets coefficients for twelve configurations (`dwt_known_vectors.h`), and the test `helia_dwt_vectors_current`, in which `generate_dwt_vectors.py --check` regenerates them and checks every filter table against PyWavelets. That test runs on the host when the Python that runs `build.py`, which `build.py` passes to CMake, imports numpy and PyWavelets. The release-qualification workflow installs both and sets `HELIA_DSP_REQUIRE_DWT_VECTORS`, which turns their absence into a configure error;
- on the host, the same corpus under AddressSanitizer and UndefinedBehaviorSanitizer, once with the Helium stages compiled against a lane model of the intrinsics; on the Corstone-300 model, MPU guards on both sides of every buffer.

## Python

The module `heliadsp.dwt` of the `heliadsp` package calls these functions on a host; [Python](https://ambiqai.github.io/helia-dsp/getting-started/python/) describes how to build it and the rules every module follows. Each function calls the C function of the same name with the `helia_` prefix:

| Python | Arguments | Returns |
| --- | --- | --- |
| `dwt_init_f32(frame_size, levels, wavelet)` | `int`, `int`, `DWT_BIOR44_PERIODIC` or `DWT_COIF5_PERIODIC` | `dwt_instance_f32` |
| `dwt_forward_f32(S, pSrc)` | instance; `float32`, shape `(N,)` | `float32`, shape `(N,)`, packed `[A_L \| D_L \| ... \| D_1]` |
| `dwt_reference_forward_f32(S, pSrc)` | as `dwt_forward_f32` | as `dwt_forward_f32` |
| `dwt_inverse_f32(S, pPackedCoeffs)` | instance; `float32`, shape `(N,)`, packed as above | `float32`, shape `(N,)` |
| `dwt_reference_inverse_f32(S, pPackedCoeffs)` | as `dwt_inverse_f32` | as `dwt_inverse_f32` |
| `dwt_packed_size_f32(S)` | instance | `int`, N |
| `dwt_workspace_size_f32(S)` | instance | `int`, workspace elements a C caller provides |

`dwt_instance_f32` holds a `helia_dwt_instance_f32`; its read-only attributes are `frame_size`, `levels` and `wavelet`. `dwt_init_f32` raises `ValueError` for a configuration `helia_dwt_init_f32` rejects. The process functions allocate the output and the workspace; arrays must be `float32`, C-contiguous and of length N, or `ValueError` is raised.

On the host, `dwt_forward_f32` and `dwt_inverse_f32` run the optimized scalar implementation, compiled without fast-math or floating-point contraction; `helia_dwt_forward_scalar_f32` and `helia_dwt_inverse_scalar_f32` run the same implementation and have no separate binding. The results equal the reference's bit for bit on the test corpus, as on a target.

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

S = dwt.dwt_init_f32(512, 6, dwt.DWT_BIOR44_PERIODIC)
x = np.random.default_rng(0).standard_normal(512).astype(np.float32)
coeffs = dwt.dwt_forward_f32(S, x)
assert np.array_equal(coeffs, dwt.dwt_reference_forward_f32(S, x))
y = dwt.dwt_inverse_f32(S, coeffs)          # x up to f32 rounding
```

`helia/python/tests/test_dwt.py` checks, for every even N from 2 to 64 at each depth up to six, both workload configurations, and 256, 1024 and 2048 samples at six levels (134 configurations), with seven input patterns: the general entry points against the reference bit for bit; both directions against a float64 filter-bank formula within a relative error of 1e-5; and, when PyWavelets is installed, the formula and the workload configurations against `wavedec` and `waverec`. It also checks the argument errors.
