autocorrDirect static method

Float32List autocorrDirect(
  1. Float32List x, {
  2. CorrOutMode mode = CorrOutMode.full,
  3. bool simd = false,
})

Direct O(n^2) autocorrelation in scipy-style output mode.

mode controls output support:

Set simd to true to use the SIMD-accelerated kernel when available.

Example:

final x = Float32List.fromList([1, 2, 3, 4]);
final acf = Conv.autocorrDirect(
  x,
  mode: CorrOutMode.same,
  simd: true,
);

Implementation

static Float32List autocorrDirect(
  Float32List x, {
  CorrOutMode mode = CorrOutMode.full,
  bool simd = false,
}) {
  final n = x.length;
  final ny = autocorrOutLen(n, mode: mode);
  ffi.Pointer<ffi.Float> xPtr = ffi.nullptr;
  ffi.Pointer<ffi.Float> yPtr = ffi.nullptr;

  try {
    xPtr = calloc<ffi.Float>(n);
    yPtr = calloc<ffi.Float>(ny);
    xPtr.asTypedList(n).setAll(0, x);

    final rc = simd
        ? yl_autocorr_direct_simd_ffi(xPtr, n, yPtr, mode.value)
        : yl_autocorr_direct_ffi(xPtr, n, yPtr, mode.value);
    if (rc != 0) {
      throw StateError(
        '${simd ? 'yl_autocorr_direct_simd_ffi' : 'yl_autocorr_direct_ffi'} failed with code $rc',
      );
    }
    return Float32List.fromList(yPtr.asTypedList(ny));
  } finally {
    if (xPtr != ffi.nullptr) calloc.free(xPtr);
    if (yPtr != ffi.nullptr) calloc.free(yPtr);
  }
}