indierf-schematic-mcp 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +91 -0
- package/README.md +96 -2
- package/lib/analysis/plot-axis.ts +346 -0
- package/lib/cascade/filter-prototype.ts +442 -0
- package/lib/coax/cable-copy.ts +71 -0
- package/lib/coax/cables.ts +1366 -0
- package/lib/coax/explorer.ts +386 -0
- package/lib/coax/fit.ts +78 -0
- package/lib/coax/format.ts +85 -0
- package/lib/coax/loss.ts +150 -0
- package/lib/coax/plot.ts +72 -0
- package/lib/filter/ladder/built-form.ts +133 -0
- package/lib/filter/ladder/forms.ts +492 -0
- package/lib/filter/ladder/index.ts +110 -0
- package/lib/filter/ladder/linear-phase.ts +52 -0
- package/lib/filter/ladder/poly.ts +209 -0
- package/lib/filter/ladder/prototype.ts +91 -0
- package/lib/filter/ladder/synthesis.ts +431 -0
- package/lib/filter/ladder/terminations.ts +212 -0
- package/lib/filter/part-table.ts +317 -0
- package/lib/filter/roots.ts +305 -0
- package/lib/filter/stop-floor.ts +15 -0
- package/lib/import/delimited.ts +260 -0
- package/lib/import/types.ts +42 -0
- package/lib/match/cx.ts +72 -0
- package/lib/match/eseries.ts +166 -0
- package/lib/match/units.ts +178 -0
- package/lib/reflection/metrics.ts +118 -0
- package/lib/rf-engine/math.ts +434 -0
- package/lib/schematic/export/sparams.ts +323 -0
- package/lib/schematic/headless/node.ts +103 -0
- package/lib/schematic/headless/source.ts +97 -0
- package/lib/schematic/headless/tools.ts +945 -0
- package/lib/schematic/optimize/apply.ts +109 -0
- package/lib/schematic/optimize/differential-evolution.ts +129 -0
- package/lib/schematic/optimize/fold.ts +5 -0
- package/lib/schematic/optimize/goals.ts +483 -0
- package/lib/schematic/optimize/levenberg-marquardt.ts +178 -0
- package/lib/schematic/optimize/nelder-mead.ts +145 -0
- package/lib/schematic/optimize/optimize.ts +883 -0
- package/lib/schematic/optimize/random-search.ts +70 -0
- package/lib/schematic/optimize/runtime-host.ts +155 -0
- package/lib/schematic/sim/ac-results.ts +144 -0
- package/lib/schematic/sim/cache-key.ts +170 -0
- package/lib/schematic/sim/data-provider.ts +14 -0
- package/lib/schematic/sim/dc-results.ts +186 -0
- package/lib/schematic/sim/hash.ts +5 -0
- package/lib/schematic/sim/hb-results.ts +600 -0
- package/lib/schematic/sim/load-pull.ts +627 -0
- package/lib/schematic/sim/result-cache.ts +289 -0
- package/lib/schematic/sim/results.ts +185 -0
- package/lib/schematic/sim/runtime.ts +1003 -0
- package/lib/schematic/sim/stb-results.ts +21 -0
- package/lib/schematic/storage/file-name.ts +15 -0
- package/lib/schematic/yield/boards.ts +265 -0
- package/lib/schematic/yield/host.ts +120 -0
- package/lib/schematic/yield/knobs.ts +170 -0
- package/lib/schematic/yield/stats.ts +104 -0
- package/lib/schematic/yield/yield.ts +339 -0
- package/lib/schematic-doc/designs.ts +50 -0
- package/lib/schematic-doc/document.ts +374 -0
- package/lib/schematic-doc/equalizer-synthesis.ts +1039 -0
- package/lib/schematic-doc/exact-decimal.ts +47 -0
- package/lib/schematic-doc/filter-synthesis.ts +1745 -0
- package/lib/schematic-doc/fit-to-data.ts +442 -0
- package/lib/schematic-doc/fixes.ts +802 -0
- package/lib/schematic-doc/flatten.ts +787 -0
- package/lib/schematic-doc/goal-ripple.ts +48 -0
- package/lib/schematic-doc/ids.ts +18 -0
- package/lib/schematic-doc/ir.ts +97 -0
- package/lib/schematic-doc/layout-map.ts +317 -0
- package/lib/schematic-doc/layout-twin.ts +47 -0
- package/lib/schematic-doc/layout.ts +444 -0
- package/lib/schematic-doc/make-subcircuit.ts +463 -0
- package/lib/schematic-doc/migrate-line-pins.ts +317 -0
- package/lib/schematic-doc/migrations.ts +53 -0
- package/lib/schematic-doc/mixed-mode.ts +84 -0
- package/lib/schematic-doc/model-card.ts +542 -0
- package/lib/schematic-doc/models.ts +304 -0
- package/lib/schematic-doc/names.ts +59 -0
- package/lib/schematic-doc/netlist-text.ts +172 -0
- package/lib/schematic-doc/netlister.ts +273 -0
- package/lib/schematic-doc/opamp-extension.ts +376 -0
- package/lib/schematic-doc/ops/apply.ts +2162 -0
- package/lib/schematic-doc/ops/index.ts +19 -0
- package/lib/schematic-doc/ops/marker-search.ts +87 -0
- package/lib/schematic-doc/ops/schema.ts +1062 -0
- package/lib/schematic-doc/ops/transact.ts +168 -0
- package/lib/schematic-doc/optimizer-objective.ts +31 -0
- package/lib/schematic-doc/order.ts +65 -0
- package/lib/schematic-doc/sat-tilt.ts +68 -0
- package/lib/schematic-doc/schema.ts +643 -0
- package/lib/schematic-doc/share.ts +70 -0
- package/lib/schematic-doc/sp-step.ts +108 -0
- package/lib/schematic-doc/spice-model.ts +194 -0
- package/lib/schematic-doc/spice-subckt.ts +1483 -0
- package/lib/schematic-doc/subckt-import.ts +183 -0
- package/lib/schematic-doc/substrate-block.ts +109 -0
- package/lib/schematic-doc/swap-em.ts +312 -0
- package/lib/schematic-doc/swap.ts +168 -0
- package/lib/schematic-doc/targets.ts +559 -0
- package/lib/schematic-doc/test-states.ts +335 -0
- package/lib/schematic-doc/tolerance.ts +333 -0
- package/lib/schematic-engine/node-engine.ts +51 -0
- package/lib/schematic-engine/pkg/rfsim_wasm.js +748 -0
- package/lib/schematic-engine/pkg/rfsim_wasm_bg.wasm +0 -0
- package/lib/schematic-engine/protocol.ts +1074 -0
- package/lib/schematic-engine/quantities.ts +863 -0
- package/lib/schematic-engine/worker-core.ts +707 -0
- package/lib/schematic-engine/worker-inline.ts +69 -0
- package/lib/schematic-engine/worker-pool.ts +1042 -0
- package/lib/schematic-library/param-meaning.ts +75 -0
- package/lib/schematic-library/param-visibility.ts +110 -0
- package/lib/schematic-library/pin-geometry.ts +354 -0
- package/lib/schematic-library/planned-models.ts +18 -0
- package/lib/schematic-library/schemas.gen.ts +6857 -0
- package/lib/schematic-library/substrates.ts +24 -0
- package/lib/schematic-library/transform.ts +80 -0
- package/lib/share/base64url.ts +25 -0
- package/lib/share/compress.ts +103 -0
- package/lib/sparam/complex.ts +129 -0
- package/lib/sparam/convert.ts +223 -0
- package/lib/sparam/format.ts +160 -0
- package/lib/sparam/parse-touchstone.ts +1683 -0
- package/lib/sparam/renormalize.ts +100 -0
- package/lib/sparam/tags.ts +80 -0
- package/lib/sparam/write-touchstone.ts +194 -0
- package/mcp/schematic/package.json +5 -0
- package/mcp/schematic/server.mjs +112 -0
- package/mcp/schematic/tools.mjs +265 -0
- package/package.json +33 -4
- package/scripts/lib/ts-hooks.mjs +152 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure prototype maths of a filter shape: the analog low-pass prototypes
|
|
3
|
+
* as poles, zeros and gain, their attenuation, dissipation and transmission
|
|
4
|
+
* zeros. Split out of filter-shape.ts (card FB-11) so the Filter Bench and
|
|
5
|
+
* Insert Filter, which reach this from lib/schematic-doc, import nothing of
|
|
6
|
+
* Cascade's parameter models, block types, components or store. It imports
|
|
7
|
+
* nothing at all. filter-shape.ts re-exports every name here, so Cascade's
|
|
8
|
+
* imports are unchanged; the conventions are documented there.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type FilterShapeType = "lowpass" | "highpass" | "bandpass" | "bandstop";
|
|
12
|
+
export type FilterFamily = "butterworth" | "chebyshev" | "chebyshev2" | "elliptic" | "bessel";
|
|
13
|
+
|
|
14
|
+
export interface FilterShape {
|
|
15
|
+
type: FilterShapeType;
|
|
16
|
+
family: FilterFamily;
|
|
17
|
+
/** Prototype order, 1 to 12. */
|
|
18
|
+
order: number;
|
|
19
|
+
/** Passband ripple, dB: Chebyshev I and elliptic. Ignored otherwise. */
|
|
20
|
+
ripple_dB?: number;
|
|
21
|
+
/** Minimum stopband attenuation, dB: Chebyshev II and elliptic. Ignored otherwise. */
|
|
22
|
+
stopband_dB?: number;
|
|
23
|
+
/** Unloaded Q of every resonator. Absent means lossless. */
|
|
24
|
+
unloadedQ?: number;
|
|
25
|
+
/** Low-pass and high-pass edge, Hz. */
|
|
26
|
+
cutoff_Hz?: number;
|
|
27
|
+
/** Band-pass and band-stop geometric centre, Hz. */
|
|
28
|
+
center_Hz?: number;
|
|
29
|
+
/** Band-pass and band-stop edge-to-edge width, Hz. */
|
|
30
|
+
bandwidth_Hz?: number;
|
|
31
|
+
/** Loss in the passband, dB, added at every frequency. */
|
|
32
|
+
passbandLoss_dB: number;
|
|
33
|
+
/** The most the stopband rises above the passband loss, dB. */
|
|
34
|
+
maxRejection_dB: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const isBand = (type: FilterShapeType) =>
|
|
38
|
+
type === "bandpass" || type === "bandstop";
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Analog prototypes as poles, zeros and gain.
|
|
42
|
+
|
|
43
|
+
export interface Complex {
|
|
44
|
+
re: number;
|
|
45
|
+
im: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** H(S) = gain · Π(S − zero) / Π(S − pole), edge at Ω = 1. */
|
|
49
|
+
export interface FilterPrototype {
|
|
50
|
+
zeros: Complex[];
|
|
51
|
+
poles: Complex[];
|
|
52
|
+
gain: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const pow10m1 = (x: number) => Math.expm1(x * Math.LN10);
|
|
56
|
+
|
|
57
|
+
function productOfNegated(roots: readonly Complex[]): Complex {
|
|
58
|
+
let re = 1;
|
|
59
|
+
let im = 0;
|
|
60
|
+
for (const r of roots) {
|
|
61
|
+
const nr = -r.re;
|
|
62
|
+
const ni = -r.im;
|
|
63
|
+
const next = re * nr - im * ni;
|
|
64
|
+
im = re * ni + im * nr;
|
|
65
|
+
re = next;
|
|
66
|
+
}
|
|
67
|
+
return { re, im };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function reciprocal(c: Complex): Complex {
|
|
71
|
+
const d = c.re * c.re + c.im * c.im;
|
|
72
|
+
return { re: c.re / d, im: -c.im / d };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Gain making |H(0)| one, or 1/√(1 + ε²) for an even ripple prototype. */
|
|
76
|
+
function dcGain(zeros: Complex[], poles: Complex[], evenRippleEps2 = 0): number {
|
|
77
|
+
const num = productOfNegated(poles).re;
|
|
78
|
+
const den = zeros.length ? productOfNegated(zeros).re : 1;
|
|
79
|
+
const k = num / den;
|
|
80
|
+
return evenRippleEps2 > 0 ? k / Math.sqrt(1 + evenRippleEps2) : k;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function butterworthPrototype(n: number): FilterPrototype {
|
|
84
|
+
const poles: Complex[] = [];
|
|
85
|
+
for (let k = 1; k <= n; k += 1) {
|
|
86
|
+
const theta = ((2 * k - 1) * Math.PI) / (2 * n);
|
|
87
|
+
poles.push({ re: -Math.sin(theta), im: Math.cos(theta) });
|
|
88
|
+
}
|
|
89
|
+
return { zeros: [], poles, gain: dcGain([], poles) };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function chebyshev1Prototype(n: number, ripple_dB: number): FilterPrototype {
|
|
93
|
+
const eps2 = pow10m1(ripple_dB / 10);
|
|
94
|
+
const mu = Math.asinh(1 / Math.sqrt(eps2)) / n;
|
|
95
|
+
const poles: Complex[] = [];
|
|
96
|
+
for (let k = 1; k <= n; k += 1) {
|
|
97
|
+
const theta = ((2 * k - 1) * Math.PI) / (2 * n);
|
|
98
|
+
poles.push({ re: -Math.sinh(mu) * Math.sin(theta), im: Math.cosh(mu) * Math.cos(theta) });
|
|
99
|
+
}
|
|
100
|
+
return { zeros: [], poles, gain: dcGain([], poles, n % 2 === 0 ? eps2 : 0) };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Stopband edge Ω_s of the −3 dB-normalised Chebyshev II, where the loss
|
|
105
|
+
* first reaches the stopband value: cosh(acosh(√(10^(rs/10) − 1)) / n).
|
|
106
|
+
*/
|
|
107
|
+
export function chebyshev2StopOmega(n: number, stopband_dB: number): number {
|
|
108
|
+
return Math.cosh(Math.acosh(Math.sqrt(pow10m1(stopband_dB / 10))) / n);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* scipy's cheb2ap (stopband edge at 1), then every root times Ω_s so the
|
|
113
|
+
* −3 dB point sits at 1: H(S) = H_cheb2ap(S/Ω_s).
|
|
114
|
+
*/
|
|
115
|
+
function chebyshev2Prototype(n: number, stopband_dB: number): FilterPrototype {
|
|
116
|
+
const de = 1 / Math.sqrt(pow10m1(stopband_dB / 10));
|
|
117
|
+
const mu = Math.asinh(1 / de) / n;
|
|
118
|
+
const scale = chebyshev2StopOmega(n, stopband_dB);
|
|
119
|
+
const zeros: Complex[] = [];
|
|
120
|
+
const poles: Complex[] = [];
|
|
121
|
+
for (let m = -n + 1; m < n; m += 2) {
|
|
122
|
+
const angle = (Math.PI * m) / (2 * n);
|
|
123
|
+
if (m !== 0) zeros.push({ re: 0, im: scale / Math.sin(angle) });
|
|
124
|
+
// −exp(jπm/2n), stretched by sinh μ and cosh μ, then inverted.
|
|
125
|
+
const p = reciprocal({ re: -Math.sinh(mu) * Math.cos(angle), im: -Math.cosh(mu) * Math.sin(angle) });
|
|
126
|
+
poles.push({ re: p.re * scale, im: p.im * scale });
|
|
127
|
+
}
|
|
128
|
+
return { zeros, poles, gain: dcGain(zeros, poles) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Arithmetic-geometric mean. */
|
|
132
|
+
function agm(a: number, b: number): number {
|
|
133
|
+
for (let i = 0; i < 64 && Math.abs(a - b) > 1e-16 * a; i += 1) {
|
|
134
|
+
const next = (a + b) / 2;
|
|
135
|
+
b = Math.sqrt(a * b);
|
|
136
|
+
a = next;
|
|
137
|
+
}
|
|
138
|
+
return (a + b) / 2;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Complete elliptic integral K from its complementary parameter m' = 1 − m. */
|
|
142
|
+
export const ellipKFromComplement = (mc: number) => Math.PI / (2 * agm(1, Math.sqrt(mc)));
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Jacobi sn, cn, dn of real u at parameter m (complement mc = 1 − m given
|
|
146
|
+
* separately so m near 1 keeps its digits): the AGM and descending Landen
|
|
147
|
+
* steps of Abramowitz and Stegun 16.4, as in Cephes ellpj.
|
|
148
|
+
*/
|
|
149
|
+
export function jacobi(u: number, m: number, mc: number): { sn: number; cn: number; dn: number } {
|
|
150
|
+
const a = [1];
|
|
151
|
+
const c = [Math.sqrt(m)];
|
|
152
|
+
let b = Math.sqrt(mc);
|
|
153
|
+
let twon = 1;
|
|
154
|
+
let i = 0;
|
|
155
|
+
while (Math.abs(c[i] / a[i]) > 2.2e-16 && i < 24) {
|
|
156
|
+
const ai = a[i];
|
|
157
|
+
i += 1;
|
|
158
|
+
c[i] = (ai - b) / 2;
|
|
159
|
+
const t = Math.sqrt(ai * b);
|
|
160
|
+
a[i] = (ai + b) / 2;
|
|
161
|
+
b = t;
|
|
162
|
+
twon *= 2;
|
|
163
|
+
}
|
|
164
|
+
let phi = twon * a[i] * u;
|
|
165
|
+
let prev = phi;
|
|
166
|
+
for (; i > 0; i -= 1) {
|
|
167
|
+
const t = (c[i] * Math.sin(phi)) / a[i];
|
|
168
|
+
prev = phi;
|
|
169
|
+
phi = (Math.asin(t) + phi) / 2;
|
|
170
|
+
}
|
|
171
|
+
const sn = Math.sin(phi);
|
|
172
|
+
const cn = Math.cos(phi);
|
|
173
|
+
const dn = prev === phi ? 1 : cn / Math.cos(prev - phi);
|
|
174
|
+
return { sn, cn, dn };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The elliptic degree equation by nomes (Orfanidis eq. 49): the m with
|
|
179
|
+
* n·K(m)/K'(m) = K(m1)/K'(m1), as scipy's _ellipdeg, which stops the theta
|
|
180
|
+
* series at seven terms; here they run until the terms vanish (it matters
|
|
181
|
+
* only where the transition band is under 1e-6 of the edge).
|
|
182
|
+
*/
|
|
183
|
+
export function ellipticDegree(n: number, m1: number): { m: number; mc: number } {
|
|
184
|
+
const k1 = ellipKFromComplement(1 - m1);
|
|
185
|
+
const k1p = ellipKFromComplement(m1);
|
|
186
|
+
const q = Math.exp((-Math.PI * k1p) / k1 / n);
|
|
187
|
+
let num = 0; // Σ q^(j(j+1)), j ≥ 0
|
|
188
|
+
let den = 1; // 1 + 2Σ q^(j²), j ≥ 1
|
|
189
|
+
for (let j = 0; j < 64; j += 1) {
|
|
190
|
+
const t2 = q ** (j * (j + 1));
|
|
191
|
+
const t3 = j > 0 ? q ** (j * j) : 0;
|
|
192
|
+
num += t2;
|
|
193
|
+
den += 2 * t3;
|
|
194
|
+
if (t2 < 1e-18 && j > 0 && t3 < 1e-18) break;
|
|
195
|
+
}
|
|
196
|
+
const m = 16 * q * (num / den) ** 4;
|
|
197
|
+
return { m, mc: 1 - m };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Real z with w = sc(z, 1 − m), from the inverse sn of an imaginary argument
|
|
202
|
+
* by descending Landen steps (scipy's _arc_jac_sc1, Orfanidis eq. 56). An
|
|
203
|
+
* imaginary argument stays imaginary through every step, so this is real.
|
|
204
|
+
*/
|
|
205
|
+
function inverseJacobiSc1(w: number, m: number): number {
|
|
206
|
+
const complement = (x: number) => Math.sqrt((1 - x) * (1 + x));
|
|
207
|
+
const ks = [Math.sqrt(m)];
|
|
208
|
+
while (ks[ks.length - 1] !== 0 && ks.length < 64) {
|
|
209
|
+
const kp = complement(ks[ks.length - 1]);
|
|
210
|
+
ks.push((1 - kp) / (1 + kp));
|
|
211
|
+
}
|
|
212
|
+
let K = Math.PI / 2;
|
|
213
|
+
for (let i = 1; i < ks.length; i += 1) K *= 1 + ks[i];
|
|
214
|
+
let y = w; // the argument is j·y
|
|
215
|
+
for (let i = 0; i + 1 < ks.length; i += 1) {
|
|
216
|
+
const kn = ks[i];
|
|
217
|
+
y = (2 * y) / ((1 + ks[i + 1]) * (1 + Math.sqrt(1 + kn * kn * y * y)));
|
|
218
|
+
}
|
|
219
|
+
return ((K * 2) / Math.PI) * Math.asinh(y);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** scipy's ellipap: ripple edge at 1, stopband from 1/√m. */
|
|
223
|
+
function ellipticPrototype(n: number, ripple_dB: number, stopband_dB: number): FilterPrototype {
|
|
224
|
+
const eps2 = pow10m1(ripple_dB / 10);
|
|
225
|
+
if (n === 1) {
|
|
226
|
+
const p = -Math.sqrt(1 / eps2);
|
|
227
|
+
return { zeros: [], poles: [{ re: p, im: 0 }], gain: -p };
|
|
228
|
+
}
|
|
229
|
+
const eps = Math.sqrt(eps2);
|
|
230
|
+
const ck1 = eps2 / pow10m1(stopband_dB / 10);
|
|
231
|
+
const K1 = ellipKFromComplement(1 - ck1);
|
|
232
|
+
const { m, mc } = ellipticDegree(n, ck1);
|
|
233
|
+
const capk = ellipKFromComplement(mc);
|
|
234
|
+
const r = inverseJacobiSc1(1 / eps, ck1);
|
|
235
|
+
const v0 = (capk * r) / (n * K1);
|
|
236
|
+
const { sn: sv, cn: cv, dn: dv } = jacobi(v0, mc, m);
|
|
237
|
+
const zeros: Complex[] = [];
|
|
238
|
+
const poles: Complex[] = [];
|
|
239
|
+
for (let j = 1 - (n % 2); j < n; j += 2) {
|
|
240
|
+
const { sn: s, cn: c, dn: d } = jacobi((j * capk) / n, m, mc);
|
|
241
|
+
if (Math.abs(s) > 1e-16) {
|
|
242
|
+
const z = 1 / (Math.sqrt(m) * s);
|
|
243
|
+
zeros.push({ re: 0, im: z }, { re: 0, im: -z });
|
|
244
|
+
}
|
|
245
|
+
const den = 1 - (d * sv) ** 2;
|
|
246
|
+
const p = { re: -(c * d * sv * cv) / den, im: -(s * dv) / den };
|
|
247
|
+
poles.push(p);
|
|
248
|
+
if (j !== 0) poles.push({ re: p.re, im: -p.im });
|
|
249
|
+
}
|
|
250
|
+
return { zeros, poles, gain: dcGain(zeros, poles, n % 2 === 0 ? eps2 : 0) };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Stopband edge of the elliptic prototype, 1/√m. */
|
|
254
|
+
export function ellipticStopOmega(n: number, ripple_dB: number, stopband_dB: number): number {
|
|
255
|
+
if (n === 1) {
|
|
256
|
+
// First order: 10·log10(1 + ε²Ω²) reaches the stopband value here.
|
|
257
|
+
return Math.sqrt(pow10m1(stopband_dB / 10) / pow10m1(ripple_dB / 10));
|
|
258
|
+
}
|
|
259
|
+
const ck1 = pow10m1(ripple_dB / 10) / pow10m1(stopband_dB / 10);
|
|
260
|
+
return 1 / Math.sqrt(ellipticDegree(n, ck1).m);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Coefficients a_k, k = 0..n, of the reverse Bessel polynomial θ_n. */
|
|
264
|
+
function reverseBesselCoefficients(n: number): number[] {
|
|
265
|
+
const fact = (k: number) => {
|
|
266
|
+
let out = BigInt(1);
|
|
267
|
+
for (let i = 2; i <= k; i += 1) out *= BigInt(i);
|
|
268
|
+
return out;
|
|
269
|
+
};
|
|
270
|
+
const out: number[] = [];
|
|
271
|
+
for (let k = 0; k <= n; k += 1) {
|
|
272
|
+
const value = fact(2 * n - k) / (BigInt(2) ** BigInt(n - k) * fact(k) * fact(n - k));
|
|
273
|
+
out.push(Number(value));
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function evalPolynomial(coefficients: readonly number[], s: Complex): { p: Complex; dp: Complex } {
|
|
279
|
+
let pr = 0;
|
|
280
|
+
let pi = 0;
|
|
281
|
+
let dr = 0;
|
|
282
|
+
let di = 0;
|
|
283
|
+
for (let k = coefficients.length - 1; k >= 0; k -= 1) {
|
|
284
|
+
// dp = dp·s + p, p = p·s + a_k
|
|
285
|
+
const ndr = dr * s.re - di * s.im + pr;
|
|
286
|
+
di = dr * s.im + di * s.re + pi;
|
|
287
|
+
dr = ndr;
|
|
288
|
+
const npr = pr * s.re - pi * s.im + coefficients[k];
|
|
289
|
+
pi = pr * s.im + pi * s.re;
|
|
290
|
+
pr = npr;
|
|
291
|
+
}
|
|
292
|
+
return { p: { re: pr, im: pi }, dp: { re: dr, im: di } };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Bessel (Thomson) poles: the roots of θ_n by Durand-Kerner and a Newton
|
|
297
|
+
* polish, divided by the frequency where |H| = 1/√2 (scipy norm="mag").
|
|
298
|
+
*/
|
|
299
|
+
function besselPrototype(n: number): FilterPrototype {
|
|
300
|
+
const a = reverseBesselCoefficients(n);
|
|
301
|
+
const radius = a[0] ** (1 / n);
|
|
302
|
+
let roots: Complex[] = [];
|
|
303
|
+
for (let i = 0; i < n; i += 1) {
|
|
304
|
+
const angle = (2 * Math.PI * i) / n + 0.4;
|
|
305
|
+
roots.push({ re: radius * Math.cos(angle), im: radius * Math.sin(angle) });
|
|
306
|
+
}
|
|
307
|
+
for (let iteration = 0; iteration < 2000; iteration += 1) {
|
|
308
|
+
let moved = 0;
|
|
309
|
+
const next = roots.map((root, i) => {
|
|
310
|
+
const { p } = evalPolynomial(a, root);
|
|
311
|
+
let dr = 1;
|
|
312
|
+
let di = 0;
|
|
313
|
+
roots.forEach((other, j) => {
|
|
314
|
+
if (j === i) return;
|
|
315
|
+
const xr = root.re - other.re;
|
|
316
|
+
const xi = root.im - other.im;
|
|
317
|
+
const t = dr * xr - di * xi;
|
|
318
|
+
di = dr * xi + di * xr;
|
|
319
|
+
dr = t;
|
|
320
|
+
});
|
|
321
|
+
const d = dr * dr + di * di;
|
|
322
|
+
const stepR = (p.re * dr + p.im * di) / d;
|
|
323
|
+
const stepI = (p.im * dr - p.re * di) / d;
|
|
324
|
+
moved = Math.max(moved, Math.hypot(stepR, stepI) / Math.max(Math.hypot(root.re, root.im), 1e-300));
|
|
325
|
+
return { re: root.re - stepR, im: root.im - stepI };
|
|
326
|
+
});
|
|
327
|
+
roots = next;
|
|
328
|
+
if (moved < 1e-15) break;
|
|
329
|
+
}
|
|
330
|
+
roots = roots.map((root) => {
|
|
331
|
+
let r = root;
|
|
332
|
+
for (let k = 0; k < 4; k += 1) {
|
|
333
|
+
const { p, dp } = evalPolynomial(a, r);
|
|
334
|
+
const d = dp.re * dp.re + dp.im * dp.im;
|
|
335
|
+
if (d === 0) break;
|
|
336
|
+
r = { re: r.re - (p.re * dp.re + p.im * dp.im) / d, im: r.im - (p.im * dp.re - p.re * dp.im) / d };
|
|
337
|
+
}
|
|
338
|
+
// Real-coefficient roots: snap a real root's imaginary dust to zero.
|
|
339
|
+
return Math.abs(r.im) < 1e-14 * Math.hypot(r.re, r.im) ? { re: r.re, im: 0 } : r;
|
|
340
|
+
});
|
|
341
|
+
// Scale so the loss at Ω = 1 is 10·log10(2): bisect on the monotone loss.
|
|
342
|
+
const target = 10 * Math.log10(2);
|
|
343
|
+
const lossAt = (w: number) => {
|
|
344
|
+
let sum = 0;
|
|
345
|
+
for (const p of roots) sum += Math.log10(((p.re * p.re + (w - p.im) ** 2) / (p.re * p.re + p.im * p.im)));
|
|
346
|
+
return 10 * sum;
|
|
347
|
+
};
|
|
348
|
+
let lo = 0;
|
|
349
|
+
let hi = 1;
|
|
350
|
+
while (lossAt(hi) < target) hi *= 2;
|
|
351
|
+
for (let k = 0; k < 200 && hi - lo > 1e-16 * hi; k += 1) {
|
|
352
|
+
const mid = (lo + hi) / 2;
|
|
353
|
+
if (lossAt(mid) < target) lo = mid;
|
|
354
|
+
else hi = mid;
|
|
355
|
+
}
|
|
356
|
+
const w3 = (lo + hi) / 2;
|
|
357
|
+
const poles = roots.map((p) => ({ re: p.re / w3, im: p.im / w3 }));
|
|
358
|
+
return { zeros: [], poles, gain: dcGain([], poles) };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const prototypeCache = new Map<string, FilterPrototype>();
|
|
362
|
+
|
|
363
|
+
/** The shape's normalised low-pass prototype (cached by its defining numbers). */
|
|
364
|
+
export function filterPrototype(shape: FilterShape): FilterPrototype {
|
|
365
|
+
const n = shape.order;
|
|
366
|
+
const ripple = shape.ripple_dB ?? 0.1;
|
|
367
|
+
const stop = shape.stopband_dB ?? 40;
|
|
368
|
+
const key =
|
|
369
|
+
shape.family === "butterworth" || shape.family === "bessel"
|
|
370
|
+
? `${shape.family}|${n}`
|
|
371
|
+
: shape.family === "chebyshev"
|
|
372
|
+
? `${shape.family}|${n}|${ripple}`
|
|
373
|
+
: shape.family === "chebyshev2"
|
|
374
|
+
? `${shape.family}|${n}|${stop}`
|
|
375
|
+
: `${shape.family}|${n}|${ripple}|${stop}`;
|
|
376
|
+
const cached = prototypeCache.get(key);
|
|
377
|
+
if (cached) return cached;
|
|
378
|
+
const built =
|
|
379
|
+
shape.family === "butterworth"
|
|
380
|
+
? butterworthPrototype(n)
|
|
381
|
+
: shape.family === "chebyshev"
|
|
382
|
+
? chebyshev1Prototype(n, ripple)
|
|
383
|
+
: shape.family === "chebyshev2"
|
|
384
|
+
? chebyshev2Prototype(n, stop)
|
|
385
|
+
: shape.family === "elliptic"
|
|
386
|
+
? ellipticPrototype(n, ripple, stop)
|
|
387
|
+
: besselPrototype(n);
|
|
388
|
+
if (prototypeCache.size > 256) prototypeCache.clear();
|
|
389
|
+
prototypeCache.set(key, built);
|
|
390
|
+
return built;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** −20·log10|H(s)| of a prototype; null is S = ∞. */
|
|
394
|
+
function prototypeLoss_dB(proto: FilterPrototype, s: Complex | null): number {
|
|
395
|
+
if (s === null) {
|
|
396
|
+
return proto.zeros.length === proto.poles.length
|
|
397
|
+
? -20 * Math.log10(Math.abs(proto.gain))
|
|
398
|
+
: Infinity;
|
|
399
|
+
}
|
|
400
|
+
let sum = -Math.log10(Math.abs(proto.gain));
|
|
401
|
+
for (const z of proto.zeros) sum -= Math.log10(Math.hypot(s.re - z.re, s.im - z.im));
|
|
402
|
+
for (const p of proto.poles) sum += Math.log10(Math.hypot(s.re - p.re, s.im - p.im));
|
|
403
|
+
return 20 * sum;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Uniform dissipation δ from the unloaded Q: 1/Qu for low-pass and
|
|
408
|
+
* high-pass, 1/(Qu·FBW) with FBW = B/f0 for band-pass and band-stop.
|
|
409
|
+
*/
|
|
410
|
+
export function filterDissipation(shape: FilterShape): number {
|
|
411
|
+
const q = shape.unloadedQ;
|
|
412
|
+
if (q === undefined || !(q > 0)) return 0;
|
|
413
|
+
return isBand(shape.type) ? shape.center_Hz! / (shape.bandwidth_Hz! * q) : 1 / q;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The complex prototype variable at band variable Ω ≥ 0; null is S = ∞. */
|
|
417
|
+
function prototypeVariable(shape: FilterShape, omega: number, delta: number): Complex | null {
|
|
418
|
+
if (shape.type === "bandstop") {
|
|
419
|
+
// S = 1/(δ + 1/(jΩ)) = 1/(δ − j/Ω).
|
|
420
|
+
if (!Number.isFinite(omega)) return delta > 0 ? { re: 1 / delta, im: 0 } : null;
|
|
421
|
+
if (omega === 0) return { re: 0, im: 0 };
|
|
422
|
+
if (delta === 0) return { re: 0, im: omega };
|
|
423
|
+
return reciprocal({ re: delta, im: -1 / omega });
|
|
424
|
+
}
|
|
425
|
+
if (!Number.isFinite(omega)) return null;
|
|
426
|
+
return { re: delta, im: omega };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Prototype attenuation in dB at Ω ≥ 0, with no passband loss and no limit. */
|
|
430
|
+
export function prototypeAttenuation_dB(shape: FilterShape, omega: number): number {
|
|
431
|
+
return prototypeLoss_dB(
|
|
432
|
+
filterPrototype(shape),
|
|
433
|
+
prototypeVariable(shape, Math.abs(omega), filterDissipation(shape)),
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** Ω of every finite transmission zero of the prototype (the notches). */
|
|
438
|
+
export function filterZeroOmegas(shape: FilterShape): number[] {
|
|
439
|
+
const out = new Set<number>();
|
|
440
|
+
for (const z of filterPrototype(shape).zeros) if (z.im > 0) out.add(z.im);
|
|
441
|
+
return [...out].sort((a, b) => a - b);
|
|
442
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { COAX_CABLES, type CoaxCable } from "@/lib/coax/cables";
|
|
2
|
+
import { BAND_MHZ, cableAt, titleSpot } from "@/lib/coax/explorer";
|
|
3
|
+
import { formatMhzShort, formatPer100 } from "@/lib/coax/format";
|
|
4
|
+
|
|
5
|
+
export const EXPLORER_TITLE = `Coax cable explorer: loss of ${COAX_CABLES.length} cables at any frequency | IndieRF`;
|
|
6
|
+
export const EXPLORER_DESCRIPTION =
|
|
7
|
+
`Rank ${COAX_CABLES.length} coax cables by loss at one frequency and length: RG-58, RG-316, LMR-240, LMR-400, Ecoflex, semi-rigid and lab cables, each fitted to its maker's printed table. Drag the frequency, set the run, read watts out.`;
|
|
8
|
+
|
|
9
|
+
export function cableDisplayName(cable: Pick<CoaxCable, "name" | "rg">): string {
|
|
10
|
+
return cable.rg && !cable.name.includes(cable.rg) ? `${cable.name} (${cable.rg})` : cable.name;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** What people search for: the RG type when there is one, else the part name. */
|
|
14
|
+
function cableSearchName(cable: Pick<CoaxCable, "name" | "rg">): string {
|
|
15
|
+
return cable.rg ?? cable.name;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The maker or part behind an RG type: "Belden 8262", or "Taoglas" when the name already holds the type. */
|
|
19
|
+
function cableMaker(cable: Pick<CoaxCable, "name" | "rg">): string | null {
|
|
20
|
+
if (!cable.rg) return null;
|
|
21
|
+
return cable.name.replace(cable.rg, "").trim() || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** "RG-58 cable loss (Belden 8262)". */
|
|
25
|
+
export function cablePageHeading(cable: Pick<CoaxCable, "name" | "rg">): string {
|
|
26
|
+
const maker = cableMaker(cable);
|
|
27
|
+
return `${cableSearchName(cable)} cable loss${maker ? ` (${maker})` : ""}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* "RG-58 cable loss: 2.82 dB per 10 m at 915 MHz (Belden 8262) | IndieRF". The
|
|
32
|
+
* number is `titleSpot`: 2.4 GHz when the sheet prints that far, else the
|
|
33
|
+
* highest printed spot frequency, marked extrapolated when it is past the table.
|
|
34
|
+
*/
|
|
35
|
+
export function cablePageTitle(cable: CoaxCable): string {
|
|
36
|
+
const maker = cableMaker(cable);
|
|
37
|
+
const spot = titleSpot(cable);
|
|
38
|
+
const loss = spot
|
|
39
|
+
? `: ${formatPer100(spot.per10m)} dB per 10 m at ${formatMhzShort(spot.mhz)}${spot.range === "extrapolated" ? " (extrapolated)" : ""}`
|
|
40
|
+
: " per 10 m and 100 ft";
|
|
41
|
+
return `${cableSearchName(cable)} cable loss${loss}${maker ? ` (${maker})` : ""} | IndieRF`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Two quoted bands for the description: 144 MHz and 2.4 GHz when usable, else the lowest and highest usable bands. */
|
|
45
|
+
export function quotedBands(cable: CoaxCable): number[] {
|
|
46
|
+
const usable = BAND_MHZ.filter((mhz) => cableAt(cable, mhz * 1e6, 10).ok);
|
|
47
|
+
const preferred = [144, 2400].filter((mhz) => usable.includes(mhz as (typeof BAND_MHZ)[number]));
|
|
48
|
+
if (preferred.length === 2) return preferred;
|
|
49
|
+
if (usable.length === 0) return [];
|
|
50
|
+
return usable.length === 1 ? [usable[0]] : [usable[0], usable[usable.length - 1]];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function lossPhrase(cable: CoaxCable, mhz: number): string {
|
|
54
|
+
const at = cableAt(cable, mhz * 1e6, 10);
|
|
55
|
+
if (!at.ok) return "";
|
|
56
|
+
return `${formatPer100(at.lossDb)} dB at ${formatMhzShort(mhz)}${at.range === "extrapolated" ? " (extrapolated)" : ""}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** "Loses 0.93 dB per 10 m at 144 MHz and 4.4 dB at 2.4 GHz". */
|
|
60
|
+
export function cableLossSentence(cable: CoaxCable): string {
|
|
61
|
+
const bands = quotedBands(cable);
|
|
62
|
+
if (bands.length === 0) return `${cable.name} has no usable number at the listed bands.`;
|
|
63
|
+
const phrases = bands.map((mhz) => lossPhrase(cable, mhz));
|
|
64
|
+
const [first, ...rest] = phrases;
|
|
65
|
+
const firstPer = first.replace(" dB at", " dB per 10 m at");
|
|
66
|
+
return `${cable.name} loses ${[firstPer, ...rest].join(" and ")}.`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function cablePageDescription(cable: CoaxCable): string {
|
|
70
|
+
return `${cableLossSentence(cable)} Fitted to the maker's printed attenuation table, with a band table from 1.8 MHz to 5.8 GHz, a to-scale cross-section and the usable range.`;
|
|
71
|
+
}
|