rf-touchstone 0.0.2 → 0.0.3

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/src/frequency.ts CHANGED
@@ -1,16 +1,15 @@
1
1
  /**
2
- * Frequency units: 'Hz', 'kHz', 'MHz', 'GHz'
3
- * Touchstone standard doesn't support THz as the frequency unit in Touchstone file
2
+ * Supported frequency units in the Touchstone specification.
3
+ * Note: THz is not officially supported as a file header unit in Touchstone v1.x/v2.x.
4
4
  */
5
5
  export const FrequencyUnits = ['Hz', 'kHz', 'MHz', 'GHz'] as const
6
6
 
7
7
  /**
8
- * Type definition for frequency units
9
- * - Hz: Hertz (cycles per second)
10
- * - kHz: Kilohertz (10³ Hz)
11
- * - MHz: Megahertz (10 Hz)
12
- * - GHz: Gigahertz (10 Hz)
13
- * Touchstone standard doesn't support THz as the frequency unit in Touchstone file
8
+ * Type representing the frequency unit.
9
+ * - Hz: Hertz ($10^0$ Hz)
10
+ * - kHz: Kilohertz ($10^3$ Hz)
11
+ * - MHz: Megahertz ($10^6$ Hz)
12
+ * - GHz: Gigahertz ($10^9$ Hz)
14
13
  */
15
14
  export type FrequencyUnit = (typeof FrequencyUnits)[number]
16
15
 
@@ -42,19 +41,29 @@ export const WAVELENGTH_MULTIPLIERS_TO_M: Record<string, number> = {
42
41
  }
43
42
 
44
43
  /**
45
- * Class representing frequency data in RF and microwave engineering
44
+ * Represents frequency data and provides utility methods for unit conversion and wavelength calculation.
46
45
  *
47
46
  * @remarks
48
- * The Frequency class manages frequency-related data, including:
49
- * - Frequency unit management
50
- * - Storage of frequency points
51
- * - Unit conversion capabilities
47
+ * The `Frequency` class is designed to handle frequency points commonly used in RF, microwave,
48
+ * and high-speed digital engineering. It maintains an internal unit and a set of frequency points,
49
+ * allowing for seamless conversion between different frequency and wavelength units.
50
+ *
51
+ * ##### Key Features:
52
+ * - **Unit Awareness**: Keeps track of whether frequencies are defined in Hz, MHz, GHz, etc.
53
+ * - **Automatic Conversion**: Automatically scales frequency points when the `unit` property is changed.
54
+ * - **Wavelength Utilities**: Provides getters and setters for wavelength ($\lambda = c/f$) in various metric units.
52
55
  *
53
56
  * @example
57
+ * #### Basic Usage
54
58
  * ```typescript
59
+ * import { Frequency } from 'rf-touchstone';
60
+ *
55
61
  * const freq = new Frequency();
56
62
  * freq.unit = 'GHz';
57
- * freq.f_scale = [1.0, 2.0, 3.0];
63
+ * freq.f_scaled = [1.0, 2.0, 5.0];
64
+ *
65
+ * console.log(freq.f_Hz); // [1e9, 2e9, 5e9]
66
+ * console.log(freq.wavelength_mm); // [299.79, 149.90, 59.96]
58
67
  * ```
59
68
  */
60
69
  export class Frequency {
@@ -65,16 +74,11 @@ export class Frequency {
65
74
  private _unit: FrequencyUnit = 'Hz'
66
75
 
67
76
  /**
68
- * Sets the frequency unit for the instance
69
- *
70
- * @param newUnit - The frequency unit to set
71
- * @throws {Error} If the provided unit is not one of the supported frequency units
77
+ * Sets the frequency unit. If frequency points already exist, they will be
78
+ * automatically rescaled to the new unit.
72
79
  *
73
- * @example
74
- * ```typescript
75
- * const freq = new Frequency();
76
- * freq.unit = 'MHz'; // Sets unit to Megahertz
77
- * ```
80
+ * @param newUnit - The target frequency unit (Hz, kHz, MHz, or GHz).
81
+ * @throws Error if the provided unit is invalid.
78
82
  */
79
83
  set unit(newUnit: FrequencyUnit) {
80
84
  if (typeof newUnit !== 'string') {
@@ -126,15 +130,7 @@ export class Frequency {
126
130
  }
127
131
 
128
132
  /**
129
- * Gets the current frequency unit
130
- *
131
- * @returns The current frequency unit
132
- *
133
- * @example
134
- * ```typescript
135
- * const freq = new Frequency();
136
- * console.log(freq.unit); // Outputs: 'Hz'
137
- * ```
133
+ * Gets the current frequency unit.
138
134
  */
139
135
  get unit(): FrequencyUnit {
140
136
  return this._unit
@@ -148,20 +144,10 @@ export class Frequency {
148
144
  private _f_scaled: number[] = []
149
145
 
150
146
  /**
151
- * Array of frequency points in the current frequency unit
152
- * Each element represents a discrete frequency point for measurement or analysis
153
- *
154
- * @param value - Array of frequency points
155
- * @throws {Error} If the input is not an array
156
- * @throws {Error} If any element in the array is not a number
157
- * @throws {Error} If the array contains negative frequencies
147
+ * Sets the array of frequency points in the current frequency unit.
158
148
  *
159
- * @example
160
- * ```typescript
161
- * const freq = new Frequency();
162
- * freq.unit = 'GHz';
163
- * freq.f_scaled = [1.0, 1.5, 2.0]; // Sets frequency points in GHz
164
- * ```
149
+ * @param value - Array of numerical frequency points.
150
+ * @throws Error if the input is not a non-negative number array.
165
151
  */
166
152
  set f_scaled(value: number[]) {
167
153
  // Validate input is an array
@@ -187,16 +173,7 @@ export class Frequency {
187
173
  }
188
174
 
189
175
  /**
190
- * Gets the array of frequency points
191
- * @returns Array of frequency points in the current unit
192
- *
193
- * @example
194
- * ```typescript
195
- * const freq = new Frequency();
196
- * freq.unit = 'MHz';
197
- * freq.f_scaled = [100, 200, 300];
198
- * console.log(freq.f_scaled); // Outputs: [100, 200, 300]
199
- * ```
176
+ * Gets the array of frequency points in the current unit.
200
177
  */
201
178
  get f_scaled(): number[] {
202
179
  return this._f_scaled
@@ -234,9 +211,9 @@ export class Frequency {
234
211
  }
235
212
 
236
213
  /**
237
- * Private helper method to set frequency values from a source unit.
238
- * @param values - Array of frequency points in the source unit.
239
- * @param sourceUnit - The key of the source frequency unit in FREQUENCY_MULTIPLIERS.
214
+ * Internal helper to set frequency values from a source unit.
215
+ * @param values - Array of frequency points.
216
+ * @param sourceUnit - The source frequency unit key.
240
217
  */
241
218
  private _setFrequencyFromTargetUnit(
242
219
  values: number[],
@@ -279,160 +256,70 @@ export class Frequency {
279
256
  }
280
257
 
281
258
  /**
282
- * Gets frequency points in Hz
283
- * @returns Array of frequency points in Hz
284
- *
285
- * @example
286
- * ```typescript
287
- * const freq = new Frequency();
288
- * freq.unit = 'GHz';
289
- * freq.f_scaled = [1, 2, 3]; // 1 GHz, 2 GHz, 3 GHz
290
- * console.log(freq.f_Hz); // Outputs: [1e9, 2e9, 3e9]
291
- * ```
259
+ * Gets the frequency points in Hertz (Hz).
292
260
  */
293
261
  get f_Hz(): number[] {
294
262
  return this._getFrequencyInTargetUnit('Hz')
295
263
  }
296
264
 
297
265
  /**
298
- * Sets frequency points assuming input is in Hz
299
- * @param values - Array of frequency points in Hz
300
- *
301
- * @example
302
- * ```typescript
303
- * const freq = new Frequency();
304
- * freq.unit = 'MHz';
305
- * freq.f_Hz = [1e9, 2e9, 3e9]; // Sets frequencies as 1 GHz, 2 GHz, 3 GHz
306
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in MHz)
307
- * ```
266
+ * Sets the frequency points in Hertz (Hz).
308
267
  */
309
268
  set f_Hz(values: number[]) {
310
269
  this._setFrequencyFromTargetUnit(values, 'Hz')
311
270
  }
312
271
 
313
272
  /**
314
- * Gets frequency points in kHz
315
- * @returns Array of frequency points in kHz
316
- *
317
- * @example
318
- * ```typescript
319
- * const freq = new Frequency();
320
- * freq.unit = 'Hz';
321
- * freq.f_scaled = [1000, 2000, 3000]; // 1 kHz, 2 kHz, 3 kHz
322
- * console.log(freq.f_kHz); // Outputs: [1, 2, 3]
323
- * ```
273
+ * Gets the frequency points in Kilohertz (kHz).
324
274
  */
325
275
  get f_kHz(): number[] {
326
276
  return this._getFrequencyInTargetUnit('kHz')
327
277
  }
328
278
 
329
279
  /**
330
- * Sets frequency points assuming input is in kHz
331
- * @param values - Array of frequency points in kHz
332
- *
333
- * @example
334
- * ```typescript
335
- * const freq = new Frequency();
336
- * freq.unit = 'Hz';
337
- * freq.f_kHz = [1, 2, 3]; // Sets frequencies as 1 kHz, 2 kHz, 3 kHz
338
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in Hz)
339
- * ```
280
+ * Sets the frequency points in Kilohertz (kHz).
340
281
  */
341
282
  set f_kHz(values: number[]) {
342
283
  this._setFrequencyFromTargetUnit(values, 'kHz')
343
284
  }
344
285
 
345
286
  /**
346
- * Gets frequency points in MHz
347
- * @returns Array of frequency points in MHz
348
- *
349
- * @example
350
- * ```typescript
351
- * const freq = new Frequency();
352
- * freq.unit = 'GHz';
353
- * freq.f_scaled = [0.1, 0.2, 0.3]; // 0.1 GHz, 0.2 GHz, 0.3 GHz
354
- * console.log(freq.f_MHz); // Outputs: [100, 200, 300]
355
- * ```
287
+ * Gets the frequency points in Megahertz (MHz).
356
288
  */
357
289
  get f_MHz(): number[] {
358
290
  return this._getFrequencyInTargetUnit('MHz')
359
291
  }
360
292
 
361
293
  /**
362
- * Sets frequency points assuming input is in MHz
363
- * @param values - Array of frequency points in MHz
364
- *
365
- * @example
366
- * ```typescript
367
- * const freq = new Frequency();
368
- * freq.unit = 'GHz';
369
- * freq.f_MHz = [100, 200, 300]; // Sets frequencies as 100 MHz, 200 MHz, 300 MHz
370
- * console.log(freq.f_scaled); // Outputs: [0.1, 0.2, 0.3] (in GHz)
371
- * ```
294
+ * Sets the frequency points in Megahertz (MHz).
372
295
  */
373
296
  set f_MHz(values: number[]) {
374
297
  this._setFrequencyFromTargetUnit(values, 'MHz')
375
298
  }
376
299
 
377
300
  /**
378
- * Gets frequency points in GHz
379
- * @returns Array of frequency points in GHz
380
- *
381
- * @example
382
- * ```typescript
383
- * const freq = new Frequency();
384
- * freq.unit = 'MHz';
385
- * freq.f_scaled = [1000, 2000, 3000]; // 1000 MHz, 2000 MHz, 3000 MHz
386
- * console.log(freq.f_GHz); // Outputs: [1, 2, 3]
387
- * ```
301
+ * Gets the frequency points in Gigahertz (GHz).
388
302
  */
389
303
  get f_GHz(): number[] {
390
304
  return this._getFrequencyInTargetUnit('GHz')
391
305
  }
392
306
 
393
307
  /**
394
- * Sets frequency points assuming input is in GHz
395
- * @param values - Array of frequency points in GHz
396
- *
397
- * @example
398
- * ```typescript
399
- * const freq = new Frequency();
400
- * freq.unit = 'MHz';
401
- * freq.f_GHz = [1, 2, 3]; // Sets frequencies as 1 GHz, 2 GHz, 3 GHz
402
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in MHz)
403
- * ```
308
+ * Sets the frequency points in Gigahertz (GHz).
404
309
  */
405
310
  set f_GHz(values: number[]) {
406
311
  this._setFrequencyFromTargetUnit(values, 'GHz')
407
312
  }
408
313
 
409
314
  /**
410
- * Gets frequency points in THz
411
- * @returns Array of frequency points in THz
412
- *
413
- * @example
414
- * ```typescript
415
- * const freq = new Frequency();
416
- * freq.unit = 'GHz';
417
- * freq.f_scaled = [1000, 2000, 3000]; // 1000 GHz, 2000 GHz, 3000 GHz
418
- * console.log(freq.f_THz); // Outputs: [1, 2, 3]
419
- * ```
315
+ * Gets the frequency points in Terahertz (THz).
420
316
  */
421
317
  get f_THz(): number[] {
422
318
  return this._getFrequencyInTargetUnit('THz')
423
319
  }
424
320
 
425
321
  /**
426
- * Sets frequency points assuming input is in THz
427
- * @param values - Array of frequency points in THz
428
- *
429
- * @example
430
- * ```typescript
431
- * const freq = new Frequency();
432
- * freq.unit = 'GHz';
433
- * freq.f_THz = [1, 2, 3]; // Sets frequencies as 1 THz, 2 THz, 3 THz
434
- * console.log(freq.f_scaled); // Outputs: [1000, 2000, 3000] (in GHz)
435
- * ```
322
+ * Sets the frequency points in Terahertz (THz).
436
323
  */
437
324
  set f_THz(values: number[]) {
438
325
  this._setFrequencyFromTargetUnit(values, 'THz')
@@ -532,160 +419,71 @@ export class Frequency {
532
419
  }
533
420
 
534
421
  /**
535
- * Gets wavelength in meters
536
- * @returns Array of wavelength values in meters
537
- *
538
- * @example
539
- * ```typescript
540
- * const freq = new Frequency();
541
- * freq.unit = 'GHz';
542
- * freq.f_scaled = [1, 2, 3]; // Frequencies in GHz
543
- * console.log(freq.wavelength_m); // Outputs: Wavelengths in meters
544
- * ```
422
+ * Gets the wavelength in meters (m).
545
423
  */
546
424
  get wavelength_m(): number[] {
547
425
  return this._getWavelengthInTargetUnit('m')
548
426
  }
549
427
 
550
428
  /**
551
- * Sets wavelength in meters. Converts to frequency and stores.
552
- * @param values - Array of wavelength values in meters
553
- *
554
- * @example
555
- * ```typescript
556
- * const freq = new Frequency();
557
- * freq.unit = 'GHz';
558
- * freq.wavelength_m = [0.3, 0.15, 0.1]; // Wavelengths in meters
559
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
560
- * ```
429
+ * Sets the wavelength in meters (m).
430
+ * Note: Changing wavelengths will update the underlying frequency points.
561
431
  */
562
432
  set wavelength_m(values: number[]) {
563
433
  this._setWavelengthFromTargetUnit(values, 'm')
564
434
  }
565
435
 
566
436
  /**
567
- * Gets wavelength in centimeters
568
- * @returns Array of wavelength values in centimeters
569
- *
570
- * @example
571
- * ```typescript
572
- * const freq = new Frequency();
573
- * freq.unit = 'GHz';
574
- * freq.f_scaled = [10, 20, 30]; // Frequencies in GHz
575
- * console.log(freq.wavelength_cm); // Outputs: Wavelengths in centimeters
576
- * ```
437
+ * Gets the wavelength in centimeters (cm).
577
438
  */
578
439
  get wavelength_cm(): number[] {
579
440
  return this._getWavelengthInTargetUnit('cm')
580
441
  }
581
442
 
582
443
  /**
583
- * Sets wavelength in centimeters. Converts to frequency and stores.
584
- * @param values - Array of wavelength values in centimeters
585
- *
586
- * @example
587
- * ```typescript
588
- * const freq = new Frequency();
589
- * freq.unit = 'GHz';
590
- * freq.wavelength_cm = [30, 15, 10]; // Wavelengths in centimeters
591
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
592
- * ```
444
+ * Sets the wavelength in centimeters (cm).
593
445
  */
594
446
  set wavelength_cm(values: number[]) {
595
447
  this._setWavelengthFromTargetUnit(values, 'cm')
596
448
  }
597
449
 
598
450
  /**
599
- * Gets wavelength in millimeters
600
- * @returns Array of wavelength values in millimeters
601
- *
602
- * @example
603
- * ```typescript
604
- * const freq = new Frequency();
605
- * freq.unit = 'GHz';
606
- * freq.f_scaled = [100, 200, 300]; // Frequencies in GHz
607
- * console.log(freq.wavelength_mm); // Outputs: Wavelengths in millimeters
608
- * ```
451
+ * Gets the wavelength in millimeters (mm).
609
452
  */
610
453
  get wavelength_mm(): number[] {
611
454
  return this._getWavelengthInTargetUnit('mm')
612
455
  }
613
456
 
614
457
  /**
615
- * Sets wavelength in millimeters. Converts to frequency and stores.
616
- * @param values - Array of wavelength values in millimeters
617
- *
618
- * @example
619
- * ```typescript
620
- * const freq = new Frequency();
621
- * freq.unit = 'GHz';
622
- * freq.wavelength_mm = [300, 150, 100]; // Wavelengths in millimeters
623
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
624
- * ```
458
+ * Sets the wavelength in millimeters (mm).
625
459
  */
626
460
  set wavelength_mm(values: number[]) {
627
461
  this._setWavelengthFromTargetUnit(values, 'mm')
628
462
  }
629
463
 
630
464
  /**
631
- * Gets wavelength in micrometers
632
- * @returns Array of wavelength values in micrometers
633
- *
634
- * @example
635
- * ```typescript
636
- * const freq = new Frequency();
637
- * freq.unit = 'GHz';
638
- * freq.f_scaled = [1000, 2000, 3000]; // Frequencies in GHz
639
- * console.log(freq.wavelength_um); // Outputs: Wavelengths in micrometers
640
- * ```
465
+ * Gets the wavelength in micrometers (μm).
641
466
  */
642
467
  get wavelength_um(): number[] {
643
468
  return this._getWavelengthInTargetUnit('um')
644
469
  }
645
470
 
646
471
  /**
647
- * Sets wavelength in micrometers. Converts to frequency and stores.
648
- * @param values - Array of wavelength values in micrometers
649
- *
650
- * @example
651
- * ```typescript
652
- * const freq = new Frequency();
653
- * freq.unit = 'GHz';
654
- * freq.wavelength_um = [300000, 150000, 100000]; // Wavelengths in micrometers
655
- * console.log(freq.f_scaled); // Outputs: Frequencies in GHz
656
- * ```
472
+ * Sets the wavelength in micrometers (μm).
657
473
  */
658
474
  set wavelength_um(values: number[]) {
659
475
  this._setWavelengthFromTargetUnit(values, 'um')
660
476
  }
661
477
 
662
478
  /**
663
- * Gets wavelength in nanometers
664
- * @returns Array of wavelength values in nanometers
665
- *
666
- * @example
667
- * ```typescript
668
- * const freq = new Frequency();
669
- * freq.unit = 'THz';
670
- * freq.f_scaled = [100, 200, 300]; // Frequencies in THz
671
- * console.log(freq.wavelength_nm); // Outputs: Wavelengths in nanometers
672
- * ```
479
+ * Gets the wavelength in nanometers (nm).
673
480
  */
674
481
  get wavelength_nm(): number[] {
675
482
  return this._getWavelengthInTargetUnit('nm')
676
483
  }
677
484
 
678
485
  /**
679
- * Sets wavelength in nanometers. Converts to frequency and stores.
680
- * @param values - Array of wavelength values in nanometers
681
- *
682
- * @example
683
- * ```typescript
684
- * const freq = new Frequency();
685
- * freq.unit = 'THz';
686
- * freq.wavelength_nm = [300, 150, 100]; // Wavelengths in nanometers
687
- * console.log(freq.f_scaled); // Outputs: Frequencies in THz
688
- * ```
486
+ * Sets the wavelength in nanometers (nm).
689
487
  */
690
488
  set wavelength_nm(values: number[]) {
691
489
  this._setWavelengthFromTargetUnit(values, 'nm')
package/src/index.ts CHANGED
@@ -1,17 +1,4 @@
1
- import {
2
- abs,
3
- add,
4
- arg,
5
- complex as _complex,
6
- log10,
7
- index,
8
- multiply,
9
- pi,
10
- pow,
11
- range as _range,
12
- round,
13
- subset,
14
- } from 'mathjs'
1
+ import { complex as _complex, range as _range } from 'mathjs'
15
2
 
16
3
  /**
17
4
  * Creates a complex value or converts a value to a complex value.
@@ -27,7 +14,19 @@ export const complex = _complex
27
14
  */
28
15
  export const range = _range
29
16
 
30
- export { abs, add, arg, log10, index, multiply, pi, pow, round, subset }
17
+ export {
18
+ abs,
19
+ add,
20
+ arg,
21
+ log10,
22
+ index,
23
+ multiply,
24
+ pi,
25
+ pow,
26
+ round,
27
+ subset,
28
+ } from 'mathjs'
29
+ export type { Complex } from 'mathjs'
31
30
 
32
31
  export * from './frequency'
33
32
  export * from './touchstone'