Skip to content

Revert "Added power spectral density curves at the spectrum chart" #824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ <h4>Workspace</h4>
<option value="1">Freq. vs Throttle</option>
<option value="3">Freq. vs RPM</option>
<option value="2">Error vs Setpoint</option>
<option value="4">Power spectral density</option>
</select>
</div>

Expand Down
9 changes: 0 additions & 9 deletions src/graph_spectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
fftData = GraphSpectrumCalc.dataLoadPidErrorVsSetpoint();
break;

case SPECTRUM_TYPE.POWER_SPECTRAL_DENSITY:
fftData = GraphSpectrumCalc.dataLoadPSD(analyserZoomY);
break;

case SPECTRUM_TYPE.FREQUENCY:
default:
fftData = GraphSpectrumCalc.dataLoadFrequency();
Expand Down Expand Up @@ -191,11 +187,6 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
debounce(100, function () {
analyserZoomY = 1 / (analyserZoomYElem.val() / 100);
GraphSpectrumPlot.setZoom(analyserZoomX, analyserZoomY);
// Recalculate PSD with updated samples per segment count
if (userSettings.spectrumType == SPECTRUM_TYPE.POWER_SPECTRAL_DENSITY) {
dataLoad();
GraphSpectrumPlot.setData(fftData, userSettings.spectrumType);
}
that.refresh();
})
)
Expand Down
148 changes: 2 additions & 146 deletions src/graph_spectrum_calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,6 @@ GraphSpectrumCalc.dataLoadFrequency = function() {
return fftData;
};

GraphSpectrumCalc.dataLoadPSD = function(analyserZoomY) {
const flightSamples = this._getFlightSamplesFreq(false);

let pointsPerSegment = 512;
const multiplier = Math.floor(1 / analyserZoomY); // 0. ... 10
if (multipiler == 0) {
pointsPerSegment = 256;
} else if (multiplier > 1) {
pointsPerSegment *= 2 ** Math.floor(multiplier / 2);
}
pointsPerSegment = Math.min(pointsPerSegment, flightSamples.samples.length);
const overlapCount = Math.floor(pointsPerSegment / 2);

const psd = this._psd(flightSamples.samples, pointsPerSegment, overlapCount);

const psdData = {
fieldIndex : this._dataBuffer.fieldIndex,
fieldName : this._dataBuffer.fieldName,
psdLength : psd.psdOutput.length,
psdOutput : psd.psdOutput,
blackBoxRate : this._blackBoxRate,
minimum: psd.min,
maximum: psd.max,
maxNoiseIdx: psd.maxNoiseIdx,
};
return psdData;
};

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

Expand Down Expand Up @@ -310,7 +283,7 @@ GraphSpectrumCalc._getFlightChunks = function() {
return allChunks;
};

GraphSpectrumCalc._getFlightSamplesFreq = function(scaled = true) {
GraphSpectrumCalc._getFlightSamplesFreq = function() {

const allChunks = this._getFlightChunks();

Expand All @@ -320,11 +293,7 @@ GraphSpectrumCalc._getFlightSamplesFreq = function(scaled = true) {
let samplesCount = 0;
for (const chunk of allChunks) {
for (const frame of chunk.frames) {
if (scaled) {
samples[samplesCount] = this._dataBuffer.curve.lookupRaw(frame[this._dataBuffer.fieldIndex]);
} else {
samples[samplesCount] = frame[this._dataBuffer.fieldIndex];
}
samples[samplesCount] = (this._dataBuffer.curve.lookupRaw(frame[this._dataBuffer.fieldIndex]));
samplesCount++;
}
}
Expand Down Expand Up @@ -516,116 +485,3 @@ GraphSpectrumCalc._normalizeFft = function(fftOutput, fftLength) {

return fftData;
};

/**
* Compute PSD for data samples by Welch method follow Python code
*/
GraphSpectrumCalc._psd = function(samples, pointsPerSegment, overlapCount, scaling = 'density') {
// Compute FFT for samples segments
const fftOutput = this._fft_segmented(samples, pointsPerSegment, overlapCount);

const dataCount = fftOutput[0].length;
const segmentsCount = fftOutput.length;
const psdOutput = new Float64Array(dataCount);

// Compute power scale coef
let scale = 1;
if (userSettings.analyserHanning) {
const window = Array(pointsPerSegment).fill(1);
this._hanningWindow(window, pointsPerSegment);
if (scaling == 'density') {
let skSum = 0;
for (const value of window) {
skSum += value ** 2;
}
scale = 1 / (this._blackBoxRate * skSum);
} else if (scaling == 'spectrum') {
let sum = 0;
for (const value of window) {
sum += value;
}
scale = 1 / sum ** 2;
}
} else if (scaling == 'density') {
scale = 1 / pointsPerSegment;
} else if (scaling == 'spectrum') {
scale = 1 / pointsPerSegment ** 2;
}

// Compute average for scaled power
let min = 1e6,
max = -1e6;
// Early exit if no segments were processed
if (segmentsCount === 0) {
return {
psdOutput: new Float64Array(0),
min: 0,
max: 0,
maxNoiseIdx: 0
};
}
const maxFrequency = (this._blackBoxRate / 2.0);
const noise50HzIdx = 50 / maxFrequency * dataCount;
const noise3HzIdx = 3 / maxFrequency * dataCount;
let maxNoiseIdx = 0;
let maxNoise = -100;
for (let i = 0; i < dataCount; i++) {
psdOutput[i] = 0.0;
for (let j = 0; j < segmentsCount; j++) {
let p = scale * fftOutput[j][i] ** 2;
if (i != dataCount - 1) {
p *= 2;
}
psdOutput[i] += p;
}

const min_avg = 1e-7; // limit min value for -70db
let avg = psdOutput[i] / segmentsCount;
avg = Math.max(avg, min_avg);
psdOutput[i] = 10 * Math.log10(avg);
if (i > noise3HzIdx) { // Miss big zero freq magnitude
min = Math.min(psdOutput[i], min);
max = Math.max(psdOutput[i], max);
}
if (i > noise50HzIdx && psdOutput[i] > maxNoise) {
maxNoise = psdOutput[i];
maxNoiseIdx = i;
}
}

const maxNoiseFrequency = maxNoiseIdx / dataCount * maxFrequency;

return {
psdOutput: psdOutput,
min: min,
max: max,
maxNoiseIdx: maxNoiseFrequency,
};
};


/**
* Compute FFT for samples segments by lenghts as n_per_seg with n_overlap overlap points count
*/
GraphSpectrumCalc._fft_segmented = function(samples, n_per_seg, n_overlap) {
const samplesCount = samples.length;
let output = [];
for (let i = 0; i < samplesCount - n_per_seg; i += n_per_seg - n_overlap) {
const fftInput = samples.slice(i, i + n_per_seg);

if (userSettings.analyserHanning) {
this._hanningWindow(fftInput, n_per_seg);
}

const fftComplex = this._fft(fftInput);
const magnitudes = new Float64Array(n_per_seg / 2);
for (let i = 0; i < n_per_seg / 2; i++) {
const re = fftComplex[2 * i];
const im = fftComplex[2 * i + 1];
magnitudes[i] = Math.hypot(re, im);
}
output.push(magnitudes);
}

return output;
};
Loading
Loading