use super::consts::CEA2034_CURVE_NAMES;
use super::create::create_cea2034_combined_traces;
use super::create::create_cea2034_traces;
use super::create::create_cea2034_with_eq_combined_traces;
use super::create::create_cea2034_with_eq_traces;
use super::misc::shorten_curve_name;
use crate::filter_color::filter_color;
use crate::ref_lines::make_ref_lines;
use crate::trend_lines::{
calculate_tonal_balance, create_regression_trace, generate_regression_line,
};
use ndarray::Array1;
use plotly::common::AxisSide;
use plotly::layout::{AxisType, GridPattern, LayoutGrid, RowOrder};
use plotly::{Layout, Plot};
use std::collections::HashMap;
pub fn plot_spin_details(
cea2034_curves: Option<&HashMap<String, crate::Curve>>,
eq_response: Option<&Array1<f64>>,
) -> plotly::Plot {
let mut plot = Plot::new();
let x_axis1_title = "On Axis".to_string();
let x_axis2_title = "Listening Window".to_string();
let x_axis3_title = "Early Reflections".to_string();
let x_axis4_title = "Sound Power".to_string();
if let Some(curves) = cea2034_curves {
let cea2034_traces = create_cea2034_traces(curves);
for trace in cea2034_traces {
plot.add_trace(Box::new(trace));
}
if let Some(eq_resp) = eq_response {
let cea2034_eq_traces = create_cea2034_with_eq_traces(curves, eq_resp);
for trace in cea2034_eq_traces {
plot.add_trace(Box::new(trace));
}
}
}
for t in make_ref_lines("x", "y") {
plot.add_trace(Box::new(t));
}
for t in make_ref_lines("x2", "y2") {
plot.add_trace(Box::new(t));
}
let layout = Layout::new()
.grid(
LayoutGrid::new()
.rows(2)
.columns(2)
.pattern(GridPattern::Independent)
.row_order(RowOrder::BottomToTop),
)
.width(1024)
.height(600)
.x_axis(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis1_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.0, 0.45]),
)
.y_axis(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-10.0, 10.0])
.domain(&[0.55, 1.0]),
)
.x_axis2(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis2_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.55, 1.0]),
)
.y_axis2(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-10.0, 10.0])
.domain(&[0.55, 1.0]),
)
.x_axis3(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis3_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.0, 0.45]),
)
.y_axis3(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-15.0, 5.0])
.domain(&[0.0, 0.45]),
)
.x_axis4(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis4_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.55, 1.0]),
)
.y_axis4(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-15.0, 5.0])
.domain(&[0.0, 0.45]),
);
plot.set_layout(layout);
plot
}
pub fn plot_spin_tonal(
cea2034_curves: Option<&HashMap<String, crate::Curve>>,
eq_response: Option<&Array1<f64>>,
) -> plotly::Plot {
let mut plot = Plot::new();
let x_axis1_title = "On Axis Tonal Balance".to_string();
let x_axis2_title = "Listening Window Tonal Balance".to_string();
let x_axis3_title = "Early Reflections Tonal Balance".to_string();
let x_axis4_title = "Sound Power Tonal Balance".to_string();
if let Some(curves) = cea2034_curves {
for (i, curve_name) in CEA2034_CURVE_NAMES.iter().enumerate() {
let x_axis = if i == 0 {
"x".to_string()
} else {
format!("x{}", i + 1)
};
let y_axis = if i == 0 {
"y".to_string()
} else {
format!("y{}", i + 1)
};
if let Some(curve) = curves.get(*curve_name)
&& let Some((slope, intercept)) =
calculate_tonal_balance(&curve.freq, &curve.spl, 100.0, 10000.0)
{
let regression_line = generate_regression_line(slope, intercept, &curve.freq);
let trace = create_regression_trace(
&curve.freq,
®ression_line,
&format!("{} {:.2} dB/oct", shorten_curve_name(curve_name), slope),
filter_color(i),
Some(&x_axis),
Some(&y_axis),
);
plot.add_trace(Box::new(trace));
}
}
if let Some(eq_resp) = eq_response {
for (i, curve_name) in CEA2034_CURVE_NAMES.iter().enumerate() {
let x_axis = if i == 0 {
"x".to_string()
} else {
format!("x{}", i + 1)
};
let y_axis = if i == 0 {
"y".to_string()
} else {
format!("y{}", i + 1)
};
if let Some(curve) = curves.get(*curve_name) {
let eq_applied = &curve.spl + eq_resp;
if let Some((slope, intercept)) =
calculate_tonal_balance(&curve.freq, &eq_applied, 100.0, 10000.0)
{
let regression_line =
generate_regression_line(slope, intercept, &curve.freq);
let trace = create_regression_trace(
&curve.freq,
®ression_line,
&format!(
"{} w/EQ {:.2} dB/oct",
shorten_curve_name(curve_name),
slope
),
filter_color(i + 4),
Some(&x_axis),
Some(&y_axis),
);
plot.add_trace(Box::new(trace));
}
}
}
}
}
let layout = Layout::new()
.grid(
LayoutGrid::new()
.rows(2)
.columns(2)
.pattern(GridPattern::Independent)
.row_order(RowOrder::BottomToTop),
)
.width(1024)
.height(600)
.x_axis(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis1_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.0, 0.45]),
)
.y_axis(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-10.0, 10.0])
.domain(&[0.55, 1.0]),
)
.x_axis2(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis2_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.55, 1.0]),
)
.y_axis2(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-10.0, 10.0])
.domain(&[0.55, 1.0]),
)
.x_axis3(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis3_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.0, 0.45]),
)
.y_axis3(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-15.0, 5.0])
.domain(&[0.0, 0.45]),
)
.x_axis4(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(x_axis4_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.55, 1.0]),
)
.y_axis4(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.range(vec![-15.0, 5.0])
.domain(&[0.0, 0.45]),
);
plot.set_layout(layout);
plot
}
pub fn plot_spin(
cea2034_curves: Option<&HashMap<String, crate::Curve>>,
eq_response: Option<&Array1<f64>>,
) -> plotly::Plot {
let mut plot = Plot::new();
let x_axis1_title = "CEA2034".to_string();
let x_axis3_title = "CEA2034 + EQ".to_string();
if let Some(curves) = cea2034_curves {
let cea2034_traces = create_cea2034_combined_traces(curves, "x", "y", "y2");
for trace in cea2034_traces {
plot.add_trace(Box::new(trace));
}
if let Some(eq_resp) = eq_response {
let cea2034_traces =
create_cea2034_with_eq_combined_traces(curves, eq_resp, "x3", "y3", "y4");
for trace in cea2034_traces {
plot.add_trace(Box::new(trace));
}
}
}
let layout = Layout::new()
.grid(
LayoutGrid::new()
.rows(1)
.columns(2)
.pattern(GridPattern::Independent),
)
.width(1024)
.height(450)
.x_axis(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(&x_axis1_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0., 0.4]),
)
.y_axis(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.dtick(5.0)
.range(vec![-40.0, 10.0]),
)
.y_axis2(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(
"DI (dB) ⌃",
))
.range(vec![-5.0, 45.0])
.tick_values(vec![-5.0, 0.0, 5.0, 10.0, 15.0])
.overlaying("y")
.side(AxisSide::Right),
)
.x_axis3(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(&x_axis3_title))
.type_(AxisType::Log)
.range(vec![1.301, 4.301])
.domain(&[0.55, 0.95]),
)
.y_axis3(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text("SPL (dB)"))
.dtick(5.0)
.range(vec![-40.0, 10.0])
.anchor("x3"),
)
.y_axis4(
plotly::layout::Axis::new()
.title(plotly::common::Title::with_text(
"DI (dB) ⌃",
))
.range(vec![-5.0, 45.0])
.tick_values(vec![-5.0, 0.0, 5.0, 10.0, 15.0])
.anchor("x3")
.overlaying("y3")
.side(AxisSide::Right),
);
plot.set_layout(layout);
plot
}