1use std::io::Write;
2
3pub fn python_plot(xs: Vec<&[f32]>) {
11 let script = format!(
12 r#"
13import matplotlib.pyplot as plt
14import matplotlib.gridspec as gridspec
15from scipy.signal import correlate
16
17xs = {:?}
18fig = plt.figure(figsize=(12, 12))
19gs = gridspec.GridSpec(len(xs), 2)
20for i, x in enumerate(xs):
21 ax = plt.subplot(gs[i, 0])
22 ax.plot(x, label = f"C{{i}}")
23 ax.legend()
24 ax.set_xlabel("Samples")
25 ax = plt.subplot(gs[i, 1])
26 autocorr = correlate(x, x, mode='full')
27 normcorr = autocorr / autocorr.max()
28 offsets = range(-len(x) + 1, len(x))
29 ax.plot(offsets, normcorr, label = f"Autocorrelation of C{{i}}")
30 ax.legend()
31 ax.set_xlabel("Lag")
32plt.show()
33"#,
34 xs
35 );
36 let script = script.as_bytes();
38 let mut python = match std::process::Command::new("python")
39 .stdin(std::process::Stdio::piped())
40 .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::null()) .spawn()
43 {
44 Ok(p) => p,
45 Err(_) => return, };
47
48 if let Some(mut stdin) = python.stdin.take() {
49 if stdin.write_all(script).is_err() {
50 return; }
52 } else {
53 return; }
55
56 let _ = python.wait(); }