@@ -344,38 +344,36 @@ class FFT {
344
344
345
345
if ( typeof frequency1 !== 'number' ) {
346
346
throw 'invalid input for getEnergy()' ;
347
- } else if ( ! frequency2 ) {
347
+ }
348
+ if ( typeof frequency2 !== 'number' ) {
348
349
// if only one parameter:
349
350
var index = Math . round ( ( frequency1 / nyquist ) * this . freqDomain . length ) ;
350
351
return this . freqDomain [ index ] ;
351
- } else if ( frequency1 && frequency2 ) {
352
- // if two parameters:
353
- // if second is higher than first
354
- if ( frequency1 > frequency2 ) {
355
- var swap = frequency2 ;
356
- frequency2 = frequency1 ;
357
- frequency1 = swap ;
358
- }
359
- var lowIndex = Math . round (
360
- ( frequency1 / nyquist ) * this . freqDomain . length
361
- ) ;
362
- var highIndex = Math . round (
363
- ( frequency2 / nyquist ) * this . freqDomain . length
364
- ) ;
365
-
366
- var total = 0 ;
367
- var numFrequencies = 0 ;
368
- // add up all of the values for the frequencies
369
- for ( var i = lowIndex ; i <= highIndex ; i ++ ) {
370
- total += this . freqDomain [ i ] ;
371
- numFrequencies += 1 ;
372
- }
373
- // divide by total number of frequencies
374
- var toReturn = total / numFrequencies ;
375
- return toReturn ;
376
- } else {
352
+ }
353
+ if ( frequency1 < 0 || frequency2 < 0 ) {
377
354
throw 'invalid input for getEnergy()' ;
378
355
}
356
+
357
+ // if two parameters:
358
+ // if second is higher than first
359
+ if ( frequency1 > frequency2 ) {
360
+ var swap = frequency2 ;
361
+ frequency2 = frequency1 ;
362
+ frequency1 = swap ;
363
+ }
364
+ var lowIndex = Math . round ( ( frequency1 / nyquist ) * this . freqDomain . length ) ;
365
+ var highIndex = Math . round ( ( frequency2 / nyquist ) * this . freqDomain . length ) ;
366
+
367
+ var total = 0 ;
368
+ var numFrequencies = 0 ;
369
+ // add up all of the values for the frequencies
370
+ for ( var i = lowIndex ; i <= highIndex ; i ++ ) {
371
+ total += this . freqDomain [ i ] ;
372
+ numFrequencies += 1 ;
373
+ }
374
+ // divide by total number of frequencies
375
+ var toReturn = total / numFrequencies ;
376
+ return toReturn ;
379
377
}
380
378
381
379
// compatability with v.012, changed to getEnergy in v.0121. Will be deprecated...
0 commit comments