doFft function
Performs FFT (Fast Fourier Transform) calculation on the given waveform data.
This method accepts a list of 8-bit unsigned integers representing the waveform data and outputs the FFT result into the provided list of 8-bit unsigned integers.
The input waveform
list should contain pairs of bytes representing the waveform data.
The output fft
list will contain the FFT result, with each pair of bytes representing
the real and imaginary components of the FFT result.
The method processes the input waveform data into a workspace, performs the FFT calculation, and then processes the workspace data into the output FFT buffer.
param fft
The list of 8-bit unsigned integers to store the FFT result.
param waveform
The list of 8-bit unsigned integers representing the waveform data.
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] = uint8(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] = uint8(tmp);
}
}