smithkit 0.1.1 → 0.2.0

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.
Files changed (132) hide show
  1. package/README.md +119 -40
  2. package/dist/lib/MarkerMeasurements.d.ts +6 -0
  3. package/dist/lib/MarkerMeasurements.js +22 -0
  4. package/dist/lib/Smith.d.ts +9 -2
  5. package/dist/lib/Smith.js +68 -36
  6. package/dist/lib/SmithFormatter.d.ts +10 -0
  7. package/dist/lib/SmithFormatter.js +19 -0
  8. package/dist/lib/grid/CircleLayerControl.d.ts +15 -0
  9. package/dist/lib/grid/CircleLayerControl.js +40 -0
  10. package/dist/lib/{draw → grid}/ConstCircles.d.ts +4 -6
  11. package/dist/lib/{draw → grid}/ConstCircles.js +2 -2
  12. package/dist/lib/{draw → grid}/ConstQCircles.d.ts +2 -2
  13. package/dist/lib/{draw → grid}/ConstQCircles.js +3 -3
  14. package/dist/lib/{draw → grid}/ConstSwrCircles.d.ts +2 -2
  15. package/dist/lib/{draw → grid}/ConstSwrCircles.js +3 -3
  16. package/dist/lib/grid/GridDefinitions.d.ts +12 -0
  17. package/dist/lib/grid/GridDefinitions.js +138 -0
  18. package/dist/lib/grid/GridGeometry.d.ts +26 -0
  19. package/dist/lib/grid/GridGeometry.js +58 -0
  20. package/dist/lib/grid/GridLabels.d.ts +8 -0
  21. package/dist/lib/grid/GridLabels.js +45 -0
  22. package/dist/lib/grid/GridLayerControl.d.ts +12 -0
  23. package/dist/lib/grid/GridLayerControl.js +39 -0
  24. package/dist/lib/grid/LayerControl.d.ts +6 -0
  25. package/dist/lib/grid/LayerControl.js +16 -0
  26. package/dist/lib/{draw → grid}/SmithGridLayer.d.ts +5 -4
  27. package/dist/lib/{draw → grid}/SmithGridLayer.js +26 -9
  28. package/dist/lib/grid/types.d.ts +1 -1
  29. package/dist/lib/index.d.ts +8 -10
  30. package/dist/lib/index.js +5 -7
  31. package/dist/lib/{draw → interaction}/SmithCursor.d.ts +3 -3
  32. package/dist/lib/{draw → interaction}/SmithCursor.js +6 -6
  33. package/dist/lib/io/Touchstone.d.ts +10 -0
  34. package/dist/lib/io/Touchstone.js +75 -0
  35. package/dist/lib/layers.d.ts +2 -8
  36. package/dist/lib/layers.js +1 -73
  37. package/dist/lib/{complex → math}/Complex.d.ts +1 -0
  38. package/dist/lib/{complex → math}/Complex.js +1 -1
  39. package/dist/lib/math/geometry.d.ts +14 -0
  40. package/dist/lib/measurements.d.ts +2 -4
  41. package/dist/lib/measurements.js +1 -19
  42. package/dist/lib/rf/RfCalculations.d.ts +65 -0
  43. package/dist/lib/rf/RfCalculations.js +161 -0
  44. package/dist/lib/{SmithConstantCircle.d.ts → rf/SmithConstantCircle.d.ts} +2 -3
  45. package/dist/lib/{SmithConstantCircle.js → rf/SmithConstantCircle.js} +3 -4
  46. package/dist/lib/scales/PeripheralScaleDefinitions.d.ts +16 -0
  47. package/dist/lib/scales/PeripheralScaleDefinitions.js +54 -0
  48. package/dist/lib/scales/RadialScaleDefinitions.d.ts +15 -0
  49. package/dist/lib/scales/RadialScaleDefinitions.js +112 -0
  50. package/dist/lib/scales/SmithPeripheralScales.d.ts +4 -2
  51. package/dist/lib/scales/SmithPeripheralScales.js +46 -26
  52. package/dist/lib/scales/SmithScales.d.ts +3 -1
  53. package/dist/lib/scales/SmithScales.js +8 -2
  54. package/dist/lib/svg/LabelLayout.d.ts +17 -0
  55. package/dist/lib/svg/LabelLayout.js +145 -0
  56. package/dist/lib/{draw → svg}/SmithArc.d.ts +1 -1
  57. package/dist/lib/{draw → svg}/SmithCircle.d.ts +1 -1
  58. package/dist/lib/{draw → svg}/SmithLine.d.ts +1 -1
  59. package/dist/lib/{draw → svg}/SmithScaler.d.ts +1 -4
  60. package/dist/lib/{draw → svg}/SmithText.d.ts +1 -1
  61. package/dist/lib/svg/SvgExporter.d.ts +14 -0
  62. package/dist/lib/svg/SvgExporter.js +137 -0
  63. package/dist/lib/{draw → traces}/SmithData.d.ts +24 -35
  64. package/dist/lib/traces/SmithData.js +195 -0
  65. package/dist/lib/{draw → traces}/SmithMarker.d.ts +14 -3
  66. package/dist/lib/traces/SmithMarker.js +237 -0
  67. package/dist/lib/traces/TraceModel.d.ts +31 -0
  68. package/dist/lib/traces/TraceModel.js +123 -0
  69. package/dist/lib/traces/TraceRenderer.d.ts +33 -0
  70. package/dist/lib/traces/TraceRenderer.js +152 -0
  71. package/package.json +4 -1
  72. package/dist/lib/SmithArcsDefs.d.ts +0 -10
  73. package/dist/lib/SmithArcsDefs.js +0 -10
  74. package/dist/lib/components.d.ts +0 -14
  75. package/dist/lib/components.js +0 -26
  76. package/dist/lib/draw/SmithData.js +0 -366
  77. package/dist/lib/draw/SmithMarker.js +0 -122
  78. package/dist/lib/formatting.d.ts +0 -7
  79. package/dist/lib/formatting.js +0 -16
  80. package/dist/lib/grid/definitions.d.ts +0 -3
  81. package/dist/lib/grid/definitions.js +0 -123
  82. package/dist/lib/grid/geometry.d.ts +0 -20
  83. package/dist/lib/grid/geometry.js +0 -55
  84. package/dist/lib/grid/labels.d.ts +0 -3
  85. package/dist/lib/grid/labels.js +0 -41
  86. package/dist/lib/io/touchstone.d.ts +0 -7
  87. package/dist/lib/io/touchstone.js +0 -72
  88. package/dist/lib/math/complexRatio.d.ts +0 -3
  89. package/dist/lib/math/complexRatio.js +0 -30
  90. package/dist/lib/renormalization.d.ts +0 -10
  91. package/dist/lib/renormalization.js +0 -62
  92. package/dist/lib/rf.d.ts +0 -35
  93. package/dist/lib/rf.js +0 -74
  94. package/dist/lib/scales/peripheralScales.d.ts +0 -12
  95. package/dist/lib/scales/peripheralScales.js +0 -49
  96. package/dist/lib/scales/radialScales.d.ts +0 -12
  97. package/dist/lib/scales/radialScales.js +0 -109
  98. package/dist/lib/shapes/Arc.d.ts +0 -6
  99. package/dist/lib/shapes/Circle.d.ts +0 -5
  100. package/dist/lib/shapes/Circle.js +0 -1
  101. package/dist/lib/shapes/Line.d.ts +0 -5
  102. package/dist/lib/shapes/Line.js +0 -1
  103. package/dist/lib/shapes/Point.d.ts +0 -1
  104. package/dist/lib/shapes/Point.js +0 -1
  105. /package/dist/lib/{draw → grid}/ConstConductance.d.ts +0 -0
  106. /package/dist/lib/{draw → grid}/ConstConductance.js +0 -0
  107. /package/dist/lib/{draw → grid}/ConstReactance.d.ts +0 -0
  108. /package/dist/lib/{draw → grid}/ConstReactance.js +0 -0
  109. /package/dist/lib/{draw → grid}/ConstResistance.d.ts +0 -0
  110. /package/dist/lib/{draw → grid}/ConstResistance.js +0 -0
  111. /package/dist/lib/{draw → grid}/ConstSusceptance.d.ts +0 -0
  112. /package/dist/lib/{draw → grid}/ConstSusceptance.js +0 -0
  113. /package/dist/lib/{arcs → grid}/Tick.d.ts +0 -0
  114. /package/dist/lib/{arcs → grid}/Tick.js +0 -0
  115. /package/dist/lib/{draw → interaction}/MouseGesture.d.ts +0 -0
  116. /package/dist/lib/{draw → interaction}/MouseGesture.js +0 -0
  117. /package/dist/lib/{draw/SmithDrawOptions.js → math/geometry.js} +0 -0
  118. /package/dist/lib/{draw → svg}/SmithArc.js +0 -0
  119. /package/dist/lib/{draw → svg}/SmithCircle.js +0 -0
  120. /package/dist/lib/{draw → svg}/SmithDrawOptions.d.ts +0 -0
  121. /package/dist/lib/{shapes/Arc.js → svg/SmithDrawOptions.js} +0 -0
  122. /package/dist/lib/{draw → svg}/SmithGroup.d.ts +0 -0
  123. /package/dist/lib/{draw → svg}/SmithGroup.js +0 -0
  124. /package/dist/lib/{draw → svg}/SmithLine.js +0 -0
  125. /package/dist/lib/{draw → svg}/SmithPath.d.ts +0 -0
  126. /package/dist/lib/{draw → svg}/SmithPath.js +0 -0
  127. /package/dist/lib/{draw → svg}/SmithScaler.js +0 -0
  128. /package/dist/lib/{draw → svg}/SmithShape.d.ts +0 -0
  129. /package/dist/lib/{draw → svg}/SmithShape.js +0 -0
  130. /package/dist/lib/{draw → svg}/SmithSvg.d.ts +0 -0
  131. /package/dist/lib/{draw → svg}/SmithSvg.js +0 -0
  132. /package/dist/lib/{draw → svg}/SmithText.js +0 -0
@@ -1,9 +1,9 @@
1
- import { SmithGroup } from './SmithGroup.js';
2
- import { SmithCircle } from './SmithCircle.js';
3
- import { SmithLine } from './SmithLine.js';
4
- import { SmithArc } from './SmithArc.js';
5
- import { SmithConstantCircle } from '../SmithConstantCircle.js';
6
- import { Complex } from '../complex/Complex.js';
1
+ import { SmithGroup } from '../svg/SmithGroup.js';
2
+ import { SmithCircle } from '../svg/SmithCircle.js';
3
+ import { SmithLine } from '../svg/SmithLine.js';
4
+ import { SmithArc } from '../svg/SmithArc.js';
5
+ import { SmithConstantCircle } from '../rf/SmithConstantCircle.js';
6
+ import { Complex } from '../math/Complex.js';
7
7
  export class SmithCursor {
8
8
  scaler;
9
9
  epsilon = 1.5e-4;
@@ -0,0 +1,10 @@
1
+ import type { TraceSamples } from '../samples.js';
2
+ export interface TouchstoneData {
3
+ samples: TraceSamples;
4
+ referenceImpedanceOhms: number;
5
+ }
6
+ export declare class Touchstone {
7
+ private constructor();
8
+ /** Read one-port Touchstone 1.x data; frequencies in the result are always Hz. */
9
+ static parse(text: string): TouchstoneData;
10
+ }
@@ -0,0 +1,75 @@
1
+ export class Touchstone {
2
+ constructor() { }
3
+ /** Read one-port Touchstone 1.x data; frequencies in the result are always Hz. */
4
+ static parse(text) {
5
+ const units = { HZ: 1, KHZ: 1e3, MHZ: 1e6, GHZ: 1e9 };
6
+ let frequencyScale = 1e9;
7
+ let format = 'MA';
8
+ let referenceImpedanceOhms = 50;
9
+ let optionsSeen = false;
10
+ const samples = [];
11
+ for (const [index, raw] of text
12
+ .replace(/^\uFEFF/, '')
13
+ .split(/\r?\n/)
14
+ .entries()) {
15
+ const line = raw.split('!')[0].trim();
16
+ if (!line) {
17
+ continue;
18
+ }
19
+ const fail = (message) => {
20
+ throw new Error(`Line ${index + 1}: ${message}`);
21
+ };
22
+ if (line.startsWith('[')) {
23
+ fail('Only Touchstone 1.x one-port files (.s1p) are supported.');
24
+ }
25
+ if (line.startsWith('#')) {
26
+ if (optionsSeen || samples.length) {
27
+ fail('The option line must appear once, before the data.');
28
+ }
29
+ optionsSeen = true;
30
+ const options = line.slice(1).trim().toUpperCase().split(/\s+/).filter(Boolean);
31
+ if (!options.length) {
32
+ continue;
33
+ }
34
+ const [unit = 'GHZ', parameter = 'S', representation = 'MA', r = 'R', impedance = '50'] = options;
35
+ if (!(unit in units) ||
36
+ parameter !== 'S' ||
37
+ !['RI', 'MA', 'DB'].includes(representation) ||
38
+ r !== 'R' ||
39
+ options.length > 5) {
40
+ fail('Expected: # Hz|kHz|MHz|GHz S RI|MA|DB R impedance.');
41
+ }
42
+ frequencyScale = units[unit];
43
+ format = representation;
44
+ referenceImpedanceOhms = Number(impedance);
45
+ if (!Number.isFinite(referenceImpedanceOhms) || referenceImpedanceOhms <= 0) {
46
+ fail('Reference impedance must be positive.');
47
+ }
48
+ continue;
49
+ }
50
+ const fields = line.split(/\s+/).map(Number);
51
+ if (fields.length !== 3 || !fields.every(Number.isFinite)) {
52
+ fail('Expected frequency and two finite S11 components.');
53
+ }
54
+ const [frequency, a, b] = fields;
55
+ const frequencyHz = frequency * frequencyScale;
56
+ if (frequencyHz < 0 || !Number.isFinite(frequencyHz)) {
57
+ fail('Frequency must be finite and non-negative.');
58
+ }
59
+ if (format === 'MA' && a < 0) {
60
+ fail('Magnitude must be non-negative.');
61
+ }
62
+ const magnitude = format === 'DB' ? 10 ** (a / 20) : a;
63
+ const radians = (b * Math.PI) / 180;
64
+ const reflectionCoefficient = format === 'RI' ? [a, b] : [magnitude * Math.cos(radians), magnitude * Math.sin(radians)];
65
+ if (!reflectionCoefficient.every(Number.isFinite)) {
66
+ fail('S11 is outside the supported numeric range.');
67
+ }
68
+ samples.push({ frequencyHz, reflectionCoefficient });
69
+ }
70
+ if (!samples.length) {
71
+ throw new Error('The file contains no S11 samples.');
72
+ }
73
+ return { samples, referenceImpedanceOhms };
74
+ }
75
+ }
@@ -1,7 +1,4 @@
1
- import type { ConstCircles } from './draw/ConstCircles.js';
2
- import type { ConstQCircles } from './draw/ConstQCircles.js';
3
- import type { ConstSwrCircles } from './draw/ConstSwrCircles.js';
4
- import type { Complex } from './complex/Complex.js';
1
+ import type { Complex } from './math/Complex.js';
5
2
  export interface GridStyle {
6
3
  stroke: string;
7
4
  /** Non-scaling stroke width in CSS pixels. */
@@ -10,7 +7,7 @@ export interface GridStyle {
10
7
  minorWidth: number;
11
8
  textColor: string;
12
9
  textFontFamily: string;
13
- /** Font size in SVG chart units; labels scale with chart geometry. */
10
+ /** Base font size in chart units; adaptive layout enforces a 9 CSS px minimum in the default view. */
14
11
  textFontSize: number;
15
12
  }
16
13
  export interface CircleStyle {
@@ -43,6 +40,3 @@ export interface PeripheralScales {
43
40
  /** Pass null to clear the indicators. */
44
41
  update(reflectionCoefficient: Complex | null): void;
45
42
  }
46
- /** Keep renderer instances out of the public chart API. */
47
- export declare function gridLayer(layer: ConstCircles, assertAlive: () => void): GridLayer;
48
- export declare function circleLayer(layer: ConstQCircles | ConstSwrCircles, minimum: number, assertAlive: () => void): CircleLayer;
@@ -1,73 +1 @@
1
- /** Keep renderer instances out of the public chart API. */
2
- export function gridLayer(layer, assertAlive) {
3
- return {
4
- setVisible(visible) {
5
- assertAlive();
6
- layer.visibility(visible);
7
- },
8
- setMinorVisible(visible) {
9
- assertAlive();
10
- layer.displayMinor(visible);
11
- },
12
- setStyle(style) {
13
- assertAlive();
14
- validateLengths(style, ['majorWidth', 'minorWidth', 'textFontSize']);
15
- const properties = {
16
- stroke: 'Stroke',
17
- majorWidth: 'MajorWidth',
18
- minorWidth: 'MinorWidth',
19
- textColor: 'TextColor',
20
- textFontFamily: 'TextFontFamily',
21
- textFontSize: 'TextFontSize',
22
- };
23
- for (const key of Object.keys(properties)) {
24
- const value = style[key];
25
- if (value !== undefined) {
26
- layer[properties[key]] = String(value);
27
- }
28
- }
29
- },
30
- };
31
- }
32
- export function circleLayer(layer, minimum, assertAlive) {
33
- const validate = (value) => {
34
- if (!Number.isFinite(value) || value < minimum || value <= 0) {
35
- throw new RangeError(`Circle value must be finite, positive, and at least ${minimum}.`);
36
- }
37
- };
38
- return {
39
- setVisible(visible) {
40
- assertAlive();
41
- layer.visibility(visible);
42
- },
43
- setStyle(style) {
44
- assertAlive();
45
- validateLengths(style, ['strokeWidth']);
46
- if (style.stroke !== undefined) {
47
- layer.Stroke = style.stroke;
48
- }
49
- if (style.strokeWidth !== undefined) {
50
- layer.StrokeWidth = String(style.strokeWidth);
51
- }
52
- },
53
- addValue(value) {
54
- assertAlive();
55
- validate(value);
56
- layer.append(value);
57
- },
58
- removeValue(value) {
59
- assertAlive();
60
- validate(value);
61
- layer.remove(value);
62
- },
63
- };
64
- }
65
- function validateLengths(style, keys) {
66
- for (const key of keys) {
67
- const value = style[key];
68
- if (value !== undefined &&
69
- (typeof value !== 'number' || !Number.isFinite(value) || value <= 0)) {
70
- throw new RangeError(`${String(key)} must be a positive finite number.`);
71
- }
72
- }
73
- }
1
+ export {};
@@ -126,5 +126,6 @@ export declare class Complex {
126
126
  static equals(z1: Complex, z2: Complex, epsilon?: number): boolean;
127
127
  static toPolar(z: Complex): [number, number];
128
128
  static toVector(z: Complex): [number, number];
129
+ /** Format Cartesian components with Number.toFixed(dp), defaulting to three decimal places. */
129
130
  static toString(z: Complex, dp?: number): string;
130
131
  }
@@ -626,7 +626,7 @@ export class Complex {
626
626
  static toVector(z) {
627
627
  return [z.re, z.im];
628
628
  }
629
- // TODO: format
629
+ /** Format Cartesian components with Number.toFixed(dp), defaulting to three decimal places. */
630
630
  static toString(z, dp = 3) {
631
631
  const re = z.re.toFixed(dp);
632
632
  const im = Math.abs(z.im).toFixed(dp);
@@ -0,0 +1,14 @@
1
+ export type Point = [number, number];
2
+ export interface Line {
3
+ p1: Point;
4
+ p2: Point;
5
+ }
6
+ export interface Circle {
7
+ p: Point;
8
+ r: number;
9
+ }
10
+ export interface Arc {
11
+ p1: Point;
12
+ p2: Point;
13
+ r: number;
14
+ }
@@ -1,5 +1,5 @@
1
- import { Complex } from './complex/Complex.js';
2
- import type { SmithReading } from './rf.js';
1
+ import { Complex } from './math/Complex.js';
2
+ import type { SmithReading } from './rf/RfCalculations.js';
3
3
  export type TraceRenderMode = 'points' | 'line' | 'both';
4
4
  export interface TraceStyle {
5
5
  mode: TraceRenderMode;
@@ -49,5 +49,3 @@ export interface MarkerComparison {
49
49
  /** Shortest signed B − A phase difference in [−180, 180); undefined at Γ = 0. */
50
50
  readonly phaseDeltaDegrees: number | undefined;
51
51
  }
52
- /** Compare B against A without a chart or browser. Frequencies must be finite and non-negative. */
53
- export declare function compareMarkerReadings(a: Pick<MarkerSnapshot, 'frequencyHz' | 'impedanceOhms' | 'reflectionCoefficient'>, b: Pick<MarkerSnapshot, 'frequencyHz' | 'impedanceOhms' | 'reflectionCoefficient'>): MarkerComparison;
@@ -1,19 +1 @@
1
- /** Compare B against A without a chart or browser. Frequencies must be finite and non-negative. */
2
- export function compareMarkerReadings(a, b) {
3
- if (![a.frequencyHz, b.frequencyHz].every((frequencyHz) => Number.isFinite(frequencyHz) && frequencyHz >= 0)) {
4
- throw new RangeError('Marker frequencies must be finite and non-negative.');
5
- }
6
- const isFinite = (value) => Number.isFinite(value.re) && Number.isFinite(value.im);
7
- const hasPhase = (gamma) => isFinite(gamma) && (gamma.re !== 0 || gamma.im !== 0);
8
- const phaseDelta = hasPhase(a.reflectionCoefficient) && hasPhase(b.reflectionCoefficient)
9
- ? ((b.reflectionCoefficient.arg() - a.reflectionCoefficient.arg()) * 180) / Math.PI
10
- : undefined;
11
- const impedanceDelta = a.impedanceOhms && b.impedanceOhms && isFinite(a.impedanceOhms) && isFinite(b.impedanceOhms)
12
- ? b.impedanceOhms.sub(a.impedanceOhms)
13
- : undefined;
14
- return {
15
- frequencyDeltaHz: b.frequencyHz - a.frequencyHz,
16
- impedanceDeltaOhms: impedanceDelta && isFinite(impedanceDelta) ? impedanceDelta : undefined,
17
- phaseDeltaDegrees: phaseDelta === undefined ? undefined : ((phaseDelta + 540) % 360) - 180,
18
- };
19
- }
1
+ export {};
@@ -0,0 +1,65 @@
1
+ import type { TraceSamples } from '../samples.js';
2
+ import { Complex } from '../math/Complex.js';
3
+ /** Physical quantities derived from a voltage reflection coefficient Γ. */
4
+ export interface SmithReading {
5
+ readonly reflectionCoefficient: Complex;
6
+ readonly impedanceOhms: Complex | undefined;
7
+ readonly admittanceSiemens: Complex | undefined;
8
+ readonly phaseDegrees: number | undefined;
9
+ readonly vswr: number | undefined;
10
+ readonly standingWaveRatioDb: number | undefined;
11
+ readonly returnLossDb: number;
12
+ readonly reflectionLossDb: number | undefined;
13
+ readonly attenuationDb: number | undefined;
14
+ readonly q: number | undefined;
15
+ readonly powerReflectionCoefficient: number;
16
+ readonly voltageReflectionCoefficientMagnitude: number;
17
+ readonly powerTransmissionCoefficient: number | undefined;
18
+ readonly voltageTransmissionCoefficientMagnitude: number;
19
+ readonly currentTransmissionCoefficientMagnitude: number;
20
+ readonly standingWavePeak: number | undefined;
21
+ readonly standingWaveLossCoefficient: number | undefined;
22
+ }
23
+ /** An ideal series component equivalent to a reactance at one frequency. */
24
+ export type ReactiveComponent = {
25
+ readonly kind: 'inductor';
26
+ readonly inductanceHenries: number;
27
+ } | {
28
+ readonly kind: 'capacitor';
29
+ readonly capacitanceFarads: number;
30
+ };
31
+ export declare class RfCalculations {
32
+ private constructor();
33
+ private static readonly calcs;
34
+ private static validateReference;
35
+ private static validateComplex;
36
+ private static finite;
37
+ /** Convert Γ to physical impedance in ohms; undefined at the open circuit Γ = 1. */
38
+ static reflectionToImpedance(gamma: Complex, referenceImpedanceOhms?: number): Complex | undefined;
39
+ /** Convert physical impedance in ohms to Γ. Undefined for a singular result. */
40
+ static impedanceToReflection(impedanceOhms: Complex, referenceImpedanceOhms?: number): Complex | undefined;
41
+ /** Convert Γ to physical admittance in siemens; undefined at the short circuit Γ = −1. */
42
+ static reflectionToAdmittance(gamma: Complex, referenceImpedanceOhms?: number): Complex | undefined;
43
+ /** Convert physical admittance in siemens to Γ. Undefined for a singular result. */
44
+ static admittanceToReflection(admittanceSiemens: Complex, referenceImpedanceOhms?: number): Complex | undefined;
45
+ /**
46
+ * Read Γ without a chart or DOM. Undefined means a singular or inapplicable quantity.
47
+ * Passive-load quantities are undefined outside |Γ| ≤ 1; exact infinite limits are retained.
48
+ * Attenuation assumes a fully reflecting termination and is half the return loss.
49
+ */
50
+ static readReflection(gamma: Complex, referenceImpedanceOhms?: number): SmithReading;
51
+ /**
52
+ * Change the positive real reference impedance of a voltage reflection coefficient.
53
+ * Preserves physical impedance, including exact open/short limits. A singular or
54
+ * unrepresentable result is undefined; non-finite inputs throw RangeError.
55
+ */
56
+ static renormalizeReflection(gamma: Complex, fromOhms: number, toOhms: number): Complex | undefined;
57
+ /** Copy and renormalize every sample; reject invalid/singular data without modifying the input. */
58
+ static renormalizeSamples(samples: TraceSamples, fromOhms: number, toOhms: number): TraceSamples;
59
+ /**
60
+ * Convert reactance in ohms at a frequency in Hz to an ideal series L or C.
61
+ * Zero frequency, zero reactance, or an unrepresentable component returns undefined.
62
+ * Non-finite reactance and negative/non-finite frequency throw RangeError.
63
+ */
64
+ static reactanceToComponent(reactanceOhms: number, frequencyHz: number): ReactiveComponent | undefined;
65
+ }
@@ -0,0 +1,161 @@
1
+ import { Complex } from '../math/Complex.js';
2
+ import { SmithConstantCircle } from './SmithConstantCircle.js';
3
+ export class RfCalculations {
4
+ constructor() { }
5
+ static calcs = new SmithConstantCircle();
6
+ static validateReference(referenceImpedanceOhms) {
7
+ if (!Number.isFinite(referenceImpedanceOhms) || referenceImpedanceOhms <= 0) {
8
+ throw new RangeError('Reference impedance must be positive and finite.');
9
+ }
10
+ }
11
+ static validateComplex(value) {
12
+ if (!Number.isFinite(value.re) || !Number.isFinite(value.im)) {
13
+ throw new RangeError('Complex components must be finite.');
14
+ }
15
+ }
16
+ static finite(value) {
17
+ return value && Number.isFinite(value.re) && Number.isFinite(value.im) ? value : undefined;
18
+ }
19
+ /** Convert Γ to physical impedance in ohms; undefined at the open circuit Γ = 1. */
20
+ static reflectionToImpedance(gamma, referenceImpedanceOhms = 50) {
21
+ RfCalculations.validateReference(referenceImpedanceOhms);
22
+ RfCalculations.validateComplex(gamma);
23
+ return RfCalculations.finite(RfCalculations.calcs.rflCoeffToImpedance(gamma)?.mul(referenceImpedanceOhms));
24
+ }
25
+ /** Convert physical impedance in ohms to Γ. Undefined for a singular result. */
26
+ static impedanceToReflection(impedanceOhms, referenceImpedanceOhms = 50) {
27
+ RfCalculations.validateReference(referenceImpedanceOhms);
28
+ RfCalculations.validateComplex(impedanceOhms);
29
+ return RfCalculations.finite(RfCalculations.calcs.impedanceToRflCoeff(impedanceOhms.div(referenceImpedanceOhms)));
30
+ }
31
+ /** Convert Γ to physical admittance in siemens; undefined at the short circuit Γ = −1. */
32
+ static reflectionToAdmittance(gamma, referenceImpedanceOhms = 50) {
33
+ RfCalculations.validateReference(referenceImpedanceOhms);
34
+ RfCalculations.validateComplex(gamma);
35
+ return RfCalculations.finite(RfCalculations.calcs.rflCoeffToAdmittance(gamma)?.div(referenceImpedanceOhms));
36
+ }
37
+ /** Convert physical admittance in siemens to Γ. Undefined for a singular result. */
38
+ static admittanceToReflection(admittanceSiemens, referenceImpedanceOhms = 50) {
39
+ RfCalculations.validateReference(referenceImpedanceOhms);
40
+ RfCalculations.validateComplex(admittanceSiemens);
41
+ return RfCalculations.finite(RfCalculations.calcs.admittanceToRflCoeff(admittanceSiemens.mul(referenceImpedanceOhms)));
42
+ }
43
+ /**
44
+ * Read Γ without a chart or DOM. Undefined means a singular or inapplicable quantity.
45
+ * Passive-load quantities are undefined outside |Γ| ≤ 1; exact infinite limits are retained.
46
+ * Attenuation assumes a fully reflecting termination and is half the return loss.
47
+ */
48
+ static readReflection(gamma, referenceImpedanceOhms = 50) {
49
+ const impedanceOhms = RfCalculations.reflectionToImpedance(gamma, referenceImpedanceOhms);
50
+ const admittanceSiemens = RfCalculations.reflectionToAdmittance(gamma, referenceImpedanceOhms);
51
+ const magnitude = gamma.abs();
52
+ const passive = magnitude <= 1;
53
+ const returnLossDb = -20 * Math.log10(magnitude);
54
+ const vswr = passive ? (1 + magnitude) / (1 - magnitude) : undefined;
55
+ return {
56
+ reflectionCoefficient: gamma,
57
+ impedanceOhms,
58
+ admittanceSiemens,
59
+ phaseDegrees: magnitude === 0 ? undefined : (gamma.arg() * 180) / Math.PI,
60
+ vswr,
61
+ standingWaveRatioDb: vswr === undefined ? undefined : 20 * Math.log10(vswr),
62
+ returnLossDb,
63
+ reflectionLossDb: passive ? (-10 / Math.LN10) * Math.log1p(-(magnitude ** 2)) : undefined,
64
+ attenuationDb: passive ? returnLossDb / 2 : undefined,
65
+ q: impedanceOhms && (impedanceOhms.re !== 0 || impedanceOhms.im !== 0)
66
+ ? Math.abs(impedanceOhms.im / impedanceOhms.re)
67
+ : undefined,
68
+ powerReflectionCoefficient: magnitude ** 2,
69
+ voltageReflectionCoefficientMagnitude: magnitude,
70
+ powerTransmissionCoefficient: passive ? 1 - magnitude ** 2 : undefined,
71
+ voltageTransmissionCoefficientMagnitude: gamma.add(1).abs(),
72
+ currentTransmissionCoefficientMagnitude: Complex.one().sub(gamma).abs(),
73
+ standingWavePeak: vswr === undefined ? undefined : Math.sqrt(vswr),
74
+ standingWaveLossCoefficient: passive
75
+ ? (1 + magnitude ** 2) / (1 - magnitude ** 2)
76
+ : undefined,
77
+ };
78
+ }
79
+ /**
80
+ * Change the positive real reference impedance of a voltage reflection coefficient.
81
+ * Preserves physical impedance, including exact open/short limits. A singular or
82
+ * unrepresentable result is undefined; non-finite inputs throw RangeError.
83
+ */
84
+ static renormalizeReflection(gamma, fromOhms, toOhms) {
85
+ RfCalculations.validateReference(fromOhms);
86
+ RfCalculations.validateReference(toOhms);
87
+ if (!Number.isFinite(gamma.re) || !Number.isFinite(gamma.im)) {
88
+ throw new RangeError('Reflection coefficient components must be finite.');
89
+ }
90
+ if (fromOhms === toOhms || (gamma.im === 0 && Math.abs(gamma.re) === 1)) {
91
+ return gamma;
92
+ }
93
+ // Γnew = (Γold + k) / (1 + k Γold). Scale references before summing.
94
+ const referenceScale = Math.max(fromOhms, toOhms);
95
+ const from = fromOhms / referenceScale;
96
+ const to = toOhms / referenceScale;
97
+ const k = (fromOhms - toOhms) / referenceScale / (from + to);
98
+ const scale = Math.max(1, Math.abs(gamma.re), Math.abs(gamma.im));
99
+ const x = gamma.re / scale;
100
+ const y = gamma.im / scale;
101
+ const inverseScale = 1 / scale;
102
+ const nr = x + k * inverseScale;
103
+ const dr = inverseScale + k * x;
104
+ const di = k * y;
105
+ const denominatorScale = Math.max(Math.abs(dr), Math.abs(di));
106
+ if (denominatorScale === 0) {
107
+ return;
108
+ }
109
+ const real = dr / denominatorScale;
110
+ const imaginary = di / denominatorScale;
111
+ const denominator = real ** 2 + imaginary ** 2;
112
+ const re = ((nr / denominatorScale) * real + (y / denominatorScale) * imaginary) / denominator;
113
+ // Analytic imaginary numerator avoids cancellation for very large finite Γ.
114
+ const im = (((1 - k) * (1 + k) * (y / denominatorScale)) / denominator) *
115
+ (inverseScale / denominatorScale);
116
+ return Number.isFinite(re) && Number.isFinite(im) ? Complex.from(re, im) : undefined;
117
+ }
118
+ /** Copy and renormalize every sample; reject invalid/singular data without modifying the input. */
119
+ static renormalizeSamples(samples, fromOhms, toOhms) {
120
+ RfCalculations.validateReference(fromOhms);
121
+ RfCalculations.validateReference(toOhms);
122
+ return samples.map((sample, index) => {
123
+ if (!Number.isFinite(sample.frequencyHz) ||
124
+ sample.frequencyHz < 0 ||
125
+ sample.reflectionCoefficient.length !== 2) {
126
+ throw new RangeError(`Invalid sample at index ${index}.`);
127
+ }
128
+ const gamma = RfCalculations.renormalizeReflection(Complex.from(...sample.reflectionCoefficient), fromOhms, toOhms);
129
+ if (!gamma) {
130
+ throw new RangeError(`Renormalization is singular or outside the numeric range at sample ${index}.`);
131
+ }
132
+ return { frequencyHz: sample.frequencyHz, reflectionCoefficient: gamma.toVector() };
133
+ });
134
+ }
135
+ /**
136
+ * Convert reactance in ohms at a frequency in Hz to an ideal series L or C.
137
+ * Zero frequency, zero reactance, or an unrepresentable component returns undefined.
138
+ * Non-finite reactance and negative/non-finite frequency throw RangeError.
139
+ */
140
+ static reactanceToComponent(reactanceOhms, frequencyHz) {
141
+ if (!Number.isFinite(reactanceOhms)) {
142
+ throw new RangeError('Reactance must be finite.');
143
+ }
144
+ if (!Number.isFinite(frequencyHz) || frequencyHz < 0) {
145
+ throw new RangeError('Frequency must be finite and non-negative.');
146
+ }
147
+ if (frequencyHz === 0 || reactanceOhms === 0) {
148
+ return;
149
+ }
150
+ // Compute in log space so intermediate products/quotients cannot overflow.
151
+ const logReactance = Math.log(Math.abs(reactanceOhms));
152
+ const logAngularFrequency = Math.log(2 * Math.PI) + Math.log(frequencyHz);
153
+ const value = Math.exp((reactanceOhms > 0 ? logReactance : -logReactance) - logAngularFrequency);
154
+ if (!Number.isFinite(value) || value === 0) {
155
+ return;
156
+ }
157
+ return reactanceOhms > 0
158
+ ? { kind: 'inductor', inductanceHenries: value }
159
+ : { kind: 'capacitor', capacitanceFarads: value };
160
+ }
161
+ }
@@ -1,6 +1,5 @@
1
- import { Circle } from './shapes/Circle.js';
2
- import { Point } from './shapes/Point.js';
3
- import { Complex } from './complex/Complex.js';
1
+ import type { Circle, Point } from '../math/geometry.js';
2
+ import { Complex } from '../math/Complex.js';
4
3
  export declare class SmithConstantCircle {
5
4
  Z0: number;
6
5
  constructor(Z0?: number);
@@ -1,5 +1,4 @@
1
- import { complexRatio } from './math/complexRatio.js';
2
- import { Complex } from './complex/Complex.js';
1
+ import { Complex } from '../math/Complex.js';
3
2
  export class SmithConstantCircle {
4
3
  Z0;
5
4
  constructor(Z0 = 50) {
@@ -18,8 +17,8 @@ export class SmithConstantCircle {
18
17
  return this.transform(c, 1 - c.re, -c.im, 1 + c.re, c.im, -2);
19
18
  }
20
19
  transform(value, nr, ni, dr, di, determinant) {
21
- const quotient = complexRatio(nr, ni, dr, di);
22
- if (!quotient) {
20
+ const quotient = Complex.div(Complex.from(nr, ni), Complex.from(dr, di));
21
+ if (!Number.isFinite(quotient.re) || !Number.isFinite(quotient.im)) {
23
22
  return;
24
23
  }
25
24
  // The analytic imaginary numerator avoids cancellation of nearly equal products.
@@ -0,0 +1,16 @@
1
+ import type { Complex } from '../math/Complex.js';
2
+ export interface PeripheralScale {
3
+ id: string;
4
+ title: string;
5
+ unit: '°' | 'λ';
6
+ ticks: number[];
7
+ major: (value: number) => boolean;
8
+ angle: (value: number) => number;
9
+ read: (gamma: Complex) => number | null;
10
+ }
11
+ export declare class PeripheralScaleDefinitions {
12
+ private constructor();
13
+ private static phase;
14
+ /** A full reflection-coefficient rotation represents half a wavelength along the line. */
15
+ static create(): PeripheralScale[];
16
+ }
@@ -0,0 +1,54 @@
1
+ export class PeripheralScaleDefinitions {
2
+ constructor() { }
3
+ static phase(gamma) {
4
+ return gamma.re === 0 && gamma.im === 0
5
+ ? null
6
+ : (Math.atan2(gamma.im, gamma.re) * 180) / Math.PI;
7
+ }
8
+ /** A full reflection-coefficient rotation represents half a wavelength along the line. */
9
+ static create() {
10
+ const wavelengths = (gamma, direction) => {
11
+ const degrees = PeripheralScaleDefinitions.phase(gamma);
12
+ return degrees === null ? null : (0.25 + (direction * degrees) / 720 + 0.5) % 0.5;
13
+ };
14
+ return [
15
+ {
16
+ id: 'transmission-phase',
17
+ title: 'Transmission phase · °',
18
+ unit: '°',
19
+ ticks: Array.from({ length: 179 }, (_, i) => -89 + i),
20
+ major: (v) => v % 5 === 0,
21
+ // Project from Γ = −1 to the perimeter, not from the chart center.
22
+ angle: (v) => 2 * v,
23
+ read: (gamma) => PeripheralScaleDefinitions.phase({ re: 1 + gamma.re, im: gamma.im }),
24
+ },
25
+ {
26
+ id: 'reflection-phase',
27
+ title: 'Reflection phase · °',
28
+ unit: '°',
29
+ ticks: Array.from({ length: 180 }, (_, i) => -178 + i * 2),
30
+ major: (v) => v % 10 === 0,
31
+ angle: (v) => v,
32
+ read: PeripheralScaleDefinitions.phase,
33
+ },
34
+ {
35
+ id: 'wavelengths-load',
36
+ title: 'Wavelengths → load',
37
+ unit: 'λ',
38
+ ticks: Array.from({ length: 250 }, (_, i) => i / 500),
39
+ major: (v) => Math.round(v * 500) % 5 === 0,
40
+ angle: (v) => 720 * v - 180,
41
+ read: (gamma) => wavelengths(gamma, 1),
42
+ },
43
+ {
44
+ id: 'wavelengths-generator',
45
+ title: 'Wavelengths → generator',
46
+ unit: 'λ',
47
+ ticks: Array.from({ length: 250 }, (_, i) => i / 500),
48
+ major: (v) => Math.round(v * 500) % 5 === 0,
49
+ angle: (v) => 180 - 720 * v,
50
+ read: (gamma) => wavelengths(gamma, -1),
51
+ },
52
+ ];
53
+ }
54
+ }
@@ -0,0 +1,15 @@
1
+ import type { Complex } from '../math/Complex.js';
2
+ export interface RadialScale {
3
+ id: string;
4
+ title: string;
5
+ description?: string;
6
+ unit: 'dB' | 'ratio';
7
+ read: (gamma: Complex) => number;
8
+ values: number[];
9
+ position: (value: number) => number;
10
+ }
11
+ export declare class RadialScaleDefinitions {
12
+ private constructor();
13
+ /** Normalized positions along each scale, from zero to one. */
14
+ static create(): RadialScale[];
15
+ }