diff --git a/src/graph_spectrum_calc.js b/src/graph_spectrum_calc.js index 88338f9c..4445daf0 100644 --- a/src/graph_spectrum_calc.js +++ b/src/graph_spectrum_calc.js @@ -1,85 +1,84 @@ import { FlightLogFieldPresenter } from "./flightlog_fields_presenter"; -const FIELD_THROTTLE_NAME = ["rcCommands[3]"], +const + FIELD_THROTTLE_NAME = ['rcCommands[3]'], FIELD_RPM_NAMES = [ - "eRPM[0]", - "eRPM[1]", - "eRPM[2]", - "eRPM[3]", - "eRPM[4]", - "eRPM[5]", - "eRPM[6]", - "eRPM[7]", + "eRPM[0]", + "eRPM[1]", + "eRPM[2]", + "eRPM[3]", + "eRPM[4]", + "eRPM[5]", + "eRPM[6]", + "eRPM[7]", ], FREQ_VS_THR_CHUNK_TIME_MS = 300, FREQ_VS_THR_WINDOW_DIVISOR = 6, MAX_ANALYSER_LENGTH = 300 * 1000 * 1000, // 5min NUM_VS_BINS = 100, WARNING_RATE_DIFFERENCE = 0.05, - MAX_RPM_VALUE = 10000; + MAX_RPM_HZ_VALUE = 800, + MAX_RPM_AXIS_GAP = 1.05; + export const GraphSpectrumCalc = { - _analyserTimeRange: { - in: 0, - out: MAX_ANALYSER_LENGTH, + _analyserTimeRange : { + in: 0, + out: MAX_ANALYSER_LENGTH, }, - _blackBoxRate: 0, - _dataBuffer: { - fieldIndex: 0, - curve: 0, - fieldName: null, + _blackBoxRate : 0, + _dataBuffer : { + fieldIndex: 0, + curve: 0, + fieldName: null, }, - _flightLog: null, - _sysConfig: null, + _flightLog : null, + _sysConfig : null, + _motorPoles : null, }; -GraphSpectrumCalc.initialize = function (flightLog, sysConfig) { +GraphSpectrumCalc.initialize = function(flightLog, sysConfig) { + this._flightLog = flightLog; this._sysConfig = sysConfig; - let gyroRate = (1000000 / this._sysConfig["looptime"]).toFixed(0); - this._blackBoxRate = - (gyroRate * this._sysConfig["frameIntervalPNum"]) / - this._sysConfig["frameIntervalPDenom"]; + const gyroRate = (1000000 / this._sysConfig['looptime']).toFixed(0); + this._motorPoles = flightLog.getSysConfig()['motor_poles']; + this._blackBoxRate = gyroRate * this._sysConfig['frameIntervalPNum'] / this._sysConfig['frameIntervalPDenom']; if (this._sysConfig.pid_process_denom != null) { this._blackBoxRate = this._blackBoxRate / this._sysConfig.pid_process_denom; } this._BetaflightRate = this._blackBoxRate; - let minTime = this._flightLog.getMinTime(), - maxTime = this._flightLog.getMaxTime(); - let timeRange = maxTime - minTime; + const minTime = this._flightLog.getMinTime(), + maxTime = this._flightLog.getMaxTime(), + timeRange = maxTime - minTime; const length = flightLog.getCurrentLogRowsCount(); - this._actualeRate = (1e6 * length) / timeRange; - if ( - Math.abs(this._BetaflightRate - this._actualeRate) / this._actualeRate > - WARNING_RATE_DIFFERENCE - ) - this._blackBoxRate = Math.round(this._actualeRate); + this._actualeRate = 1e6 * length / timeRange; + if (Math.abs(this._BetaflightRate - this._actualeRate) / this._actualeRate > WARNING_RATE_DIFFERENCE) + this._blackBoxRate = Math.round(this._actualeRate); if (this._BetaflightRate !== this._blackBoxRate) { - $(".actual-lograte").text( - `${this._actualeRate.toFixed(0)}/${this._BetaflightRate.toFixed(0)}Hz` - ); + $('.actual-lograte').text(this._actualeRate.toFixed(0) + "/" + this._BetaflightRate.toFixed(0)+"Hz"); return { actualRate: this._actualeRate, betaflightRate: this._BetaflightRate, }; } else { - $(".actual-lograte").text(""); + $('.actual-lograte').text(""); } return undefined; }; -GraphSpectrumCalc.setInTime = function (time) { +GraphSpectrumCalc.setInTime = function(time) { this._analyserTimeRange.in = time; return this._analyserTimeRange.in; }; -GraphSpectrumCalc.setOutTime = function (time) { - if (time - this._analyserTimeRange.in <= MAX_ANALYSER_LENGTH) { +GraphSpectrumCalc.setOutTime = function(time) { + if ((time - this._analyserTimeRange.in) <= MAX_ANALYSER_LENGTH) { this._analyserTimeRange.out = time; } else { this._analyserTimeRange.out = analyserTimeRange.in + MAX_ANALYSER_LENGTH; @@ -87,61 +86,48 @@ GraphSpectrumCalc.setOutTime = function (time) { return this._analyserTimeRange.out; }; -GraphSpectrumCalc.setDataBuffer = function (dataBuffer) { +GraphSpectrumCalc.setDataBuffer = function(dataBuffer) { this._dataBuffer = dataBuffer; return undefined; }; -GraphSpectrumCalc.dataLoadFrequency = function () { - let flightSamples = this._getFlightSamplesFreq(); +GraphSpectrumCalc.dataLoadFrequency = function() { + + const flightSamples = this._getFlightSamplesFreq(); if (userSettings.analyserHanning) { this._hanningWindow(flightSamples.samples, flightSamples.count); } //calculate fft - let fftOutput = this._fft(flightSamples.samples); + const fftOutput = this._fft(flightSamples.samples); // Normalize the result - let fftData = this._normalizeFft(fftOutput, flightSamples.samples.length); + const fftData = this._normalizeFft(fftOutput, flightSamples.samples.length); return fftData; }; -GraphSpectrumCalc._dataLoadFrequencyVsX = function ( - vsFieldNames, - minValue = Infinity, - maxValue = -Infinity -) { - let flightSamples = this._getFlightSamplesFreqVsX( - vsFieldNames, - minValue, - maxValue - ); + +GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infinity, maxValue = -Infinity) { + + const flightSamples = this._getFlightSamplesFreqVsX(vsFieldNames, minValue, maxValue); // We divide it into FREQ_VS_THR_CHUNK_TIME_MS FFT chunks, we calculate the average throttle // for each chunk. We use a moving window to get more chunks available. - let fftChunkLength = (this._blackBoxRate * FREQ_VS_THR_CHUNK_TIME_MS) / 1000; - let fftChunkWindow = Math.round(fftChunkLength / FREQ_VS_THR_WINDOW_DIVISOR); + const fftChunkLength = this._blackBoxRate * FREQ_VS_THR_CHUNK_TIME_MS / 1000; + const fftChunkWindow = Math.round(fftChunkLength / FREQ_VS_THR_WINDOW_DIVISOR); let maxNoise = 0; // Stores the maximum amplitude of the fft over all chunks - // Matrix where each row represents a bin of vs values, and the columns are amplitudes at frequencies - let matrixFftOutput = new Array(NUM_VS_BINS) - .fill(null) - .map(() => new Float64Array(fftChunkLength * 2)); - - let numberSamples = new Uint32Array(NUM_VS_BINS); // Number of samples in each vs value, used to average them later. - - let fft = new FFT.complex(fftChunkLength, false); - for ( - let fftChunkIndex = 0; - fftChunkIndex + fftChunkLength < flightSamples.samples.length; - fftChunkIndex += fftChunkWindow - ) { - let fftInput = flightSamples.samples.slice( - fftChunkIndex, - fftChunkIndex + fftChunkLength - ); + // Matrix where each row represents a bin of vs values, and the columns are amplitudes at frequencies + const matrixFftOutput = new Array(NUM_VS_BINS).fill(null).map(() => new Float64Array(fftChunkLength * 2)); + + const numberSamples = new Uint32Array(NUM_VS_BINS); // Number of samples in each vs value, used to average them later. + + const fft = new FFT.complex(fftChunkLength, false); + for (let fftChunkIndex = 0; fftChunkIndex + fftChunkLength < flightSamples.samples.length; fftChunkIndex += fftChunkWindow) { + + const fftInput = flightSamples.samples.slice(fftChunkIndex, fftChunkIndex + fftChunkLength); let fftOutput = new Float64Array(fftChunkLength * 2); // Hanning window applied to input data @@ -149,7 +135,7 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function ( this._hanningWindow(fftInput, fftChunkLength); } - fft.simple(fftOutput, fftInput, "real"); + fft.simple(fftOutput, fftInput, 'real'); fftOutput = fftOutput.slice(0, fftChunkLength); @@ -163,23 +149,14 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function ( for (const vsValueArray of flightSamples.vsValues) { // Calculate average of the VS values in the chunk let sumVsValues = 0; - for ( - let indexVs = fftChunkIndex; - indexVs < fftChunkIndex + fftChunkLength; - indexVs++ - ) { + for (let indexVs = fftChunkIndex; indexVs < fftChunkIndex + fftChunkLength; indexVs++) { sumVsValues += vsValueArray[indexVs]; } // Translate the average vs value to a bin index const avgVsValue = sumVsValues / fftChunkLength; - let vsBinIndex = Math.floor( - (NUM_VS_BINS * (avgVsValue - flightSamples.minValue)) / - (flightSamples.maxValue - flightSamples.minValue) - ); + let vsBinIndex = Math.floor(NUM_VS_BINS * (avgVsValue - flightSamples.minValue) / (flightSamples.maxValue - flightSamples.minValue)); // ensure that avgVsValue == flightSamples.maxValue does not result in an out of bounds access - if (vsBinIndex === NUM_VS_BINS) { - vsBinIndex = NUM_VS_BINS - 1; - } + if (vsBinIndex === NUM_VS_BINS) { vsBinIndex = NUM_VS_BINS - 1; } numberSamples[vsBinIndex]++; // add the output from the fft to the row given by the vs value bin index @@ -202,49 +179,48 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function ( // but after some tests we let the data as is, an we prefer to apply a // blur algorithm to the heat map image - let fftData = { - fieldIndex: this._dataBuffer.fieldIndex, - fieldName: this._dataBuffer.fieldName, - fftLength: fftChunkLength, - fftOutput: matrixFftOutput, - maxNoise: maxNoise, - blackBoxRate: this._blackBoxRate, - vsRange: { min: flightSamples.minValue, max: flightSamples.maxValue }, + const fftData = { + fieldIndex : this._dataBuffer.fieldIndex, + fieldName : this._dataBuffer.fieldName, + fftLength : fftChunkLength, + fftOutput : matrixFftOutput, + maxNoise : maxNoise, + blackBoxRate : this._blackBoxRate, + vsRange : { min: flightSamples.minValue, max: flightSamples.maxValue}, }; return fftData; + }; -GraphSpectrumCalc.dataLoadFrequencyVsThrottle = function () { +GraphSpectrumCalc.dataLoadFrequencyVsThrottle = function() { return this._dataLoadFrequencyVsX(FIELD_THROTTLE_NAME, 0, 100); }; -GraphSpectrumCalc.dataLoadFrequencyVsRpm = function () { - let fftData = this._dataLoadFrequencyVsX(FIELD_RPM_NAMES, 0); - const motorPoles = this._flightLog.getSysConfig()["motor_poles"]; - fftData.vsRange.max *= 3.333 / motorPoles; - fftData.vsRange.min *= 3.333 / motorPoles; +GraphSpectrumCalc.dataLoadFrequencyVsRpm = function() { + const fftData = this._dataLoadFrequencyVsX(FIELD_RPM_NAMES, 0); + fftData.vsRange.max *= 3.333 / this._motorPoles; + fftData.vsRange.min *= 3.333 / this._motorPoles; return fftData; }; -GraphSpectrumCalc.dataLoadPidErrorVsSetpoint = function () { +GraphSpectrumCalc.dataLoadPidErrorVsSetpoint = function() { + // Detect the axis let axisIndex; - if (this._dataBuffer.fieldName.indexOf("[roll]") >= 0) { + if (this._dataBuffer.fieldName.indexOf('[roll]') >= 0) { axisIndex = 0; - } else if (this._dataBuffer.fieldName.indexOf("[pitch]") >= 0) { + } else if (this._dataBuffer.fieldName.indexOf('[pitch]') >= 0) { axisIndex = 1; - } else if (this._dataBuffer.fieldName.indexOf("[yaw]") >= 0) { + } else if (this._dataBuffer.fieldName.indexOf('[yaw]') >= 0) { axisIndex = 2; } const flightSamples = this._getFlightSamplesPidErrorVsSetpoint(axisIndex); // Add the total error by absolute position - const errorBySetpoint = Array.from({ length: flightSamples.maxSetpoint + 1 }); - const numberOfSamplesBySetpoint = Array.from({ - length: flightSamples.maxSetpoint + 1, - }); + const errorBySetpoint = Array.from({length: flightSamples.maxSetpoint + 1}); + const numberOfSamplesBySetpoint = Array.from({length: flightSamples.maxSetpoint + 1}); // Initialize for (let i = 0; i <= flightSamples.maxSetpoint; i++) { @@ -254,6 +230,7 @@ GraphSpectrumCalc.dataLoadPidErrorVsSetpoint = function () { // Sum by position for (let i = 0; i < flightSamples.count; i++) { + const pidErrorValue = Math.abs(flightSamples.piderror[i]); const setpointValue = Math.abs(flightSamples.setpoint[i]); @@ -275,17 +252,17 @@ GraphSpectrumCalc.dataLoadPidErrorVsSetpoint = function () { } return { - fieldIndex: this._dataBuffer.fieldIndex, - fieldName: this._dataBuffer.fieldName, - axisName: FlightLogFieldPresenter.fieldNameToFriendly( - `axisError[${axisIndex}]` - ), - fftOutput: errorBySetpoint, - fftMaxOutput: maxErrorBySetpoint, + fieldIndex : this._dataBuffer.fieldIndex, + fieldName : this._dataBuffer.fieldName, + axisName : FlightLogFieldPresenter.fieldNameToFriendly(`axisError[${axisIndex}]`), + fftOutput : errorBySetpoint, + fftMaxOutput : maxErrorBySetpoint, }; + }; -GraphSpectrumCalc._getFlightChunks = function () { +GraphSpectrumCalc._getFlightChunks = function() { + let logStart = 0; if (this._analyserTimeRange.in) { logStart = this._analyserTimeRange.in; @@ -301,43 +278,36 @@ GraphSpectrumCalc._getFlightChunks = function () { } // Limit size - logEnd = - logEnd - logStart <= MAX_ANALYSER_LENGTH - ? logEnd - : logStart + MAX_ANALYSER_LENGTH; + logEnd = (logEnd - logStart <= MAX_ANALYSER_LENGTH)? logEnd : logStart + MAX_ANALYSER_LENGTH; - let allChunks = this._flightLog.getChunksInTimeRange(logStart, logEnd); + const allChunks = this._flightLog.getChunksInTimeRange(logStart, logEnd); return allChunks; }; -GraphSpectrumCalc._getFlightSamplesFreq = function () { - let allChunks = this._getFlightChunks(); +GraphSpectrumCalc._getFlightSamplesFreq = function() { + + const allChunks = this._getFlightChunks(); - let samples = new Float64Array( - (MAX_ANALYSER_LENGTH / (1000 * 1000)) * this._blackBoxRate - ); + const samples = new Float64Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate); // Loop through all the samples in the chunks and assign them to a sample array ready to pass to the FFT. let samplesCount = 0; - for (let chunkIndex = 0; chunkIndex < allChunks.length; chunkIndex++) { - let chunk = allChunks[chunkIndex]; - for (let frameIndex = 0; frameIndex < chunk.frames.length; frameIndex++) { - samples[samplesCount] = this._dataBuffer.curve.lookupRaw( - chunk.frames[frameIndex][this._dataBuffer.fieldIndex] - ); + for (const chunk of allChunks) { + for (const frame of chunk.frames) { + samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(frame[this._dataBuffer.fieldIndex])); samplesCount++; } } return { - samples: samples, - count: samplesCount, + samples : samples, + count : samplesCount, }; }; -GraphSpectrumCalc._getVsIndexes = function (vsFieldNames) { - let fieldIndexes = []; +GraphSpectrumCalc._getVsIndexes = function(vsFieldNames) { + const fieldIndexes = []; for (const fieldName of vsFieldNames) { if (Object.hasOwn(this._flightLog.getMainFieldIndexes(), fieldName)) { fieldIndexes.push(this._flightLog.getMainFieldIndexByName(fieldName)); @@ -346,62 +316,62 @@ GraphSpectrumCalc._getVsIndexes = function (vsFieldNames) { return fieldIndexes; }; -GraphSpectrumCalc._getFlightSamplesFreqVsX = function ( - vsFieldNames, - minValue = Infinity, - maxValue = -Infinity -) { - let allChunks = this._getFlightChunks(); - let vsIndexes = this._getVsIndexes(vsFieldNames); - - let samples = new Float64Array( - (MAX_ANALYSER_LENGTH / (1000 * 1000)) * this._blackBoxRate - ); - let vsValues = new Array(vsIndexes.length) - .fill(null) - .map( - () => - new Float64Array( - (MAX_ANALYSER_LENGTH / (1000 * 1000)) * this._blackBoxRate - ) - ); +GraphSpectrumCalc._getFlightSamplesFreqVsX = function(vsFieldNames, minValue = Infinity, maxValue = -Infinity) { + + const allChunks = this._getFlightChunks(); + const vsIndexes = this._getVsIndexes(vsFieldNames); + + const samples = new Float64Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate); + const vsValues = new Array(vsIndexes.length).fill(null).map(() => new Float64Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate)); let samplesCount = 0; - let lastRPM = 0; - for (let chunkIndex = 0; chunkIndex < allChunks.length; chunkIndex++) { - let chunk = allChunks[chunkIndex]; + for (const chunk of allChunks) { for (let frameIndex = 0; frameIndex < chunk.frames.length; frameIndex++) { - samples[samplesCount] = this._dataBuffer.curve.lookupRaw( - chunk.frames[frameIndex][this._dataBuffer.fieldIndex] - ); - + samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(chunk.frames[frameIndex][this._dataBuffer.fieldIndex])); for (let i = 0; i < vsIndexes.length; i++) { let vsFieldIx = vsIndexes[i]; let value = chunk.frames[frameIndex][vsFieldIx]; if (vsFieldNames == FIELD_RPM_NAMES) { - if (value > MAX_RPM_VALUE || value < 0) value = lastRPM; - lastRPM = value; + const maxRPM = MAX_RPM_HZ_VALUE * this._motorPoles / 3.333; + if (value > maxRPM) { + value = maxRPM; + } + else if (value < 0) { + value = 0; + } } - maxValue = Math.max(maxValue, value); - minValue = Math.min(minValue, value); vsValues[i][samplesCount] = value; } samplesCount++; } } + // Calculate min max average of the VS values in the chunk what will used by spectrum data definition + const fftChunkLength = this._blackBoxRate * FREQ_VS_THR_CHUNK_TIME_MS / 1000; + const fftChunkWindow = Math.round(fftChunkLength / FREQ_VS_THR_WINDOW_DIVISOR); + for (let fftChunkIndex = 0; fftChunkIndex + fftChunkLength < samplesCount; fftChunkIndex += fftChunkWindow) { + for (const vsValueArray of vsValues) { + // Calculate average of the VS values in the chunk + let sumVsValues = 0; + for (let indexVs = fftChunkIndex; indexVs < fftChunkIndex + fftChunkLength; indexVs++) { + sumVsValues += vsValueArray[indexVs]; + } + // Find min max average of the VS values in the chunk + const avgVsValue = sumVsValues / fftChunkLength; + maxValue = Math.max(maxValue, avgVsValue); + minValue = Math.min(minValue, avgVsValue); + } + } + + maxValue *= MAX_RPM_AXIS_GAP; + if (minValue > maxValue) { - if (minValue == Infinity) { - // this should never happen + if (minValue == Infinity) { // this should never happen minValue = 0; maxValue = 100; console.log("Invalid minimum value"); } else { - console.log( - "Maximum value %f smaller than minimum value %d", - maxValue, - minValue - ); + console.log("Maximum value %f smaller than minimum value %d", maxValue, minValue); minValue = 0; maxValue = 100; } @@ -412,40 +382,32 @@ GraphSpectrumCalc._getFlightSamplesFreqVsX = function ( slicedVsValues.push(vsValueArray.slice(0, samplesCount)); } return { - samples: samples.slice(0, samplesCount), - vsValues: slicedVsValues, - count: samplesCount, - minValue: minValue, - maxValue: maxValue, - }; + samples : samples.slice(0, samplesCount), + vsValues : slicedVsValues, + count : samplesCount, + minValue : minValue, + maxValue : maxValue, + }; }; -GraphSpectrumCalc._getFlightSamplesPidErrorVsSetpoint = function (axisIndex) { +GraphSpectrumCalc._getFlightSamplesPidErrorVsSetpoint = function(axisIndex) { + const allChunks = this._getFlightChunks(); // Get the PID Error field - const FIELD_PIDERROR_INDEX = this._flightLog.getMainFieldIndexByName( - `axisError[${axisIndex}]` - ); - const FIELD_SETPOINT_INDEX = this._flightLog.getMainFieldIndexByName( - `setpoint[${axisIndex}]` - ); - - const piderror = new Int16Array( - (MAX_ANALYSER_LENGTH / (1000 * 1000)) * this._blackBoxRate - ); - const setpoint = new Int16Array( - (MAX_ANALYSER_LENGTH / (1000 * 1000)) * this._blackBoxRate - ); + const FIELD_PIDERROR_INDEX = this._flightLog.getMainFieldIndexByName(`axisError[${axisIndex}]`); + const FIELD_SETPOINT_INDEX = this._flightLog.getMainFieldIndexByName(`setpoint[${axisIndex}]`); + + const piderror = new Int16Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate); + const setpoint = new Int16Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate); // Loop through all the samples in the chunks and assign them to a sample array. let samplesCount = 0; let maxSetpoint = 0; - for (let chunkIndex = 0; chunkIndex < allChunks.length; chunkIndex++) { - const chunk = allChunks[chunkIndex]; - for (let frameIndex = 0; frameIndex < chunk.frames.length; frameIndex++) { - piderror[samplesCount] = chunk.frames[frameIndex][FIELD_PIDERROR_INDEX]; - setpoint[samplesCount] = chunk.frames[frameIndex][FIELD_SETPOINT_INDEX]; + for (const chunk of allChunks) { + for (const frame of chunk.frames) { + piderror[samplesCount] = frame[FIELD_PIDERROR_INDEX]; + setpoint[samplesCount] = frame[FIELD_SETPOINT_INDEX]; if (setpoint[samplesCount] > maxSetpoint) { maxSetpoint = setpoint[samplesCount]; } @@ -461,41 +423,45 @@ GraphSpectrumCalc._getFlightSamplesPidErrorVsSetpoint = function (axisIndex) { }; }; -GraphSpectrumCalc._hanningWindow = function (samples, size) { +GraphSpectrumCalc._hanningWindow = function(samples, size) { + if (!size) { size = samples.length; } - for (let i = 0; i < size; i++) { - samples[i] *= 0.5 * (1 - Math.cos((2 * Math.PI * i) / (size - 1))); + for(let i=0; i < size; i++) { + samples[i] *= 0.5 * (1-Math.cos((2*Math.PI*i)/(size - 1))); } }; -GraphSpectrumCalc._fft = function (samples, type) { +GraphSpectrumCalc._fft = function(samples, type) { + if (!type) { - type = "real"; + type = 'real'; } - let fftLength = samples.length; - let fftOutput = new Float64Array(fftLength * 2); - let fft = new FFT.complex(fftLength, false); + const fftLength = samples.length; + const fftOutput = new Float64Array(fftLength * 2); + const fft = new FFT.complex(fftLength, false); fft.simple(fftOutput, samples, type); return fftOutput; }; + /** * Makes all the values absolute and returns the index of maxFrequency found */ -GraphSpectrumCalc._normalizeFft = function (fftOutput, fftLength) { +GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) { + if (!fftLength) { fftLength = fftOutput.length; } // Make all the values absolute, and calculate some useful values (max noise, etc.) - let maxFrequency = this._blackBoxRate / 2.0; - let noiseLowEndIdx = (100 / maxFrequency) * fftLength; + const maxFrequency = (this._blackBoxRate / 2.0); + const noiseLowEndIdx = 100 / maxFrequency * fftLength; let maxNoiseIdx = 0; let maxNoise = 0; @@ -507,15 +473,15 @@ GraphSpectrumCalc._normalizeFft = function (fftOutput, fftLength) { } } - maxNoiseIdx = (maxNoiseIdx / fftLength) * maxFrequency; + maxNoiseIdx = maxNoiseIdx / fftLength * maxFrequency; - let fftData = { - fieldIndex: this._dataBuffer.fieldIndex, - fieldName: this._dataBuffer.fieldName, - fftLength: fftLength, - fftOutput: fftOutput, - maxNoiseIdx: maxNoiseIdx, - blackBoxRate: this._blackBoxRate, + const fftData = { + fieldIndex : this._dataBuffer.fieldIndex, + fieldName : this._dataBuffer.fieldName, + fftLength : fftLength, + fftOutput : fftOutput, + maxNoiseIdx : maxNoiseIdx, + blackBoxRate : this._blackBoxRate, }; return fftData;