utils ¤
Utility functions: pole initialisation, data reshaping, weights, error.
Functions:
| Name | Description |
|---|---|
build_dk | Build the Dk basis matrix (Ns, N+offs) using the real representation. |
compute_cindex | Classify each pole for the real-valued LS representation. |
compute_rmserr | Root-mean-square error: sqrt(sum|H - Hfit|² / (Nc² * Ns)). |
compute_weights | Compute fitting weights for the selected matrix elements. |
init_poles_lincmplx | N/2 complex conjugate pairs, linearly spaced over the frequency range. |
init_poles_linlogcmplx | N/2 complex pairs: first N/4 linearly spaced, next N/4 logarithmically spaced. |
init_poles_logcmplx | N/2 complex conjugate pairs, logarithmically spaced over the frequency range. |
sort_poles | Sort poles: real poles first, then complex pairs sorted by magnitude. |
stack_upper_triangle | Stack upper-triangle elements of H into a (nnn, Ns) matrix. |
unstack_upper_triangle | Reconstruct symmetric H from upper-triangle elements. |
build_dk ¤
Build the Dk basis matrix (Ns, N+offs) using the real representation.
For real pole (cindex[m]==0): Dk[:, m] = 1/(s - poles[m]) For first of complex pair (cindex[m]==1): Dk[:, m] = 1/(s-a) + 1/(s-a) (= 2Re(1/(s-a))) Dk[:, m+1] = i/(s-a) - i/(s-a) (= -2Im(1/(s-a))*i... see below) For second of pair (cindex[m]==2): (column already set by m-1 step above)
Constant and s terms (for asymp): Dk[:, N] = 1 (if offs >= 1) Dk[:, N+1] = s (if offs >= 2)
Complex pairs: poles[m+1] = conj(poles[m]) so dk_conj[:,m] = dk_direct[:,m+1].
Source code in circulax/fitting/utils.py
compute_cindex ¤
Classify each pole for the real-valued LS representation.
Returns cindex array where
0 = real pole 1 = first pole of a complex conjugate pair 2 = second pole of a complex conjugate pair (conjugate of the previous)
Equivalent to the cindex computation in vectfit4.m.
Source code in circulax/fitting/utils.py
compute_rmserr ¤
Root-mean-square error: sqrt(sum|H - Hfit|² / (Nc² * Ns)).
compute_weights ¤
Compute fitting weights for the selected matrix elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
H | ndarray | (Nc, Nc, Ns) complex — full matrix data. | required |
weightparam | int | 1=uniform, 2=1/|H_ij|, 3=1/√|H_ij|, 4=1/‖H‖F, 5=1/√‖H‖F. | required |
reciprocal | bool | Use only one triangle when true, or every ordered matrix element when false. | True |
Returns:
| Name | Type | Description |
|---|---|---|
weight | ndarray | (nresponses, Ns) or (1, Ns) real — per-element weights. |
Source code in circulax/fitting/utils.py
init_poles_lincmplx ¤
N/2 complex conjugate pairs, linearly spaced over the frequency range.
Source code in circulax/fitting/utils.py
init_poles_linlogcmplx ¤
N/2 complex pairs: first N/4 linearly spaced, next N/4 logarithmically spaced.
Matches the default pole generation in VFdriver.m.
Source code in circulax/fitting/utils.py
init_poles_logcmplx ¤
N/2 complex conjugate pairs, logarithmically spaced over the frequency range.
Source code in circulax/fitting/utils.py
sort_poles ¤
Sort poles: real poles first, then complex pairs sorted by magnitude.
Within each conjugate pair the negative-imaginary pole is first (matching MATLAB's sort-by-magnitude + sort-by-phase-angle behaviour). This ordering is required by compute_cindex which assumes pairs appear as (a-ib, a+ib).
Also applies conjugation cleanup: poles with negligible imaginary parts become exactly real (matching vectfit4 line 530: roetter - 2i*imag(roetter)).
Source code in circulax/fitting/utils.py
stack_upper_triangle ¤
Stack upper-triangle elements of H into a (nnn, Ns) matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
H | ndarray | (Nc, Nc, Ns) complex — frequency-domain matrix data. | required |
Returns:
| Name | Type | Description |
|---|---|---|
f | ndarray | (nnn, Ns) complex where nnn = Nc*(Nc+1)//2. |
Source code in circulax/fitting/utils.py
unstack_upper_triangle ¤
Reconstruct symmetric H from upper-triangle elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f | ndarray | (nnn, Ns) complex. | required |
Nc | int | number of ports. | required |
Returns:
| Name | Type | Description |
|---|---|---|
H | ndarray | (Nc, Nc, Ns) complex — symmetric. |