From cf924cc898d46af5a949b02847bd9ca56b248ed7 Mon Sep 17 00:00:00 2001 From: KevinGrajeda Date: Mon, 16 Jan 2023 18:02:56 -0600 Subject: [PATCH] add negative number error to getEnergy() --- src/fft.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fft.js b/src/fft.js index f6b81e06..f4567ac3 100644 --- a/src/fft.js +++ b/src/fft.js @@ -298,7 +298,7 @@ class FFT { * * frequency, or the average amount of energy between two * frequencies. Accepts Number(s) corresponding - * to frequency (in Hz), or a "string" corresponding to predefined + * to frequency (in Hz) (frequency must be >= 0), or a "string" corresponding to predefined * frequency ranges ("bass", "lowMid", "mid", "highMid", "treble"). * Returns a range between 0 (no energy/volume at that frequency) and * 255 (maximum energy). @@ -318,8 +318,8 @@ class FFT { * will return average amount of * energy that exists between the * two frequencies. - * @return {Number} Energy Energy (volume/amplitude) from - * 0 and 255. + * @return {Number} Energy (volume/amplitude) from + * 0 and 255. * */ getEnergy(frequency1, frequency2) { @@ -350,7 +350,9 @@ class FFT { var index = Math.round((frequency1 / nyquist) * this.freqDomain.length); return this.freqDomain[index]; } - + if (frequency1 < 0 || frequency2 < 0) { + throw 'invalid input for getEnergy(), frequency cannot be a negative number'; + } // if two parameters: // if second is higher than first if (frequency1 > frequency2) {