Skip to content

Commit 576a7d7

Browse files
demvladhaslinghuis
andauthored
Added spectrum comparison by using import spectrum data from CSV file (#780)
* Added 'Import from CSW' button at the spectrum analizer chart * Added importSpectrumFromCSV method to FlightLogAnalyser * Added drawing imported spectrum data * Added events handler for spectrum import button * missing semicoma issue is resolved * The buttons caption is changed Co-authored-by: Mark Haslinghuis <[email protected]> * the imported spectrum curves color is changed to blue --------- Co-authored-by: Mark Haslinghuis <[email protected]>
1 parent 3269938 commit 576a7d7

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ <h4>Workspace</h4>
475475
<div id="spectrumExport" data-toggle="tooltip" title="Export spectrum to CSV">
476476
<button id="btn-spectrum-export" type="button">Export to CSV</button>
477477
</div>
478+
<div id="spectrumImport" data-toggle="tooltip" title="Import spectrum from CSV">
479+
<button type="button" onclick="document.getElementById('btn-spectrum-import').click()">Import from CSV</button>
480+
<input type="file" id="btn-spectrum-import" accept=".csv" style="display:none" multiple/>
481+
</div>
478482
<div id="analyserResize" class="btn-nobg view-analyser-fullscreen" data-toggle="tooltip" title="Zoom Analyser Window">
479483
<span class="glyphicon glyphicon-resize-full"></span>
480484
<span class="glyphicon glyphicon-resize-small"></span>

src/css/main.css

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,9 @@ html.has-analyser-fullscreen.has-analyser
637637

638638
.analyser #spectrumExport {
639639
height: 0;
640-
width: 200px;
641640
overflow: hidden;
642641
opacity: 0;
643-
left: 270px;
642+
left: 260px;
644643
float: left;
645644
z-index: 9;
646645
position: absolute;
@@ -653,6 +652,29 @@ html.has-analyser-fullscreen.has-analyser
653652
color: black;
654653
}
655654

655+
.analyser:hover .non-shift #spectrumImport {
656+
opacity: 1;
657+
height: auto;
658+
transition: opacity 500ms ease-in;
659+
}
660+
661+
.analyser #spectrumImport {
662+
height: 0;
663+
width: 55px;
664+
overflow: hidden;
665+
opacity: 0;
666+
left: 310px;
667+
float: left;
668+
z-index: 9;
669+
position: absolute;
670+
font-size: 9px;
671+
}
672+
673+
.analyser #spectrumImport select {
674+
border-radius: 3px;
675+
padding: 0px 5px;
676+
color: black;
677+
}
656678
.analyser input#analyserZoomX {
657679
width: 100px;
658680
height: 10px;

src/graph_spectrum.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,35 @@ export function FlightLogAnalyser(flightLog, canvas, analyserCanvas) {
289289
this.exportSpectrumToCSV = function(onSuccess, options) {
290290
SpectrumExporter(fftData, options).dump(onSuccess);
291291
};
292+
this.importSpectrumFromCSV = function(files) {
293+
const reader = new FileReader();
294+
reader.onload = function (e) {
295+
try {
296+
const stringRows = e.target.result.split("\n");
297+
298+
const header = stringRows[0].split(",");
299+
if (header.length != 2 || header[0] != "freq" || header[1] != "value") {
300+
throw new SyntaxError("Wrong spectrum CSV data format");
301+
}
302+
303+
stringRows.shift();
304+
const spectrumData = stringRows.map( function(row) {
305+
const data = row.split(",");
306+
return {
307+
freq: parseFloat(data[0]),
308+
value: parseFloat(data[1]),
309+
};
310+
});
311+
GraphSpectrumPlot.setImportedSpectrumData(spectrumData);
312+
} catch (e) {
313+
alert('Spectrum data import error: ' + e.message);
314+
return;
315+
}
316+
};
317+
reader.readAsText(files[0]);
318+
};
319+
292320
} catch (e) {
293-
console.log(`Failed to create analyser... error:${e}`);
321+
console.log(`Failed to create analyser... error: ${e}`);
294322
}
295323
}

src/graph_spectrum_plot.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ GraphSpectrumPlot.initialize = function (canvas, sysConfig) {
5757
this._logRateWarning = undefined;
5858
this._zoomX = 1;
5959
this._zoomY = 1;
60+
this._importedSpectrumData = null;
6061
};
6162

6263
GraphSpectrumPlot.setZoom = function (zoomX, zoomY) {
@@ -89,6 +90,12 @@ GraphSpectrumPlot.setData = function (fftData, spectrumType) {
8990
this._invalidateDataCache();
9091
};
9192

93+
GraphSpectrumPlot.setImportedSpectrumData = function (curvesData) {
94+
this._importedSpectrumData = curvesData;
95+
this._invalidateCache();
96+
this._invalidateDataCache();
97+
GraphSpectrumPlot.draw();
98+
};
9299
GraphSpectrumPlot.setOverdraw = function (overdrawType) {
93100
this._overdrawType = overdrawType;
94101
this._invalidateCache();
@@ -187,7 +194,7 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
187194
"rgba(255,128,128,1.0)"
188195
);
189196

190-
canvasCtx.fillStyle = barGradient; //'rgba(0,255,0,0.3)'; //green
197+
canvasCtx.fillStyle = barGradient;
191198

192199
const fftScale = HEIGHT / (this._zoomY * 100);
193200
for (let i = 0; i < PLOTTED_BUFFER_LENGTH; i += 10) {
@@ -196,6 +203,41 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
196203
x += barWidth + 1;
197204
}
198205

206+
207+
//Draw imported spectrum
208+
if (this._importedSpectrumData) {
209+
const curvesPonts = this._importedSpectrumData;
210+
const pointsCount = curvesPonts.length;
211+
const scaleX = 2 * WIDTH / PLOTTED_BLACKBOX_RATE * this._zoomX;
212+
213+
canvasCtx.beginPath();
214+
canvasCtx.strokeStyle = "blue";
215+
canvasCtx.moveTo(0, HEIGHT);
216+
const filterPointsCount = 20;
217+
for (let i = 0; i < pointsCount; i++) {
218+
// Apply moving average filter at spectrum points to get visible line
219+
let filterStartPoint = i - filterPointsCount / 2;
220+
if (filterStartPoint < 0) {
221+
filterStartPoint = 0;
222+
}
223+
let filterStopPoint = filterStartPoint + filterPointsCount;
224+
if (filterStopPoint > pointsCount) {
225+
filterStopPoint = pointsCount;
226+
filterStartPoint = filterStopPoint - filterPointsCount;
227+
if (filterStartPoint < 0) {
228+
filterStartPoint = 0;
229+
}
230+
}
231+
let middleValue = 0;
232+
for (let j = filterStartPoint; j < filterStopPoint; j++) {
233+
middleValue += curvesPonts[j].value;
234+
}
235+
middleValue /= filterPointsCount;
236+
237+
canvasCtx.lineTo(curvesPonts[i].freq * scaleX, HEIGHT - middleValue * fftScale);
238+
}
239+
canvasCtx.stroke();
240+
}
199241
this._drawAxisLabel(
200242
canvasCtx,
201243
this._fftData.fieldName,

src/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,11 @@ function BlackboxLogViewer() {
17361736
e.preventDefault();
17371737
});
17381738

1739+
$("#btn-spectrum-import").change(function (e) {
1740+
graph.getAnalyser().importSpectrumFromCSV(e.target.files);
1741+
e.preventDefault();
1742+
e.target.value = "";
1743+
});
17391744
$(".btn-gpx-export").click(function (e) {
17401745
setGraphState(GRAPH_STATE_PAUSED);
17411746
exportGpx();

0 commit comments

Comments
 (0)