Update visualization.py
Browse files- visualization.py +304 -1
visualization.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import numpy as np
|
2 |
from matplotlib import pyplot as plt
|
|
|
|
|
3 |
|
4 |
from utils import to_db
|
5 |
|
@@ -23,7 +25,7 @@ def plot_loudspeaker_response(
|
|
23 |
else:
|
24 |
phase = np.angle(response_array, deg=True)
|
25 |
|
26 |
-
fig, ax1 = plt.subplots(figsize=(12,
|
27 |
fig.suptitle(title)
|
28 |
|
29 |
ax1.semilogx(freq_array, magnitude, label="Magnitude", color="C0")
|
@@ -45,3 +47,304 @@ def plot_loudspeaker_response(
|
|
45 |
ax1.grid(axis="x")
|
46 |
ax2.grid(axis="y")
|
47 |
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
from matplotlib import pyplot as plt
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
from plotly.subplots import make_subplots
|
5 |
|
6 |
from utils import to_db
|
7 |
|
|
|
25 |
else:
|
26 |
phase = np.angle(response_array, deg=True)
|
27 |
|
28 |
+
fig, ax1 = plt.subplots(figsize=(12, 3))
|
29 |
fig.suptitle(title)
|
30 |
|
31 |
ax1.semilogx(freq_array, magnitude, label="Magnitude", color="C0")
|
|
|
47 |
ax1.grid(axis="x")
|
48 |
ax2.grid(axis="y")
|
49 |
return fig
|
50 |
+
|
51 |
+
|
52 |
+
def plotly_full_figure(
|
53 |
+
freq_array,
|
54 |
+
loudspeaker_responses_1,
|
55 |
+
loudspeaker_responses_2=None,
|
56 |
+
single_speaker=False,
|
57 |
+
):
|
58 |
+
fig = make_subplots(
|
59 |
+
rows=3,
|
60 |
+
cols=1,
|
61 |
+
shared_xaxes=True,
|
62 |
+
vertical_spacing=0.05,
|
63 |
+
specs=[
|
64 |
+
[{"secondary_y": True}],
|
65 |
+
[{"secondary_y": True}],
|
66 |
+
[{"secondary_y": True}],
|
67 |
+
], # Enable secondary y-axis for both rows
|
68 |
+
)
|
69 |
+
|
70 |
+
###### Speaker 1
|
71 |
+
### Acoustical Pressure
|
72 |
+
fig.add_trace(
|
73 |
+
go.Scatter(
|
74 |
+
x=freq_array,
|
75 |
+
y=to_db(loudspeaker_responses_1["acoustical_pressure"]),
|
76 |
+
mode="lines",
|
77 |
+
name="Speaker 1",
|
78 |
+
line=dict(color="blue"),
|
79 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} " + "dB",
|
80 |
+
legendgroup="Speaker 1 (Magnitude)",
|
81 |
+
showlegend=True,
|
82 |
+
),
|
83 |
+
row=1,
|
84 |
+
col=1,
|
85 |
+
secondary_y=False,
|
86 |
+
)
|
87 |
+
|
88 |
+
fig.add_trace(
|
89 |
+
go.Scatter(
|
90 |
+
x=freq_array,
|
91 |
+
y=np.angle(loudspeaker_responses_1["acoustical_pressure"], deg=True),
|
92 |
+
mode="lines",
|
93 |
+
name="Speaker 1",
|
94 |
+
line=dict(color="red"),
|
95 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} º",
|
96 |
+
legendgroup="Speaker 1 (Phase)",
|
97 |
+
showlegend=True,
|
98 |
+
),
|
99 |
+
row=1,
|
100 |
+
col=1,
|
101 |
+
secondary_y=True,
|
102 |
+
)
|
103 |
+
|
104 |
+
### Electrical Impedance
|
105 |
+
fig.add_trace(
|
106 |
+
go.Scatter(
|
107 |
+
x=freq_array,
|
108 |
+
y=np.abs(loudspeaker_responses_1["electrical_impedance"]),
|
109 |
+
mode="lines",
|
110 |
+
name="Speaker 1",
|
111 |
+
line=dict(color="blue"),
|
112 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} " + "Ohm",
|
113 |
+
legendgroup="Speaker 1 (Magnitude)",
|
114 |
+
showlegend=False,
|
115 |
+
),
|
116 |
+
row=2,
|
117 |
+
col=1,
|
118 |
+
secondary_y=False,
|
119 |
+
)
|
120 |
+
|
121 |
+
fig.add_trace(
|
122 |
+
go.Scatter(
|
123 |
+
x=freq_array,
|
124 |
+
y=np.angle(loudspeaker_responses_1["electrical_impedance"], deg=True),
|
125 |
+
mode="lines",
|
126 |
+
name="Speaker 1",
|
127 |
+
line=dict(color="red"),
|
128 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} º",
|
129 |
+
legendgroup="Speaker 1 (Phase)",
|
130 |
+
showlegend=False,
|
131 |
+
),
|
132 |
+
row=2,
|
133 |
+
col=1,
|
134 |
+
secondary_y=True,
|
135 |
+
)
|
136 |
+
|
137 |
+
### Mechanical Velocity
|
138 |
+
fig.add_trace(
|
139 |
+
go.Scatter(
|
140 |
+
x=freq_array,
|
141 |
+
y=np.abs(loudspeaker_responses_1["mechanical_velocity"]),
|
142 |
+
mode="lines",
|
143 |
+
name="Speaker 1",
|
144 |
+
line=dict(color="blue"),
|
145 |
+
hovertemplate="%{x:.2f} Hz<br>%{y:.2f} " + "m/s",
|
146 |
+
legendgroup="Speaker 1 (Magnitude)",
|
147 |
+
showlegend=False,
|
148 |
+
),
|
149 |
+
row=3,
|
150 |
+
col=1,
|
151 |
+
secondary_y=False,
|
152 |
+
)
|
153 |
+
|
154 |
+
fig.add_trace(
|
155 |
+
go.Scatter(
|
156 |
+
x=freq_array,
|
157 |
+
y=np.angle(-loudspeaker_responses_1["mechanical_velocity"], deg=True),
|
158 |
+
mode="lines",
|
159 |
+
name="Speaker 1",
|
160 |
+
line=dict(color="red"),
|
161 |
+
hovertemplate="%{x:.2f} Hz<br>%{y:.2f} º",
|
162 |
+
legendgroup="Speaker 1 (Phase)",
|
163 |
+
showlegend=False,
|
164 |
+
),
|
165 |
+
row=3,
|
166 |
+
col=1,
|
167 |
+
secondary_y=True,
|
168 |
+
)
|
169 |
+
|
170 |
+
###### Speaker 2
|
171 |
+
if not single_speaker:
|
172 |
+
### Acoustical Pressure
|
173 |
+
fig.add_trace(
|
174 |
+
go.Scatter(
|
175 |
+
x=freq_array,
|
176 |
+
y=to_db(loudspeaker_responses_2["acoustical_pressure"]),
|
177 |
+
mode="lines",
|
178 |
+
name="Speaker 2",
|
179 |
+
line=dict(color="blue", dash="dot"),
|
180 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} " + "dB",
|
181 |
+
legendgroup="Speaker 2 (Magnitude)",
|
182 |
+
showlegend=True,
|
183 |
+
),
|
184 |
+
row=1,
|
185 |
+
col=1,
|
186 |
+
secondary_y=False,
|
187 |
+
)
|
188 |
+
|
189 |
+
fig.add_trace(
|
190 |
+
go.Scatter(
|
191 |
+
x=freq_array,
|
192 |
+
y=np.angle(loudspeaker_responses_2["acoustical_pressure"], deg=True),
|
193 |
+
mode="lines",
|
194 |
+
name="Speaker 2",
|
195 |
+
line=dict(color="red", dash="dot"),
|
196 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} º",
|
197 |
+
legendgroup="Speaker 2 (Phase)",
|
198 |
+
showlegend=True,
|
199 |
+
),
|
200 |
+
row=1,
|
201 |
+
col=1,
|
202 |
+
secondary_y=True,
|
203 |
+
)
|
204 |
+
|
205 |
+
### Electrical Impedance
|
206 |
+
fig.add_trace(
|
207 |
+
go.Scatter(
|
208 |
+
x=freq_array,
|
209 |
+
y=np.abs(loudspeaker_responses_2["electrical_impedance"]),
|
210 |
+
mode="lines",
|
211 |
+
name="Speaker 2",
|
212 |
+
line=dict(color="blue", dash="dot"),
|
213 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} " + "Ohm",
|
214 |
+
legendgroup="Speaker 2 (Magnitude)",
|
215 |
+
showlegend=False,
|
216 |
+
),
|
217 |
+
row=2,
|
218 |
+
col=1,
|
219 |
+
secondary_y=False,
|
220 |
+
)
|
221 |
+
|
222 |
+
fig.add_trace(
|
223 |
+
go.Scatter(
|
224 |
+
x=freq_array,
|
225 |
+
y=np.angle(loudspeaker_responses_2["electrical_impedance"], deg=True),
|
226 |
+
mode="lines",
|
227 |
+
name="Speaker 2",
|
228 |
+
line=dict(color="red", dash="dot"),
|
229 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} º",
|
230 |
+
legendgroup="Speaker 2 (Phase)",
|
231 |
+
showlegend=False,
|
232 |
+
),
|
233 |
+
row=2,
|
234 |
+
col=1,
|
235 |
+
secondary_y=True,
|
236 |
+
)
|
237 |
+
|
238 |
+
### Mechanical Velocity
|
239 |
+
fig.add_trace(
|
240 |
+
go.Scatter(
|
241 |
+
x=freq_array,
|
242 |
+
y=np.abs(loudspeaker_responses_2["mechanical_velocity"]),
|
243 |
+
mode="lines",
|
244 |
+
name="Speaker 2",
|
245 |
+
line=dict(color="blue", dash="dot"),
|
246 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} " + "m/s",
|
247 |
+
legendgroup="Speaker 2 (Magnitude)",
|
248 |
+
showlegend=False,
|
249 |
+
),
|
250 |
+
row=3,
|
251 |
+
col=1,
|
252 |
+
secondary_y=False,
|
253 |
+
)
|
254 |
+
|
255 |
+
fig.add_trace(
|
256 |
+
go.Scatter(
|
257 |
+
x=freq_array,
|
258 |
+
y=np.angle(-loudspeaker_responses_2["mechanical_velocity"], deg=True),
|
259 |
+
mode="lines",
|
260 |
+
name="Speaker 2",
|
261 |
+
line=dict(color="red", dash="dot"),
|
262 |
+
hovertemplate="%{x:.1f} Hz<br>%{y:.1f} º",
|
263 |
+
legendgroup="Speaker 2 (Phase)",
|
264 |
+
showlegend=False,
|
265 |
+
),
|
266 |
+
row=3,
|
267 |
+
col=1,
|
268 |
+
secondary_y=True,
|
269 |
+
)
|
270 |
+
|
271 |
+
### Update layout
|
272 |
+
fig.update_layout(
|
273 |
+
template="ggplot2",
|
274 |
+
xaxis=dict(
|
275 |
+
type="log",
|
276 |
+
tickvals=[16, 31, 63, 125, 250, 500, 1000, 2000, 4000],
|
277 |
+
range=[np.log10(10), np.log10(4000)],
|
278 |
+
),
|
279 |
+
xaxis2=dict(
|
280 |
+
type="log",
|
281 |
+
tickvals=[16, 31, 63, 125, 250, 500, 1000, 2000, 4000],
|
282 |
+
range=[np.log10(10), np.log10(4000)],
|
283 |
+
),
|
284 |
+
xaxis3=dict(
|
285 |
+
title="Frequency [Hz]",
|
286 |
+
titlefont=dict(size=14),
|
287 |
+
tickfont=dict(size=12),
|
288 |
+
type="log",
|
289 |
+
tickvals=[16, 31, 63, 125, 250, 500, 1000, 2000, 4000],
|
290 |
+
range=[np.log10(10), np.log10(4000)],
|
291 |
+
),
|
292 |
+
yaxis=dict(title="Pressure level [dB]", color="blue"),
|
293 |
+
yaxis2=dict(
|
294 |
+
title="Phase [º]",
|
295 |
+
color="red",
|
296 |
+
tickvals=[-180, -90, 0, 90, 180],
|
297 |
+
ticktext=["-180º", "-90º", "0º", "90º", "180º"],
|
298 |
+
range=[-180, 180],
|
299 |
+
),
|
300 |
+
yaxis3=dict(title="Impedance [Ohm]", color="blue"),
|
301 |
+
yaxis4=dict(
|
302 |
+
title="Phase [º]",
|
303 |
+
color="red",
|
304 |
+
tickvals=[-180, -90, 0, 90, 180],
|
305 |
+
ticktext=["-180º", "-90º", "0º", "90º", "180º"],
|
306 |
+
range=[-180, 180],
|
307 |
+
),
|
308 |
+
yaxis5=dict(title="Velocity [m/s]", color="blue"),
|
309 |
+
yaxis6=dict(
|
310 |
+
title="Phase [º]",
|
311 |
+
color="red",
|
312 |
+
tickvals=[-180, -90, 0, 90, 180],
|
313 |
+
ticktext=["-180º", "-90º", "0º", "90º", "180º"],
|
314 |
+
range=[-180, 180],
|
315 |
+
),
|
316 |
+
legend=dict(font=dict(size=10), x=0.85, y=0.99),
|
317 |
+
width=1200,
|
318 |
+
height=800,
|
319 |
+
margin=dict(l=20, r=20, t=30, b=20),
|
320 |
+
annotations=[
|
321 |
+
dict(
|
322 |
+
text="<b>Acoustical Pressure</b>",
|
323 |
+
x=0.47,
|
324 |
+
y=1.03,
|
325 |
+
xref="paper",
|
326 |
+
yref="paper",
|
327 |
+
showarrow=False,
|
328 |
+
font=dict(size=18),
|
329 |
+
),
|
330 |
+
dict(
|
331 |
+
text="<b>Electrical Impedance</b>",
|
332 |
+
x=0.47,
|
333 |
+
y=0.68,
|
334 |
+
xref="paper",
|
335 |
+
yref="paper",
|
336 |
+
showarrow=False,
|
337 |
+
font=dict(size=18),
|
338 |
+
),
|
339 |
+
dict(
|
340 |
+
text="<b>Mechanical Velocity</b>",
|
341 |
+
x=0.47,
|
342 |
+
y=0.30,
|
343 |
+
xref="paper",
|
344 |
+
yref="paper",
|
345 |
+
showarrow=False,
|
346 |
+
font=dict(size=18),
|
347 |
+
),
|
348 |
+
],
|
349 |
+
)
|
350 |
+
return fig
|