Skip to content

Commit dc19b37

Browse files
committed
Added the maximal noise frequency label
1 parent b3bcdc3 commit dc19b37

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/graph_spectrum_calc.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,17 @@ GraphSpectrumCalc.dataLoadPSD = function(analyserZoomY) {
119119
pointsPerSegment = Math.min(pointsPerSegment, flightSamples.samples.length);
120120
const overlapCount = pointsPerSegment / 2;
121121

122-
const psd = this._psd(flightSamples.samples, this._blackBoxRate, pointsPerSegment, overlapCount);
123-
let min = 1e6,
124-
max = -1e6;
125-
for (const value of psd) {
126-
min = Math.min(value, min);
127-
max = Math.max(value, max);
128-
}
122+
const psd = this._psd(flightSamples.samples, pointsPerSegment, overlapCount);
129123

130124
const psdData = {
131125
fieldIndex : this._dataBuffer.fieldIndex,
132126
fieldName : this._dataBuffer.fieldName,
133-
psdLength : psd.length,
134-
psdOutput : psd,
127+
psdLength : psd.psdOutput.length,
128+
psdOutput : psd.psdOutput,
135129
blackBoxRate : this._blackBoxRate,
136-
minimum: min,
137-
maximum: max,
130+
minimum: psd.min,
131+
maximum: psd.max,
132+
maxNoiseIdx: psd.maxNoiseIdx,
138133
};
139134
return psdData;
140135
};
@@ -525,7 +520,7 @@ GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {
525520
/**
526521
* Compute PSD for data samples by Welch method follow Python code
527522
*/
528-
GraphSpectrumCalc._psd = function(samples, samplesRate, pointsPerSegment, overlapCount, scaling = 'density') {
523+
GraphSpectrumCalc._psd = function(samples, pointsPerSegment, overlapCount, scaling = 'density') {
529524
// Compute FFT for samples segments
530525
const fftOutput = this._fft_segmented(samples, pointsPerSegment, overlapCount);
531526

@@ -543,7 +538,7 @@ GraphSpectrumCalc._psd = function(samples, samplesRate, pointsPerSegment, overl
543538
for (const value of window) {
544539
skSum += value ** 2;
545540
}
546-
scale = 1 / (samplesRate * skSum);
541+
scale = 1 / (this._blackBoxRate * skSum);
547542
} else if (scaling == 'spectrum') {
548543
let sum = 0;
549544
for (const value of window) {
@@ -558,6 +553,12 @@ GraphSpectrumCalc._psd = function(samples, samplesRate, pointsPerSegment, overl
558553
}
559554

560555
// Compute average for scaled power
556+
let min = 1e6,
557+
max = -1e6;
558+
const maxFrequency = (this._blackBoxRate / 2.0);
559+
const noiseLowEndIdx = 100 / maxFrequency * dataCount;
560+
let maxNoiseIdx = 0;
561+
let maxNoise = 0;
561562
for (let i = 0; i < dataCount; i++) {
562563
psdOutput[i] = 0.0;
563564
for (let j = 0; j < segmentsCount; j++) {
@@ -572,9 +573,22 @@ GraphSpectrumCalc._psd = function(samples, samplesRate, pointsPerSegment, overl
572573
let avg = psdOutput[i] / segmentsCount;
573574
avg = Math.max(avg, min_avg);
574575
psdOutput[i] = 10 * Math.log10(avg);
576+
min = Math.min(psdOutput[i], min);
577+
max = Math.max(psdOutput[i], max);
578+
if (i > noiseLowEndIdx && psdOutput[i] > maxNoise) {
579+
maxNoise = psdOutput[i];
580+
maxNoiseIdx = i;
581+
}
575582
}
576583

577-
return psdOutput;
584+
const maxNoiseFrequency = maxNoiseIdx / dataCount * maxFrequency;
585+
586+
return {
587+
psdOutput: psdOutput,
588+
min: min,
589+
max: max,
590+
maxNoiseIdx: maxNoiseFrequency,
591+
};
578592
};
579593

580594

src/graph_spectrum_plot.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ GraphSpectrumPlot._drawPowerSpectralDensityGraph = function (canvasCtx) {
374374
"db/Hz",
375375
ticksCount,
376376
);
377+
this._drawInterestFrequency(
378+
canvasCtx,
379+
this._fftData.maxNoiseIdx,
380+
PLOTTED_BLACKBOX_RATE,
381+
"Max motor noise",
382+
WIDTH,
383+
HEIGHT,
384+
15 * 5 + MARGIN,
385+
"rgba(255,0,0,0.50)",
386+
3,
387+
);
377388

378389
canvasCtx.restore();
379390
};

0 commit comments

Comments
 (0)