|
import streamlit as st |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
|
|
from loudspeaker_tmatrix.core import simulate_loudspeaker |
|
from loudspeaker_tmatrix.visualization import plot_loudspeaker_response |
|
from loudspeaker_tmatrix.config import ( |
|
load_config, |
|
load_loudspeaker_config, |
|
AcousticalConstantsConfig, |
|
) |
|
|
|
config_path = "configs/config.yaml" |
|
default_loudspeaker_path = "configs/default_loudspeaker.yaml" |
|
|
|
cfg = load_config(config_path) |
|
loudspeaker_cfg = load_loudspeaker_config(default_loudspeaker_path) |
|
|
|
freq_array = np.logspace( |
|
np.log10(cfg.frequency.min), np.log10(cfg.frequency.max), num=cfg.frequency.n_bins |
|
) |
|
|
|
angular_freq_array = 2 * np.pi * freq_array |
|
|
|
st.sidebar.title("Thiele Small parameters") |
|
|
|
|
|
slider_Re = st.sidebar.slider( |
|
"Electrical Coil Resistance (Re) [Ohm]", |
|
0.0, |
|
10.0, |
|
loudspeaker_cfg.electrical.coil_resistance, |
|
) |
|
slider_Le = st.sidebar.slider( |
|
"Electrical Coil Inductance (Le) [mH]", |
|
0.0, |
|
10.0, |
|
loudspeaker_cfg.electrical.coil_inductance * 1e3, |
|
) |
|
slider_Le /= 1e3 |
|
slider_Bl = st.sidebar.slider( |
|
"Electromechanical factor (Bl) [N/A]", |
|
0.0, |
|
10.0, |
|
loudspeaker_cfg.electromechanical_factor, |
|
) |
|
slider_Mm = st.sidebar.slider( |
|
"Mechanical Mass (Mm) [mg]", |
|
0.0, |
|
50.0, |
|
loudspeaker_cfg.mechanical.mass * 1e3, |
|
) |
|
slider_Mm /= 1e3 |
|
slider_Cm = st.sidebar.slider( |
|
"Mechanical Compliance (Cm) [mm/N]", |
|
0.0, |
|
5.0, |
|
loudspeaker_cfg.mechanical.compliance * 1e3, |
|
) |
|
slider_Cm /= 1e3 |
|
slider_Rm = st.sidebar.slider( |
|
"Mechanical Resistance (Rm) [kg/s]", |
|
0.0, |
|
10.0, |
|
loudspeaker_cfg.mechanical.resistance, |
|
) |
|
slider_diam = st.sidebar.slider( |
|
"Effective diameter of radiation [cm]", |
|
0.0, |
|
50.0, |
|
loudspeaker_cfg.acoustical.effective_diameter * 1e2, |
|
) |
|
slider_diam /= 1e2 |
|
|
|
default_params = st.sidebar.button("Set default parameters") |
|
|
|
if default_params: |
|
st.rerun(scope="app") |
|
|
|
|
|
freq_array = np.logspace( |
|
np.log10(cfg.frequency.min), np.log10(cfg.frequency.max), num=cfg.frequency.n_bins |
|
) |
|
|
|
angular_freq_array = 2 * np.pi * freq_array |
|
|
|
thiele_small_params = { |
|
"Re": slider_Re, |
|
"Le": slider_Le, |
|
"Bl": slider_Bl, |
|
"Mm": slider_Mm, |
|
"Cm": slider_Cm, |
|
"Rm": slider_Rm, |
|
"effective_diameter": slider_diam, |
|
} |
|
|
|
loudspeaker_responses = simulate_loudspeaker( |
|
thiele_small_params, angular_freq_array, cfg.acoustical_constants |
|
) |
|
|
|
|
|
|
|
electrical_impedance_plot = plot_loudspeaker_response( |
|
response_array=loudspeaker_responses["electrical_impedance"], |
|
freq_array=freq_array, |
|
title="Electrical Impedance", |
|
magnitude_in_db=False, |
|
magnitude_units="Ohm", |
|
shift_phase=False, |
|
) |
|
|
|
|
|
mechanical_velocity_plot = plot_loudspeaker_response( |
|
response_array=loudspeaker_responses["mechanical_velocity"], |
|
freq_array=freq_array, |
|
title="Mechanical Velocity", |
|
magnitude_in_db=False, |
|
magnitude_units="m/s", |
|
shift_phase=True, |
|
) |
|
|
|
|
|
acoustical_pressure = plot_loudspeaker_response( |
|
response_array=loudspeaker_responses["acoustical_pressure"], |
|
freq_array=freq_array, |
|
title="Acoustical Pressure", |
|
magnitude_in_db=True, |
|
magnitude_units="dB", |
|
shift_phase=False, |
|
) |
|
|
|
st.pyplot(electrical_impedance_plot) |
|
st.pyplot(mechanical_velocity_plot) |
|
st.pyplot(acoustical_pressure) |
|
|