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.
- package/coverage/coverage-badge.svg +4 -4
- package/dist/Touchstone.cjs.js +7 -13
- package/dist/Touchstone.es.js +3502 -3440
- package/dist/Touchstone.umd.js +7 -13
- package/dist/frequency.d.ts +394 -0
- package/dist/index.d.ts +24 -0
- package/dist/touchstone.d.ts +367 -803
- package/package.json +33 -30
- package/readme.md +3 -1
- package/src/frequency.ts +693 -0
- package/src/index.ts +33 -0
- package/src/touchstone.ts +774 -0
- package/development.md +0 -205
- /package/{LICENSE → LICENSE.md} +0 -0
package/src/frequency.ts
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frequency units: 'Hz', 'kHz', 'MHz', 'GHz'
|
|
3
|
+
* Touchstone standard doesn't support THz as the frequency unit in Touchstone file
|
|
4
|
+
*/
|
|
5
|
+
export const FrequencyUnits = ['Hz', 'kHz', 'MHz', 'GHz'] as const
|
|
6
|
+
|
|
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
|
|
14
|
+
*/
|
|
15
|
+
export type FrequencyUnit = (typeof FrequencyUnits)[number]
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Speed of light in m/s
|
|
19
|
+
*/
|
|
20
|
+
export const SPEED_OF_LIGHT = 299792458
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Multipliers for converting from any FrequencyUnit or THz to Hz
|
|
24
|
+
*/
|
|
25
|
+
export const FREQUENCY_MULTIPLIERS: Record<FrequencyUnit | 'THz', number> = {
|
|
26
|
+
Hz: 1,
|
|
27
|
+
kHz: 1e3,
|
|
28
|
+
MHz: 1e6,
|
|
29
|
+
GHz: 1e9,
|
|
30
|
+
THz: 1e12,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Multipliers for converting from other wavelength units to meters
|
|
35
|
+
*/
|
|
36
|
+
export const WAVELENGTH_MULTIPLIERS_TO_M: Record<string, number> = {
|
|
37
|
+
m: 1,
|
|
38
|
+
cm: 1e-2,
|
|
39
|
+
mm: 1e-3,
|
|
40
|
+
um: 1e-6,
|
|
41
|
+
nm: 1e-9,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Class representing frequency data in RF and microwave engineering
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* The Frequency class manages frequency-related data, including:
|
|
49
|
+
* - Frequency unit management
|
|
50
|
+
* - Storage of frequency points
|
|
51
|
+
* - Unit conversion capabilities
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const freq = new Frequency();
|
|
56
|
+
* freq.unit = 'GHz';
|
|
57
|
+
* freq.f_scale = [1.0, 2.0, 3.0];
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export class Frequency {
|
|
61
|
+
/**
|
|
62
|
+
* Internal storage for frequency unit
|
|
63
|
+
* Defaults to 'Hz' as the base SI unit for frequency
|
|
64
|
+
*/
|
|
65
|
+
private _unit: FrequencyUnit = 'Hz'
|
|
66
|
+
|
|
67
|
+
/**
|
|
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
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const freq = new Frequency();
|
|
76
|
+
* freq.unit = 'MHz'; // Sets unit to Megahertz
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
set unit(newUnit: FrequencyUnit) {
|
|
80
|
+
if (typeof newUnit !== 'string') {
|
|
81
|
+
throw new Error(`Unknown frequency unit: ${newUnit}`)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const oldUnit = this._unit // Store the old unit
|
|
85
|
+
|
|
86
|
+
// Validate and parse the new unit
|
|
87
|
+
let parsedUnit: FrequencyUnit
|
|
88
|
+
switch (newUnit.toLowerCase()) {
|
|
89
|
+
case 'hz':
|
|
90
|
+
parsedUnit = 'Hz'
|
|
91
|
+
break
|
|
92
|
+
case 'khz':
|
|
93
|
+
parsedUnit = 'kHz'
|
|
94
|
+
break
|
|
95
|
+
case 'mhz':
|
|
96
|
+
parsedUnit = 'MHz'
|
|
97
|
+
break
|
|
98
|
+
case 'ghz':
|
|
99
|
+
parsedUnit = 'GHz'
|
|
100
|
+
break
|
|
101
|
+
default:
|
|
102
|
+
throw new Error(`Unknown frequency unit: ${newUnit}`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// If the unit is actually changing and f_scaled is populated
|
|
106
|
+
if (parsedUnit !== oldUnit && this.f_scaled && this.f_scaled.length > 0) {
|
|
107
|
+
const oldMultiplier = FREQUENCY_MULTIPLIERS[oldUnit]
|
|
108
|
+
const newMultiplier = FREQUENCY_MULTIPLIERS[parsedUnit]
|
|
109
|
+
|
|
110
|
+
/* v8 ignore start */
|
|
111
|
+
if (oldMultiplier && newMultiplier) {
|
|
112
|
+
// Ensure multipliers are found
|
|
113
|
+
this.f_scaled = this.f_scaled.map(
|
|
114
|
+
(freq) => (freq * oldMultiplier) / newMultiplier
|
|
115
|
+
)
|
|
116
|
+
} else {
|
|
117
|
+
// This case should ideally not happen if units are validated correctly
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Could not find frequency multipliers (old: ${oldMultiplier}, new: ${newMultiplier}) for unit conversion`
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
/* v8 ignore stop */
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this._unit = parsedUnit // Update the internal unit
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
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
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
get unit(): FrequencyUnit {
|
|
140
|
+
return this._unit
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Internal storage for frequency points array
|
|
145
|
+
* Each element represents a frequency point in the specified unit
|
|
146
|
+
* @private
|
|
147
|
+
*/
|
|
148
|
+
private _f_scaled: number[] = []
|
|
149
|
+
|
|
150
|
+
/**
|
|
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
|
|
158
|
+
*
|
|
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
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
set f_scaled(value: number[]) {
|
|
167
|
+
// Validate input is an array
|
|
168
|
+
if (!Array.isArray(value)) {
|
|
169
|
+
throw new Error('Frequency value must be an array')
|
|
170
|
+
}
|
|
171
|
+
// Validate all elements are numbers and non-negative
|
|
172
|
+
for (const val of value) {
|
|
173
|
+
if (typeof val !== 'number') {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`Frequency value must be an array of numbers, but received: ${val}`
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
if (val < 0) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`Frequency values cannot be negative, but received: ${val}`
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Store the validated frequency points
|
|
186
|
+
this._f_scaled = value
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
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
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
get f_scaled(): number[] {
|
|
202
|
+
return this._f_scaled
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Private helper method to get frequency values in a target unit.
|
|
207
|
+
* @param targetUnit - The key of the target frequency unit in FREQUENCY_MULTIPLIERS.
|
|
208
|
+
* @returns Array of frequency points in the target unit.
|
|
209
|
+
*/
|
|
210
|
+
private _getFrequencyInTargetUnit(
|
|
211
|
+
targetUnit: keyof typeof FREQUENCY_MULTIPLIERS
|
|
212
|
+
): number[] {
|
|
213
|
+
if (!this.f_scaled || this.f_scaled.length === 0) {
|
|
214
|
+
return []
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const currentUnitMultiplier = FREQUENCY_MULTIPLIERS[this.unit]
|
|
218
|
+
/* v8 ignore start */
|
|
219
|
+
if (!currentUnitMultiplier) {
|
|
220
|
+
throw new Error(`Multiplier for current unit ${this.unit} not found.`)
|
|
221
|
+
}
|
|
222
|
+
/* v8 ignore stop */
|
|
223
|
+
|
|
224
|
+
const targetMultiplier = FREQUENCY_MULTIPLIERS[targetUnit]
|
|
225
|
+
/* v8 ignore start */
|
|
226
|
+
if (!targetMultiplier) {
|
|
227
|
+
throw new Error(`Multiplier for target unit ${targetUnit} not found.`)
|
|
228
|
+
}
|
|
229
|
+
/* v8 ignore stop */
|
|
230
|
+
|
|
231
|
+
return this.f_scaled.map(
|
|
232
|
+
(val) => (val * currentUnitMultiplier) / targetMultiplier
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
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.
|
|
240
|
+
*/
|
|
241
|
+
private _setFrequencyFromTargetUnit(
|
|
242
|
+
values: number[],
|
|
243
|
+
sourceUnit: keyof typeof FREQUENCY_MULTIPLIERS
|
|
244
|
+
): void {
|
|
245
|
+
/* v8 ignore start */
|
|
246
|
+
if (!values) {
|
|
247
|
+
// Handle null or undefined input array
|
|
248
|
+
this.f_scaled = []
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
/* v8 ignore stop */
|
|
252
|
+
if (values.length === 0) {
|
|
253
|
+
/* v8 ignore start */
|
|
254
|
+
this.f_scaled = []
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
/* v8 ignore stop */
|
|
258
|
+
|
|
259
|
+
const sourceMultiplier = FREQUENCY_MULTIPLIERS[sourceUnit]
|
|
260
|
+
/* v8 ignore start */
|
|
261
|
+
if (!sourceMultiplier) {
|
|
262
|
+
throw new Error(`Multiplier for source unit ${sourceUnit} not found.`)
|
|
263
|
+
}
|
|
264
|
+
/* v8 ignore stop */
|
|
265
|
+
|
|
266
|
+
const currentFreqMultiplier = FREQUENCY_MULTIPLIERS[this.unit]
|
|
267
|
+
/* v8 ignore start */
|
|
268
|
+
if (!currentFreqMultiplier) {
|
|
269
|
+
throw new Error(
|
|
270
|
+
`Multiplier for current internal unit ${this.unit} not found.`
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
/* v8 ignore stop */
|
|
274
|
+
|
|
275
|
+
const convertedValues = values.map(
|
|
276
|
+
(val) => (val * sourceMultiplier) / currentFreqMultiplier
|
|
277
|
+
)
|
|
278
|
+
this.f_scaled = convertedValues
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
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
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
get f_Hz(): number[] {
|
|
294
|
+
return this._getFrequencyInTargetUnit('Hz')
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
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
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
set f_Hz(values: number[]) {
|
|
310
|
+
this._setFrequencyFromTargetUnit(values, 'Hz')
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
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
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
get f_kHz(): number[] {
|
|
326
|
+
return this._getFrequencyInTargetUnit('kHz')
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
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
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
set f_kHz(values: number[]) {
|
|
342
|
+
this._setFrequencyFromTargetUnit(values, 'kHz')
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
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
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
get f_MHz(): number[] {
|
|
358
|
+
return this._getFrequencyInTargetUnit('MHz')
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
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
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
set f_MHz(values: number[]) {
|
|
374
|
+
this._setFrequencyFromTargetUnit(values, 'MHz')
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
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
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
get f_GHz(): number[] {
|
|
390
|
+
return this._getFrequencyInTargetUnit('GHz')
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
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
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
set f_GHz(values: number[]) {
|
|
406
|
+
this._setFrequencyFromTargetUnit(values, 'GHz')
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
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
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
get f_THz(): number[] {
|
|
422
|
+
return this._getFrequencyInTargetUnit('THz')
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
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
|
+
* ```
|
|
436
|
+
*/
|
|
437
|
+
set f_THz(values: number[]) {
|
|
438
|
+
this._setFrequencyFromTargetUnit(values, 'THz')
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Private helper method to get wavelength values in a target unit.
|
|
443
|
+
* @param targetWavelengthUnit - The key of the target wavelength unit in WAVELENGTH_MULTIPLIERS_TO_M.
|
|
444
|
+
* @returns Array of wavelength points in the target unit.
|
|
445
|
+
*/
|
|
446
|
+
private _getWavelengthInTargetUnit(
|
|
447
|
+
targetWavelengthUnit: keyof typeof WAVELENGTH_MULTIPLIERS_TO_M
|
|
448
|
+
): number[] {
|
|
449
|
+
if (!this.f_scaled || this.f_scaled.length === 0) {
|
|
450
|
+
return []
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const currentFreqUnitMultiplier = FREQUENCY_MULTIPLIERS[this.unit]
|
|
454
|
+
/* v8 ignore start */
|
|
455
|
+
if (!currentFreqUnitMultiplier) {
|
|
456
|
+
throw new Error(
|
|
457
|
+
`Frequency multiplier for current unit ${this.unit} not found.`
|
|
458
|
+
)
|
|
459
|
+
}
|
|
460
|
+
/* v8 ignore stop */
|
|
461
|
+
|
|
462
|
+
const targetWavelengthToMMultiplier =
|
|
463
|
+
WAVELENGTH_MULTIPLIERS_TO_M[targetWavelengthUnit]
|
|
464
|
+
/* v8 ignore start */
|
|
465
|
+
if (!targetWavelengthToMMultiplier) {
|
|
466
|
+
throw new Error(
|
|
467
|
+
`Wavelength multiplier to meters for target wavelength unit ${targetWavelengthUnit} not found.`
|
|
468
|
+
)
|
|
469
|
+
}
|
|
470
|
+
/* v8 ignore stop */
|
|
471
|
+
|
|
472
|
+
return this.f_scaled.map((val) => {
|
|
473
|
+
const freqInHz = val * currentFreqUnitMultiplier
|
|
474
|
+
if (freqInHz === 0) {
|
|
475
|
+
return Infinity
|
|
476
|
+
}
|
|
477
|
+
const wavelengthInMeters = SPEED_OF_LIGHT / freqInHz
|
|
478
|
+
return wavelengthInMeters / targetWavelengthToMMultiplier
|
|
479
|
+
})
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Private helper method to set frequency values from wavelength values in a source unit.
|
|
484
|
+
* @param values - Array of wavelength points in the source unit.
|
|
485
|
+
* @param sourceWavelengthUnit - The key of the source wavelength unit in WAVELENGTH_MULTIPLIERS_TO_M.
|
|
486
|
+
*/
|
|
487
|
+
private _setWavelengthFromTargetUnit(
|
|
488
|
+
values: number[],
|
|
489
|
+
sourceWavelengthUnit: keyof typeof WAVELENGTH_MULTIPLIERS_TO_M
|
|
490
|
+
): void {
|
|
491
|
+
/* v8 ignore start */
|
|
492
|
+
if (!values) {
|
|
493
|
+
// Handle null or undefined input array
|
|
494
|
+
this.f_scaled = []
|
|
495
|
+
return
|
|
496
|
+
}
|
|
497
|
+
/* v8 ignore stop */
|
|
498
|
+
if (values.length === 0) {
|
|
499
|
+
this.f_scaled = []
|
|
500
|
+
return
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const sourceWavelengthToMMultiplier =
|
|
504
|
+
WAVELENGTH_MULTIPLIERS_TO_M[sourceWavelengthUnit]
|
|
505
|
+
/* v8 ignore start */
|
|
506
|
+
if (!sourceWavelengthToMMultiplier) {
|
|
507
|
+
throw new Error(
|
|
508
|
+
`Wavelength multiplier to meters for source unit ${sourceWavelengthUnit} not found.`
|
|
509
|
+
)
|
|
510
|
+
}
|
|
511
|
+
/* v8 ignore stop */
|
|
512
|
+
|
|
513
|
+
const currentFreqUnitMultiplier = FREQUENCY_MULTIPLIERS[this.unit]
|
|
514
|
+
/* v8 ignore start */
|
|
515
|
+
if (!currentFreqUnitMultiplier) {
|
|
516
|
+
throw new Error(
|
|
517
|
+
`Frequency multiplier for current unit ${this.unit} not found.`
|
|
518
|
+
)
|
|
519
|
+
}
|
|
520
|
+
/* v8 ignore stop */
|
|
521
|
+
|
|
522
|
+
const convertedFrequencies = values.map((val) => {
|
|
523
|
+
const wavelengthInMeters = val * sourceWavelengthToMMultiplier
|
|
524
|
+
if (wavelengthInMeters === 0) {
|
|
525
|
+
throw new Error('Cannot convert zero wavelength to frequency.')
|
|
526
|
+
}
|
|
527
|
+
const freqInHz = SPEED_OF_LIGHT / wavelengthInMeters
|
|
528
|
+
return freqInHz / currentFreqUnitMultiplier
|
|
529
|
+
})
|
|
530
|
+
|
|
531
|
+
this.f_scaled = convertedFrequencies
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
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
|
+
* ```
|
|
545
|
+
*/
|
|
546
|
+
get wavelength_m(): number[] {
|
|
547
|
+
return this._getWavelengthInTargetUnit('m')
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
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
|
+
* ```
|
|
561
|
+
*/
|
|
562
|
+
set wavelength_m(values: number[]) {
|
|
563
|
+
this._setWavelengthFromTargetUnit(values, 'm')
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
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
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
get wavelength_cm(): number[] {
|
|
579
|
+
return this._getWavelengthInTargetUnit('cm')
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
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
|
+
* ```
|
|
593
|
+
*/
|
|
594
|
+
set wavelength_cm(values: number[]) {
|
|
595
|
+
this._setWavelengthFromTargetUnit(values, 'cm')
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
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
|
+
* ```
|
|
609
|
+
*/
|
|
610
|
+
get wavelength_mm(): number[] {
|
|
611
|
+
return this._getWavelengthInTargetUnit('mm')
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
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
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
set wavelength_mm(values: number[]) {
|
|
627
|
+
this._setWavelengthFromTargetUnit(values, 'mm')
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
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
|
+
* ```
|
|
641
|
+
*/
|
|
642
|
+
get wavelength_um(): number[] {
|
|
643
|
+
return this._getWavelengthInTargetUnit('um')
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
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
|
+
* ```
|
|
657
|
+
*/
|
|
658
|
+
set wavelength_um(values: number[]) {
|
|
659
|
+
this._setWavelengthFromTargetUnit(values, 'um')
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
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
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
get wavelength_nm(): number[] {
|
|
675
|
+
return this._getWavelengthInTargetUnit('nm')
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
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
|
+
* ```
|
|
689
|
+
*/
|
|
690
|
+
set wavelength_nm(values: number[]) {
|
|
691
|
+
this._setWavelengthFromTargetUnit(values, 'nm')
|
|
692
|
+
}
|
|
693
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
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'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a complex value or converts a value to a complex value.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://mathjs.org/docs/reference/functions/complex.html | Math.js documentation for complex}
|
|
20
|
+
*/
|
|
21
|
+
export const complex = _complex
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create an array or matrix with a range of numbers.
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://mathjs.org/docs/reference/functions/range.html | Math.js documentation for range}
|
|
27
|
+
*/
|
|
28
|
+
export const range = _range
|
|
29
|
+
|
|
30
|
+
export { abs, add, arg, log10, index, multiply, pi, pow, round, subset }
|
|
31
|
+
|
|
32
|
+
export * from './frequency'
|
|
33
|
+
export * from './touchstone'
|