1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Example: Sine and cosine live traces
//!
//! What it demonstrates
//! - Streaming two traces concurrently into the multi-trace UI.
//! - Visual comparison of phase between two signals.
//!
//! How to run
//! ```bash
//! cargo run --example sine_cosine
//! ```
//! The UI shows two traces (`sine` and `cosine`) updated at 1 kHz.
use liveplot::{channel_plot, run_liveplot, LivePlotConfig, PlotPoint};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
fn main() -> eframe::Result<()> {
// Create multi-trace plot channel
let (sink, rx) = channel_plot();
let tr_sine = sink.create_trace("sine", None);
let tr_cos = sink.create_trace("cosine", None);
// Producer: 1 kHz sample rate, 3 Hz sine and cosine
std::thread::spawn(move || {
const FS_HZ: f64 = 1000.0; // 1 kHz sampling rate
const F_HZ: f64 = 3.0; // 3 Hz
let dt = Duration::from_millis(1);
let mut n: u64 = 0;
loop {
let t = n as f64 / FS_HZ;
let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
let t_s = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0);
// Ignore error if the UI closed (receiver dropped)
let _ = sink.send_point(&tr_sine, PlotPoint { x: t_s, y: s_val });
let _ = sink.send_point(&tr_cos, PlotPoint { x: t_s, y: c_val });
n = n.wrapping_add(1);
std::thread::sleep(dt);
}
});
// Run the UI until closed (default: FFT hidden). Uses the unified multi-trace engine.
run_liveplot(rx, LivePlotConfig::default())
}