Files
ovf/main.py

136 lines
6.1 KiB
Python

import skrf as rf
from core.VFManager import VFManager
from core.utils import generate_starting_poles
from core.sample import auto_select_multple_ports
from core.basis.MultiPortOrthonormalBasis import MultiPortOrthonormalBasis
if __name__ == "__main__":
start_point = 0
id = 3000
network = rf.Network(f"/tmp/paramer/simulation/{id}/{id}.s2p")
# network = rf.data.ring_slot
ports = network.nports
K = 100
full_freqences = network.f[start_point:]
noised_sampled_points = network.y[start_point:,:,:].reshape(-1,ports,ports)
sampled_points = network.y[start_point:,:,:].reshape(-1,ports,ports)
# noised_sampled_points = network.y[start_point:,0,0].reshape(-1,1,1)
# sampled_points = network.y[start_point:,0,0].reshape(-1,1,1)
H,freqs = auto_select_multple_ports(noised_sampled_points,full_freqences,max_points=20)
vf = VFManager(npoles_cplx=2,freqs=freqs,H=H,model=MultiPortOrthonormalBasis,iterations=K,verbose=False)
model = vf.fit()
vf.plot_metrics(show=False,save_path="outputs")
model_responses = vf.get_model_responses(full_freqences)
vf.plot_model_responses(show=False,save_path="outputs")
# # Original plot functions
# Dt_1 = np.ones((len(freqs),1),np.complex128)
# # Levi step (no weighting):
# basis = MultiPortOrthonormalBasis(H,freqs,poles=poles)
# Dt = basis.Dt
# poles = basis.next_poles
# print("Levi step (no weighting):")
# print("A:",basis.A)
# print("B:",basis.B)
# print("C:",basis.C)
# print("D:",basis.D)
# print("next_pozles:",basis.next_poles)
# print("Dt:",Dt, "norm:",np.linalg.norm(Dt))
# # SK weighting (optional, after first pass):
# least_squares_condition = []
# least_squares_rms_error = []
# eigenval_condition = []
# eigenval_rms_error = []
# for i in range(K):
# basis = MultiPortOrthonormalBasis(H,freqs,poles=poles,weights=Dt)
# Dt_1 = Dt
# Dt = basis.Dt
# poles = basis.next_poles
# print(f"SK Iteration {i+1}/{K}")
# print("A:",basis.A)
# print("B:",basis.B)
# print("C:",basis.C)
# print("D:",basis.D)
# print("z:",basis.next_poles)
# print("Dt:",Dt)
# print("Dt/Dt-1",np.linalg.norm(Dt) / np.linalg.norm(Dt_1))
# least_squares_condition.append(basis.least_squares_condition)
# least_squares_rms_error.append(basis.least_squares_rms_error)
# eigenval_condition.append(basis.eigenval_condition)
# eigenval_rms_error.append(basis.eigenval_rms_error)
# # H11_evaluated = basis.evaluate_pole_residue(network.f[1:],poles,basis.C[0])
# H_evaluated = basis.get_model_responses(full_freqences)
# fitted_points = H_evaluated
# sliced_freqences = freqs
# input_points = H
# for i in range(ports):
# for j in range(ports):
# fig, axes = plt.subplots(3, 2, figsize=(15, 16), sharex=False)
# ax00 = axes[0][0]
# ax00.plot(full_freqences, np.abs(sampled_points[:,i,j]), 'o', ms=4, color='red', label='Samples')
# ax00.plot(full_freqences, np.abs(fitted_points[:,i,j]), '-', lw=2, color='k', label='Fit')
# ax00.plot(sliced_freqences, np.abs(input_points[:,i,j]), 'x', ms=4, color='blue', label='Input Samples')
# ax00.set_title(f"Response i={i+1}, j={j+1}")
# ax00.set_ylabel("Magnitude")
# ax00.legend(loc="best")
# ax01 = axes[0][1]
# ax01.set_title(f"Response i={i+1}, j={j+1}")
# ax01.set_ylabel("Phase (deg)")
# ax01.plot(full_freqences, np.angle(sampled_points[:,i,j],deg=True), 'o', ms=4, color='red', label='Samples')
# ax01.plot(full_freqences, np.angle(fitted_points[:,i,j],deg=True), '-', lw=2, color='k', label='Fit')
# ax01.plot(sliced_freqences, np.angle(input_points[:,i,j],deg=True), 'x', ms=4, color='blue', label='Input Samples')
# ax01.legend(loc="best")
# # ax00 = axes[0][0]
# # ax00.plot(full_freqences, np.real(sampled_points[:,i,j]), 'o', ms=4, color='red', label='Samples')
# # ax00.plot(full_freqences, np.real(fitted_points[:,i,j]), '-', lw=2, color='k', label='Fit')
# # ax00.plot(sliced_freqences, np.real(input_points[:,i,j]), 'x', ms=4, color='blue', label='Input Samples')
# # ax00.set_title(f"Response i={i+1}, j={j+1}")
# # ax00.set_ylabel("Real Part")
# # ax00.legend(loc="best")
# # ax01 = axes[0][1]
# # ax01.set_title(f"Response i={i+1}, j={j+1}")
# # ax01.set_ylabel("Imag Part")
# # ax01.plot(full_freqences, np.imag(sampled_points[:,i,j]), 'o', ms=4, color='red', label='Samples')
# # ax01.plot(full_freqences, np.imag(fitted_points[:,i,j]), '-', lw=2, color='k', label='Fit')
# # ax01.plot(sliced_freqences, np.imag(input_points[:,i,j]), 'x', ms=4, color='blue', label='Input Samples')
# # ax01.legend(loc="best")
# ax10 = axes[1][0]
# ax10.plot(least_squares_condition, label='Least Squares Condition')
# ax10.set_title("least_squares_condition")
# ax10.set_ylabel("Magnitude")
# ax10.legend(loc="best")
# ax11 = axes[1][1]
# ax11.plot(least_squares_rms_error, label='Least Squares RMS Error')
# ax11.set_title("least_squares_rms_error")
# ax11.set_ylabel("Magnitude")
# ax11.legend(loc="best")
# ax20 = axes[2][0]
# ax20.plot(eigenval_condition, label='Eigenvalue Condition')
# ax20.set_title("eigenval_condition")
# ax20.set_ylabel("Magnitude")
# ax20.legend(loc="best")
# ax21 = axes[2][1]
# ax21.plot(eigenval_rms_error, label='Eigenvalue RMS Error')
# ax21.set_title("eigenval_rms_error")
# ax21.set_ylabel("Magnitude")
# ax21.legend(loc="best")
# fig.tight_layout()
# plt.savefig(f"MultiplePortQR_port_{i+1}{j+1}.png")
# print(f"Saved MultiplePortQR_port_{i+1}{j+1}.png")