Skip to content

Commit e87e436

Browse files
author
krzywonos
committed
Revert "Reassigning local variables to prevent fft duplicate configuration" Subtracting mean from the signal
1 parent 1a14910 commit e87e436

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src-tauri/src/main.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,11 @@ fn rpc_controls(args: &[String], column_names: Vec<String>) -> Vec<(String, bool
272272

273273
fn calc_fft(signals: Option<&Vec<f32>>, fs: f32 ) -> (Vec<f32>, Vec<f32>) {
274274
if let Some(signal) = signals{
275+
let mean = signal.iter().sum::<f32>() / signal.len() as f32;
276+
let mean_adjusted_signal: Vec<f32> = signal.iter().map(|x| x - mean).collect();
277+
275278
let welch: SpectralDensity<f32> =
276-
SpectralDensity::<f32>::builder(signal, fs).build();
279+
SpectralDensity::<f32>::builder(&mean_adjusted_signal, fs).build();
277280

278281
let result = std::panic::catch_unwind(|| {welch.periodogram();});
279282
if result.is_err(){
@@ -473,15 +476,16 @@ fn fft_data(window: Window) {
473476
if let Some(columns) = sorted_columns.get(name){
474477
if spectrum_data.len() == columns.len() + 1{
475478
let averaged_spec_data: Vec<Vec<f32>> = spectrum_data
476-
.iter()
477-
.map(|col| {
478-
let chunk_size = (col.len() as f32/ (fs as f32/20.0)).ceil() as usize;
479-
col.chunks(chunk_size)
480-
.map(|chunk| chunk.iter().sum::<f32>() /chunk.len() as f32)
481-
.collect()
482-
})
483-
.collect();
484-
let _ = window.emit(&name.clone(), averaged_spec_data);
479+
.iter()
480+
.map(|col| {
481+
let chunk_size = (col.len() as f32/ (fs as f32/20.0)).ceil() as usize;
482+
col.chunks(chunk_size)
483+
.map(|chunk| chunk.iter().sum::<f32>() /chunk.len() as f32)
484+
.collect()
485+
})
486+
.collect();
487+
488+
let _ = window.emit(&name.clone(), spectrum_data);
485489
}
486490
}
487491
}

src/main.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ window.onload = () => {
315315
});
316316
};
317317

318-
var seriesConfig = [{label: "Frequency (Hz)"}];
319-
let gotSeries = false;
320318
function createFFT(eventName, containerId, labels) {
321319
const template = document.getElementById('fft-template');
322320
const clone = template.content.cloneNode(true);
@@ -349,9 +347,9 @@ function createFFT(eventName, containerId, labels) {
349347
}
350348
}, 100);
351349
}).then(() => {
352-
for (let i = 0; i < spectrum.length; i++) {
350+
/*for (let i = 0; i < spectrum.length; i++) {
353351
fftPlot.data[i] = [];
354-
}
352+
}*/
355353

356354
for (let i = 0; i< spectrum[0].length; i++){
357355
for (let j = 0; j< spectrum.length; j++){
@@ -360,6 +358,12 @@ function createFFT(eventName, containerId, labels) {
360358
}
361359
}
362360
}
361+
362+
while (fftPlot.data[0].length > 500){
363+
for (let i = 0; i < fftPlot.data.length; i++){
364+
fftPlot.data[i].shift();
365+
}
366+
}
363367
fftPlot.setData(fftPlot.data, true);
364368
})
365369

0 commit comments

Comments
 (0)