Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Oct 10, 2023
1 parent 222b58d commit 2c240c9
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 79 deletions.
112 changes: 76 additions & 36 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use anyhow::Result;
use clap::Parser;
use eframe::egui::plot::{Legend, Line, Plot, PlotPoints};
use eframe::egui::{self, ComboBox, Slider};
use eframe::egui::{self, ComboBox, ProgressBar, Slider};
use stabilizer_streaming::{AvgOpts, MergeOpts};
use std::sync::mpsc;
use std::time::Duration;

Expand Down Expand Up @@ -37,19 +38,19 @@ pub struct Opts {
pub struct AcqOpts {
/// Exclude PSD stages with less than or equal this averaging level
#[arg(short, long, default_value_t = 1)]
min_avg: usize,
min_avg: u32,

/// Segment detrending method
#[arg(short, long, default_value = "mid")]
#[arg(short, long, default_value = "midpoint")]
detrend: Detrend,

/// Sample rate in Hertz
#[arg(short, long, default_value_t = 1.0f32)]
fs: f32,

/// Exponential averaging
#[arg(short, long, default_value_t = 100000)]
max_avg: usize,
/// Boxcar/Exponential averaging count
#[arg(short, long, default_value_t = 1000)]
max_avg: u32,

/// Enable for constant time averaging across stages
/// Disable for constant count averaging across stages
Expand Down Expand Up @@ -78,7 +79,10 @@ fn main() -> Result<()> {
let mut dec = PsdCascade::<{ 1 << 9 }>::default();
dec.set_stage_depth(3);
dec.set_detrend(acq.detrend);
dec.set_avg(acq.scale_avg, acq.max_avg);
dec.set_avg(AvgOpts {
scale: acq.scale_avg,
count: acq.max_avg,
});
dec
}));
}
Expand All @@ -98,16 +102,23 @@ fn main() -> Result<()> {
Err(mpsc::TryRecvError::Empty) => {}
Ok(Cmd::Send(opts)) => {
acq = opts;
let merge_opts = MergeOpts {
remove_overlap: acq.min_avg > 0,
min_count: acq.min_avg,
};
for dec in dec.iter_mut() {
dec.set_detrend(acq.detrend);
dec.set_avg(acq.scale_avg, acq.max_avg);
dec.set_avg(AvgOpts {
scale: acq.scale_avg,
count: acq.max_avg,
});
}
let logfs = acq.fs.log10();
let trace = dec
.iter()
.map(|dec| {
let (p, b) = dec.psd(acq.min_avg);
let f = Break::frequencies(&b, acq.min_avg > 0);
let (p, b) = dec.psd(&merge_opts);
let f = Break::frequencies(&b, &merge_opts);
let (mut p0, mut f0) = (0.0, 0.0);
Trace {
breaks: b,
Expand Down Expand Up @@ -156,11 +167,11 @@ fn main() -> Result<()> {
..Default::default()
};
eframe::run_native(
"FLS",
"PSD",
options,
Box::new(move |_cc| {
// cc.egui_ctx.set_visuals(egui::Visuals::light());
Box::new(FLS::new(trace_recv, cmd_send, acq))
Box::new(App::new(trace_recv, cmd_send, acq))
}),
)
.unwrap();
Expand All @@ -170,15 +181,15 @@ fn main() -> Result<()> {
Ok(())
}

pub struct FLS {
pub struct App {
trace_recv: mpsc::Receiver<Vec<Trace>>,
cmd_send: mpsc::Sender<Cmd>,
current: Vec<Trace>,
acq: AcqOpts,
repaint: f32,
}

impl FLS {
impl App {
fn new(
trace_recv: mpsc::Receiver<Vec<Trace>>,
cmd_send: mpsc::Sender<Cmd>,
Expand All @@ -194,35 +205,36 @@ impl FLS {
}
}

impl eframe::App for FLS {
impl eframe::App for App {
fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) {
self.cmd_send.send(Cmd::Exit).unwrap();
}

fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
match self.trace_recv.try_recv() {
Err(mpsc::TryRecvError::Empty) => {}
Ok(new) => {
self.current = new;
ctx.request_repaint_after(Duration::from_secs_f32(self.repaint))
}
Err(mpsc::TryRecvError::Disconnected) => panic!("lost data processing thread"),
};
match self.trace_recv.try_recv() {
Err(mpsc::TryRecvError::Empty) => {}
Ok(new) => {
self.current = new;
ctx.request_repaint_after(Duration::from_secs_f32(self.repaint))
}
Err(mpsc::TryRecvError::Disconnected) => panic!("lost data processing thread"),
};

egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
ui.add(
Slider::new(&mut self.repaint, 0.01..=10.0)
.text("Repaint")
.suffix(" s")
.logarithmic(true),
).on_hover_text("Request repaint after timeout (seconds)");
)
.on_hover_text("Request repaint after timeout (seconds)");
ui.separator();
ComboBox::from_label("Detrend")
.selected_text(format!("{:?}", self.acq.detrend))
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.acq.detrend, Detrend::None, "None");
ui.selectable_value(&mut self.acq.detrend, Detrend::Mid, "Mid");
ui.selectable_value(&mut self.acq.detrend, Detrend::Midpoint, "Midpoint");
ui.selectable_value(&mut self.acq.detrend, Detrend::Span, "Span");
ui.selectable_value(&mut self.acq.detrend, Detrend::Mean, "Mean");
// ui.selectable_value(&mut self.acq.detrend, Detrend::Linear, "Linear");
Expand All @@ -234,15 +246,20 @@ impl eframe::App for FLS {
Slider::new(&mut self.acq.max_avg, 1..=1_000_000)
.text("Averages")
.logarithmic(true),
).on_hover_text("Averaging count: when below, the averaging is boxcar, when above it continues with exponential averaging");
)
.on_hover_text(
"Averaging count: the averaging starts as boxcar, then continues exponential",
);
ui.separator();
ui.checkbox(&mut self.acq.scale_avg, "Constant time avg").on_hover_text("Scale stage averaging count by stage dependent sample rate");
ui.checkbox(&mut self.acq.scale_avg, "Scale averages")
.on_hover_text("Scale stage averaging count by stage dependent sample rate");
ui.separator();
ui.add(
Slider::new(&mut self.acq.min_avg, 0..=self.acq.max_avg)
.text("Min Count")
.text("Min averages")
.logarithmic(true),
).on_hover_text("Minimum averaging count to show data from a stage");
)
.on_hover_text("Minimum averaging count to show data from a stage");
});
ui.horizontal(|ui| {
ui.add(
Expand All @@ -254,12 +271,34 @@ impl eframe::App for FLS {
)
.on_hover_text("Input sample rate");
ui.separator();
ui.checkbox(&mut self.acq.integrate, "Integrate").on_hover_text("Integrate PSD into cumulative sum");
ui.separator();
self.current
.first()
.and_then(|t| t.breaks.first())
.map(|bi| ui.label(format!("{:.2e}", bi.count as f32)).on_hover_text("Top level average count"));
ui.checkbox(&mut self.acq.integrate, "Integrate")
.on_hover_text("Integrate PSD into linear cumulative sum");
if let Some(t) = self.current.first() {
if let Some(bi) = t.breaks.first() {
ui.separator();
ui.add(
ProgressBar::new(bi.count as f32 / bi.avg as f32)
.desired_width(50.0)
.show_percentage(),
)
.on_hover_text("Top averaging fill");
}
if let Some(bi) = t.breaks.last() {
ui.separator();
ui.label(format!(
"{:.2e}",
bi.processed as f32 * (1u64 << bi.decimation) as f32
))
.on_hover_text("Bottom effective number of input samples processed");
ui.separator();
ui.add(
ProgressBar::new(bi.pending as f32 / bi.fft_size as f32)
.desired_width(50.0)
.show_percentage(),
)
.on_hover_text("Bottom buffer fill (incl overlap)");
}
}
ui.separator();
if ui
.button("Reset")
Expand All @@ -284,6 +323,7 @@ impl eframe::App for FLS {
}
});
});

self.cmd_send.send(Cmd::Send(self.acq)).unwrap();
}
}
14 changes: 9 additions & 5 deletions src/bin/stream_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use clap::Parser;
use stabilizer_streaming::{
source::{Source, SourceOpts},
Break, Detrend, PsdCascade, VarBuilder,
Break, Detrend, MergeOpts, PsdCascade, VarBuilder,
};
use std::sync::mpsc;
use std::time::Duration;
Expand All @@ -28,6 +28,10 @@ fn main() -> Result<()> {
duration,
trace,
} = Opts::parse();
let merge_opts = MergeOpts {
remove_overlap: true,
min_count: 1,
};

let (cmd_send, cmd_recv) = mpsc::channel();
let receiver = std::thread::spawn(move || {
Expand All @@ -37,7 +41,7 @@ fn main() -> Result<()> {
.map(|_| {
let mut c = PsdCascade::<{ 1 << 9 }>::default();
c.set_stage_depth(3);
c.set_detrend(Detrend::Mid);
c.set_detrend(Detrend::Midpoint);
c
})
.collect();
Expand All @@ -53,16 +57,16 @@ fn main() -> Result<()> {
};
}

let (y, b) = dec[trace].psd(1);
let (y, b) = dec[trace].psd(&merge_opts);
log::info!("breaks: {:?}", b);
log::info!("psd: {:?}", y);

if let Some(b0) = b.last() {
let var = VarBuilder::default().dc_cut(1).clip(1.0).build().unwrap();
let mut fdev = vec![];
let mut tau = 1.0;
let f = Break::frequencies(&b, true);
while tau <= (b0.effective_fft_size / 2) as f32 {
let f = Break::frequencies(&b, &merge_opts);
while tau <= (b0.effective_fft_size() / 2) as f32 {
fdev.push((tau, var.eval(&y, &f, tau).sqrt()));
tau *= 2.0;
}
Expand Down
Loading

0 comments on commit 2c240c9

Please sign in to comment.