ifft static method

Complex32List ifft(
  1. Float32List real,
  2. Float32List imag, {
  3. int? n,
})

Computes the complex inverse FFT of a complex spectrum.

real and imag hold the real and imaginary parts of the input spectrum and are interpreted as a length-n complex sequence. If n is omitted, the transform length defaults to real.length.

When n is larger than the input length, the spectrum is zero-padded. When n is smaller, the spectrum is truncated to the first n bins.

Returns a complex time-domain sequence of length n. The native inverse path is normalized, so no extra 1 / n scaling is required.

Implementation

static Complex32List ifft(Float32List real, Float32List imag, {int? n}) {
  n = n ?? real.length;

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

  ffi.Pointer<ffi.Float> realInPtr = ffi.nullptr;
  ffi.Pointer<ffi.Float> imagInPtr = ffi.nullptr;
  ffi.Pointer<ffi.Float> realOutPtr = ffi.nullptr;
  ffi.Pointer<ffi.Float> imagOutPtr = ffi.nullptr;
  try {
    realInPtr = calloc<ffi.Float>(n);
    imagInPtr = calloc<ffi.Float>(n);
    realOutPtr = calloc<ffi.Float>(n);
    imagOutPtr = calloc<ffi.Float>(n);

    final copyLen = math.min(real.length, n);
    realInPtr.asTypedList(n).setRange(0, copyLen, real);
    imagInPtr.asTypedList(n).setRange(0, copyLen, imag);

    yl_fft_plan_execute_c2c_backward(
      plan,
      realInPtr,
      imagInPtr,
      realOutPtr,
      imagOutPtr,
    );

    return Complex32List.fromRealImagPtr(realOutPtr, imagOutPtr, n);
  } finally {
    if (realInPtr != ffi.nullptr) calloc.free(realInPtr);
    if (imagInPtr != ffi.nullptr) calloc.free(imagInPtr);
    if (realOutPtr != ffi.nullptr) calloc.free(realOutPtr);
    if (imagOutPtr != ffi.nullptr) calloc.free(imagOutPtr);
    yl_fft_plan_destroy(plan);
  }
}