gainlineup 0.22.2

A Gain Lineup toolbox for RF Modeling
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
411
412
413
414
415
416
417
418
#![warn(missing_docs)]
//! # gainlineup
//!
//! A gain lineup toolbox for RF cascade analysis and signal chain modeling.
//!
//! This crate provides types and functions for computing cascaded gain, noise figure,
//! IP3, dynamic range, and compression through a chain of RF blocks (amplifiers,
//! attenuators, filters, mixers, etc.).
//!
//! # Quick Start
//!
//! ```
//! use gainlineup::{Input, Block, cascade_vector_return_output};
//!
//! let input = Input::new(1.0e9, 1.0e6, -30.0, Some(270.0));
//! let blocks = vec![
//!     Block {
//!         name: "LNA".to_string(),
//!         gain_db: 30.0,
//!         noise_figure_db: 1.5,
//!         output_p1db_dbm: None,
//!         output_ip3_dbm: None,
//!     },
//! ];
//! let output = cascade_vector_return_output(input, blocks);
//! assert_eq!(output.signal_power_dbm, 0.0);
//! ```

mod block;

/// Command-line interface for the gainlineup tool.
#[cfg(feature = "cli")]
#[allow(missing_docs)]
pub mod cli;
mod constants;
mod file_operations;
mod input;
mod node;
mod open;

#[cfg(feature = "plot")]
mod plot;

mod amplifier_model;

pub use amplifier_model::{AmplifierModel, AmplifierModelBuilder, AmplifierPoint};
pub use block::{Block, Imd3Point};
pub use input::Input;
pub use node::{DynamicRange, SignalNode};

/// Cascade a vector of blocks and return only the final output [`SignalNode`].
///
/// # Examples
///
/// ```
/// use gainlineup::{Input, Block, cascade_vector_return_output};
///
/// let input = Input::new(1.0e9, 1.0e6, -30.0, Some(270.0));
/// let blocks = vec![
///     Block {
///         name: "LNA".to_string(),
///         gain_db: 30.0,
///         noise_figure_db: 1.5,
///         output_p1db_dbm: None,
///         output_ip3_dbm: None,
///     },
///     Block {
///         name: "Attenuator".to_string(),
///         gain_db: -6.0,
///         noise_figure_db: 6.0,
///         output_p1db_dbm: None,
///         output_ip3_dbm: None,
///     },
/// ];
/// let output = cascade_vector_return_output(input, blocks);
/// assert_eq!(output.signal_power_dbm, -6.0); // -30 + 30 - 6
/// assert_eq!(output.cumulative_gain_db, 24.0);
/// ```
#[doc(alias = "cascade")]
#[doc(alias = "signal chain")]
#[doc(alias = "gain lineup")]
#[must_use]
pub fn cascade_vector_return_output(input: Input, blocks: Vec<Block>) -> SignalNode {
    let mut cascading_signal: SignalNode = SignalNode::default(); // will be overwritten in first iteration

    for (i, block) in blocks.iter().enumerate() {
        if i == 0 {
            cascading_signal = input.cascade_block(block);
        } else {
            cascading_signal = cascading_signal.cascade_block(block);
        }
    }

    cascading_signal
}

/// Cascade a vector of blocks and return a [`SignalNode`] for each stage output.
///
/// # Examples
///
/// ```
/// use gainlineup::{Input, Block, cascade_vector_return_vector};
///
/// let input = Input::new(1.0e9, 1.0e6, -30.0, Some(270.0));
/// let blocks = vec![
///     Block {
///         name: "LNA".to_string(),
///         gain_db: 30.0,
///         noise_figure_db: 1.5,
///         output_p1db_dbm: None,
///         output_ip3_dbm: None,
///     },
///     Block {
///         name: "Filter".to_string(),
///         gain_db: -3.0,
///         noise_figure_db: 3.0,
///         output_p1db_dbm: None,
///         output_ip3_dbm: None,
///     },
/// ];
/// let nodes = cascade_vector_return_vector(input, blocks);
/// assert_eq!(nodes.len(), 2);
/// assert_eq!(nodes[0].signal_power_dbm, 0.0);  // after LNA
/// assert_eq!(nodes[1].signal_power_dbm, -3.0);  // after filter
/// ```
#[doc(alias = "cascade")]
#[doc(alias = "signal chain")]
#[must_use]
pub fn cascade_vector_return_vector(input: Input, blocks: Vec<Block>) -> Vec<SignalNode> {
    let mut cascading_signal: SignalNode = SignalNode::default(); // will be overwritten in first iteration

    // initialize node vector without input node, since the signal nodes are created in the loop and start with the output of the first block
    let mut node_vector: Vec<SignalNode> = vec![];
    for (i, block) in blocks.iter().enumerate() {
        if i == 0 {
            cascading_signal = input.cascade_block(block);
        } else {
            cascading_signal = cascading_signal.cascade_block(block);
        }
        node_vector.push(cascading_signal.clone());
    }
    node_vector
}

/// Sweep input power through a cascade of blocks and return the AM-AM curve.
///
/// For each input power, the signal is passed through every block in sequence
/// using each block's compression model. Returns Vec of `(Pin_dBm, Pout_dBm)`.
///
/// # Examples
///
/// ```
/// use gainlineup::{Block, cascade_am_am_sweep};
///
/// let blocks = vec![
///     Block {
///         name: "LNA".to_string(),
///         gain_db: 20.0,
///         noise_figure_db: 3.0,
///         output_p1db_dbm: Some(10.0),
///         output_ip3_dbm: None,
///     },
/// ];
/// let sweep = cascade_am_am_sweep(&blocks, -40.0, -20.0, 10.0);
/// assert_eq!(sweep.len(), 3);
/// assert!((sweep[0].1 - (-20.0)).abs() < 0.01); // -40 + 20 = -20 (linear)
/// ```
#[doc(alias = "P1dB")]
#[doc(alias = "compression")]
#[must_use]
pub fn cascade_am_am_sweep(
    blocks: &[Block],
    start_dbm: f64,
    stop_dbm: f64,
    step_db: f64,
) -> Vec<(f64, f64)> {
    let mut powers = vec![];
    let mut pin = start_dbm;
    while pin <= stop_dbm + step_db * 0.01 {
        powers.push(pin);
        pin += step_db;
    }
    powers
        .iter()
        .map(|&pin| {
            let mut power = pin;
            for block in blocks {
                power = block.output_power(power);
            }
            (pin, power)
        })
        .collect()
}

/// Sweep input power through a cascade and return gain compression curve.
///
/// Returns Vec of `(Pin_dBm, Gain_dB)` showing total cascade gain vs. input power.
///
/// # Examples
///
/// ```
/// use gainlineup::{Block, cascade_gain_compression_sweep};
///
/// let blocks = vec![
///     Block {
///         name: "Amp".to_string(),
///         gain_db: 20.0,
///         noise_figure_db: 3.0,
///         output_p1db_dbm: Some(10.0),
///         output_ip3_dbm: None,
///     },
/// ];
/// let sweep = cascade_gain_compression_sweep(&blocks, -40.0, 0.0, 10.0);
/// assert!((sweep[0].1 - 20.0).abs() < 0.01); // full gain at low power
/// assert!(sweep.last().unwrap().1 < 20.0);    // compressed at high power
/// ```
#[doc(alias = "gain compression")]
#[must_use]
pub fn cascade_gain_compression_sweep(
    blocks: &[Block],
    start_dbm: f64,
    stop_dbm: f64,
    step_db: f64,
) -> Vec<(f64, f64)> {
    cascade_am_am_sweep(blocks, start_dbm, stop_dbm, step_db)
        .iter()
        .map(|&(pin, pout)| (pin, pout - pin))
        .collect()
}

#[cfg(test)]
mod tests {
    #[test]
    fn two_part_node_cascade_vector_return_output() {
        let input_power: f64 = -30.0;
        let input = super::Input {
            power_dbm: input_power,
            frequency_hz: 1.0e9, // 1 GHz
            bandwidth_hz: 0.0,   // CW
            noise_temperature_k: Some(270.0),
        };
        let amplifier = super::Block {
            name: "Low Noise Amplifier".to_string(),
            gain_db: 30.0,
            noise_figure_db: 3.0,
            output_p1db_dbm: None,
            output_ip3_dbm: None,
        };
        let attenuator = super::Block {
            name: "Attenuator".to_string(),
            gain_db: -6.0,
            noise_figure_db: 6.0,
            output_p1db_dbm: None,
            output_ip3_dbm: None,
        };
        let blocks = vec![amplifier, attenuator];
        let output_node = super::cascade_vector_return_output(input, blocks);

        assert_eq!(output_node.signal_power_dbm, -6.0);
        assert_eq!(output_node.cumulative_gain_db, 24.0);

        assert_eq!(output_node.name, "Attenuator Output");

        // round to 3 decimal places for comparison, because floating point math is not exact
        let rounded_noise_figure = (output_node.cumulative_noise_figure_db * 1e3).round() / 1e3;
        assert_eq!(rounded_noise_figure, 3.006);
    }

    #[test]
    fn two_part_node_cascade_vector_return_vector() {
        let input_power: f64 = -30.0;
        let input = super::Input {
            power_dbm: input_power,
            frequency_hz: 1.0e9, // 1 GHz
            bandwidth_hz: 0.0,   // CW
            noise_temperature_k: Some(270.0),
        };
        let amplifier = super::Block {
            name: "Low Noise Amplifier".to_string(),
            gain_db: 30.0,
            noise_figure_db: 3.0,
            output_p1db_dbm: None,
            output_ip3_dbm: None,
        };
        let attenuator = super::Block {
            name: "Attenuator".to_string(),
            gain_db: -6.0,
            noise_figure_db: 6.0,
            output_p1db_dbm: None,
            output_ip3_dbm: None,
        };
        let blocks = vec![amplifier, attenuator];
        let cascade_vector = super::cascade_vector_return_vector(input, blocks);

        let output_node = cascade_vector.last().unwrap();
        assert_eq!(output_node.signal_power_dbm, -6.0);
        assert_eq!(output_node.cumulative_gain_db, 24.0);

        assert_eq!(output_node.name, "Attenuator Output");

        // round to 3 decimal places for comparison, because floating point math is not exact
        let rounded_noise_figure = (output_node.cumulative_noise_figure_db * 1e3).round() / 1e3;
        assert_eq!(rounded_noise_figure, 3.006);
    }

    #[test]
    fn cascade_am_am_linear() {
        let blocks = vec![
            super::Block {
                name: "LNA".to_string(),
                gain_db: 20.0,
                noise_figure_db: 3.0,
                output_p1db_dbm: None,
                output_ip3_dbm: None,
            },
            super::Block {
                name: "Atten".to_string(),
                gain_db: -6.0,
                noise_figure_db: 6.0,
                output_p1db_dbm: None,
                output_ip3_dbm: None,
            },
        ];
        let sweep = super::cascade_am_am_sweep(&blocks, -40.0, -20.0, 10.0);
        assert_eq!(sweep.len(), 3);
        // Total gain = 20 - 6 = 14 dB
        assert!((sweep[0].1 - (-26.0)).abs() < 0.01); // -40 + 14 = -26
        assert!((sweep[1].1 - (-16.0)).abs() < 0.01); // -30 + 14 = -16
        assert!((sweep[2].1 - (-6.0)).abs() < 0.01); // -20 + 14 = -6
    }

    #[test]
    fn cascade_am_am_with_compression() {
        let blocks = vec![
            super::Block {
                name: "LNA".to_string(),
                gain_db: 30.0,
                noise_figure_db: 3.0,
                output_p1db_dbm: Some(5.0),
                output_ip3_dbm: None,
            },
            super::Block {
                name: "Driver".to_string(),
                gain_db: 10.0,
                noise_figure_db: 5.0,
                output_p1db_dbm: Some(15.0),
                output_ip3_dbm: None,
            },
        ];
        let sweep = super::cascade_am_am_sweep(&blocks, -50.0, 0.0, 10.0);
        // At -50: LNA out = -20, Driver out = -10 (linear)
        assert!((sweep[0].1 - (-10.0)).abs() < 0.01);
        // At high power, should compress
        let last = sweep.last().unwrap();
        assert!(last.1 <= 16.0, "Should compress at high input");
    }

    #[test]
    fn cascade_gain_compression() {
        let blocks = vec![super::Block {
            name: "Amp".to_string(),
            gain_db: 20.0,
            noise_figure_db: 3.0,
            output_p1db_dbm: Some(10.0),
            output_ip3_dbm: None,
        }];
        let sweep = super::cascade_gain_compression_sweep(&blocks, -40.0, 0.0, 10.0);
        // At -40: linear, gain = 20
        assert!((sweep[0].1 - 20.0).abs() < 0.01);
        // At 0: compressed, gain < 20
        let last = sweep.last().unwrap();
        assert!(last.1 < 20.0, "Gain should compress at high input");
    }

    #[test]
    fn two_part_node_cascade_vector_return_vector_with_compression() {
        let input_power: f64 = -30.0;
        let input = super::Input {
            power_dbm: input_power,
            frequency_hz: 1.0e9, // 1 GHz
            bandwidth_hz: 0.0,   // CW
            noise_temperature_k: Some(270.0),
        };
        let low_noise_amplifier = super::Block {
            name: "Low Noise Amplifier".to_string(),
            gain_db: 30.0,
            noise_figure_db: 3.0,
            output_p1db_dbm: Some(5.0),
            output_ip3_dbm: None,
        };
        let attenuator = super::Block {
            name: "Attenuator".to_string(),
            gain_db: -6.0,
            noise_figure_db: 6.0,
            output_p1db_dbm: None,
            output_ip3_dbm: None,
        };
        let high_power_amplifier = super::Block {
            name: "High Power Amplifier".to_string(),
            gain_db: 30.0,
            noise_figure_db: 3.0,
            output_p1db_dbm: Some(20.0),
            output_ip3_dbm: None,
        };
        let blocks = vec![low_noise_amplifier, attenuator, high_power_amplifier];
        let cascade_vector = super::cascade_vector_return_vector(input, blocks);

        let output_node = cascade_vector.last().unwrap();
        assert_eq!(output_node.signal_power_dbm, 21.0);
        assert_eq!(output_node.cumulative_gain_db, 51.0);

        assert_eq!(output_node.name, "High Power Amplifier Output");

        // round to 3 decimal places for comparison, because floating point math is not exact
        let rounded_noise_figure = (output_node.cumulative_noise_figure_db * 1e3).round() / 1e3;
        assert_eq!(rounded_noise_figure, 3.015);
    }
}