rfftMag static method

Float32List rfftMag(
  1. Float32List input, {
  2. int? n,
  3. SpecMode mode = SpecMode.mag,
  4. double dbFloor = -120,
})

Computes a real-input magnitude, power, or dB spectrum.

If n is omitted, the transform length defaults to input.length. When n is larger than the input length, the input is zero-padded. When n is smaller, the input is truncated to the first n samples.

Returns n ~/ 2 + 1 bins.

Implementation

static Float32List rfftMag(
  Float32List input, {
  int? n,
  SpecMode mode = SpecMode.mag,
  double dbFloor = -120,
}) {
  n = n ?? input.length;
  final bins = yl_rfft_bins(n);

  final plan = yl_fft_plan_r2c_create(n);
  if (plan == ffi.nullptr) {
    throw StateError('yl_fft_plan_r2c_create($n) returned nullptr');
  }

  ffi.Pointer<ffi.Float> inPtr = ffi.nullptr;
  ffi.Pointer<ffi.Float> specPtr = ffi.nullptr;

  try {
    inPtr = calloc<ffi.Float>(n);
    specPtr = calloc<ffi.Float>(bins);

    final copyLen = math.min(input.length, n);
    inPtr.asTypedList(n).setRange(0, copyLen, input);

    // allocate output Dart list
    final out = Float32List(bins);

    yl_fft_plan_execute_r2c_spec(plan, inPtr, specPtr, mode.value, dbFloor);

    out.setAll(0, specPtr.asTypedList(bins));
    return out;
  } finally {
    if (inPtr != ffi.nullptr) calloc.free(inPtr);
    if (specPtr != ffi.nullptr) calloc.free(specPtr);
    yl_fft_plan_destroy(plan);
  }
}