Skip to main content

autoeq_plot/plot_spin/
plot.rs

1use super::consts::CEA2034_CURVE_NAMES;
2use super::create::create_cea2034_combined_traces;
3use super::create::create_cea2034_traces;
4use super::create::create_cea2034_with_eq_combined_traces;
5use super::create::create_cea2034_with_eq_traces;
6use super::misc::shorten_curve_name;
7use crate::filter_color::filter_color;
8use crate::ref_lines::make_ref_lines;
9use crate::trend_lines::{
10    calculate_tonal_balance, create_regression_trace, generate_regression_line,
11};
12use ndarray::Array1;
13use plotly::common::AxisSide;
14use plotly::layout::{AxisType, GridPattern, LayoutGrid, RowOrder};
15use plotly::{Layout, Plot};
16use std::collections::HashMap;
17
18/// Create a detailed CEA2034 spinorama plot with multiple subplots.
19///
20/// Shows On Axis, Listening Window, Early Reflections, and Sound Power curves,
21/// optionally with EQ-applied variants overlaid.
22pub fn plot_spin_details(
23    cea2034_curves: Option<&HashMap<String, crate::Curve>>,
24    eq_response: Option<&Array1<f64>>,
25) -> plotly::Plot {
26    let mut plot = Plot::new();
27    // Add each CEA2034 curves if provided
28    let x_axis1_title = "On Axis".to_string();
29    let x_axis2_title = "Listening Window".to_string();
30    let x_axis3_title = "Early Reflections".to_string();
31    let x_axis4_title = "Sound Power".to_string();
32    if let Some(curves) = cea2034_curves {
33        let cea2034_traces = create_cea2034_traces(curves);
34        for trace in cea2034_traces {
35            plot.add_trace(Box::new(trace));
36        }
37        // Also plot the EQ-applied variants if provided
38        if let Some(eq_resp) = eq_response {
39            let cea2034_eq_traces = create_cea2034_with_eq_traces(curves, eq_resp);
40            for trace in cea2034_eq_traces {
41                plot.add_trace(Box::new(trace));
42            }
43        }
44    }
45
46    // Add reference lines y=1 and y=-1 from x=100 to x=10000 (black) on both subplots
47    for t in make_ref_lines("x", "y") {
48        plot.add_trace(Box::new(t));
49    }
50    for t in make_ref_lines("x2", "y2") {
51        plot.add_trace(Box::new(t));
52    }
53
54    // Configure layout with subplots
55    let layout = Layout::new()
56        .grid(
57            LayoutGrid::new()
58                .rows(2)
59                .columns(2)
60                .pattern(GridPattern::Independent)
61                .row_order(RowOrder::BottomToTop),
62        )
63        .width(1024)
64        .height(600)
65        .x_axis(
66            plotly::layout::Axis::new()
67                .title(plotly::common::Title::with_text(x_axis1_title))
68                .type_(AxisType::Log)
69                .range(vec![1.301, 4.301])
70                .domain(&[0.0, 0.45]),
71        )
72        .y_axis(
73            plotly::layout::Axis::new()
74                .title(plotly::common::Title::with_text("SPL (dB)"))
75                .range(vec![-10.0, 10.0])
76                .domain(&[0.55, 1.0]),
77        )
78        .x_axis2(
79            plotly::layout::Axis::new()
80                .title(plotly::common::Title::with_text(x_axis2_title))
81                .type_(AxisType::Log)
82                .range(vec![1.301, 4.301])
83                .domain(&[0.55, 1.0]),
84        )
85        .y_axis2(
86            plotly::layout::Axis::new()
87                .title(plotly::common::Title::with_text("SPL (dB)"))
88                .range(vec![-10.0, 10.0])
89                .domain(&[0.55, 1.0]),
90        )
91        .x_axis3(
92            plotly::layout::Axis::new()
93                .title(plotly::common::Title::with_text(x_axis3_title))
94                .type_(AxisType::Log)
95                .range(vec![1.301, 4.301])
96                .domain(&[0.0, 0.45]),
97        )
98        .y_axis3(
99            plotly::layout::Axis::new()
100                .title(plotly::common::Title::with_text("SPL (dB)"))
101                .range(vec![-15.0, 5.0])
102                .domain(&[0.0, 0.45]),
103        )
104        .x_axis4(
105            plotly::layout::Axis::new()
106                .title(plotly::common::Title::with_text(x_axis4_title))
107                .type_(AxisType::Log)
108                .range(vec![1.301, 4.301])
109                .domain(&[0.55, 1.0]),
110        )
111        .y_axis4(
112            plotly::layout::Axis::new()
113                .title(plotly::common::Title::with_text("SPL (dB)"))
114                .range(vec![-15.0, 5.0])
115                .domain(&[0.0, 0.45]),
116        );
117
118    plot.set_layout(layout);
119
120    plot
121}
122
123/// Create a tonal balance plot showing smoothed CEA2034 curves.
124///
125/// Shows the tonal balance (1/2 octave smoothed) for On Axis, Listening Window,
126/// Early Reflections, and Sound Power measurements.
127pub fn plot_spin_tonal(
128    cea2034_curves: Option<&HashMap<String, crate::Curve>>,
129    eq_response: Option<&Array1<f64>>,
130) -> plotly::Plot {
131    let mut plot = Plot::new();
132    // Add each CEA2034 curves if provided
133    let x_axis1_title = "On Axis Tonal Balance".to_string();
134    let x_axis2_title = "Listening Window Tonal Balance".to_string();
135    let x_axis3_title = "Early Reflections Tonal Balance".to_string();
136    let x_axis4_title = "Sound Power Tonal Balance".to_string();
137    if let Some(curves) = cea2034_curves {
138        // Add regression lines for original curves (tonal balance)
139        for (i, curve_name) in CEA2034_CURVE_NAMES.iter().enumerate() {
140            let x_axis = if i == 0 {
141                "x".to_string()
142            } else {
143                format!("x{}", i + 1)
144            };
145            let y_axis = if i == 0 {
146                "y".to_string()
147            } else {
148                format!("y{}", i + 1)
149            };
150            if let Some(curve) = curves.get(*curve_name)
151                && let Some((slope, intercept)) =
152                    calculate_tonal_balance(&curve.freq, &curve.spl, 100.0, 10000.0)
153            {
154                let regression_line = generate_regression_line(slope, intercept, &curve.freq);
155                let trace = create_regression_trace(
156                    &curve.freq,
157                    &regression_line,
158                    &format!("{} {:.2} dB/oct", shorten_curve_name(curve_name), slope),
159                    filter_color(i),
160                    Some(&x_axis),
161                    Some(&y_axis),
162                );
163                plot.add_trace(Box::new(trace));
164            }
165        }
166
167        // Add regression lines for EQ-applied curves (tonal balance)
168        if let Some(eq_resp) = eq_response {
169            for (i, curve_name) in CEA2034_CURVE_NAMES.iter().enumerate() {
170                let x_axis = if i == 0 {
171                    "x".to_string()
172                } else {
173                    format!("x{}", i + 1)
174                };
175                let y_axis = if i == 0 {
176                    "y".to_string()
177                } else {
178                    format!("y{}", i + 1)
179                };
180                if let Some(curve) = curves.get(*curve_name) {
181                    let eq_applied = &curve.spl + eq_resp;
182                    if let Some((slope, intercept)) =
183                        calculate_tonal_balance(&curve.freq, &eq_applied, 100.0, 10000.0)
184                    {
185                        let regression_line =
186                            generate_regression_line(slope, intercept, &curve.freq);
187                        let trace = create_regression_trace(
188                            &curve.freq,
189                            &regression_line,
190                            &format!(
191                                "{} w/EQ {:.2} dB/oct",
192                                shorten_curve_name(curve_name),
193                                slope
194                            ),
195                            filter_color(i + 4),
196                            Some(&x_axis),
197                            Some(&y_axis),
198                        );
199                        plot.add_trace(Box::new(trace));
200                    }
201                }
202            }
203        }
204    }
205
206    // Configure layout with subplots
207    let layout = Layout::new()
208        .grid(
209            LayoutGrid::new()
210                .rows(2)
211                .columns(2)
212                .pattern(GridPattern::Independent)
213                .row_order(RowOrder::BottomToTop),
214        )
215        .width(1024)
216        .height(600)
217        .x_axis(
218            plotly::layout::Axis::new()
219                .title(plotly::common::Title::with_text(x_axis1_title))
220                .type_(AxisType::Log)
221                .range(vec![1.301, 4.301])
222                .domain(&[0.0, 0.45]),
223        )
224        .y_axis(
225            plotly::layout::Axis::new()
226                .title(plotly::common::Title::with_text("SPL (dB)"))
227                .range(vec![-10.0, 10.0])
228                .domain(&[0.55, 1.0]),
229        )
230        .x_axis2(
231            plotly::layout::Axis::new()
232                .title(plotly::common::Title::with_text(x_axis2_title))
233                .type_(AxisType::Log)
234                .range(vec![1.301, 4.301])
235                .domain(&[0.55, 1.0]),
236        )
237        .y_axis2(
238            plotly::layout::Axis::new()
239                .title(plotly::common::Title::with_text("SPL (dB)"))
240                .range(vec![-10.0, 10.0])
241                .domain(&[0.55, 1.0]),
242        )
243        .x_axis3(
244            plotly::layout::Axis::new()
245                .title(plotly::common::Title::with_text(x_axis3_title))
246                .type_(AxisType::Log)
247                .range(vec![1.301, 4.301])
248                .domain(&[0.0, 0.45]),
249        )
250        .y_axis3(
251            plotly::layout::Axis::new()
252                .title(plotly::common::Title::with_text("SPL (dB)"))
253                .range(vec![-15.0, 5.0])
254                .domain(&[0.0, 0.45]),
255        )
256        .x_axis4(
257            plotly::layout::Axis::new()
258                .title(plotly::common::Title::with_text(x_axis4_title))
259                .type_(AxisType::Log)
260                .range(vec![1.301, 4.301])
261                .domain(&[0.55, 1.0]),
262        )
263        .y_axis4(
264            plotly::layout::Axis::new()
265                .title(plotly::common::Title::with_text("SPL (dB)"))
266                .range(vec![-15.0, 5.0])
267                .domain(&[0.0, 0.45]),
268        );
269
270    plot.set_layout(layout);
271
272    plot
273}
274
275/// Create a CEA2034 spinorama overview plot.
276///
277/// Shows directivity indices (ERDI, SPDI) and the main CEA2034 curves
278/// with optional EQ response overlaid.
279pub fn plot_spin(
280    cea2034_curves: Option<&HashMap<String, crate::Curve>>,
281    eq_response: Option<&Array1<f64>>,
282) -> plotly::Plot {
283    let mut plot = Plot::new();
284
285    // ----------------------------------------------------------------------
286    // Add CEA2034 if provided with and without EQ
287    // ----------------------------------------------------------------------
288    let x_axis1_title = "CEA2034".to_string();
289    let x_axis3_title = "CEA2034 + EQ".to_string();
290    if let Some(curves) = cea2034_curves {
291        let cea2034_traces = create_cea2034_combined_traces(curves, "x", "y", "y2");
292        for trace in cea2034_traces {
293            plot.add_trace(Box::new(trace));
294        }
295
296        if let Some(eq_resp) = eq_response {
297            let cea2034_traces =
298                create_cea2034_with_eq_combined_traces(curves, eq_resp, "x3", "y3", "y4");
299            for trace in cea2034_traces {
300                plot.add_trace(Box::new(trace));
301            }
302        }
303    }
304
305    // Configure layout with subplots
306    let layout = Layout::new()
307        .grid(
308            LayoutGrid::new()
309                .rows(1)
310                .columns(2)
311                .pattern(GridPattern::Independent),
312        )
313        .width(1024)
314        .height(450)
315        // cea2034
316        .x_axis(
317            plotly::layout::Axis::new()
318                .title(plotly::common::Title::with_text(&x_axis1_title))
319                .type_(AxisType::Log)
320                .range(vec![1.301, 4.301])
321                .domain(&[0., 0.4]),
322        )
323        .y_axis(
324            plotly::layout::Axis::new()
325                .title(plotly::common::Title::with_text("SPL (dB)"))
326                .dtick(5.0)
327                .range(vec![-40.0, 10.0]),
328        )
329        .y_axis2(
330            plotly::layout::Axis::new()
331                .title(plotly::common::Title::with_text(
332                    "DI (dB)                      ⌃",
333                ))
334                .range(vec![-5.0, 45.0])
335                .tick_values(vec![-5.0, 0.0, 5.0, 10.0, 15.0])
336                .overlaying("y")
337                .side(AxisSide::Right),
338        )
339        // cea2034 with eq
340        .x_axis3(
341            plotly::layout::Axis::new()
342                .title(plotly::common::Title::with_text(&x_axis3_title))
343                .type_(AxisType::Log)
344                .range(vec![1.301, 4.301])
345                .domain(&[0.55, 0.95]),
346        )
347        .y_axis3(
348            plotly::layout::Axis::new()
349                .title(plotly::common::Title::with_text("SPL (dB)"))
350                .dtick(5.0)
351                .range(vec![-40.0, 10.0])
352                .anchor("x3"),
353        )
354        .y_axis4(
355            plotly::layout::Axis::new()
356                .title(plotly::common::Title::with_text(
357                    "DI (dB)                      ⌃",
358                ))
359                .range(vec![-5.0, 45.0])
360                .tick_values(vec![-5.0, 0.0, 5.0, 10.0, 15.0])
361                .anchor("x3")
362                .overlaying("y3")
363                .side(AxisSide::Right),
364        );
365    plot.set_layout(layout);
366
367    plot
368}