Skip to content

Commit f208873

Browse files
committed
Resolved issue of magnitude computing at the spectrum charts by throttle and rpm
1 parent 4044877 commit f208873

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/graph_spectrum_calc.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infi
118118

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

123124
const numberSamples = new Uint32Array(NUM_VS_BINS); // Number of samples in each vs value, used to average them later.
124125

@@ -135,12 +136,15 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infi
135136

136137
fft.simple(fftOutput, fftInput, 'real');
137138

138-
fftOutput = fftOutput.slice(0, fftChunkLength);
139139

140-
// Use only abs values
141-
for (let i = 0; i < fftChunkLength; i++) {
142-
fftOutput[i] = Math.abs(fftOutput[i]);
143-
maxNoise = Math.max(fftOutput[i], maxNoise);
140+
const magnitudes = new Float64Array(magnitudeLength);
141+
142+
// Compute magnitude
143+
for (let i = 0; i < magnitudeLength; i++) {
144+
const re = fftOutput[2 * i],
145+
im = fftOutput[2 * i + 1];
146+
magnitudes[i] = Math.hypot(re, im);
147+
maxNoise = Math.max(magnitudes[i], maxNoise);
144148
}
145149

146150
// calculate a bin index and put the fft value in that bin for each field (e.g. eRPM[0], eRPM[1]..) sepparately
@@ -158,8 +162,8 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infi
158162
numberSamples[vsBinIndex]++;
159163

160164
// add the output from the fft to the row given by the vs value bin index
161-
for (let i = 0; i < fftOutput.length; i++) {
162-
matrixFftOutput[vsBinIndex][i] += fftOutput[i];
165+
for (let i = 0; i < magnitudeLength; i++) {
166+
matrixFftOutput[vsBinIndex][i] += magnitudes[i];
163167
}
164168
}
165169
}
@@ -180,7 +184,7 @@ GraphSpectrumCalc._dataLoadFrequencyVsX = function(vsFieldNames, minValue = Infi
180184
const fftData = {
181185
fieldIndex : this._dataBuffer.fieldIndex,
182186
fieldName : this._dataBuffer.fieldName,
183-
fftLength : fftChunkLength,
187+
fftLength : magnitudeLength,
184188
fftOutput : matrixFftOutput,
185189
maxNoise : maxNoise,
186190
blackBoxRate : this._blackBoxRate,

0 commit comments

Comments
 (0)