Skip to content

Commit

Permalink
add mag
Browse files Browse the repository at this point in the history
  • Loading branch information
clysto committed May 4, 2024
1 parent f00c784 commit 5d54b8b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl App {
const RETURN: u32 = 4;
const PSD: u32 = 5;
const ABOUT: u32 = 6;
const MAG: u32 = 7;

pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
let ctx = &cc.egui_ctx;
Expand Down Expand Up @@ -93,6 +94,7 @@ impl App {
Modifiers::COMMAND,
Key::ArrowUp,
),
MenuItem::single(Self::MAG, "Toogle Magnitude"),
MenuItem::separator(),
MenuItem::single_with_shortcut(Self::PSD, "PSD", Modifiers::COMMAND, Key::P),
],
Expand Down Expand Up @@ -173,6 +175,9 @@ impl App {
&Self::PSD => {
self.psd();
}
&Self::MAG => {
self.signal_plot.toggle_magnitude();
}
_ => {}
}
}
Expand Down
46 changes: 32 additions & 14 deletions src/signal_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct SignalPlot {
sample_rate: u32,
zoom_history: Vec<PlotBounds>,
bounds: PlotBounds,
magnitude_visible: bool,
}

fn auto_ratio(max_points: usize, max_ratio: usize, nsamples: usize) -> usize {
Expand All @@ -43,6 +44,7 @@ impl SignalPlot {
sample_rate: 1,
zoom_history: Vec::new(),
bounds: PlotBounds::from_min_max([0., 0.], [0., 0.]),
magnitude_visible: false,
}
}

Expand Down Expand Up @@ -157,20 +159,32 @@ impl SignalPlot {
let ratio =
auto_ratio(max_samples, signal.max_ratio(), index_end - index_start);
let data = signal.get(index_start..index_end, ratio);
let re = PlotPoints::new(
data.iter()
.enumerate()
.map(|(i, y)| [(index_start + i * ratio) as f64, y.re as f64])
.collect(),
);
let im = PlotPoints::new(
data.iter()
.enumerate()
.map(|(i, y)| [(index_start + i * ratio) as f64, y.im as f64])
.collect(),
);
plot_ui.line(Line::new(re).name("inphase"));
plot_ui.line(Line::new(im).name("quadrature"));
if self.magnitude_visible {
let mag = PlotPoints::new(
data.iter()
.enumerate()
.map(|(i, y)| {
[(index_start + i * ratio) as f64, y.norm() as f64]
})
.collect(),
);
plot_ui.line(Line::new(mag).name("magnitude"));
} else {
let re = PlotPoints::new(
data.iter()
.enumerate()
.map(|(i, y)| [(index_start + i * ratio) as f64, y.re as f64])
.collect(),
);
let im = PlotPoints::new(
data.iter()
.enumerate()
.map(|(i, y)| [(index_start + i * ratio) as f64, y.im as f64])
.collect(),
);
plot_ui.line(Line::new(re).name("inphase"));
plot_ui.line(Line::new(im).name("quadrature"));
}
}
}
});
Expand Down Expand Up @@ -232,4 +246,8 @@ impl SignalPlot {
}
index_end + 1 - index_start
}

pub fn toggle_magnitude(&mut self) {
self.magnitude_visible = !self.magnitude_visible;
}
}

0 comments on commit 5d54b8b

Please sign in to comment.