File size: 3,233 Bytes
22da9a9 50f2907 22da9a9 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
from core import simulate_loudspeaker
from visualization import plot_loudspeaker_response
from 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")
### Sliders
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
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)
|