Skip to content

Commit 4ed8afa

Browse files
committed
graph_spectrum_calc.js code refactoring
1 parent 04331c7 commit 4ed8afa

File tree

1 file changed

+44
-47
lines changed

1 file changed

+44
-47
lines changed

src/graph_spectrum_calc.js

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,41 @@ GraphSpectrumCalc.setDataBuffer = function(dataBuffer) {
8585

8686
GraphSpectrumCalc.dataLoadFrequency = function() {
8787

88-
var flightSamples = this._getFlightSamplesFreq();
88+
const flightSamples = this._getFlightSamplesFreq();
8989

9090
if(userSettings.analyserHanning) {
9191
this._hanningWindow(flightSamples.samples, flightSamples.count);
9292
}
9393

9494
//calculate fft
95-
var fftOutput = this._fft(flightSamples.samples);
95+
const fftOutput = this._fft(flightSamples.samples);
9696

9797
// Normalize the result
98-
var fftData = this._normalizeFft(fftOutput, flightSamples.samples.length);
98+
const fftData = this._normalizeFft(fftOutput, flightSamples.samples.length);
9999

100100
return fftData;
101101
};
102102

103103

104104
GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infinity, maxValue = -Infinity) {
105105

106-
let flightSamples = this._getFlightSamplesFreqVsX(vsFieldNames, minValue, maxValue);
106+
const flightSamples = this._getFlightSamplesFreqVsX(vsFieldNames, minValue, maxValue);
107107

108108
// We divide it into FREQ_VS_THR_CHUNK_TIME_MS FFT chunks, we calculate the average throttle
109109
// for each chunk. We use a moving window to get more chunks available.
110-
var fftChunkLength = this._blackBoxRate * FREQ_VS_THR_CHUNK_TIME_MS / 1000;
111-
var fftChunkWindow = Math.round(fftChunkLength / FREQ_VS_THR_WINDOW_DIVISOR);
110+
const fftChunkLength = this._blackBoxRate * FREQ_VS_THR_CHUNK_TIME_MS / 1000;
111+
const fftChunkWindow = Math.round(fftChunkLength / FREQ_VS_THR_WINDOW_DIVISOR);
112112

113113
let maxNoise = 0; // Stores the maximum amplitude of the fft over all chunks
114114
// Matrix where each row represents a bin of vs values, and the columns are amplitudes at frequencies
115-
let matrixFftOutput = new Array(NUM_VS_BINS).fill(null).map(() => new Float64Array(fftChunkLength * 2));
115+
const matrixFftOutput = new Array(NUM_VS_BINS).fill(null).map(() => new Float64Array(fftChunkLength * 2));
116116

117-
let numberSamples = new Uint32Array(NUM_VS_BINS); // Number of samples in each vs value, used to average them later.
117+
const numberSamples = new Uint32Array(NUM_VS_BINS); // Number of samples in each vs value, used to average them later.
118118

119-
var fft = new FFT.complex(fftChunkLength, false);
120-
for (var fftChunkIndex = 0; fftChunkIndex + fftChunkLength < flightSamples.samples.length; fftChunkIndex += fftChunkWindow) {
119+
const fft = new FFT.complex(fftChunkLength, false);
120+
for (let fftChunkIndex = 0; fftChunkIndex + fftChunkLength < flightSamples.samples.length; fftChunkIndex += fftChunkWindow) {
121121

122-
let fftInput = flightSamples.samples.slice(fftChunkIndex, fftChunkIndex + fftChunkLength);
122+
const fftInput = flightSamples.samples.slice(fftChunkIndex, fftChunkIndex + fftChunkLength);
123123
let fftOutput = new Float64Array(fftChunkLength * 2);
124124

125125
// Hanning window applied to input data
@@ -161,7 +161,7 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infi
161161
// Divide the values from the fft in each row (vs value bin) by the number of samples in the bin
162162
for (let i = 0; i < NUM_VS_BINS; i++) {
163163
if (numberSamples[i] > 1) {
164-
for (var j = 0; j < matrixFftOutput[i].length; j++) {
164+
for (let j = 0; j < matrixFftOutput[i].length; j++) {
165165
matrixFftOutput[i][j] /= numberSamples[i];
166166
}
167167
}
@@ -171,7 +171,7 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infi
171171
// but after some tests we let the data as is, an we prefer to apply a
172172
// blur algorithm to the heat map image
173173

174-
var fftData = {
174+
const fftData = {
175175
fieldIndex : this._dataBuffer.fieldIndex,
176176
fieldName : this._dataBuffer.fieldName,
177177
fftLength : fftChunkLength,
@@ -190,7 +190,7 @@ GraphSpectrumCalc.dataLoadFrequencyVsThrottle = function() {
190190
};
191191

192192
GraphSpectrumCalc.dataLoadFrequencyVsRpm = function() {
193-
let fftData = this._dataLoadFrequencyVsX(FIELD_RPM_NAMES, 0);
193+
const fftData = this._dataLoadFrequencyVsX(FIELD_RPM_NAMES, 0);
194194
fftData.vsRange.max *= 3.333 / this._motorPoles;
195195
fftData.vsRange.min *= 3.333 / this._motorPoles;
196196
return fftData;
@@ -255,14 +255,14 @@ GraphSpectrumCalc.dataLoadPidErrorVsSetpoint = function() {
255255

256256
GraphSpectrumCalc._getFlightChunks = function() {
257257

258-
var logStart = 0;
258+
let logStart = 0;
259259
if(this._analyserTimeRange.in) {
260260
logStart = this._analyserTimeRange.in;
261261
} else {
262262
logStart = this._flightLog.getMinTime();
263263
}
264264

265-
var logEnd = 0;
265+
let logEnd = 0;
266266
if(this._analyserTimeRange.out) {
267267
logEnd = this._analyserTimeRange.out;
268268
} else {
@@ -272,23 +272,22 @@ GraphSpectrumCalc._getFlightChunks = function() {
272272
// Limit size
273273
logEnd = (logEnd - logStart <= MAX_ANALYSER_LENGTH)? logEnd : logStart + MAX_ANALYSER_LENGTH;
274274

275-
var allChunks = this._flightLog.getChunksInTimeRange(logStart, logEnd);
275+
const allChunks = this._flightLog.getChunksInTimeRange(logStart, logEnd);
276276

277277
return allChunks;
278278
}
279279

280280
GraphSpectrumCalc._getFlightSamplesFreq = function() {
281281

282-
var allChunks = this._getFlightChunks();
282+
const allChunks = this._getFlightChunks();
283283

284-
var samples = new Float64Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate);
284+
const samples = new Float64Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate);
285285

286286
// Loop through all the samples in the chunks and assign them to a sample array ready to pass to the FFT.
287-
var samplesCount = 0;
288-
for (var chunkIndex = 0; chunkIndex < allChunks.length; chunkIndex++) {
289-
var chunk = allChunks[chunkIndex];
290-
for (var frameIndex = 0; frameIndex < chunk.frames.length; frameIndex++) {
291-
samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(chunk.frames[frameIndex][this._dataBuffer.fieldIndex]));
287+
let samplesCount = 0;
288+
for (const chunk of allChunks) {
289+
for (const frame of chunk.frames) {
290+
samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(frame[this._dataBuffer.fieldIndex]));
292291
samplesCount++;
293292
}
294293
}
@@ -300,7 +299,7 @@ GraphSpectrumCalc._getFlightSamplesFreq = function() {
300299
};
301300

302301
GraphSpectrumCalc._getVsIndexes = function(vsFieldNames) {
303-
let fieldIndexes = [];
302+
const fieldIndexes = [];
304303
for (const fieldName of vsFieldNames) {
305304
if (Object.hasOwn(this._flightLog.getMainFieldIndexes(), fieldName)) {
306305
fieldIndexes.push(this._flightLog.getMainFieldIndexByName(fieldName));
@@ -318,15 +317,14 @@ GraphSpectrumCalc._getFlightSamplesFreqVsX = function(vsFieldNames, minValue = I
318317
const vsValues = new Array(vsIndexes.length).fill(null).map(() => new Float64Array(MAX_ANALYSER_LENGTH / (1000 * 1000) * this._blackBoxRate));
319318

320319
let samplesCount = 0;
321-
for (var chunkIndex = 0; chunkIndex < allChunks.length; chunkIndex++) {
322-
var chunk = allChunks[chunkIndex];
323-
for (var frameIndex = 0; frameIndex < chunk.frames.length; frameIndex++) {
324-
samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(chunk.frames[frameIndex][this._dataBuffer.fieldIndex]));
325-
const time = chunk.frames[frameIndex][1];
320+
for (const chunk of allChunks) {
321+
for (const frame of chunk.frames) {
322+
samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(frame[this._dataBuffer.fieldIndex]));
323+
const time = frame[1];
326324
const dT = this._prevTime != null ? (time - this._prevTime)/1000000 : undefined;
327325
for (let i = 0; i < vsIndexes.length; i++) {
328326
const vsFieldIx = vsIndexes[i];
329-
let value = chunk.frames[frameIndex][vsFieldIx];
327+
let value = frame[vsFieldIx];
330328
if (vsFieldNames == FIELD_RPM_NAMES) {
331329
if (this._prevRPM[i] != undefined && dT != undefined) {
332330
const veloRPM = (value - this._prevRPM[i]) / dT * 3.333 / this._motorPoles;
@@ -358,7 +356,7 @@ GraphSpectrumCalc._getFlightSamplesFreqVsX = function(vsFieldNames, minValue = I
358356
}
359357
}
360358

361-
let slicedVsValues = [];
359+
const slicedVsValues = [];
362360
for (const vsValueArray of vsValues) {
363361
slicedVsValues.push(vsValueArray.slice(0, samplesCount));
364362
}
@@ -385,11 +383,10 @@ GraphSpectrumCalc._getFlightSamplesPidErrorVsSetpoint = function(axisIndex) {
385383
// Loop through all the samples in the chunks and assign them to a sample array.
386384
let samplesCount = 0;
387385
let maxSetpoint = 0;
388-
for (let chunkIndex = 0; chunkIndex < allChunks.length; chunkIndex++) {
389-
const chunk = allChunks[chunkIndex];
390-
for (let frameIndex = 0; frameIndex < chunk.frames.length; frameIndex++) {
391-
piderror[samplesCount] = chunk.frames[frameIndex][FIELD_PIDERROR_INDEX];
392-
setpoint[samplesCount] = chunk.frames[frameIndex][FIELD_SETPOINT_INDEX];
386+
for (const chunk of allChunks) {
387+
for (const frame of chunk.frames) {
388+
piderror[samplesCount] = frame[FIELD_PIDERROR_INDEX];
389+
setpoint[samplesCount] = frame[FIELD_SETPOINT_INDEX];
393390
if (setpoint[samplesCount] > maxSetpoint) {
394391
maxSetpoint = setpoint[samplesCount];
395392
}
@@ -411,7 +408,7 @@ GraphSpectrumCalc._hanningWindow = function(samples, size) {
411408
size = samples.length;
412409
}
413410

414-
for(var i=0; i < size; i++) {
411+
for(let i=0; i < size; i++) {
415412
samples[i] *= 0.5 * (1-Math.cos((2*Math.PI*i)/(size - 1)));
416413
}
417414
};
@@ -422,9 +419,9 @@ GraphSpectrumCalc._fft = function(samples, type) {
422419
type = 'real';
423420
}
424421

425-
var fftLength = samples.length;
426-
var fftOutput = new Float64Array(fftLength * 2);
427-
var fft = new FFT.complex(fftLength, false);
422+
const fftLength = samples.length;
423+
const fftOutput = new Float64Array(fftLength * 2);
424+
const fft = new FFT.complex(fftLength, false);
428425

429426
fft.simple(fftOutput, samples, type);
430427

@@ -442,12 +439,12 @@ GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {
442439
}
443440

444441
// Make all the values absolute, and calculate some useful values (max noise, etc.)
445-
var maxFrequency = (this._blackBoxRate / 2.0);
446-
var noiseLowEndIdx = 100 / maxFrequency * fftLength;
447-
var maxNoiseIdx = 0;
448-
var maxNoise = 0;
442+
const maxFrequency = (this._blackBoxRate / 2.0);
443+
const noiseLowEndIdx = 100 / maxFrequency * fftLength;
444+
let maxNoiseIdx = 0;
445+
let maxNoise = 0;
449446

450-
for (var i = 0; i < fftLength; i++) {
447+
for (let i = 0; i < fftLength; i++) {
451448
fftOutput[i] = Math.abs(fftOutput[i]);
452449
if (i > noiseLowEndIdx && fftOutput[i] > maxNoise) {
453450
maxNoise = fftOutput[i];
@@ -457,7 +454,7 @@ GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {
457454

458455
maxNoiseIdx = maxNoiseIdx / fftLength * maxFrequency;
459456

460-
var fftData = {
457+
const fftData = {
461458
fieldIndex : this._dataBuffer.fieldIndex,
462459
fieldName : this._dataBuffer.fieldName,
463460
fftLength : fftLength,

0 commit comments

Comments
 (0)