Skip to content

Commit 46a6b04

Browse files
committed
fix getEnergy parameter validation
1 parent 959de65 commit 46a6b04

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/fft.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -344,38 +344,36 @@ class FFT {
344344

345345
if (typeof frequency1 !== 'number') {
346346
throw 'invalid input for getEnergy()';
347-
} else if (!frequency2) {
347+
}
348+
if (typeof frequency2 !== 'number') {
348349
// if only one parameter:
349350
var index = Math.round((frequency1 / nyquist) * this.freqDomain.length);
350351
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) {
377354
throw 'invalid input for getEnergy()';
378355
}
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;
379377
}
380378

381379
// compatability with v.012, changed to getEnergy in v.0121. Will be deprecated...

0 commit comments

Comments
 (0)