feat: 打包以及添加数据集
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
import numpy as np
|
||||
|
||||
# 创建一个复数矩阵
|
||||
A = np.array([[1+2j, 2-1j], [3+4j, 4+0j]])
|
||||
|
||||
Q, R = np.linalg.qr(A)
|
||||
|
||||
print("Q =\n", Q)
|
||||
print("R =\n", R)
|
||||
@@ -1,102 +0,0 @@
|
||||
import numpy as np
|
||||
from core.orthonormal_basis import generate_muntz_laguerre_basis
|
||||
|
||||
# ---------- 可选:离散再正交 (加权 QR) ----------
|
||||
# def trapezoid_weights(freqs: np.ndarray):
|
||||
# if len(freqs) == 1:
|
||||
# return np.ones(1)
|
||||
# df = np.diff(freqs)
|
||||
# w = np.zeros_like(freqs, dtype=float)
|
||||
# w[0] = 0.5 * df[0]
|
||||
# w[-1] = 0.5 * df[-1]
|
||||
# if len(freqs) > 2:
|
||||
# w[1:-1] = 0.5 * (df[:-1] + df[1:])
|
||||
# return w
|
||||
|
||||
def weighted_qr_from_basis(basis_cols: list[np.ndarray], weights: np.ndarray | None = None):
|
||||
A = np.column_stack(basis_cols) # (Nf, M)
|
||||
if weights is None:
|
||||
sw = np.ones(A.shape[0])
|
||||
else:
|
||||
sw = np.sqrt(weights.real)
|
||||
Aw = sw[:, None] * A
|
||||
Qw, R = np.linalg.qr(Aw)
|
||||
Phi = Qw / (sw[:, None] + 1e-30)
|
||||
return Phi, R # Raw = Phi R
|
||||
|
||||
def check_discrete_orthogonality(Phi: np.ndarray, w: np.ndarray):
|
||||
G = Phi.conj().T @ (w[:, None] * Phi)
|
||||
off = np.max(np.abs(G - np.eye(G.shape[0])))
|
||||
return G, off
|
||||
|
||||
def verify_orthonormal(Phi: np.ndarray, w: np.ndarray, atol=1e-10, rtol=1e-8):
|
||||
"""
|
||||
返回:
|
||||
G : Gram 矩阵 (Φ^H W Φ)
|
||||
max_off : 最大非对角幅值
|
||||
diag_err : max |diag(G)-1|
|
||||
passed : 是否满足阈值
|
||||
"""
|
||||
G = Phi.conj().T @ (w[:, None] * Phi)
|
||||
I = np.eye(G.shape[0])
|
||||
diag_err = np.max(np.abs(np.diag(G) - 1.0))
|
||||
max_off = np.max(np.abs(G - I + np.diag(np.diag(G) - 1.0)))
|
||||
passed = (diag_err <= atol) and (max_off <= rtol)
|
||||
return G, max_off, diag_err, passed
|
||||
|
||||
def omega_weights(freqs_hz: np.ndarray):
|
||||
"""
|
||||
基于 ω=2πf 的梯形法得到 w_ω = Δω/(2π),使得
|
||||
(1/2π) ∫_{-∞}^{∞} → Σ w_ω,k
|
||||
"""
|
||||
f = freqs_hz
|
||||
if len(f) == 1:
|
||||
return np.ones(1)
|
||||
df = np.diff(f)
|
||||
w_f = np.zeros_like(f)
|
||||
w_f[0] = 0.5 * df[0]
|
||||
w_f[-1] = 0.5 * df[-1]
|
||||
if len(f) > 2:
|
||||
w_f[1:-1] = 0.5 * (df[:-1] + df[1:])
|
||||
# dω = 2π df, (1/2π) * dω = df ⇒ 直接 w_f 就是 w_ω
|
||||
return w_f # 已等价于 Δω/(2π)
|
||||
|
||||
def evaluate(basis,freqs):
|
||||
w = omega_weights(freqs)
|
||||
Phi_num, R = weighted_qr_from_basis(basis, w)
|
||||
Gram, off = check_discrete_orthogonality(Phi_num, w)
|
||||
print("离散 Gram 最大非对角元素 =", off)
|
||||
print("R 形状:", R.shape)
|
||||
# 验证 Raw ≈ Phi R
|
||||
raw = np.column_stack(basis)
|
||||
err = np.max(np.abs(raw - Phi_num @ R))
|
||||
print("重构误差 ||Raw - Phi R||_∞ =", err)
|
||||
|
||||
# 验证正交性
|
||||
print("离散 Gram 矩阵 (前5x5):")
|
||||
print(Gram[:5, :5])
|
||||
|
||||
Gcheck, max_off, diag_err, ok = verify_orthonormal(Phi_num, w)
|
||||
print(f"Diag 误差={diag_err:.3e}, Max off={max_off:.3e}, Orthonormal={ok}")
|
||||
# 额外: 验证 R
|
||||
# raw = Φ R => R ≈ Φ^H W raw (因为 Φ^H W Φ = I)
|
||||
R_alt = Phi_num.conj().T @ (w[:,None] * raw)
|
||||
print("R 差异 ||R - R_alt||_max =", np.max(np.abs(R - R_alt)))
|
||||
|
||||
# ------------------ 示例 ------------------
|
||||
if __name__ == "__main__":
|
||||
# 示例稳定极点 (复对正虚部在前)
|
||||
init_poles = [
|
||||
-1.0e3 + 2.5e9j,
|
||||
-1.0e3 - 2.5e9j,
|
||||
]
|
||||
freqs = np.linspace(1e8, 8e9, 40)
|
||||
s = 1j * 2 * np.pi * freqs
|
||||
|
||||
basis = generate_muntz_laguerre_basis(s, init_poles)
|
||||
print("解析基函数数量 =", len(basis))
|
||||
print("基函数:")
|
||||
for i in range(len(basis)):
|
||||
print(f"φ_{i}:", basis[i][:])
|
||||
|
||||
evaluate(basis, freqs)
|
||||
@@ -1,60 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
from skrf import Network
|
||||
from core.freqency import auto_select_multple_ports
|
||||
from core.freqency import auto_select
|
||||
from core.vectorFitting import VectorFitting
|
||||
import numpy as np
|
||||
from typing import Literal
|
||||
import skrf as rf
|
||||
|
||||
def vector_fitting_info(vf:VectorFitting,sampled_freqs,sampled_responses,title:str='vf',parameter_type:Literal['y','s','z']='y'):
|
||||
for i in range(vf.network.nports):
|
||||
for j in range(vf.network.nports):
|
||||
rms_error = vf.get_rms_error(i,j,parameter_type=parameter_type)
|
||||
print(f'RMS Error Port {i+1} to Port {j+1}: {rms_error}')
|
||||
fitted_points = vf.get_model_response(i,j,sampled_freqs)
|
||||
if parameter_type=='y':
|
||||
input_points = vf.network.y[:,i,j]
|
||||
elif parameter_type=='s':
|
||||
input_points = vf.network.s[:,i,j]
|
||||
elif parameter_type=='z':
|
||||
input_points = vf.network.z[:,i,j]
|
||||
plt.figure(figsize=(20,12))
|
||||
plt.suptitle(f'{title} Port {i+1} to Port {j+1}')
|
||||
plt.subplot(2,2,1)
|
||||
plt.title('Magnitude')
|
||||
plt.plot(sampled_freqs,np.abs(sampled_responses[:,i,j]),'o', ms=4, color='red', label='Samples')
|
||||
plt.plot(vf.network.f,np.abs(input_points),'x', ms=4, color='blue', label='Input Samples')
|
||||
plt.plot(sampled_freqs,np.abs(fitted_points),'-', lw=2, color='k', label='Fit')
|
||||
plt.ylabel('Magnitude (dB)')
|
||||
plt.grid()
|
||||
plt.legend()
|
||||
plt.subplot(2,2,2)
|
||||
plt.title('Phase')
|
||||
plt.plot(sampled_freqs,np.angle(sampled_responses[:,i,j]),'o', ms=4, color='red', label='Samples')
|
||||
plt.plot(vf.network.f,np.angle(input_points),'x', ms=4, color='blue', label='Input Samples')
|
||||
plt.plot(sampled_freqs,np.angle(fitted_points),'-', lw=2, color='k', label='Fit')
|
||||
plt.ylabel('Phase (deg)')
|
||||
plt.xlabel('Frequency (GHz)')
|
||||
plt.grid()
|
||||
plt.legend()
|
||||
plt.subplot(2,2,3)
|
||||
plt.title('RMS_Error')
|
||||
plt.plot(rms_error, 'b', label='RMS Error')
|
||||
plt.ylabel('RMS Error (dB)')
|
||||
plt.grid()
|
||||
plt.legend()
|
||||
plt.savefig(f"outputs/{title.replace(' ','_')}_{i+1}_{j+1}.png")
|
||||
|
||||
|
||||
original_network = Network('/tmp/paramer/simulation/3000/3000.s2p')
|
||||
# original_network = rf.data.ring_slot
|
||||
|
||||
H,freqs = auto_select_multple_ports(original_network.y,original_network.f,max_points=20)
|
||||
fitted_network = Network(y=H,f=freqs)
|
||||
vf = VectorFitting(fitted_network)
|
||||
vf.vector_fit(n_poles_cmplx=2,n_poles_real=0,parameter_type='y')
|
||||
# vf.auto_fit(parameter_type='y')
|
||||
|
||||
vector_fitting_info(vf,original_network.f,original_network.y,parameter_type='y')
|
||||
|
||||
Reference in New Issue
Block a user