Skip to content

Commit 5b2bf5c

Browse files
authored
Merge pull request #722 from KevinGrajeda/fft-getEnergy
fix FFT.getEnergy parameter validation
2 parents 959de65 + d99a907 commit 5b2bf5c

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

src/fft.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -344,38 +344,33 @@ 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-
);
352+
}
365353

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 {
377-
throw 'invalid input for getEnergy()';
354+
// if two parameters:
355+
// if second is higher than first
356+
if (frequency1 > frequency2) {
357+
var swap = frequency2;
358+
frequency2 = frequency1;
359+
frequency1 = swap;
360+
}
361+
var lowIndex = Math.round((frequency1 / nyquist) * this.freqDomain.length);
362+
var highIndex = Math.round((frequency2 / nyquist) * this.freqDomain.length);
363+
364+
var total = 0;
365+
var numFrequencies = 0;
366+
// add up all of the values for the frequencies
367+
for (var i = lowIndex; i <= highIndex; i++) {
368+
total += this.freqDomain[i];
369+
numFrequencies += 1;
378370
}
371+
// divide by total number of frequencies
372+
var toReturn = total / numFrequencies;
373+
return toReturn;
379374
}
380375

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

0 commit comments

Comments
 (0)