touchstone 0.12.1

Touchstone (s2p, etc.) file parser, plotter, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::utils::str_to_f64;

use crate::data_pairs::DecibelAngle;
use crate::data_pairs::DecibelAngleMatrix;
use crate::data_pairs::MagnitudeAngle;
use crate::data_pairs::MagnitudeAngleMatrix;
use crate::data_pairs::RealImaginary;
use crate::data_pairs::RealImaginaryMatrix;

#[derive(Clone, Debug)]
pub struct ParsedDataLine {
    pub frequency: f64,
    pub s_ri: RealImaginaryMatrix,
    pub s_db: DecibelAngleMatrix,
    pub s_ma: MagnitudeAngleMatrix,
}

pub(crate) fn parse_data_line(
    data_lines: Vec<String>,
    format: &String,
    n: &i32,
    frequency_unit: &String,
) -> ParsedDataLine {
    // println!("\n");
    // println!("format:\n{:?}", *format);
    // println!("n (number of ports): {:?}", *n);

    // FROM docs/touchstone_ver2_1.pdf (Page 16)
    //
    // 2-port data (line)
    // <frequency value>  <N11> <N12> <N21> <N22>

    // where
    // frequency value  frequency at which the network parameter data was taken or derived.

    // N11, N12, N21, N22   network parameter data points, where Nij represents a pair of data values
    //
    // for the network parameter from port i to port j. Each Nij consists of two numeric
    // values, whose meaning is determined by the format option specified in the option line.
    // therefore, the total number of numeric values on a 2-port data line is 1 + (2 × (2^2)) = 9.
    // generally, for an n-port data line, the total number of numeric values is 1 + (2 × (n^2)).
    let expect_number_of_parts = 1 + (2 * (n * n));
    // println!("expected number of parts: {:?}", expect_number_of_parts);

    // Combine all lines into a single vector of parts
    // This handles both single-line and multi-line format
    let mut parts = Vec::new();
    for line in &data_lines {
        // println!("Data Line: {}", line);
        let line_parts: Vec<_> = line
            .split_whitespace()
            .filter(|s| !s.starts_with('!')) // Skip inline comments
            .collect();
        parts.extend(line_parts);
    }

    let len_parts = parts.len();
    // println!("Data Line Parts (len {}): {:?}", len_parts, parts);

    if len_parts != expect_number_of_parts as usize {
        panic!(
            "Data line has unexpected number of parts. Expected {}, got {}",
            expect_number_of_parts, len_parts
        );
    }

    // split into f64 parts, after checking the expected length
    let f64_parts: Vec<_> = parts.clone().into_iter().map(str_to_f64).collect();

    // println!("{}", len_parts);
    // println!("f64_parts (len {}): {:?}", len_parts, f64_parts);

    let n_usize = *n as usize;

    let mut frequency = str_to_f64(parts[0]);

    if frequency_unit == "THz" {
        // convert to Hz
        // println!("Converting frequency from THz to Hz");
        // println!("Original frequency: {} THz", frequency);
        frequency = rfconversions::frequency::thz_to_hz(frequency);
        tracing::trace!("Converted frequency: {} Hz", frequency);
    } else if frequency_unit == "GHz" {
        // convert to Hz
        // println!("Converting frequency from GHz to Hz");
        // println!("Original frequency: {} GHz", frequency);
        frequency = rfconversions::frequency::ghz_to_hz(frequency);
        // println!("Converted frequency: {} Hz", frequency);
    } else if frequency_unit == "MHz" {
        // convert to Hz
        // println!("Converting frequency from MHz to Hz");
        // println!("Original frequency: {} MHz", frequency);
        frequency = rfconversions::frequency::mhz_to_hz(frequency);
        // println!("Converted frequency: {} Hz", frequency);
    } else if frequency_unit == "kHz" {
        // convert to Hz
        // println!("Converting frequency from kHz to Hz");
        // println!("Original frequency: {} kHz", frequency);
        frequency = rfconversions::frequency::khz_to_hz(frequency);
        // println!("Converted frequency: {} Hz", frequency);
    } else if frequency_unit == "Hz" {
        // no conversion needed
        // println!("Frequency is already in Hz: {} Hz", frequency);
    } else {
        panic!("Unsupported frequency unit: {}", frequency_unit);
    }

    if format == "RI" {
        // Real-Imaginary format
        // Build NxN matrix from f64_parts in row-major order
        let mut s_ri_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                let idx = 1 + 2 * (row * n_usize + col);
                row_vec.push(RealImaginary(f64_parts[idx], f64_parts[idx + 1]));
            }
            s_ri_data.push(row_vec);
        }
        let s_ri = RealImaginaryMatrix::from_vec(s_ri_data);

        // Convert to DecibelAngle format
        let mut s_db_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                row_vec.push(DecibelAngle::from_real_imaginary(
                    s_ri.get(row + 1, col + 1),
                ));
            }
            s_db_data.push(row_vec);
        }
        let s_db = DecibelAngleMatrix::from_vec(s_db_data);

        // Convert to MagnitudeAngle format
        let mut s_ma_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                row_vec.push(MagnitudeAngle::from_real_imaginary(
                    s_ri.get(row + 1, col + 1),
                ));
            }
            s_ma_data.push(row_vec);
        }
        let s_ma = MagnitudeAngleMatrix::from_vec(s_ma_data);

        ParsedDataLine {
            frequency,
            s_ri,
            s_db,
            s_ma,
        }
    } else if format == "MA" {
        // Magnitude-Angle format
        // Build NxN matrix from f64_parts in row-major order
        let mut s_ma_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                let idx = 1 + 2 * (row * n_usize + col);
                row_vec.push(MagnitudeAngle(f64_parts[idx], f64_parts[idx + 1]));
            }
            s_ma_data.push(row_vec);
        }
        let s_ma = MagnitudeAngleMatrix::from_vec(s_ma_data);

        // Convert to RealImaginary format
        let mut s_ri_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                row_vec.push(RealImaginary::from_magnitude_angle(
                    s_ma.get(row + 1, col + 1),
                ));
            }
            s_ri_data.push(row_vec);
        }
        let s_ri = RealImaginaryMatrix::from_vec(s_ri_data);

        // Convert to DecibelAngle format
        let mut s_db_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                row_vec.push(DecibelAngle::from_magnitude_angle(
                    s_ma.get(row + 1, col + 1),
                ));
            }
            s_db_data.push(row_vec);
        }
        let s_db = DecibelAngleMatrix::from_vec(s_db_data);

        ParsedDataLine {
            frequency,
            s_ri,
            s_db,
            s_ma,
        }
    } else if format == "DB" {
        // Decibel-Angle format
        // Build NxN matrix from f64_parts in row-major order
        let mut s_db_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                let idx = 1 + 2 * (row * n_usize + col);
                row_vec.push(DecibelAngle(f64_parts[idx], f64_parts[idx + 1]));
            }
            s_db_data.push(row_vec);
        }
        let s_db = DecibelAngleMatrix::from_vec(s_db_data);

        // Convert to RealImaginary format
        let mut s_ri_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                row_vec.push(RealImaginary::from_decibel_angle(
                    s_db.get(row + 1, col + 1),
                ));
            }
            s_ri_data.push(row_vec);
        }
        let s_ri = RealImaginaryMatrix::from_vec(s_ri_data);

        // Convert to MagnitudeAngle format
        let mut s_ma_data = Vec::new();
        for row in 0..n_usize {
            let mut row_vec = Vec::new();
            for col in 0..n_usize {
                row_vec.push(MagnitudeAngle::from_decibel_angle(
                    s_db.get(row + 1, col + 1),
                ));
            }
            s_ma_data.push(row_vec);
        }
        let s_ma = MagnitudeAngleMatrix::from_vec(s_ma_data);

        ParsedDataLine {
            frequency,
            s_ri,
            s_db,
            s_ma,
        }
    } else {
        panic!("Unsupported format: {}", format);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn approx_eq(a: f64, b: f64, tol: f64) -> bool {
        (a - b).abs() < tol
    }

    // --- 1-port RI format ---
    #[test]
    fn test_parse_1port_ri_hz() {
        let lines = vec!["1000000000 0.5 -0.3".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"Hz".to_string());
        assert!(approx_eq(result.frequency, 1e9, 1.0));
        let ri = result.s_ri.get(1, 1);
        assert!(approx_eq(ri.0, 0.5, 1e-10));
        assert!(approx_eq(ri.1, -0.3, 1e-10));
    }

    // --- Frequency unit conversions ---
    #[test]
    fn test_parse_frequency_ghz() {
        let lines = vec!["2.4 0.1 0.2".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"GHz".to_string());
        assert!(approx_eq(result.frequency, 2.4e9, 1.0));
    }

    #[test]
    fn test_parse_frequency_mhz() {
        let lines = vec!["900 0.1 0.2".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"MHz".to_string());
        assert!(approx_eq(result.frequency, 900e6, 1.0));
    }

    #[test]
    fn test_parse_frequency_khz() {
        let lines = vec!["500 0.1 0.2".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"kHz".to_string());
        assert!(approx_eq(result.frequency, 500e3, 1.0));
    }

    #[test]
    fn test_parse_frequency_thz() {
        let lines = vec!["0.3 0.1 0.2".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"THz".to_string());
        assert!(approx_eq(result.frequency, 0.3e12, 1.0));
    }

    // --- 1-port MA format ---
    #[test]
    fn test_parse_1port_ma() {
        let lines = vec!["1000000000 0.8 -45.0".to_string()];
        let result = parse_data_line(lines, &"MA".to_string(), &1, &"Hz".to_string());
        let ma = result.s_ma.get(1, 1);
        assert!(approx_eq(ma.0, 0.8, 1e-10));
        assert!(approx_eq(ma.1, -45.0, 1e-10));
        // Cross-check: RI conversion
        let ri = result.s_ri.get(1, 1);
        let expected_real = 0.8 * (-45.0_f64.to_radians()).cos();
        let expected_imag = 0.8 * (-45.0_f64.to_radians()).sin();
        assert!(approx_eq(ri.0, expected_real, 1e-6));
        assert!(approx_eq(ri.1, expected_imag, 1e-6));
    }

    // --- 1-port DB format ---
    #[test]
    fn test_parse_1port_db() {
        let lines = vec!["1000000000 -3.0 90.0".to_string()];
        let result = parse_data_line(lines, &"DB".to_string(), &1, &"Hz".to_string());
        let db = result.s_db.get(1, 1);
        assert!(approx_eq(db.0, -3.0, 1e-10));
        assert!(approx_eq(db.1, 90.0, 1e-10));
        // Cross-check: magnitude from dB
        let ma = result.s_ma.get(1, 1);
        let expected_mag = 10.0_f64.powf(-3.0 / 20.0);
        assert!(approx_eq(ma.0, expected_mag, 1e-6));
    }

    // --- 2-port RI format ---
    #[test]
    fn test_parse_2port_ri() {
        // S11, S12, S21, S22
        let lines = vec!["1e9 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &2, &"Hz".to_string());
        let s11 = result.s_ri.get(1, 1);
        assert!(approx_eq(s11.0, 0.1, 1e-10));
        assert!(approx_eq(s11.1, 0.2, 1e-10));
        let s12 = result.s_ri.get(1, 2);
        assert!(approx_eq(s12.0, 0.3, 1e-10));
        assert!(approx_eq(s12.1, 0.4, 1e-10));
        let s21 = result.s_ri.get(2, 1);
        assert!(approx_eq(s21.0, 0.5, 1e-10));
        assert!(approx_eq(s21.1, 0.6, 1e-10));
        let s22 = result.s_ri.get(2, 2);
        assert!(approx_eq(s22.0, 0.7, 1e-10));
        assert!(approx_eq(s22.1, 0.8, 1e-10));
    }

    // --- Multi-line data (2-port split across lines) ---
    #[test]
    fn test_parse_2port_multiline() {
        let lines = vec![
            "1e9 0.1 0.2 0.3 0.4".to_string(),
            "0.5 0.6 0.7 0.8".to_string(),
        ];
        let result = parse_data_line(lines, &"RI".to_string(), &2, &"Hz".to_string());
        let s22 = result.s_ri.get(2, 2);
        assert!(approx_eq(s22.0, 0.7, 1e-10));
        assert!(approx_eq(s22.1, 0.8, 1e-10));
    }

    // --- Inline comments filtered ---
    // Note: current parser filters tokens starting with '!' but not subsequent
    // words in the comment. A single-word comment works; multi-word would fail.
    // TODO: Fix parser to skip all tokens after first '!' token.
    #[test]
    fn test_parse_inline_comment_single_word_filtered() {
        let lines = vec!["1e9 0.5 -0.3 !comment".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"Hz".to_string());
        let ri = result.s_ri.get(1, 1);
        assert!(approx_eq(ri.0, 0.5, 1e-10));
        assert!(approx_eq(ri.1, -0.3, 1e-10));
    }

    // --- Format cross-conversions are consistent ---
    #[test]
    fn test_ri_round_trip_consistency() {
        let lines = vec!["1e9 0.6 -0.4".to_string()];
        let result = parse_data_line(lines, &"RI".to_string(), &1, &"Hz".to_string());
        // RI -> DB -> back to magnitude should be consistent with MA
        let ri = result.s_ri.get(1, 1);
        let ma = result.s_ma.get(1, 1);
        let db = result.s_db.get(1, 1);
        let mag = (ri.0 * ri.0 + ri.1 * ri.1).sqrt();
        assert!(approx_eq(ma.0, mag, 1e-10));
        assert!(approx_eq(db.0, 20.0 * mag.log10(), 1e-6));
    }

    // --- Panics ---
    #[test]
    #[should_panic(expected = "Unsupported format")]
    fn test_parse_unsupported_format_panics() {
        let lines = vec!["1e9 0.1 0.2".to_string()];
        parse_data_line(lines, &"XX".to_string(), &1, &"Hz".to_string());
    }

    #[test]
    #[should_panic(expected = "Unsupported frequency unit")]
    fn test_parse_unsupported_frequency_unit_panics() {
        let lines = vec!["1e9 0.1 0.2".to_string()];
        parse_data_line(lines, &"RI".to_string(), &1, &"PHz".to_string());
    }

    #[test]
    #[should_panic(expected = "unexpected number of parts")]
    fn test_parse_wrong_number_of_parts_panics() {
        let lines = vec!["1e9 0.1".to_string()]; // Missing second value for 1-port
        parse_data_line(lines, &"RI".to_string(), &1, &"Hz".to_string());
    }
}