-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreduce_order_compare.py
More file actions
371 lines (327 loc) · 11.7 KB
/
Copy pathreduce_order_compare.py
File metadata and controls
371 lines (327 loc) · 11.7 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import control as co
from control import matlab
import numpy as np
from scipy import signal
from pdb import set_trace as pdb
import matplotlib.pyplot as plt
import json
from typing import List, Tuple, Optional, Union
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def setup_plot_style() -> None:
"""Configure global matplotlib plotting style."""
try:
# Try to use seaborn if available
import seaborn as sns
sns.set_style("whitegrid")
except ImportError:
# Fallback to matplotlib's built-in style
plt.style.use('default')
# Configure basic style parameters
plt.rcParams.update({
'font.family': 'Times New Roman',
'font.size': 16,
'font.weight': 'bold',
'axes.grid': True,
'grid.alpha': 0.3,
'grid.linestyle': '--',
'axes.linewidth': 1.5,
'axes.edgecolor': 'black',
'xtick.major.width': 1.5,
'ytick.major.width': 1.5,
'xtick.minor.width': 1.5,
'ytick.minor.width': 1.5,
'xtick.labelsize': 14,
'ytick.labelsize': 14,
'axes.labelsize': 16,
'axes.titlesize': 18,
'figure.titlesize': 22,
'legend.fontsize': 14,
'legend.frameon': True,
'legend.edgecolor': 'black',
'legend.framealpha': 0.8,
'figure.figsize': [16, 12],
'figure.dpi': 100,
'savefig.dpi': 300,
'savefig.bbox': 'tight',
'savefig.pad_inches': 0.1
})
logger.info("Using default matplotlib style with custom parameters")
def create_figure(title: str, figsize: Tuple[int, int] = (16, 12)) -> Tuple[plt.Figure, plt.Axes]:
"""Create a figure with consistent styling."""
fig = plt.figure(figsize=figsize)
fig.suptitle(title, fontsize=22, weight='bold', pad=20)
return fig, plt.gca()
def Freq_Resp_Plot_Compare(
Fr_Resp_reduced_Mag: np.ndarray,
Fr_Resp_reduced_Phase: np.ndarray,
Fr_Resp_all_Mag: np.ndarray,
Fr_Resp_all_Phase: np.ndarray,
Freq: np.ndarray,
name: str,
phase_range: Tuple[float, float] = (-360, 90),
save_path: Optional[str] = None
) -> None:
"""
Compare frequency responses between reduced and full-order systems.
Args:
Fr_Resp_reduced_Mag: Magnitude of reduced-order system response
Fr_Resp_reduced_Phase: Phase of reduced-order system response
Fr_Resp_all_Mag: Magnitude of full-order system response
Fr_Resp_all_Phase: Phase of full-order system response
Freq: Frequency array
name: Plot title
phase_range: Phase plot range (min, max)
save_path: Path to save the plot
"""
setup_plot_style()
fig, ax = plt.subplots(4, 1, sharex='col', figsize=(24, 24))
fig.suptitle(name, fontsize=22, weight='bold')
# Plot reduced-order system
plot_frequency_response(
ax[0], ax[1],
Freq, Fr_Resp_reduced_Mag, Fr_Resp_reduced_Phase,
"The result of reduced-order system",
phase_range
)
# Plot full-order system
plot_frequency_response(
ax[2], ax[3],
Freq, Fr_Resp_all_Mag, Fr_Resp_all_Phase,
"The result of full-order system",
phase_range
)
if save_path:
plt.savefig(save_path)
logger.info(f"Plot saved to {save_path}")
def plot_frequency_response(
mag_ax: plt.Axes,
phase_ax: plt.Axes,
freq: np.ndarray,
mag: np.ndarray,
phase: np.ndarray,
title: str,
phase_range: Tuple[float, float]
) -> None:
"""Helper function to plot frequency response on given axes."""
mag_ax.set_title(title, fontsize=18, weight='bold')
mag_ax.set_ylabel("Gain [dB]")
mag_ax.set_xscale('log')
phase_ax.set_xlabel("Frequency [Hz]")
phase_ax.set_ylabel("Phase [deg.]")
phase_ax.set_ylim(phase_range)
for i in range(mag.shape[0]):
style = "--" if i > 5 else "-"
mag_ax.plot(freq, mag[i], style, label=f'Case {i+1}')
phase_ax.plot(freq, phase[i], style, label=f'Case {i+1}')
if mag.shape[0] > 1:
phase_ax.legend(
loc="lower left",
prop={'family': 'Times New Roman', 'size': 16, 'weight': 'bold'}
)
def Nyquist_Plot_Compare(
Fr_Resp_all_real_reduce: List[List[float]],
Fr_Resp_all_imag_reduce: List[List[float]],
Fr_Resp_all_real: List[List[float]],
Fr_Resp_all_imag: List[List[float]],
title: str,
save_path: Optional[str] = None
) -> None:
"""
Compare Nyquist plots between reduced and full-order systems.
Args:
Fr_Resp_all_real_reduce: Real part of reduced-order system response
Fr_Resp_all_imag_reduce: Imaginary part of reduced-order system response
Fr_Resp_all_real: Real part of full-order system response
Fr_Resp_all_imag: Imaginary part of full-order system response
title: Plot title
save_path: Path to save the plot
"""
setup_plot_style()
fig, ax = plt.subplots(1, 4, figsize=(48, 24))
fig.suptitle(title, fontsize=22, weight='bold')
# Plot reduced-order system
plot_nyquist(
ax[0], ax[1],
Fr_Resp_all_real_reduce, Fr_Resp_all_imag_reduce,
"Openloop (Nyquist Plot) Overall (Reduced Order)",
"Openloop (Nyquist Plot) Detail (Reduced Order)"
)
# Plot full-order system
plot_nyquist(
ax[2], ax[3],
Fr_Resp_all_real, Fr_Resp_all_imag,
"Openloop (Nyquist Plot) Overall (Full Order)",
"Openloop (Nyquist Plot) Detail (Full Order)"
)
if save_path:
plt.savefig(save_path)
logger.info(f"Plot saved to {save_path}")
def plot_nyquist(
overall_ax: plt.Axes,
detail_ax: plt.Axes,
real_data: List[List[float]],
imag_data: List[List[float]],
overall_title: str,
detail_title: str
) -> None:
"""Helper function to plot Nyquist plots on given axes."""
overall_ax.set_title(overall_title, fontsize=22, weight='bold')
overall_ax.set_xlabel("Real Axis")
overall_ax.set_ylabel("Imaginary Axis")
detail_ax.set_title(detail_title, fontsize=22, weight='bold')
detail_ax.set_xlabel("Real Axis")
detail_ax.set_ylabel("Imaginary Axis")
detail_ax.set_xlim(-7, 7)
detail_ax.set_ylim(-5, 5)
for i in range(len(real_data)):
style = "--" if i > 5 else "-"
overall_ax.plot(real_data[i], imag_data[i], style, label=f'Case {i+1}')
# Find detail view range
d_index = next(
(j for j in range(1, len(real_data[i]))
if abs(real_data[i][-j]) > 7 or abs(imag_data[i][-j]) > 5),
len(real_data[i])
)
detail_ax.plot(
real_data[i][-d_index:],
imag_data[i][-d_index:],
style,
label=f'Case {i+1}'
)
if len(real_data) > 1:
overall_ax.legend(
loc="lower left",
prop={'family': 'Times New Roman', 'size': 16, 'weight': 'bold'}
)
detail_ax.legend(
loc="lower left",
prop={'family': 'Times New Roman', 'size': 16, 'weight': 'bold'}
)
def Sensitive_Function_Plot_Compare(
Fr_Resp_all_Mag_reduce: np.ndarray,
Fr_Resp_all_Mag: np.ndarray,
Freq: np.ndarray,
name: str,
save_path: Optional[str] = None
) -> None:
"""
Compare sensitivity functions between reduced and full-order systems.
Args:
Fr_Resp_all_Mag_reduce: Magnitude of reduced-order system sensitivity
Fr_Resp_all_Mag: Magnitude of full-order system sensitivity
Freq: Frequency array
name: Plot title
save_path: Path to save the plot
"""
setup_plot_style()
fig, ax = plt.subplots(2, 1, figsize=(16, 12))
fig.suptitle(name, fontsize=22, weight='bold')
# Plot reduced-order system
plot_sensitivity(ax[0], Freq, Fr_Resp_all_Mag_reduce, "Reduced Order System")
# Plot full-order system
plot_sensitivity(ax[1], Freq, Fr_Resp_all_Mag, "Full Order System")
if save_path:
plt.savefig(save_path)
logger.info(f"Plot saved to {save_path}")
def plot_sensitivity(
ax: plt.Axes,
freq: np.ndarray,
mag: np.ndarray,
title: str
) -> None:
"""Helper function to plot sensitivity function on given axes."""
ax.set_title(title, fontsize=18, weight='bold')
ax.set_xlabel("Frequency [Hz]")
ax.set_ylabel("Gain [dB]")
ax.set_xscale('log')
for i in range(mag.shape[0]):
style = "--" if i > 5 else "-"
ax.plot(freq, mag[i], style, label=f'Case {i+1}')
if mag.shape[0] > 1:
ax.legend(
loc="upper left",
prop={'family': 'Times New Roman', 'size': 16, 'weight': 'bold'}
)
def Multi_Rate_Filter_Plot_Compare(
Fr_Resp_1_Mag_reduce: np.ndarray,
Fr_Resp_1_Phase_reduce: np.ndarray,
Fr_Resp_2_Mag_reduce: np.ndarray,
Fr_Resp_2_Phase_reduce: np.ndarray,
Fr_Resp_1_Mag: np.ndarray,
Fr_Resp_1_Phase: np.ndarray,
Fr_Resp_2_Mag: np.ndarray,
Fr_Resp_2_Phase: np.ndarray,
Freq: np.ndarray,
name: str,
save_path: Optional[str] = None
) -> None:
"""
Compare multi-rate filter responses between reduced and full-order systems.
Args:
Fr_Resp_1_Mag_reduce: Magnitude of first reduced-order filter response
Fr_Resp_1_Phase_reduce: Phase of first reduced-order filter response
Fr_Resp_2_Mag_reduce: Magnitude of second reduced-order filter response
Fr_Resp_2_Phase_reduce: Phase of second reduced-order filter response
Fr_Resp_1_Mag: Magnitude of first full-order filter response
Fr_Resp_1_Phase: Phase of first full-order filter response
Fr_Resp_2_Mag: Magnitude of second full-order filter response
Fr_Resp_2_Phase: Phase of second full-order filter response
Freq: Frequency array
name: Plot title
save_path: Path to save the plot
"""
setup_plot_style()
fig, ax = plt.subplots(4, 1, sharex='col', figsize=(16, 24))
fig.suptitle(name, fontsize=22, weight='bold')
# Plot reduced-order system
plot_filter_response(
ax[0], ax[1],
Freq,
Fr_Resp_1_Mag_reduce, Fr_Resp_1_Phase_reduce,
Fr_Resp_2_Mag_reduce, Fr_Resp_2_Phase_reduce,
"Reduced Order System"
)
# Plot full-order system
plot_filter_response(
ax[2], ax[3],
Freq,
Fr_Resp_1_Mag, Fr_Resp_1_Phase,
Fr_Resp_2_Mag, Fr_Resp_2_Phase,
"Full Order System"
)
if save_path:
plt.savefig(save_path)
logger.info(f"Plot saved to {save_path}")
def plot_filter_response(
mag_ax: plt.Axes,
phase_ax: plt.Axes,
freq: np.ndarray,
mag1: np.ndarray,
phase1: np.ndarray,
mag2: np.ndarray,
phase2: np.ndarray,
title: str
) -> None:
"""Helper function to plot filter response on given axes."""
mag_ax.set_title(title, fontsize=18, weight='bold')
mag_ax.set_xlabel("Frequency [Hz]")
mag_ax.set_ylabel("Gain [dB]")
mag_ax.set_xscale('log')
phase_ax.set_xlabel("Frequency [Hz]")
phase_ax.set_ylabel("Phase [deg.]")
phase_ax.set_ylim(-180, 180)
mag_ax.plot(freq, mag1, label='Fr_Fm_vcm')
mag_ax.plot(freq, mag2, label='Fr_Fm_pzt')
phase_ax.plot(freq, phase1, label='Fr_Fm_vcm')
phase_ax.plot(freq, phase2, label='Fr_Fm_pzt')
mag_ax.legend(
loc="lower left",
prop={'family': 'Times New Roman', 'size': 16, 'weight': 'bold'}
)
phase_ax.legend(
loc="lower left",
prop={'family': 'Times New Roman', 'size': 16, 'weight': 'bold'}
)