Skip to main content

lib_jsl_electromagnetics/networks/
smith_chart.rs

1//! Smith chart geometry: the map between normalised impedance and reflection coefficient, the
2//! constant-resistance and constant-reactance circles, and impedance loci along a line.
3//!
4//! See the book entry [Transmission lines, two-port networks and the Smith chart](https://jwkunz.github.io/lib_jsl/book/em-waves/transmission-lines.html).
5
6use crate::conventions::C64;
7
8/// The reflection coefficient of the normalised impedance `z = Z/Z₀`: `Γ = (z − 1)/(z + 1)`.
9pub fn impedance_to_gamma(z: C64) -> C64 {
10    if z.re.is_infinite() || z.im.is_infinite() {
11        return C64::new(1.0, 0.0);
12    }
13    (z - 1.0) / (z + 1.0)
14}
15
16/// The normalised impedance at reflection coefficient `Γ`: `z = (1 + Γ)/(1 − Γ)`.
17pub fn gamma_to_impedance(gamma: C64) -> C64 {
18    (1.0 + gamma) / (1.0 - gamma)
19}
20
21/// The normalised admittance at `Γ` (the impedance at `−Γ`): reading the chart as an admittance
22/// chart is a rotation by 180°.
23pub fn gamma_to_admittance(gamma: C64) -> C64 {
24    gamma_to_impedance(-gamma)
25}
26
27/// The circle of constant normalised resistance `r`: centre `(r/(1+r), 0)`, radius `1/(1+r)`.
28pub fn resistance_circle(r: f64) -> (C64, f64) {
29    (C64::new(r / (1.0 + r), 0.0), 1.0 / (1.0 + r))
30}
31
32/// The circle of constant normalised reactance `x ≠ 0`: centre `(1, 1/x)`, radius `1/|x|`.
33pub fn reactance_circle(x: f64) -> (C64, f64) {
34    (C64::new(1.0, 1.0 / x), 1.0 / x.abs())
35}
36
37/// The circle of constant VSWR through `Γ`: centred on the chart with radius `|Γ|`.
38pub fn vswr_circle(gamma: C64) -> (C64, f64) {
39    (C64::new(0.0, 0.0), gamma.norm())
40}
41
42/// The reflection coefficient seen `d` wavelengths from the load towards the generator on a
43/// lossless line: `Γ(d) = Γ_L e^{−j4πd}` (clockwise rotation, once per half wavelength).
44pub fn towards_generator(gamma_load: C64, wavelengths: f64) -> C64 {
45    gamma_load * C64::from_polar(1.0, -4.0 * std::f64::consts::PI * wavelengths)
46}
47
48/// The locus of `Γ` along a lossless line from the load, at the given distances (wavelengths).
49pub fn impedance_locus(z_load: C64, distances: &[f64]) -> Vec<C64> {
50    let g = impedance_to_gamma(z_load);
51    distances.iter().map(|&d| towards_generator(g, d)).collect()
52}
53
54/// Points of a circle (centre, radius) clipped to the unit disc, for plotting chart grids.
55pub fn circle_points(centre: C64, radius: f64, samples: usize) -> Vec<C64> {
56    (0..samples)
57        .map(|k| centre + C64::from_polar(radius, std::f64::consts::TAU * k as f64 / samples as f64))
58        .filter(|p| p.norm() <= 1.0 + 1e-12)
59        .collect()
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn chart_circles_contain_the_mapped_impedances() {
68        for r in [0.0, 0.5, 1.0, 3.0] {
69            let (centre, radius) = resistance_circle(r);
70            for x in [-5.0, -0.3, 0.0, 0.7, 20.0] {
71                let g = impedance_to_gamma(C64::new(r, x));
72                assert!(((g - centre).norm() - radius).abs() < 1e-12, "r={r} x={x}");
73                if x != 0.0 {
74                    let (cx, rx) = reactance_circle(x);
75                    assert!(((g - cx).norm() - rx).abs() < 1e-12);
76                }
77            }
78        }
79        assert_eq!(impedance_to_gamma(C64::new(1.0, 0.0)), C64::new(0.0, 0.0));
80        assert_eq!(impedance_to_gamma(C64::new(f64::INFINITY, 0.0)), C64::new(1.0, 0.0));
81        let z = C64::new(0.4, -1.3);
82        assert!((gamma_to_impedance(impedance_to_gamma(z)) - z).norm() < 1e-12);
83        assert!((gamma_to_admittance(impedance_to_gamma(z)) - 1.0 / z).norm() < 1e-12);
84    }
85
86    #[test]
87    fn rotation_towards_the_generator_inverts_at_a_quarter_wave() {
88        let z = C64::new(2.0, 0.5);
89        let quarter = gamma_to_impedance(towards_generator(impedance_to_gamma(z), 0.25));
90        assert!((quarter - 1.0 / z).norm() < 1e-12);
91        let locus = impedance_locus(z, &[0.0, 0.1, 0.5]);
92        assert!((locus[2] - locus[0]).norm() < 1e-12 && (locus[1].norm() - locus[0].norm()).abs() < 1e-12);
93        assert!((vswr_circle(locus[0]).1 - impedance_to_gamma(z).norm()).abs() < 1e-15);
94        assert!(circle_points(C64::new(0.0, 0.0), 1.0, 36).len() == 36);
95    }
96}