doFft function

void doFft(
  1. List<int> fft,
  2. List<int> waveform
)

Implementation

void doFft(List<int> fft, List<int> waveform) {
  final int captureSize = waveform.length;
  // Create workspace with half the capture size
  final Int32List workspace = Int32List(captureSize >> 1);
  int nonzero = 0;

  // Process input waveform data into workspace
  for (int i = 0; i < captureSize; i += 2) {
    // Convert pairs of bytes to 32-bit integers
    // Note: Using bitwise operations ensures 32-bit arithmetic
    workspace[i >> 1] =
        (((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8));
    nonzero |= workspace[i >> 1];
  }

  // Only perform FFT if we have non-zero data
  if (nonzero != 0) {
    fftReal(captureSize >> 1, workspace);
  }

  // Process workspace data into output FFT buffer
  for (int i = 0; i < captureSize; i += 2) {
    // Process real component
    int tmp = int16(workspace[i >> 1] >> 21);
    // Clamp to 8-bit range
    while (tmp > 127 || tmp < -128) {
      tmp >>= 1;
    }
    fft[i] = int16(tmp);

    // Process imaginary component
    tmp = int16(workspace[i >> 1]);
    tmp >>= 5;
    // Clamp to 8-bit range
    while (tmp > 127 || tmp < -128) {
      tmp >>= 1;
    }
    fft[i + 1] = int16(tmp);
  }
}