# helia_dwt.h

Periodic discrete wavelet transform (DWT), f32.

Multi-level forward (analysis) and inverse (synthesis) DWT of one frame of N real samples with periodic boundary extension, matching PyWavelets `wavedec` / `waverec` with `mode="periodization"` for the wavelets listed in `helia_dwt_wavelet_t`.

Each direction has three entry points with one contract:

| Entry point | Implementation |
| --- | --- |
| `helia_dwt_forward_f32()` | MVE when the build targets floating-point MVE (`__ARM_FEATURE_MVE & 2`) and `ARM_MATH_AUTOVECTORIZE` is not defined, optimized scalar otherwise |
| `helia_dwt_forward_scalar_f32()` | optimized scalar, on every target |
| `helia_dwt_reference_forward_f32()` | readable scalar reference (the test oracle) |

and likewise `helia_dwt_inverse_f32()`, `helia_dwt_inverse_scalar_f32()` and `helia_dwt_reference_inverse_f32()`. The implementation behind `helia_dwt_forward_f32()` is fixed by the preprocessor when the library is compiled; no entry point chooses among implementations at run time, so an application that calls one entry point links one implementation.

:::note[Version]
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, through which a consumer checks the release at compile time.

:::

:::note[Build requirements]
Include `helia/helia_dwt.h` and link the CMake target `helia_dsp` (alias `nsx::helia_dsp`; under Zephyr, the `helia-dsp` module with `CONFIG_HELIA_DSP=y`), which supplies the CMSIS-DSP provider `CMSISDSP` for `arm_math_types.h`. The entry points call `memcpy` and `memset`, which the application's C library supplies. The implementation behind the general entry points depends on the target's floating-point MVE and on ARM_MATH_AUTOVECTORIZE as defined when the library is compiled; the declarations and the instance layout depend on no build option.

:::

:::note[Coefficient layout]
A forward transform of L levels writes N coefficients packed as `[A_L | D_L | D_(L-1) | ... | D_1]`: the level-L approximation (N/2^L values) followed by the detail bands from coarsest to finest. Band D_l holds N/2^l values and starts at element N/2^l. The inverse transform reads the same layout.

:::

:::note[Numerical contract]
The qualified domain is inputs for which every input, product, partial sum and output is zero or a normal f32 number (no NaN, infinity or subnormal value), computed with FPSCR.RMode at round to nearest, its reset value. Outside it the implementations may differ. The MVE path depends on FPSCR in two ways: its floating-point instructions flush subnormal operands and results to zero whatever FPSCR.FZ selects, and they round to nearest whatever FPSCR.RMode selects, whereas the scalar implementations follow both FPSCR.FZ and FPSCR.RMode.

:::

Every entry point evaluates each output as the reference does, without fused multiply-add. A forward output is a sum, starting at +0.0f, of the products of one tap and one sample, added in tap order. An inverse output starts at +0.0f; for each input i in increasing order and each tap t in increasing order it accumulates lo[t] * a[i] + hi[t] * d[i], the two products added together first. Under that rule the optimized and MVE implementations are required to return results identical bit for bit to the reference on the library's test corpus, which is a test gate, not a proof for all finite inputs. The library compiles these sources without fast-math and without floating-point contraction whatever flags the application uses.

Signed zero: in the qualified domain every zero output is +0.0f, from every entry point, and an input of -0.0f gives the same result as an input of +0.0f. Every sum starts at +0.0f, and under round to nearest adding a zero leaves a sum's value, and an exact cancellation gives +0.0f. This follows from the evaluation rule; the library's tests check it with each zero sample of three input patterns as +0.0f and as -0.0f.

Reconstruction is not exact: an inverse following a forward transform returns the input up to f32 rounding (see the library's test report for measured errors).

The entry points do not check for NaN or infinity. Such inputs are outside the qualified domain, and no result is specified for them.

:::note[Memory contract]
- All buffers hold f32 elements with natural (4-byte) alignment; no greater alignment, padding or capacity beyond the stated sizes is required, and no element outside the stated sizes is read or written.
- Source, destination and workspace must not overlap one another or the instance.
- The source buffer is not modified.
- No entry point allocates memory, keeps a pointer after it returns, or uses mutable static state; one instance may be used concurrently from several threads with separate buffers.
- Buffers may be in any Normal memory the core reads and writes with ordinary and MVE loads and stores; no memory region is required. Cycle counts depend on the placement.
- Persistent storage is the instance alone, sizeof(helia_dwt_instance_f32) bytes, which must stay unmodified for as long as it is passed to an entry point. No state is carried from one call to the next. The workspace is used only during a call. The filter coefficients are constant tables in the library's read-only data.

:::

:::note[Status]
Every function returning arm_status returns ARM_MATH_SUCCESS or ARM_MATH_ARGUMENT_ERROR and no other value; the conditions for ARM_MATH_ARGUMENT_ERROR are stated at each function.

:::

:::note[Cycles]
Loop counts and branches depend on N, the number of levels and the wavelet, not on sample values. The Apollo510 cycle counts were measured with one dense input; whether cycles vary with the data, for example with subnormal operands outside the qualified domain, is not measured.

:::

:::note[Stack]
Each function states its stack use at its declaration. 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, with the library built at -O3; the size queries and `helia_dwt_init_f32()` call no function and their figures are the compilers' static frame sizes. No entry point recurses or allocates on the stack in proportion to N.

:::

:::note[Evidence]
The tests in `helia/tests/dwt/` run the contract on the host and on the Corstone-300 model (`tools/qualification/build.py`); Apollo510 EVB cycles and flash use under ATfE 22.1.0 and GCC 14.2.1 are in the documentation's measurements guide. The operation was admitted for compressionKIT's PPG codec (Coif5, 320 samples, six levels) and ECG codec (bior4.4, 512 samples, six levels).

:::

[Machine-readable model](https://ambiqai.github.io/helia-dsp/reference/api/heliadsp/helia_dwt.json)

Configuration of a DWT of fixed frame size and depth.

Created by `helia_dwt_init_f32()` and read-only afterwards. The layout is the same in every build configuration. Process functions do not check the fields; an instance not produced by `helia_dwt_init_f32()` is undefined behaviour.

N, samples per frame.

L, decomposition levels.

Wavelet and boundary convention.

Wavelet and boundary convention.

A deployed bitstream identifies one transform, including its boundary extension; the values of this enumeration are stable.

Biorthogonal 4.4 (PyWavelets `bior4.4`), 10-tap filters, periodic extension.

Coiflet 5 (PyWavelets `coif5`), 30-tap filters, periodic extension.

Initialize a DWT configuration.

Supported configurations: 1 &lt;= `levels` &lt;= 30, `frame_size` a nonzero multiple of 2^`levels` and at most UINT32_MAX / 2, and `wavelet` one of `helia_dwt_wavelet_t`. The coarsest stage therefore filters 2 or more samples; stages shorter than the filter wrap periodically as PyWavelets does. No implicit padding is applied.

Stack: 8 bytes (ATfE 22.1.0), 4 bytes (GCC 14.2.1).

Number of f32 elements in the packed coefficients of `S`.

Stack: 0 bytes (ATfE 22.1.0), 0 bytes (GCC 14.2.1).

Number of f32 workspace elements every forward and inverse entry point requires for `S`.

The value is max(2N, N + 10) for bior4.4 and max(2N, N + 30) for Coif5, which is 2N for N >= 10 (bior4.4) and N >= 30 (Coif5).

Stack: 8 bytes (ATfE 22.1.0), 8 bytes (GCC 14.2.1).

Forward DWT: MVE implementation on targets with floating-point MVE, unless the library is built with ARM_MATH_AUTOVECTORIZE (CMake AUTOVECTORIZE=ON), optimized scalar implementation otherwise.

Stack: 124 to 128 bytes (ATfE 22.1.0), 136 bytes (GCC 14.2.1).

Forward DWT, optimized scalar implementation on every target.

Same contract as `helia_dwt_forward_f32()`.

Stack: 136 bytes (ATfE 22.1.0), 108 bytes (GCC 14.2.1).

Forward DWT, reference implementation.

Same contract as `helia_dwt_forward_f32()`. Written for review rather than speed; the other forward entry points are tested against it.

Stack: 136 bytes (ATfE 22.1.0), 112 bytes (GCC 14.2.1).

Inverse DWT: MVE implementation on targets with floating-point MVE, unless the library is built with ARM_MATH_AUTOVECTORIZE (CMake AUTOVECTORIZE=ON), optimized scalar implementation otherwise.

Stack: 124 bytes (ATfE 22.1.0), 140 bytes (GCC 14.2.1).

Inverse DWT, optimized scalar implementation on every target.

Same contract as `helia_dwt_inverse_f32()`.

Stack: 124 bytes (ATfE 22.1.0), 116 bytes (GCC 14.2.1).

Inverse DWT, reference implementation.

Same contract as `helia_dwt_inverse_f32()`. Written for review rather than speed; the other inverse entry points are tested against it.

Stack: 124 bytes (ATfE 22.1.0), 108 bytes (GCC 14.2.1).
