Skip to content
heliaDSP
API reference
HELIA HUB

Window statistics (helia_window_stats_*)

helia/include/helia/helia_window_stats.h declares a function that reads a window of f32 samples once and returns the statistics that would otherwise take separate calls to arm_mean_f32, arm_var_f32, arm_std_f32, arm_rms_f32, arm_min_f32 and arm_max_f32. The header is the contract; this page summarizes it.

Include helia/helia_window_stats.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 helia_dsp target defines HELIA_DSP_VERSION_MAJOR, HELIA_DSP_VERSION_MINOR and HELIA_DSP_VERSION_PATCH for compile-time checks. The helpers call sqrtf; the optimized scalar implementation calls fmaf. The library compiles that implementation with -fno-math-errno, so that ATfE 22.1.0 and GCC 14.x emit fmaf as one VFMA.F32 instruction for the Cortex-M55 and Cortex-M4; on a target without a fused multiply-add instruction (a core without an FPU, or an x86-64 host compiled without FMA) it is a call to the C library’s fmaf. In Cortex-M55 application code compiled at -O2 without -fno-math-errno, GCC 14.x keeps a call to sqrtf and emits fmaf as VFMA.F32, ATfE 22.1.0 with its newlib or llvmlibc configuration keeps calls to both, and ATfE 22.1.0 with its default picolibc configuration keeps neither. The helia_dsp target links libm on a host when a link of fmaf and sqrtf needs it, and in a bare-metal build under GCC and ATfE, whose C libraries all ship a libm.a; not under Arm Compiler, whose C library has none. The declarations and the result layout depend on no build option.

helia_window_stats_result_f32 s;
if (helia_window_stats_f32(window, 320U, &s) == ARM_MATH_SUCCESS)
{
float32_t mean = helia_window_stats_mean_f32(&s);
float32_t std = sqrtf(helia_window_stats_population_variance_f32(&s));
}
Field Value
count N
shift c = x[0] (+0 when N = 0)
sum_dev sum of x[i] − c
sum_sq_dev sum of (x[i] − c)²
minimum, maximum IEEE 754-2008 minNum and maxNum of the samples (+∞ and −∞ when N = 0)

The inline helpers derive the sum, the sum of squares, the mean, the population variance (divisor N), the sample variance (divisor N − 1) and the RMS. Accumulating deviations from the first sample makes the variance’s error independent of the ratio of the mean to the spread: since c is one of the samples, (mean − c)² ≤ (N − 1)·variance, whereas a sum of raw squares loses all precision once the mean is large against the spread. For N = 1 the sample variance is NaN; CMSIS-DSP’s arm_var_f32 returns 0 there.

Entry point Implementation
helia_window_stats_f32 Helium (MVE) when the library is compiled for a target with floating-point MVE and without AUTOVECTORIZE=ON, otherwise the optimized scalar implementation
helia_window_stats_scalar_f32 Optimized scalar, on every target
helia_window_stats_reference_f32 Reference: sequential, accumulating in double precision and rounding once; the oracle the others are tested against

The preprocessor fixes the implementation behind helia_window_stats_f32 when the library is compiled, and each implementation is in its own object file, so a program that calls helia_window_stats_f32 links one kernel; tools/qualification/build.py checks this.

The samples need 4-byte alignment and no padding, and are not modified; the result must not overlap them. No entry point reads outside the N samples: the Helium implementation loads the last one to three samples with a predicated load. There is no workspace and no state carried between calls. A NULL pointer returns ARM_MATH_ARGUMENT_ERROR without writing the result; no other status is returned. The supported N is 0 to 2^24, which the entry points do not check (the Python binding does): for a larger N they return ARM_MATH_SUCCESS with count = N, but the helpers round N to f32 where f32 cannot represent it, and the error bounds do not hold.

The non-reference entry points keep four lane partial sums of x[i] − c and of (x[i] − c)², lane j taking the samples with i mod 4 = j in increasing order, the square added by a fused multiply-add (VFMA on the Helium path; fmaf on the scalar path, which assumes, where fmaf is the C library’s routine, that the routine is correctly rounded; newlib’s fmaf for cores without an FPU computes in double precision and rounds twice); the last one to three samples enter lanes 0 to 2; the lanes are added as (lane0 + lane1) + (lane2 + lane3). The scalar and Helium implementations perform the same operations in the same order and return identical results outside subnormal values. No consumer’s own accumulation order is reproduced.

With k = ⌈N/4⌉, u = 2^−24 and γ(m) = mu/(1 − mu), the accumulated sums satisfy |sum_dev − S| ≤ γ(k + 3)·Σ|x[i] − c| and |sum_sq_dev − Q| ≤ γ(k + 4)·Q against the exact values S and Q; the minimum and maximum are exact, apart from a subnormal extreme, which the Helium path returns flushed to a zero of its sign. The header states the bound of every helper, the reference’s own bounds, and the terms added for subnormal results. To first order the population variance has relative error at most (3k + 14)·u·(1 + (mean − c)²/variance). On the test corpus, the largest difference between an entry point’s result and the reference’s is 0.28 of the test’s tolerance for it: the sum of the two entry points’ bounds, times 1 + 10^−6, plus N·10^−15 times the statistic’s scale for the rounding of the test’s double-precision computation.

minNum and maxNum ignore quiet NaN samples, treat infinities as numbers and order −0 below +0. A NaN or infinite sample makes sum_dev or sum_sq_dev non-finite. Helium floating-point instructions flush subnormal operands and results to zero whatever FPSCR.FZ selects, so on the Helium path a subnormal sample enters as a zero of its sign; the header bounds the resulting difference. They also round to nearest whatever FPSCR.RMode selects, while the scalar implementations follow it, so the bounds and the equality of the Helium and scalar results require FPSCR.RMode at round to nearest, its reset value. The bounds also require FPSCR.FZ clear for the scalar implementations, the reference and the helpers, which run in the caller’s scalar code: only then do they keep subnormal values, as the bounds assume. With round to nearest, a window of zeros gives +0.0f for every sum and derived value, and a -0.0f sample elsewhere gives the results a +0.0f would, apart from the minimum and maximum, and apart from the mean when that sample is the shift x[0] and sum_dev/N is −0 (sum_dev negative and |sum_dev|/N at most 2^−150): the mean c + sum_dev/N is then c, a zero of the shift’s sign, as for [c, −0x1p−149f] or for N = 2^24 with x[1] = −2^−126 and the other samples after x[0] +0.0f. The Helium path flushes the subnormal sample of the first example, so there both means are +0.0f.

Measured on the Apollo510 EVB under ATfE 22.1.0 (Measurements):

  • For the mean, standard deviation, minimum and maximum, the general entry point with the helpers takes 2.0 to 2.4 times fewer cycles than arm_mean_f32, arm_std_f32, arm_min_f32 and arm_max_f32 (1,011 against 2,266 at 320 samples).
  • For the mean and variance alone, arm_mean_f32 followed by arm_var_f32 takes 4 to 19% fewer cycles (844 against 990 at 320 samples). Its variance is two-pass about an f32 mean and loses accuracy when the mean is large against the spread: relative error 1e-3 to 3.6e-3 at a mean-to-spread ratio of 10^5, against below 1.7e-5 here; below 10^3 both stay below 3e-6.
  • A one-pass sum of raw values and squares in four lanes, as heliaML’s prototype computes it, takes 21% fewer cycles than the general entry point (728 against 927 at 320 samples) by omitting the shift, and its variance has relative error 3e-5 to 7e-5 at a mean-to-spread ratio of 10, 4e-3 to 7e-3 at 100 and 0.25 to 0.68 at 1000.

Cycles, flash and stack on the Apollo510 EVB for every entry point and for the CMSIS-DSP calls it replaces, under ATfE 22.1.0 and GCC 14.2.1, are in Measurements.

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

  • every N from 0 to 130 and N = 320, 512, 1500 and 2048, eleven input patterns (constant, ramp, sawtooth, uniform, mean-to-spread ratios 1, 10^3 and 10^5, NaN, infinities, signed zeros, subnormals), every entry point against the reference within the stated bounds, the general and scalar entry points against each other, samples checked unmodified;
  • argument errors, N = 0 and N = 1;
  • signed zeros, bit for bit: windows of zeros of either sign for N from 1 to 130, and windows of nonzero samples with one zero as +0.0f and as -0.0f, and two windows whose sum_dev/N underflows to −0, with the shift as +0.0f and as −0.0f: [c, −0x1p−149f], and N = 2^24 with x[1] = −2^−126, which runs on the host only because its 64 MiB of samples do not fit the model’s 12 KiB arena;
  • ten known-answer windows from a NumPy float64 model (stats_known_vectors.h), and the test helia_window_stats_vectors_current, in which generate_stats_vectors.py --check regenerates them. That test runs on the host when the Python that runs build.py imports numpy; the release-qualification workflow sets HELIA_DSP_REQUIRE_VECTORS, which turns numpy’s absence into a configure error;
  • on the host, the same corpus under AddressSanitizer and UndefinedBehaviorSanitizer, once with the Helium kernel compiled against a lane model of the intrinsics, with the samples at every 4-byte alignment modulo 16 and ending at the end of their allocation; these builds compile the library’s sources with its per-source options. Where the C compiler has no ASan/UBSan, the sanitized test is not built and the lane-model test is built without them; the release-qualification workflow sets HELIA_DSP_REQUIRE_SANITIZERS, which turns that into a configure error;
  • on the Corstone-300 model, MPU guards on both sides of the samples and the result, with the samples mapped read-only; a window whose buffers do not fit the test’s arena counts as a failure.

The module heliadsp.window_stats of the heliadsp package calls these functions on a host; 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
window_stats_f32(pSrc) float32, shape (N,), 0 ≤ N ≤ 2^24 window_stats_result_f32
window_stats_reference_f32(pSrc) as window_stats_f32 as window_stats_f32
window_stats_sum_f32(s), window_stats_mean_f32(s) window_stats_result_f32 float
window_stats_population_variance_f32(s), window_stats_sample_variance_f32(s) window_stats_result_f32 float
window_stats_rms_f32(s), window_stats_sum_squares_f32(s) window_stats_result_f32 float

The length of pSrc is passed as blockSize. window_stats_result_f32 holds a helia_window_stats_result_f32; its read-only attributes are count, shift, sum_dev, sum_sq_dev, minimum and maximum, and it is returned only by the two entry points. The helper functions call the header’s inline helpers, compiled without fast-math or floating-point contraction as the bounds assume; every float they and the attributes return is a float32_t value, exactly. pSrc must be float32, one-dimensional, C-contiguous and of at most 2^24 samples, or ValueError is raised; a helper given an object other than a result raises TypeError.

On the host, window_stats_f32 runs the optimized scalar implementation, compiled without fast-math or floating-point contraction; its results are those of helia_window_stats_scalar_f32, which therefore has no separate binding, and equal those of the Helium implementation outside subnormal values. The reference differs from it within the bounds above.

import numpy as np
from heliadsp import window_stats as ws
t = np.arange(320, dtype=np.float32)
x = (1e3 + np.sin(2 * np.pi * t / 64)).astype(np.float32)
s = ws.window_stats_f32(x)
mean = ws.window_stats_mean_f32(s) # 1000.0
variance = ws.window_stats_population_variance_f32(s) # 0.4999969; NumPy float64: 0.4999968
low, high = s.minimum, s.maximum # 999.0, 1001.0

helia/python/tests/test_window_stats.py checks, for every N from 0 to 130 and N = 320, 512, 1500 and 2048, with finite patterns named after those of the C tests but generated differently (constant, ramp, sawtooth, uniform, Gaussian, mean-to-spread ratios 1, 10^3 and 10^5, signed zeros, subnormals; the C tests have no Gaussian pattern) and with NaN and infinite samples: the general entry point against the reference, with count, shift, minimum and maximum equal and the sums within the sum of the two entry points’ bounds; each entry point’s sums and every helper against the exact statistics within the header’s bounds; the helpers bit for bit against the header’s expressions evaluated in float32; and, at the window sizes 52, 320, 512 and 1500, the helpers against NumPy’s float64 statistics within stated test tolerances. It also checks N = 0, N = 1, the minNum and maxNum rules, and the argument errors.