nahue-passano commited on
Commit
1e1fcd6
·
verified ·
1 Parent(s): 558481f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+
5
+ from loudspeaker_tmatrix.core import simulate_loudspeaker
6
+ from loudspeaker_tmatrix.visualization import plot_loudspeaker_response
7
+ from loudspeaker_tmatrix.config import (
8
+ load_config,
9
+ load_loudspeaker_config,
10
+ AcousticalConstantsConfig,
11
+ )
12
+
13
+ config_path = "configs/config.yaml"
14
+ default_loudspeaker_path = "configs/default_loudspeaker.yaml"
15
+
16
+ cfg = load_config(config_path)
17
+ loudspeaker_cfg = load_loudspeaker_config(default_loudspeaker_path)
18
+
19
+ freq_array = np.logspace(
20
+ np.log10(cfg.frequency.min), np.log10(cfg.frequency.max), num=cfg.frequency.n_bins
21
+ )
22
+
23
+ angular_freq_array = 2 * np.pi * freq_array
24
+
25
+ st.sidebar.title("Thiele Small parameters")
26
+
27
+ ### Sliders
28
+ slider_Re = st.sidebar.slider(
29
+ "Electrical Coil Resistance (Re) [Ohm]",
30
+ 0.0,
31
+ 10.0,
32
+ loudspeaker_cfg.electrical.coil_resistance,
33
+ )
34
+ slider_Le = st.sidebar.slider(
35
+ "Electrical Coil Inductance (Le) [mH]",
36
+ 0.0,
37
+ 10.0,
38
+ loudspeaker_cfg.electrical.coil_inductance * 1e3,
39
+ )
40
+ slider_Le /= 1e3
41
+ slider_Bl = st.sidebar.slider(
42
+ "Electromechanical factor (Bl) [N/A]",
43
+ 0.0,
44
+ 10.0,
45
+ loudspeaker_cfg.electromechanical_factor,
46
+ )
47
+ slider_Mm = st.sidebar.slider(
48
+ "Mechanical Mass (Mm) [mg]",
49
+ 0.0,
50
+ 50.0,
51
+ loudspeaker_cfg.mechanical.mass * 1e3,
52
+ )
53
+ slider_Mm /= 1e3
54
+ slider_Cm = st.sidebar.slider(
55
+ "Mechanical Compliance (Cm) [mm/N]",
56
+ 0.0,
57
+ 5.0,
58
+ loudspeaker_cfg.mechanical.compliance * 1e3,
59
+ )
60
+ slider_Cm /= 1e3
61
+ slider_Rm = st.sidebar.slider(
62
+ "Mechanical Resistance (Rm) [kg/s]",
63
+ 0.0,
64
+ 10.0,
65
+ loudspeaker_cfg.mechanical.resistance,
66
+ )
67
+ slider_diam = st.sidebar.slider(
68
+ "Effective diameter of radiation [cm]",
69
+ 0.0,
70
+ 50.0,
71
+ loudspeaker_cfg.acoustical.effective_diameter * 1e2,
72
+ )
73
+ slider_diam /= 1e2
74
+
75
+ default_params = st.sidebar.button("Set default parameters")
76
+
77
+ if default_params:
78
+ st.rerun(scope="app")
79
+
80
+
81
+ freq_array = np.logspace(
82
+ np.log10(cfg.frequency.min), np.log10(cfg.frequency.max), num=cfg.frequency.n_bins
83
+ )
84
+
85
+ angular_freq_array = 2 * np.pi * freq_array
86
+
87
+ thiele_small_params = {
88
+ "Re": slider_Re,
89
+ "Le": slider_Le,
90
+ "Bl": slider_Bl,
91
+ "Mm": slider_Mm,
92
+ "Cm": slider_Cm,
93
+ "Rm": slider_Rm,
94
+ "effective_diameter": slider_diam,
95
+ }
96
+
97
+ loudspeaker_responses = simulate_loudspeaker(
98
+ thiele_small_params, angular_freq_array, cfg.acoustical_constants
99
+ )
100
+
101
+
102
+ # Electrical impedance
103
+ electrical_impedance_plot = plot_loudspeaker_response(
104
+ response_array=loudspeaker_responses["electrical_impedance"],
105
+ freq_array=freq_array,
106
+ title="Electrical Impedance",
107
+ magnitude_in_db=False,
108
+ magnitude_units="Ohm",
109
+ shift_phase=False,
110
+ )
111
+
112
+
113
+ mechanical_velocity_plot = plot_loudspeaker_response(
114
+ response_array=loudspeaker_responses["mechanical_velocity"],
115
+ freq_array=freq_array,
116
+ title="Mechanical Velocity",
117
+ magnitude_in_db=False,
118
+ magnitude_units="m/s",
119
+ shift_phase=True,
120
+ )
121
+
122
+
123
+ acoustical_pressure = plot_loudspeaker_response(
124
+ response_array=loudspeaker_responses["acoustical_pressure"],
125
+ freq_array=freq_array,
126
+ title="Acoustical Pressure",
127
+ magnitude_in_db=True,
128
+ magnitude_units="dB",
129
+ shift_phase=False,
130
+ )
131
+
132
+ st.pyplot(electrical_impedance_plot)
133
+ st.pyplot(mechanical_velocity_plot)
134
+ st.pyplot(acoustical_pressure)