@@ -458,27 +458,33 @@ GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {
458
458
fftLength = fftOutput . length ;
459
459
}
460
460
461
- // Make all the values absolute, and calculate some useful values (max noise, etc.)
461
+ // Make all the values absolute, and calculate some useful values (max noise, etc
462
+ // The fft output contains complex values (re, im pairs) of two-side spectrum
463
+ // Compute magnitudes for one spectrum side
464
+ const magnitudeLength = Math . floor ( fftLength / 2 ) ;
462
465
const maxFrequency = ( this . _blackBoxRate / 2.0 ) ;
463
- const noiseLowEndIdx = 100 / maxFrequency * fftLength ;
466
+ const noiseLowEndIdx = 100 / maxFrequency * magnitudeLength ;
467
+ const magnitudes = new Float64Array ( magnitudeLength ) ;
464
468
let maxNoiseIdx = 0 ;
465
469
let maxNoise = 0 ;
466
470
467
- for ( let i = 0 ; i < fftLength ; i ++ ) {
468
- fftOutput [ i ] = Math . abs ( fftOutput [ i ] ) ;
469
- if ( i > noiseLowEndIdx && fftOutput [ i ] > maxNoise ) {
470
- maxNoise = fftOutput [ i ] ;
471
+ for ( let i = 0 ; i < magnitudeLength ; i ++ ) {
472
+ const re = fftOutput [ 2 * i ] ,
473
+ im = fftOutput [ 2 * i + 1 ] ;
474
+ magnitudes [ i ] = Math . hypot ( re , im ) ;
475
+ if ( i > noiseLowEndIdx && magnitudes [ i ] > maxNoise ) {
476
+ maxNoise = magnitudes [ i ] ;
471
477
maxNoiseIdx = i ;
472
478
}
473
479
}
474
480
475
- maxNoiseIdx = maxNoiseIdx / fftLength * maxFrequency ;
481
+ maxNoiseIdx = maxNoiseIdx / magnitudeLength * maxFrequency ;
476
482
477
483
const fftData = {
478
484
fieldIndex : this . _dataBuffer . fieldIndex ,
479
485
fieldName : this . _dataBuffer . fieldName ,
480
- fftLength : fftLength ,
481
- fftOutput : fftOutput ,
486
+ fftLength : magnitudeLength ,
487
+ fftOutput : magnitudes ,
482
488
maxNoiseIdx : maxNoiseIdx ,
483
489
blackBoxRate : this . _blackBoxRate ,
484
490
} ;
0 commit comments