Skip to content

Commit 73dad8f

Browse files
committed
1 parent dc69480 commit 73dad8f

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

src/graph_spectrum_calc.js

+25-37
Original file line numberDiff line numberDiff line change
@@ -567,53 +567,41 @@ GraphSpectrumCalc._fft = function(samples, type) {
567567
* Makes all the values absolute and returns the index of maxFrequency found
568568
*/
569569
GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {
570-
// fftLength is the number of samples input to FFT.
571-
// fftOutput is the complex result from _fft.
572-
// The original _normalizeFft looped 'fftLength' times assuming 'fftOutput' contained 'fftLength' magnitudes.
573-
// This behavior is preserved. If fftOutput from _fft contains N complex values (2N floats),
574-
// and fftLength is N, this loop processes N floats from fftOutput, taking their abs value.
575-
576-
// Number of actual magnitudes to consider is fftLength / 2 (approx, up to Nyquist)
577-
// However, to match existing behavior where it iterated fftLength times over fftOutput:
578-
const iterationLength = fftLength; // Or Math.min(fftLength, fftOutput.length) if fftOutput could be shorter
570+
// Number of usable frequency bins (0 to Nyquist)
571+
const bins = Math.floor(fftLength / 2) + 1; // +1 to include Nyquist bin
572+
const magnitudes = new Float64Array(bins);
573+
574+
// Calculate magnitudes from complex values
575+
for (let bin = 0; bin < bins; bin++) {
576+
const re = fftOutput[2 * bin];
577+
const im = fftOutput[2 * bin + 1];
578+
magnitudes[bin] = Math.hypot(re, im);
579+
}
579580

580-
// Make all the values absolute, and calculate some useful values (max noise, etc.)
581+
// Find max noise after low-end cutoff
581582
const maxFrequency = (this._blackBoxRate / 2.0);
582-
// This scaling implies fftLength is the number of points up to Nyquist.
583-
// If fftLength is num_samples, then num_points_to_nyquist is num_samples / 2.
584-
// To maintain existing scaling logic with fftLength = num_samples:
585-
const noiseLowEndIdx = 100 / maxFrequency * (iterationLength / 2); // Adjusted for N/2 bins
583+
const noiseLowEndIdx = Math.floor(100 / maxFrequency * bins);
586584
let maxNoiseIdx = 0;
587585
let maxNoise = 0;
588586

589-
// Create a new array for magnitudes if we are to correctly interpret complex fftOutput
590-
// For minimal change, we continue the existing pattern of modifying fftOutput in place.
591-
// The loop below is likely incorrect for complex fftOutput but is existing behavior.
592-
for (let i = 0; i < iterationLength; i++) { // This loop is over first N floats of complex output
593-
fftOutput[i] = Math.abs(fftOutput[i]);
594-
// The condition for maxNoiseIdx should refer to frequency bins, not raw float indices
595-
const currentFreqBin = i / 2; // Approximate, assumes pairs
596-
if (currentFreqBin > noiseLowEndIdx && fftOutput[i] > maxNoise) {
597-
maxNoise = fftOutput[i];
598-
maxNoiseIdx = currentFreqBin; // Store bin index
587+
for (let bin = 0; bin < bins; bin++) {
588+
if (bin > noiseLowEndIdx && magnitudes[bin] > maxNoise) {
589+
maxNoise = magnitudes[bin];
590+
maxNoiseIdx = bin;
599591
}
600592
}
601593

602-
// maxNoiseIdx is now a bin index relative to N/2 bins.
603-
// Scale this bin index to frequency.
604-
maxNoiseIdx = maxNoiseIdx / (iterationLength / 2) * maxFrequency;
605-
594+
// Scale bin index to frequency
595+
const maxNoiseFreq = maxNoiseIdx / bins * maxFrequency;
606596

607-
const fftData = {
608-
fieldIndex : this._dataBuffer.fieldIndex,
609-
fieldName : this._dataBuffer.fieldName,
610-
fftLength : iterationLength, // Or iterationLength / 2 if this means number of frequency bins
611-
fftOutput : fftOutput.slice(0, iterationLength), // Return the part we processed
612-
maxNoiseIdx : maxNoiseIdx,
613-
blackBoxRate : this._blackBoxRate,
597+
return {
598+
fieldIndex: this._dataBuffer.fieldIndex,
599+
fieldName: this._dataBuffer.fieldName,
600+
fftLength: bins, // Return number of frequency bins
601+
fftOutput: magnitudes, // Return the magnitude spectrum
602+
maxNoiseIdx: maxNoiseFreq,
603+
blackBoxRate: this._blackBoxRate,
614604
};
615-
616-
return fftData;
617605
};
618606

619607
/**

0 commit comments

Comments
 (0)