@@ -458,27 +458,33 @@ GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {
458458 fftLength = fftOutput . length ;
459459 }
460460
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 ) ;
462465 const maxFrequency = ( this . _blackBoxRate / 2.0 ) ;
463- const noiseLowEndIdx = 100 / maxFrequency * fftLength ;
466+ const noiseLowEndIdx = 100 / maxFrequency * magnitudeLength ;
467+ const magnitudes = new Float64Array ( magnitudeLength ) ;
464468 let maxNoiseIdx = 0 ;
465469 let maxNoise = 0 ;
466470
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 ] ;
471477 maxNoiseIdx = i ;
472478 }
473479 }
474480
475- maxNoiseIdx = maxNoiseIdx / fftLength * maxFrequency ;
481+ maxNoiseIdx = maxNoiseIdx / magnitudeLength * maxFrequency ;
476482
477483 const fftData = {
478484 fieldIndex : this . _dataBuffer . fieldIndex ,
479485 fieldName : this . _dataBuffer . fieldName ,
480- fftLength : fftLength ,
481- fftOutput : fftOutput ,
486+ fftLength : magnitudeLength ,
487+ fftOutput : magnitudes ,
482488 maxNoiseIdx : maxNoiseIdx ,
483489 blackBoxRate : this . _blackBoxRate ,
484490 } ;
0 commit comments