getMagnitudes function
Computes the magnitudes from the FFT (Fast Fourier Transform) data.
This function processes the FFT data to calculate the magnitudes of the frequency components.
It scales the magnitudes to the range 0, 255
.
param fft
A list of integers representing the FFT data.
returns A list of integers representing the magnitudes of the frequency components.
Implementation
List<int> getMagnitudes(List<int> fft) {
if (fft.isEmpty) return [];
final n = fft.length;
final magnitudes = List<double>.filled(n ~/ 2 + 1, 0);
magnitudes[0] = fft[0].abs().toDouble();
magnitudes[n ~/ 2] = fft[1].abs().toDouble();
for (int k = 1; k < n ~/ 2; k++) {
int i = k * 2;
final real = fft[i];
final imag = fft[i + 1];
magnitudes[k] = hypotenuse(real, imag).toDouble();
magnitudes[k] = scale(magnitudes[k].floor(), 0, 180, 0, 255);
}
return magnitudes.map((e) => e.toInt()).toList();
}