mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
89 lines
2.5 KiB
Rust
89 lines
2.5 KiB
Rust
use core::f64;
|
|
use std::collections::VecDeque;
|
|
|
|
use egui_plot::{PlotPoint, PlotPoints};
|
|
|
|
use bioz_icd_rs::MeasurementPointSet;
|
|
|
|
pub struct TimeSeriesPlot {
|
|
pub values: VecDeque<PlotPoint>,
|
|
max_points: usize,
|
|
}
|
|
|
|
impl TimeSeriesPlot {
|
|
pub fn new() -> Self {
|
|
let max_points = 500;
|
|
Self {
|
|
values: (-(max_points as i32)..0)
|
|
.map(|i| PlotPoint::new(i, f64::NAN))
|
|
.collect(), // Create x amount of (0,0) points
|
|
max_points,
|
|
}
|
|
}
|
|
|
|
pub fn add(&mut self, val: f64) {
|
|
let last_x = self.values.back().unwrap().x;
|
|
|
|
if last_x >= 0.0 {
|
|
self.values.pop_front();
|
|
}
|
|
|
|
self.values.push_back(PlotPoint::new(last_x + 1.0, val));
|
|
}
|
|
|
|
pub fn plot_values(&self) -> PlotPoints {
|
|
PlotPoints::Owned(Vec::from_iter(self.values.iter().copied()))
|
|
}
|
|
|
|
pub fn plot_values_negative(&self) -> PlotPoints {
|
|
let mut values = Vec::from_iter(self.values.iter().copied());
|
|
for point in &mut values {
|
|
point.y = -point.y;
|
|
}
|
|
PlotPoints::Owned(Vec::from_iter(values))
|
|
}
|
|
|
|
pub fn clear(&mut self) {
|
|
self.values = (-(self.max_points as i32)..0)
|
|
.map(|i| PlotPoint::new(i, f64::NAN))
|
|
.collect();
|
|
}
|
|
}
|
|
|
|
pub struct BodePlot {
|
|
pub magnitudes: Vec<PlotPoint>,
|
|
pub phases: Vec<PlotPoint>,
|
|
}
|
|
|
|
impl BodePlot {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
magnitudes: Vec::new(),
|
|
phases: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn update_magnitudes(&mut self, points: MeasurementPointSet, magnitudes: Vec<f32>) {
|
|
let freqs = points.values().to_vec();
|
|
// self.magnitudes = freqs.into_iter().zip(magnitudes.into_iter()).map(|(f, m)| PlotPoint::new(f.log10(), 20.0 * m.log10() as f32)).collect();
|
|
self.magnitudes = freqs.into_iter().zip(magnitudes.into_iter()).map(|(f, m)| PlotPoint::new(f.log10(), m as f32)).collect();
|
|
}
|
|
|
|
pub fn update_phases(&mut self, points: MeasurementPointSet, phases: Vec<f32>) {
|
|
let freqs = points.values().to_vec();
|
|
self.phases = freqs.into_iter().zip(phases.into_iter()).map(|(f, p)| PlotPoint::new(f.log10(), p as f64)).collect();
|
|
}
|
|
|
|
pub fn plot_magnitudes(&self) -> PlotPoints {
|
|
PlotPoints::Owned(self.magnitudes.clone())
|
|
}
|
|
|
|
pub fn plot_phases(&self) -> PlotPoints {
|
|
PlotPoints::Owned(self.phases.clone())
|
|
}
|
|
|
|
pub fn clear(&mut self) {
|
|
self.magnitudes.clear();
|
|
self.phases.clear();
|
|
}
|
|
} |