rf-touchstone 0.0.1 → 0.0.2

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.
@@ -1,803 +1,367 @@
1
- import { abs } from 'mathjs';
2
- import { add } from 'mathjs';
3
- import { arg } from 'mathjs';
4
- import { Complex } from 'mathjs';
5
- import { complex } from 'mathjs';
6
- import { index } from 'mathjs';
7
- import { log10 } from 'mathjs';
8
- import { multiply } from 'mathjs';
9
- import { pi } from 'mathjs';
10
- import { pow } from 'mathjs';
11
- import { range } from 'mathjs';
12
- import { round } from 'mathjs';
13
- import { subset } from 'mathjs';
14
-
15
- export { abs }
16
-
17
- export { add }
18
-
19
- export { arg }
20
-
21
- export { Complex }
22
-
23
- export { complex }
24
-
25
- /**
26
- * Class representing frequency data in RF and microwave engineering
27
- *
28
- * @remarks
29
- * The Frequency class manages frequency-related data, including:
30
- * - Frequency unit management
31
- * - Storage of frequency points
32
- * - Unit conversion capabilities
33
- *
34
- * @example
35
- * ```typescript
36
- * const freq = new Frequency();
37
- * freq.unit = 'GHz';
38
- * freq.f_scale = [1.0, 2.0, 3.0];
39
- * ```
40
- */
41
- export declare class Frequency {
42
- /**
43
- * Internal storage for frequency unit
44
- * Defaults to 'Hz' as the base SI unit for frequency
45
- */
46
- private _unit;
47
- /**
48
- * Sets the frequency unit for the instance
49
- *
50
- * @param newUnit - The frequency unit to set
51
- * @throws {Error} If the provided unit is not one of the supported frequency units
52
- *
53
- * @example
54
- * ```typescript
55
- * const freq = new Frequency();
56
- * freq.unit = 'MHz'; // Sets unit to Megahertz
57
- * ```
58
- */
59
- set unit(newUnit: FrequencyUnit);
60
- /**
61
- * Gets the current frequency unit
62
- *
63
- * @returns The current frequency unit
64
- *
65
- * @example
66
- * ```typescript
67
- * const freq = new Frequency();
68
- * console.log(freq.unit); // Outputs: 'Hz'
69
- * ```
70
- */
71
- get unit(): FrequencyUnit;
72
- /**
73
- * Internal storage for frequency points array
74
- * Each element represents a frequency point in the specified unit
75
- * @private
76
- */
77
- private _f_scaled;
78
- /**
79
- * Array of frequency points in the current frequency unit
80
- * Each element represents a discrete frequency point for measurement or analysis
81
- *
82
- * @param value - Array of frequency points
83
- * @throws {Error} If the input is not an array
84
- * @throws {Error} If any element in the array is not a number
85
- * @throws {Error} If the array contains negative frequencies
86
- *
87
- * @example
88
- * ```typescript
89
- * const freq = new Frequency();
90
- * freq.unit = 'GHz';
91
- * freq.f_scaled = [1.0, 1.5, 2.0]; // Sets frequency points in GHz
92
- * ```
93
- */
94
- set f_scaled(value: number[]);
95
- /**
96
- * Gets the array of frequency points
97
- * @returns Array of frequency points in the current unit
98
- *
99
- * @example
100
- * ```typescript
101
- * const freq = new Frequency();
102
- * freq.unit = 'MHz';
103
- * freq.f_scaled = [100, 200, 300];
104
- * console.log(freq.f_scaled); // Outputs: [100, 200, 300]
105
- * ```
106
- */
107
- get f_scaled(): number[];
108
- /**
109
- * Private helper method to get frequency values in a target unit.
110
- * @param targetUnit - The key of the target frequency unit in FREQUENCY_MULTIPLIERS.
111
- * @returns Array of frequency points in the target unit.
112
- */
113
- private _getFrequencyInTargetUnit;
114
- /**
115
- * Private helper method to set frequency values from a source unit.
116
- * @param values - Array of frequency points in the source unit.
117
- * @param sourceUnit - The key of the source frequency unit in FREQUENCY_MULTIPLIERS.
118
- */
119
- private _setFrequencyFromTargetUnit;
120
- /**
121
- * Gets frequency points in Hz
122
- * @returns Array of frequency points in Hz
123
- *
124
- * @example
125
- * ```typescript
126
- * const freq = new Frequency();
127
- * freq.unit = 'GHz';
128
- * freq.f_scaled = [1, 2, 3]; // 1 GHz, 2 GHz, 3 GHz
129
- * console.log(freq.f_Hz); // Outputs: [1e9, 2e9, 3e9]
130
- * ```
131
- */
132
- get f_Hz(): number[];
133
- /**
134
- * Sets frequency points assuming input is in Hz
135
- * @param values - Array of frequency points in Hz
136
- *
137
- * @example
138
- * ```typescript
139
- * const freq = new Frequency();
140
- * freq.unit = 'MHz';
141
- * freq.f_Hz = [1e9, 2e9, 3e9]; // Sets frequencies as 1 GHz, 2 GHz, 3 GHz
142
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in MHz)
143
- * ```
144
- */
145
- set f_Hz(values: number[]);
146
- /**
147
- * Gets frequency points in kHz
148
- * @returns Array of frequency points in kHz
149
- *
150
- * @example
151
- * ```typescript
152
- * const freq = new Frequency();
153
- * freq.unit = 'Hz';
154
- * freq.f_scaled = [1000, 2000, 3000]; // 1 kHz, 2 kHz, 3 kHz
155
- * console.log(freq.f_kHz); // Outputs: [1, 2, 3]
156
- * ```
157
- */
158
- get f_kHz(): number[];
159
- /**
160
- * Sets frequency points assuming input is in kHz
161
- * @param values - Array of frequency points in kHz
162
- *
163
- * @example
164
- * ```typescript
165
- * const freq = new Frequency();
166
- * freq.unit = 'Hz';
167
- * freq.f_kHz = [1, 2, 3]; // Sets frequencies as 1 kHz, 2 kHz, 3 kHz
168
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in Hz)
169
- * ```
170
- */
171
- set f_kHz(values: number[]);
172
- /**
173
- * Gets frequency points in MHz
174
- * @returns Array of frequency points in MHz
175
- *
176
- * @example
177
- * ```typescript
178
- * const freq = new Frequency();
179
- * freq.unit = 'GHz';
180
- * freq.f_scaled = [0.1, 0.2, 0.3]; // 0.1 GHz, 0.2 GHz, 0.3 GHz
181
- * console.log(freq.f_MHz); // Outputs: [100, 200, 300]
182
- * ```
183
- */
184
- get f_MHz(): number[];
185
- /**
186
- * Sets frequency points assuming input is in MHz
187
- * @param values - Array of frequency points in MHz
188
- *
189
- * @example
190
- * ```typescript
191
- * const freq = new Frequency();
192
- * freq.unit = 'GHz';
193
- * freq.f_MHz = [100, 200, 300]; // Sets frequencies as 100 MHz, 200 MHz, 300 MHz
194
- * console.log(freq.f_scaled); // Outputs: [0.1, 0.2, 0.3] (in GHz)
195
- * ```
196
- */
197
- set f_MHz(values: number[]);
198
- /**
199
- * Gets frequency points in GHz
200
- * @returns Array of frequency points in GHz
201
- *
202
- * @example
203
- * ```typescript
204
- * const freq = new Frequency();
205
- * freq.unit = 'MHz';
206
- * freq.f_scaled = [1000, 2000, 3000]; // 1000 MHz, 2000 MHz, 3000 MHz
207
- * console.log(freq.f_GHz); // Outputs: [1, 2, 3]
208
- * ```
209
- */
210
- get f_GHz(): number[];
211
- /**
212
- * Sets frequency points assuming input is in GHz
213
- * @param values - Array of frequency points in GHz
214
- *
215
- * @example
216
- * ```typescript
217
- * const freq = new Frequency();
218
- * freq.unit = 'MHz';
219
- * freq.f_GHz = [1, 2, 3]; // Sets frequencies as 1 GHz, 2 GHz, 3 GHz
220
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in MHz)
221
- * ```
222
- */
223
- set f_GHz(values: number[]);
224
- /**
225
- * Gets frequency points in THz
226
- * @returns Array of frequency points in THz
227
- *
228
- * @example
229
- * ```typescript
230
- * const freq = new Frequency();
231
- * freq.unit = 'GHz';
232
- * freq.f_scaled = [1000, 2000, 3000]; // 1000 GHz, 2000 GHz, 3000 GHz
233
- * console.log(freq.f_THz); // Outputs: [1, 2, 3]
234
- * ```
235
- */
236
- get f_THz(): number[];
237
- /**
238
- * Sets frequency points assuming input is in THz
239
- * @param values - Array of frequency points in THz
240
- *
241
- * @example
242
- * ```typescript
243
- * const freq = new Frequency();
244
- * freq.unit = 'GHz';
245
- * freq.f_THz = [1, 2, 3]; // Sets frequencies as 1 THz, 2 THz, 3 THz
246
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in GHz)
247
- * ```
248
- */
249
- set f_THz(values: number[]);
250
- /**
251
- * Private helper method to get wavelength values in a target unit.
252
- * @param targetWavelengthUnit - The key of the target wavelength unit in WAVELENGTH_MULTIPLIERS_TO_M.
253
- * @returns Array of wavelength points in the target unit.
254
- */
255
- private _getWavelengthInTargetUnit;
256
- /**
257
- * Private helper method to set frequency values from wavelength values in a source unit.
258
- * @param values - Array of wavelength points in the source unit.
259
- * @param sourceWavelengthUnit - The key of the source wavelength unit in WAVELENGTH_MULTIPLIERS_TO_M.
260
- */
261
- private _setWavelengthFromTargetUnit;
262
- /**
263
- * Gets wavelength in meters
264
- * @returns Array of wavelength values in meters
265
- *
266
- * @example
267
- * ```typescript
268
- * const freq = new Frequency();
269
- * freq.unit = 'GHz';
270
- * freq.f_scaled = [1, 2, 3]; // Frequencies in GHz
271
- * console.log(freq.wavelength_m); // Outputs: Wavelengths in meters
272
- * ```
273
- */
274
- get wavelength_m(): number[];
275
- /**
276
- * Sets wavelength in meters. Converts to frequency and stores.
277
- * @param values - Array of wavelength values in meters
278
- *
279
- * @example
280
- * ```typescript
281
- * const freq = new Frequency();
282
- * freq.unit = 'GHz';
283
- * freq.wavelength_m = [0.3, 0.15, 0.1]; // Wavelengths in meters
284
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
285
- * ```
286
- */
287
- set wavelength_m(values: number[]);
288
- /**
289
- * Gets wavelength in centimeters
290
- * @returns Array of wavelength values in centimeters
291
- *
292
- * @example
293
- * ```typescript
294
- * const freq = new Frequency();
295
- * freq.unit = 'GHz';
296
- * freq.f_scaled = [10, 20, 30]; // Frequencies in GHz
297
- * console.log(freq.wavelength_cm); // Outputs: Wavelengths in centimeters
298
- * ```
299
- */
300
- get wavelength_cm(): number[];
301
- /**
302
- * Sets wavelength in centimeters. Converts to frequency and stores.
303
- * @param values - Array of wavelength values in centimeters
304
- *
305
- * @example
306
- * ```typescript
307
- * const freq = new Frequency();
308
- * freq.unit = 'GHz';
309
- * freq.wavelength_cm = [30, 15, 10]; // Wavelengths in centimeters
310
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
311
- * ```
312
- */
313
- set wavelength_cm(values: number[]);
314
- /**
315
- * Gets wavelength in millimeters
316
- * @returns Array of wavelength values in millimeters
317
- *
318
- * @example
319
- * ```typescript
320
- * const freq = new Frequency();
321
- * freq.unit = 'GHz';
322
- * freq.f_scaled = [100, 200, 300]; // Frequencies in GHz
323
- * console.log(freq.wavelength_mm); // Outputs: Wavelengths in millimeters
324
- * ```
325
- */
326
- get wavelength_mm(): number[];
327
- /**
328
- * Sets wavelength in millimeters. Converts to frequency and stores.
329
- * @param values - Array of wavelength values in millimeters
330
- *
331
- * @example
332
- * ```typescript
333
- * const freq = new Frequency();
334
- * freq.unit = 'GHz';
335
- * freq.wavelength_mm = [300, 150, 100]; // Wavelengths in millimeters
336
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
337
- * ```
338
- */
339
- set wavelength_mm(values: number[]);
340
- /**
341
- * Gets wavelength in micrometers
342
- * @returns Array of wavelength values in micrometers
343
- *
344
- * @example
345
- * ```typescript
346
- * const freq = new Frequency();
347
- * freq.unit = 'GHz';
348
- * freq.f_scaled = [1000, 2000, 3000]; // Frequencies in GHz
349
- * console.log(freq.wavelength_um); // Outputs: Wavelengths in micrometers
350
- * ```
351
- */
352
- get wavelength_um(): number[];
353
- /**
354
- * Sets wavelength in micrometers. Converts to frequency and stores.
355
- * @param values - Array of wavelength values in micrometers
356
- *
357
- * @example
358
- * ```typescript
359
- * const freq = new Frequency();
360
- * freq.unit = 'GHz';
361
- * freq.wavelength_um = [300000, 150000, 100000]; // Wavelengths in micrometers
362
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
363
- * ```
364
- */
365
- set wavelength_um(values: number[]);
366
- /**
367
- * Gets wavelength in nanometers
368
- * @returns Array of wavelength values in nanometers
369
- *
370
- * @example
371
- * ```typescript
372
- * const freq = new Frequency();
373
- * freq.unit = 'THz';
374
- * freq.f_scaled = [100, 200, 300]; // Frequencies in THz
375
- * console.log(freq.wavelength_nm); // Outputs: Wavelengths in nanometers
376
- * ```
377
- */
378
- get wavelength_nm(): number[];
379
- /**
380
- * Sets wavelength in nanometers. Converts to frequency and stores.
381
- * @param values - Array of wavelength values in nanometers
382
- *
383
- * @example
384
- * ```typescript
385
- * const freq = new Frequency();
386
- * freq.unit = 'THz';
387
- * freq.wavelength_nm = [300, 150, 100]; // Wavelengths in nanometers
388
- * console.log(freq.f_scaled); // Outputs: Frequencies in THz
389
- * ```
390
- */
391
- set wavelength_nm(values: number[]);
392
- }
393
-
394
- /**
395
- * Type definition for frequency units
396
- * - Hz: Hertz (cycles per second)
397
- * - kHz: Kilohertz (10³ Hz)
398
- * - MHz: Megahertz (10⁶ Hz)
399
- * - GHz: Gigahertz (10⁹ Hz)
400
- * Touchstone standard doesn't support THz as the frequency unit in Touchstone file
401
- */
402
- export declare type FrequencyUnit = (typeof FrequencyUnits)[number];
403
-
404
- /**
405
- * Frequency units: 'Hz', 'kHz', 'MHz', 'GHz'
406
- * Touchstone standard doesn't support THz as the frequency unit in Touchstone file
407
- */
408
- export declare const FrequencyUnits: readonly ["Hz", "kHz", "MHz", "GHz"];
409
-
410
- export { index }
411
-
412
- export { log10 }
413
-
414
- export { multiply }
415
-
416
- export { pi }
417
-
418
- export { pow }
419
-
420
- export { range }
421
-
422
- export { round }
423
-
424
- /**
425
- * Speed of light in m/s
426
- */
427
- export declare const SPEED_OF_LIGHT = 299792458;
428
-
429
- export { subset }
430
-
431
- /**
432
- * Touchstone class for reading and writing network parameter data in Touchstone format.
433
- * Supports both version 1.0 and 1.1 of the Touchstone specification.
434
- *
435
- * @remarks
436
- * #### Overview
437
- *
438
- * The **Touchstone file format** (also known as `.snp` files) is an industry-standard ASCII text format used to represent the n-port network parameters of electrical circuits. These files are commonly used in RF and microwave engineering to describe the behavior of devices such as filters, amplifiers, and interconnects.
439
- *
440
- * A Touchstone file contains data about network parameters (e.g., S-parameters, Y-parameters, Z-parameters) at specific frequencies.
441
- *
442
- * ##### Key Features:
443
- * - **File Extensions**: Traditionally, Touchstone files use extensions like `.s1p`, `.s2p`, `.s3p`, etc., where the number indicates the number of ports. For example, `.s2p` represents a 2-port network.
444
- * - **Case Insensitivity**: Touchstone files are case-insensitive, meaning keywords and values can be written in uppercase or lowercase.
445
- * - **Versioning**: **Only version 1.0 and 1.1 are supported in this class**
446
- *
447
- * ---
448
- *
449
- * #### File Structure
450
- *
451
- * A Touchstone file consists of several sections, each serving a specific purpose. Below is a breakdown of the structure:
452
- *
453
- * ##### 1. Header Section
454
- *
455
- * - **Comment Lines**: Lines starting with `!` are treated as comments and ignored during parsing.
456
- * - **Option Line**: Line starting with `#` defines global settings for the file, such as frequency units, parameter type, and data format. Example:
457
- * ```plaintext
458
- * # GHz S MA R 50
459
- * ```
460
- * - `GHz`: Frequency unit (can be `Hz`, `kHz`, `MHz`, or `GHz`).
461
- * - `S`: Parameter type (`S`, `Y`, `Z`, `H`, or `G`).
462
- * - `MA`: Data format (`MA` for magnitude-angle, `DB` for decibel-angle, or `RI` for real-imaginary).
463
- * - `R 50`: Reference resistance in ohms (default is 50 ohms if omitted).
464
- *
465
- * ##### 2. Network Data
466
- *
467
- * The core of the file contains the network parameter data, organized by frequency. Each frequency point is followed by its corresponding parameter values.
468
- *
469
- * - **Single-Ended Networks**: Data is arranged in a matrix format. For example, a 2-port network might look like this:
470
- * ```plaintext
471
- * <frequency> <N11> <N21> <N12> <N22>
472
- * ```
473
- *
474
- * ---
475
- *
476
- * #### References:
477
- * - {@link https://ibis.org/touchstone_ver2.1/touchstone_ver2_1.pdf Touchstone(R) File Format Specification (Version 2.1)}
478
- * - {@link https://books.google.com/books/about/S_Parameters_for_Signal_Integrity.html?id=_dLKDwAAQBAJ S-Parameters for Signal Integrity}
479
- * - {@link https://github.com/scikit-rf/scikit-rf/blob/master/skrf/io/touchstone.py scikit-rf: Open Source RF Engineering}
480
- * - {@link https://github.com/Nubis-Communications/SignalIntegrity/blob/master/SignalIntegrity/Lib/SParameters/SParameters.py SignalIntegrity: Signal and Power Integrity Tools}
481
- *
482
- * @example
483
- *
484
- * #### Example 1: Simple 1-Port S-Parameter File
485
- * ```plaintext
486
- * ! 1-port S-parameter file
487
- * # MHz S MA R 50
488
- * 100 0.99 -4
489
- * 200 0.80 -22
490
- * 300 0.707 -45
491
- * ```
492
- *
493
- * #### Example 2: Simple 2-Port S-Parameter File
494
- * ```plaintext
495
- * ! Sample S2P File
496
- * # HZ S RI R 50
497
- * ! Freq S11(real) S11(imag) S21(real) S21(imag) S12(real) S12(imag) S22(real) S22(imag)
498
- * 1000000000 0.9 -0.1 0.01 0.02 0.01 0.02 0.8 -0.15
499
- * 2000000000 0.8 -0.2 0.02 0.03 0.02 0.03 0.7 -0.25
500
- * ```
501
- */
502
- export declare class Touchstone {
503
- /**
504
- * Comments in the file header with "!" symbol at the beginning of each row
505
- */
506
- comments: string[];
507
- /**
508
- * Touchstone format: MA, DB, and RI
509
- */
510
- private _format;
511
- /**
512
- * Set the Touchstone format: MA, DB, RI, or undefined
513
- * - RI: real and imaginary, i.e. $A + j \cdot B$
514
- * - MA: magnitude and angle (in degrees), i.e. $A \cdot e^{j \cdot {\pi \over 180} \cdot B }$
515
- * - DB: decibels and angle (in degrees), i.e. $10^{A \over 20} \cdot e^{j \cdot {\pi \over 180} \cdot B}$
516
- * @param format
517
- * @returns
518
- * @throws Will throw an error if the format is not valid
519
- */
520
- set format(format: TouchstoneFormat | undefined | null);
521
- /**
522
- * Get the Touchstone format
523
- * @returns
524
- */
525
- get format(): TouchstoneFormat | undefined;
526
- /**
527
- * Type of network parameter: 'S' | 'Y' | 'Z' | 'G' | 'H'
528
- */
529
- private _parameter;
530
- /**
531
- * Set the type of network parameter
532
- * - S: Scattering parameters
533
- * - Y: Admittance parameters
534
- * - Z: Impedance parameters
535
- * - H: Hybrid-h parameters
536
- * - G: Hybrid-g parameters
537
- * @param parameter
538
- * @returns
539
- * @throws Will throw an error if the parameter is not valid
540
- */
541
- set parameter(parameter: TouchstoneParameter | undefined | null);
542
- /**
543
- * Get the type of network parameter
544
- */
545
- get parameter(): TouchstoneParameter | undefined | null;
546
- /**
547
- * Reference impedance(s) for the network parameters
548
- * Default: 50Ω
549
- */
550
- private _impedance;
551
- /**
552
- * Set the Touchstone impedance.
553
- * Default: 50Ω
554
- * @param impedance
555
- * @returns
556
- * @throws Will throw an error if the impedance is not valid
557
- */
558
- set impedance(impedance: TouchstoneImpedance);
559
- /**
560
- * Get the Touchstone impedance.
561
- * Default: 50Ω
562
- * @returns
563
- */
564
- get impedance(): TouchstoneImpedance;
565
- /**
566
- * The number of ports in the network
567
- */
568
- private _nports;
569
- /**
570
- * Set the ports number
571
- * @param nports
572
- * @returns
573
- * @throws Will throw an error if the number of ports is not valid
574
- */
575
- set nports(nports: number | undefined | null);
576
- /**
577
- * Get the ports number
578
- */
579
- get nports(): number | undefined | null;
580
- /**
581
- * Frequency points
582
- */
583
- frequency: Frequency | undefined;
584
- /**
585
- * 3D array to store the network parameter data
586
- * - The first dimension is the exits (output) port number
587
- * - The second dimension is the enters (input) port number
588
- * - The third dimension is the frequency index
589
- * For example, data[i][j][k] would be the parameter from j+1 port to i+1 port at frequency index k
590
- */
591
- private _matrix;
592
- /**
593
- * Sets the network parameter matrix.
594
- *
595
- * @param matrix - The 3D complex matrix to store, or undefined/null to clear
596
- * @remarks
597
- * This setter provides a way to directly assign the network parameter matrix.
598
- * Setting to undefined or null will clear the existing matrix data.
599
- */
600
- set matrix(matrix: TouchstoneMatrix | undefined | null);
601
- /**
602
- * Gets the current network parameter matrix (3D array).
603
- * Represents the S/Y/Z/G/H-parameters of the network.
604
- *
605
- * @remarks
606
- * Matrix Structure:
607
- * - First dimension [i]: Output (exit) port number (0 to nports-1)
608
- * - Second dimension [j]: Input (enter) port number (0 to nports-1)
609
- * - Third dimension [k]: Frequency point index
610
- *
611
- * Example:
612
- * - matrix[i][j][k] represents the parameter from port j+1 to port i+1 at frequency k
613
- * - For S-parameters: matrix[1][0][5] is S₂₁ at the 6th frequency point
614
- *
615
- * Special case for 2-port networks:
616
- * - Indices are swapped to match traditional Touchstone format
617
- * - matrix[0][1][k] represents S₁₂ (not S₂₁)
618
- *
619
- * @returns The current network parameter matrix, or undefined if not set
620
- */
621
- get matrix(): TouchstoneMatrix | undefined | null;
622
- /**
623
- * Reads and parses a Touchstone format string into the internal data structure.
624
- *
625
- * @param string - The Touchstone format string to parse
626
- * @param nports - Number of ports in the network
627
- *
628
- * @throws {Error} If the option line is missing or invalid
629
- * @throws {Error} If multiple option lines are found
630
- * @throws {Error} If the impedance specification is invalid
631
- * @throws {Error} If the data format is invalid or incomplete
632
- *
633
- * @remarks
634
- * The method performs the following steps:
635
- * 1. Parses comments and option line
636
- * 2. Extracts frequency points
637
- * 3. Converts raw data into complex numbers based on format
638
- * 4. Stores the results in the matrix property
639
- *
640
- * @example
641
- * ```typescript
642
- * import { Touchstone, Frequency } from 'rf-touchstone';
643
- *
644
- * const s1pString = `
645
- * ! This is a 1-port S-parameter file
646
- * # MHz S MA R 50
647
- * 100 0.99 -4
648
- * 200 0.80 -22
649
- * 300 0.707 -45
650
- * `;
651
- *
652
- * const touchstone = new Touchstone();
653
- * touchstone.readContent(s1pString, 1);
654
- *
655
- * console.log(touchstone.comments); // Outputs: [ 'This is a 1-port S-parameter file' ]
656
- * console.log(touchstone.format); // Outputs: 'MA'
657
- * console.log(touchstone.parameter); // Outputs: 'S'
658
- * console.log(touchstone.impedance); // Outputs: 50
659
- * console.log(touchstone.nports); // Outputs: 1
660
- * console.log(touchstone.frequency?.f_scaled); // Outputs: [ 100, 200, 300 ]
661
- * console.log(touchstone.matrix); // Outputs: the parsed matrix data
662
- * ```
663
- */
664
- readContent(string: string, nports: number): void;
665
- /**
666
- * Validates the internal state of the Touchstone instance.
667
- * Performs comprehensive checks on all required data and matrix dimensions.
668
- *
669
- * @throws {Error} If any of the following conditions are met:
670
- * - Number of ports is undefined
671
- * - Frequency object is not initialized
672
- * - Frequency points array is empty
673
- * - Network parameter type is undefined
674
- * - Data format is undefined
675
- * - Network parameter matrix is undefined
676
- * - Matrix dimensions don't match with nports or frequency points
677
- *
678
- * @remarks
679
- * This method performs two main validation steps:
680
- * 1. Essential Data Validation:
681
- * - Checks existence of all required properties
682
- * - Ensures frequency points are available
683
- *
684
- * 2. Matrix Dimension Validation:
685
- * - Verifies matrix row count matches port number
686
- * - Ensures each row has correct number of columns
687
- * - Validates frequency points count in each matrix element
688
- */
689
- validate(): void;
690
- /**
691
- * Generates a Touchstone format string from the internal data structure.
692
- *
693
- * @returns The generated Touchstone format string
694
- *
695
- * @throws {Error} If any required data is missing
696
- * @throws {Error} If the matrix dimensions are invalid
697
- *
698
- * @remarks
699
- * The generated string includes:
700
- * 1. Comments (if any)
701
- * 2. Option line with format, parameter type, and impedance
702
- * 3. Network parameter data in the specified format
703
- *
704
- * @example
705
- * ```typescript
706
- * import { Touchstone, Frequency } from 'rf-touchstone';
707
- * import { complex } from 'mathjs';
708
- *
709
- * const touchstone = new Touchstone();
710
- *
711
- * // Set properties and matrix data (using simplified complex number representation for example)
712
- * touchstone.comments = ['Generated by rf-touchstone'];
713
- * touchstone.nports = 1;
714
- * touchstone.frequency = new Frequency();
715
- * touchstone.frequency.unit = 'GHz';
716
- * touchstone.frequency.f_scaled = [1.0, 2.0];
717
- * touchstone.parameter = 'S';
718
- * touchstone.format = 'MA';
719
- * touchstone.impedance = 50;
720
- * touchstone.matrix = [
721
- * [ // Output port 1
722
- * [complex(0.5, 0.1), complex(0.4, 0.2)] // S11 at each frequency
723
- * ]
724
- * ];
725
- *
726
- * const s1pString = touchstone.writeContent();
727
- * console.log(s1pString);
728
- * // Expected output (approximately, due to floating point precision):
729
- * // ! Generated by rf-touchstone
730
- * // # GHz S MA R 50
731
- * // 1 0.5099 11.3099
732
- * // 2 0.4472 26.5651
733
- * ```
734
- */
735
- writeContent(): string;
736
- }
737
-
738
- /**
739
- * S-parameter format: MA, DB, and RI
740
- * - RI: real and imaginary, i.e. $A + j \cdot B$
741
- * - MA: magnitude and angle (in degrees), i.e. $A \cdot e^{j \cdot {\pi \over 180} \cdot B }$
742
- * - DB: decibels and angle (in degrees), i.e. $10^{A \over 20} \cdot e^{j \cdot {\pi \over 180} \cdot B}$
743
- */
744
- export declare type TouchstoneFormat = (typeof TouchstoneFormats)[number];
745
-
746
- /**
747
- * S-parameter format: MA, DB, and RI
748
- * - RI: real and imaginary, i.e. $A + j \cdot B$
749
- * - MA: magnitude and angle (in degrees), i.e. $A \cdot e^{j \cdot {\pi \over 180} \cdot B }$
750
- * - DB: decibels and angle (in degrees), i.e. $10^{A \over 20} \cdot e^{j \cdot {\pi \over 180} \cdot B}$
751
- */
752
- export declare const TouchstoneFormats: readonly ["RI", "MA", "DB"];
753
-
754
- /**
755
- * The reference resistance(s) for the network parameters.
756
- * The token "R" (case-insensitive) followed by one or more reference resistance values.
757
- * Default: 50Ω
758
- *
759
- * For Touchstone 1.0, this is a single value for all ports.
760
- * For Touchstone 1.1, this can be an array of values (one per port)
761
- */
762
- export declare type TouchstoneImpedance = number | number[];
763
-
764
- /**
765
- * Network parameter matrix stored as complex numbers.
766
- *
767
- * @remarks
768
- * The matrix is a 3D array with the following dimensions:
769
- * - First dimension: output port index (0 to nports-1)
770
- * - Second dimension: input port index (0 to nports-1)
771
- * - Third dimension: frequency point index
772
- *
773
- * For example:
774
- * - matrix[i][j][k] represents the parameter from port j+1 to port i+1 at frequency k
775
- * - For S-parameters: matrix[1][0][5] is S₂₁ at the 6th frequency point
776
- *
777
- * Special case for 2-port networks:
778
- * - Indices are swapped to match traditional Touchstone format
779
- * - matrix[0][1][k] represents S₁₂ (not S₂₁)
780
- */
781
- export declare type TouchstoneMatrix = Complex[][][];
782
-
783
- /**
784
- * Type of network parameters: 'S' | 'Y' | 'Z' | 'G' | 'H'
785
- * - S: Scattering parameters
786
- * - Y: Admittance parameters
787
- * - Z: Impedance parameters
788
- * - H: Hybrid-h parameters
789
- * - G: Hybrid-g parameters
790
- */
791
- export declare type TouchstoneParameter = (typeof TouchstoneParameters)[number];
792
-
793
- /**
794
- * Type of network parameters
795
- * - S: Scattering parameters
796
- * - Y: Admittance parameters
797
- * - Z: Impedance parameters
798
- * - H: Hybrid-h parameters
799
- * - G: Hybrid-g parameters
800
- */
801
- export declare const TouchstoneParameters: string[];
802
-
803
- export { }
1
+ import { Complex } from 'mathjs';
2
+ import { Frequency } from './frequency';
3
+ /**
4
+ * S-parameter format: MA, DB, and RI
5
+ * - RI: real and imaginary, i.e. $A + j \cdot B$
6
+ * - MA: magnitude and angle (in degrees), i.e. $A \cdot e^{j \cdot {\pi \over 180} \cdot B }$
7
+ * - DB: decibels and angle (in degrees), i.e. $10^{A \over 20} \cdot e^{j \cdot {\pi \over 180} \cdot B}$
8
+ */
9
+ export declare const TouchstoneFormats: readonly ["RI", "MA", "DB"];
10
+ /**
11
+ * S-parameter format: MA, DB, and RI
12
+ * - RI: real and imaginary, i.e. $A + j \cdot B$
13
+ * - MA: magnitude and angle (in degrees), i.e. $A \cdot e^{j \cdot {\pi \over 180} \cdot B }$
14
+ * - DB: decibels and angle (in degrees), i.e. $10^{A \over 20} \cdot e^{j \cdot {\pi \over 180} \cdot B}$
15
+ */
16
+ export type TouchstoneFormat = (typeof TouchstoneFormats)[number];
17
+ /**
18
+ * Type of network parameters
19
+ * - S: Scattering parameters
20
+ * - Y: Admittance parameters
21
+ * - Z: Impedance parameters
22
+ * - H: Hybrid-h parameters
23
+ * - G: Hybrid-g parameters
24
+ */
25
+ export declare const TouchstoneParameters: string[];
26
+ /**
27
+ * Type of network parameters: 'S' | 'Y' | 'Z' | 'G' | 'H'
28
+ * - S: Scattering parameters
29
+ * - Y: Admittance parameters
30
+ * - Z: Impedance parameters
31
+ * - H: Hybrid-h parameters
32
+ * - G: Hybrid-g parameters
33
+ */
34
+ export type TouchstoneParameter = (typeof TouchstoneParameters)[number];
35
+ /**
36
+ * The reference resistance(s) for the network parameters.
37
+ * The token "R" (case-insensitive) followed by one or more reference resistance values.
38
+ * Default: 50Ω
39
+ *
40
+ * For Touchstone 1.0, this is a single value for all ports.
41
+ * For Touchstone 1.1, this can be an array of values (one per port)
42
+ */
43
+ export type TouchstoneImpedance = number | number[];
44
+ /**
45
+ * Network parameter matrix stored as complex numbers.
46
+ *
47
+ * @remarks
48
+ * The matrix is a 3D array with the following dimensions:
49
+ * - First dimension: output port index (0 to nports-1)
50
+ * - Second dimension: input port index (0 to nports-1)
51
+ * - Third dimension: frequency point index
52
+ *
53
+ * For example:
54
+ * - matrix[i][j][k] represents the parameter from port j+1 to port i+1 at frequency k
55
+ * - For S-parameters: matrix[1][0][5] is S₂₁ at the 6th frequency point
56
+ *
57
+ * Special case for 2-port networks:
58
+ * - Indices are swapped to match traditional Touchstone format
59
+ * - matrix[0][1][k] represents S₁₂ (not S₂₁)
60
+ */
61
+ export type TouchstoneMatrix = Complex[][][];
62
+ /**
63
+ * Touchstone class for reading and writing network parameter data in Touchstone format.
64
+ * Supports both version 1.0 and 1.1 of the Touchstone specification.
65
+ *
66
+ * @remarks
67
+ * #### Overview
68
+ *
69
+ * The **Touchstone file format** (also known as `.snp` files) is an industry-standard ASCII text format used to represent the n-port network parameters of electrical circuits. These files are commonly used in RF and microwave engineering to describe the behavior of devices such as filters, amplifiers, and interconnects.
70
+ *
71
+ * A Touchstone file contains data about network parameters (e.g., S-parameters, Y-parameters, Z-parameters) at specific frequencies.
72
+ *
73
+ * ##### Key Features:
74
+ * - **File Extensions**: Traditionally, Touchstone files use extensions like `.s1p`, `.s2p`, `.s3p`, etc., where the number indicates the number of ports. For example, `.s2p` represents a 2-port network.
75
+ * - **Case Insensitivity**: Touchstone files are case-insensitive, meaning keywords and values can be written in uppercase or lowercase.
76
+ * - **Versioning**: **Only version 1.0 and 1.1 are supported in this class**
77
+ *
78
+ * ---
79
+ *
80
+ * #### File Structure
81
+ *
82
+ * A Touchstone file consists of several sections, each serving a specific purpose. Below is a breakdown of the structure:
83
+ *
84
+ * ##### 1. Header Section
85
+ *
86
+ * - **Comment Lines**: Lines starting with `!` are treated as comments and ignored during parsing.
87
+ * - **Option Line**: Line starting with `#` defines global settings for the file, such as frequency units, parameter type, and data format. Example:
88
+ * ```plaintext
89
+ * # GHz S MA R 50
90
+ * ```
91
+ * - `GHz`: Frequency unit (can be `Hz`, `kHz`, `MHz`, or `GHz`).
92
+ * - `S`: Parameter type (`S`, `Y`, `Z`, `H`, or `G`).
93
+ * - `MA`: Data format (`MA` for magnitude-angle, `DB` for decibel-angle, or `RI` for real-imaginary).
94
+ * - `R 50`: Reference resistance in ohms (default is 50 ohms if omitted).
95
+ *
96
+ * ##### 2. Network Data
97
+ *
98
+ * The core of the file contains the network parameter data, organized by frequency. Each frequency point is followed by its corresponding parameter values.
99
+ *
100
+ * - **Single-Ended Networks**: Data is arranged in a matrix format. For example, a 2-port network might look like this:
101
+ * ```plaintext
102
+ * <frequency> <N11> <N21> <N12> <N22>
103
+ * ```
104
+ *
105
+ * ---
106
+ *
107
+ * #### References:
108
+ * - {@link https://ibis.org/touchstone_ver2.1/touchstone_ver2_1.pdf Touchstone(R) File Format Specification (Version 2.1)}
109
+ * - {@link https://books.google.com/books/about/S_Parameters_for_Signal_Integrity.html?id=_dLKDwAAQBAJ S-Parameters for Signal Integrity}
110
+ * - {@link https://github.com/scikit-rf/scikit-rf/blob/master/skrf/io/touchstone.py scikit-rf: Open Source RF Engineering}
111
+ * - {@link https://github.com/Nubis-Communications/SignalIntegrity/blob/master/SignalIntegrity/Lib/SParameters/SParameters.py SignalIntegrity: Signal and Power Integrity Tools}
112
+ *
113
+ * @example
114
+ *
115
+ * #### Example 1: Simple 1-Port S-Parameter File
116
+ * ```plaintext
117
+ * ! 1-port S-parameter file
118
+ * # MHz S MA R 50
119
+ * 100 0.99 -4
120
+ * 200 0.80 -22
121
+ * 300 0.707 -45
122
+ * ```
123
+ *
124
+ * #### Example 2: Simple 2-Port S-Parameter File
125
+ * ```plaintext
126
+ * ! Sample S2P File
127
+ * # HZ S RI R 50
128
+ * ! Freq S11(real) S11(imag) S21(real) S21(imag) S12(real) S12(imag) S22(real) S22(imag)
129
+ * 1000000000 0.9 -0.1 0.01 0.02 0.01 0.02 0.8 -0.15
130
+ * 2000000000 0.8 -0.2 0.02 0.03 0.02 0.03 0.7 -0.25
131
+ * ```
132
+ */
133
+ export declare class Touchstone {
134
+ /**
135
+ * Comments in the file header with `!` symbol at the beginning of each row
136
+ */
137
+ comments: string[];
138
+ /**
139
+ * Touchstone format: MA, DB, and RI
140
+ */
141
+ private _format;
142
+ /**
143
+ * Set the Touchstone format: MA, DB, RI, or undefined
144
+ * - RI: real and imaginary, i.e. $A + j \cdot B$
145
+ * - MA: magnitude and angle (in degrees), i.e. $A \cdot e^{j \cdot {\pi \over 180} \cdot B }$
146
+ * - DB: decibels and angle (in degrees), i.e. $10^{A \over 20} \cdot e^{j \cdot {\pi \over 180} \cdot B}$
147
+ * @param format
148
+ * @returns
149
+ * @throws Will throw an error if the format is not valid
150
+ */
151
+ set format(format: TouchstoneFormat | undefined | null);
152
+ /**
153
+ * Get the Touchstone format
154
+ * @returns
155
+ */
156
+ get format(): TouchstoneFormat | undefined;
157
+ /**
158
+ * Type of network parameter: 'S' | 'Y' | 'Z' | 'G' | 'H'
159
+ */
160
+ private _parameter;
161
+ /**
162
+ * Set the type of network parameter
163
+ * - S: Scattering parameters
164
+ * - Y: Admittance parameters
165
+ * - Z: Impedance parameters
166
+ * - H: Hybrid-h parameters
167
+ * - G: Hybrid-g parameters
168
+ * @param parameter
169
+ * @returns
170
+ * @throws Will throw an error if the parameter is not valid
171
+ */
172
+ set parameter(parameter: TouchstoneParameter | undefined | null);
173
+ /**
174
+ * Get the type of network parameter
175
+ */
176
+ get parameter(): TouchstoneParameter | undefined | null;
177
+ /**
178
+ * Reference impedance(s) for the network parameters
179
+ * Default: 50Ω
180
+ */
181
+ private _impedance;
182
+ /**
183
+ * Set the Touchstone impedance.
184
+ * Default: 50Ω
185
+ * @param impedance
186
+ * @returns
187
+ * @throws Will throw an error if the impedance is not valid
188
+ */
189
+ set impedance(impedance: TouchstoneImpedance);
190
+ /**
191
+ * Get the Touchstone impedance.
192
+ * Default: 50Ω
193
+ * @returns
194
+ */
195
+ get impedance(): TouchstoneImpedance;
196
+ /**
197
+ * The number of ports in the network
198
+ */
199
+ private _nports;
200
+ /**
201
+ * Set the ports number
202
+ * @param nports
203
+ * @returns
204
+ * @throws Will throw an error if the number of ports is not valid
205
+ */
206
+ set nports(nports: number | undefined | null);
207
+ /**
208
+ * Get the ports number
209
+ */
210
+ get nports(): number | undefined | null;
211
+ /**
212
+ * Frequency points
213
+ */
214
+ frequency: Frequency | undefined;
215
+ /**
216
+ * 3D array to store the network parameter data
217
+ * - The first dimension is the exits (output) port number
218
+ * - The second dimension is the enters (input) port number
219
+ * - The third dimension is the frequency index
220
+ * For example, data[i][j][k] would be the parameter from j+1 port to i+1 port at frequency index k
221
+ */
222
+ private _matrix;
223
+ /**
224
+ * Sets the network parameter matrix.
225
+ *
226
+ * @param matrix - The 3D complex matrix to store, or undefined/null to clear
227
+ * @remarks
228
+ * This setter provides a way to directly assign the network parameter matrix.
229
+ * Setting to undefined or null will clear the existing matrix data.
230
+ */
231
+ set matrix(matrix: TouchstoneMatrix | undefined | null);
232
+ /**
233
+ * Gets the current network parameter matrix (3D array).
234
+ * Represents the S/Y/Z/G/H-parameters of the network.
235
+ *
236
+ * @remarks
237
+ * Matrix Structure:
238
+ * - First dimension [i]: Output (exit) port number (0 to nports-1)
239
+ * - Second dimension [j]: Input (enter) port number (0 to nports-1)
240
+ * - Third dimension [k]: Frequency point index
241
+ *
242
+ * Example:
243
+ * - matrix[i][j][k] represents the parameter from port j+1 to port i+1 at frequency k
244
+ * - For S-parameters: matrix[1][0][5] is S₂₁ at the 6th frequency point
245
+ *
246
+ * Special case for 2-port networks:
247
+ * - Indices are swapped to match traditional Touchstone format
248
+ * - matrix[0][1][k] represents S₁₂ (not S₂₁)
249
+ *
250
+ * @returns The current network parameter matrix, or undefined if not set
251
+ */
252
+ get matrix(): TouchstoneMatrix | undefined | null;
253
+ /**
254
+ * Reads and parses a Touchstone format string into the internal data structure.
255
+ *
256
+ * @param string - The Touchstone format string to parse
257
+ * @param nports - Number of ports in the network
258
+ *
259
+ * @throws {Error} If the option line is missing or invalid
260
+ * @throws {Error} If multiple option lines are found
261
+ * @throws {Error} If the impedance specification is invalid
262
+ * @throws {Error} If the data format is invalid or incomplete
263
+ *
264
+ * @remarks
265
+ * The method performs the following steps:
266
+ * 1. Parses comments and option line
267
+ * 2. Extracts frequency points
268
+ * 3. Converts raw data into complex numbers based on format
269
+ * 4. Stores the results in the matrix property
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * import { Touchstone, Frequency } from 'rf-touchstone';
274
+ *
275
+ * const s1pString = `
276
+ * ! This is a 1-port S-parameter file
277
+ * # MHz S MA R 50
278
+ * 100 0.99 -4
279
+ * 200 0.80 -22
280
+ * 300 0.707 -45
281
+ * `;
282
+ *
283
+ * const touchstone = new Touchstone();
284
+ * touchstone.readContent(s1pString, 1);
285
+ *
286
+ * console.log(touchstone.comments); // Outputs: [ 'This is a 1-port S-parameter file' ]
287
+ * console.log(touchstone.format); // Outputs: 'MA'
288
+ * console.log(touchstone.parameter); // Outputs: 'S'
289
+ * console.log(touchstone.impedance); // Outputs: 50
290
+ * console.log(touchstone.nports); // Outputs: 1
291
+ * console.log(touchstone.frequency?.f_scaled); // Outputs: [ 100, 200, 300 ]
292
+ * console.log(touchstone.matrix); // Outputs: the parsed matrix data
293
+ * ```
294
+ */
295
+ readContent(string: string, nports: number): void;
296
+ /**
297
+ * Validates the internal state of the Touchstone instance.
298
+ * Performs comprehensive checks on all required data and matrix dimensions.
299
+ *
300
+ * @throws {Error} If any of the following conditions are met:
301
+ * - Number of ports is undefined
302
+ * - Frequency object is not initialized
303
+ * - Frequency points array is empty
304
+ * - Network parameter type is undefined
305
+ * - Data format is undefined
306
+ * - Network parameter matrix is undefined
307
+ * - Matrix dimensions don't match with nports or frequency points
308
+ *
309
+ * @remarks
310
+ * This method performs two main validation steps:
311
+ * 1. Essential Data Validation:
312
+ * - Checks existence of all required properties
313
+ * - Ensures frequency points are available
314
+ *
315
+ * 2. Matrix Dimension Validation:
316
+ * - Verifies matrix row count matches port number
317
+ * - Ensures each row has correct number of columns
318
+ * - Validates frequency points count in each matrix element
319
+ */
320
+ validate(): void;
321
+ /**
322
+ * Generates a Touchstone format string from the internal data structure.
323
+ *
324
+ * @returns The generated Touchstone format string
325
+ *
326
+ * @throws {Error} If any required data is missing
327
+ * @throws {Error} If the matrix dimensions are invalid
328
+ *
329
+ * @remarks
330
+ * The generated string includes:
331
+ * 1. Comments (if any)
332
+ * 2. Option line with format, parameter type, and impedance
333
+ * 3. Network parameter data in the specified format
334
+ *
335
+ * @example
336
+ * ```typescript
337
+ * import { Touchstone, Frequency } from 'rf-touchstone';
338
+ * import { complex } from 'mathjs';
339
+ *
340
+ * const touchstone = new Touchstone();
341
+ *
342
+ * // Set properties and matrix data (using simplified complex number representation for example)
343
+ * touchstone.comments = ['Generated by rf-touchstone'];
344
+ * touchstone.nports = 1;
345
+ * touchstone.frequency = new Frequency();
346
+ * touchstone.frequency.unit = 'GHz';
347
+ * touchstone.frequency.f_scaled = [1.0, 2.0];
348
+ * touchstone.parameter = 'S';
349
+ * touchstone.format = 'MA';
350
+ * touchstone.impedance = 50;
351
+ * touchstone.matrix = [
352
+ * [ // Output port 1
353
+ * [complex(0.5, 0.1), complex(0.4, 0.2)] // S11 at each frequency
354
+ * ]
355
+ * ];
356
+ *
357
+ * const s1pString = touchstone.writeContent();
358
+ * console.log(s1pString);
359
+ * // Expected output (approximately, due to floating point precision):
360
+ * // ! Generated by rf-touchstone
361
+ * // # GHz S MA R 50
362
+ * // 1 0.5099 11.3099
363
+ * // 2 0.4472 26.5651
364
+ * ```
365
+ */
366
+ writeContent(): string;
367
+ }