Debian Codegraph @

skrf/tests/test_tlineFunctions.py

raw symbols SCIP
1 import unittest
2
3 import numpy as np
4 from numpy import array, imag, linspace, pi, real
5 from numpy.testing import assert_almost_equal, assert_equal
6
7 import skrf as rf
8
9
10 class TestBasicTransmissionLine(unittest.TestCase):
11 """
12 Test reflection coefficient-related functions
13 """
14
15 def setUp(self):
16 # define example test case
17 self.d = 1.5 # line length [m]
18 self.z0 = 100 # line characteristic impedance [Ohm]
19 self.zin = 40 - 280j # line input impedance [Ohm]
20
21 def test_input_reflection_coefficient(self):
22 """
23 Test the input reflection coefficient value of the line.
24 """
25 Gamma_in = rf.zl_2_Gamma0(self.z0, self.zin)
26
27 expected_Gamma_in = (self.zin - self.z0)/(self.zin + self.z0)
28
29 assert_equal(Gamma_in, expected_Gamma_in)
30
31 def test_propagation_constant_from_reflection_coefficient(self):
32 """
33 Test the propagation constant value deduced from reflection coef
34 """
35 Gamma_in = rf.zl_2_Gamma0(self.z0, self.zin)
36 Gamma_l = -1 # short
37
38 gamma = rf.reflection_coefficient_2_propagation_constant(Gamma_in,
39 Gamma_l,
40 self.d)
41 expected_gamma = 0.02971 + 1.272j
42
43 assert_almost_equal(real(gamma), real(expected_gamma), decimal=4)
44 assert_almost_equal(imag(gamma), imag(expected_gamma), decimal=4)
45
46
47 class ElectricalLengthTests(unittest.TestCase):
48 """
49 Test the functions related to electrical length conversions.
50 """
51 def setUp(self):
52 self.d = 1.5 # m
53 self.gamma0 = 0.2 + 5j
54 self.gammas = 0.2 + 1j*linspace(1, 10, num=50)
55 self.f0 = 50e6
56 self.fs = linspace(1, 50, num=50)*1e6
57 self.theta0 = self.gamma0 * self.d
58 self.thetas = self.gammas * self.d
59
60
61 def gamma_from_f(self, f0):
62 """
63 Dummy gamma(f)
64
65 Parameters
66 ----------
67 f0 : number of array-like
68 frequency in Hz
69
70 Returns
71 -------
72 gamma : number of array-like
73 propagation constant
74
75 """
76 return (0.2 + 5j)*f0
77
78 def test_electrical_length_from_length(self):
79 """
80 Test the conversions from physical distance to electrical lengths.
81 """
82 # test for gamma passed as scalar
83 theta_scalar = rf.electrical_length(self.gamma0, self.f0, self.d)
84 theta_scalar_expected = self.gamma0 * self.d
85 assert_almost_equal(theta_scalar, theta_scalar_expected)
86
87 # test for gamma passed as array like
88 theta_array = rf.electrical_length(self.gammas, self.f0, self.d)
89 theta_array_expected = self.gammas * self.d
90 assert_almost_equal(theta_array, theta_array_expected)
91
92 # test for gamma passed as function
93 theta_function = rf.electrical_length(self.gamma_from_f, self.fs, self.d)
94 theta_function_expected = self.gamma_from_f(self.fs) * self.d
95 assert_almost_equal(theta_function, theta_function_expected)
96
97
98 def test_length_from_electrical_distance(self):
99 """
100 Test the conversions from electrical length to physical distances.
101 """
102 # test for gamma passed as scalar
103 d_scalar = rf.electrical_length_2_distance(self.theta0, self.gamma0, self.f0, deg=False)
104 d_scalar_expected = real(self.theta0 / self.gamma0)
105 assert_almost_equal(d_scalar, d_scalar_expected)
106
107 # test for gamma passed as array-like
108 d_array = rf.electrical_length_2_distance(self.theta0, self.gammas, self.f0, deg=False)
109 d_array_expected = real(self.theta0 / self.gammas)
110 assert_almost_equal(d_array, d_array_expected)
111
112 # test for gamma passed as function
113 d_function = rf.electrical_length_2_distance(self.theta0, self.gamma_from_f, self.fs, deg=False)
114 d_function_expected = real(self.theta0 / self.gamma_from_f(self.fs))
115 assert_almost_equal(d_function, d_function_expected)
116 # with theta passed as array
117 d_function = rf.electrical_length_2_distance(self.thetas, self.gamma_from_f, self.fs, deg=False)
118 d_function_expected = real(self.thetas / self.gamma_from_f(self.fs))
119 assert_almost_equal(d_function, d_function_expected)
120
121 # with theta passed in degrees
122 d_scalar = rf.electrical_length_2_distance(self.theta0*180/pi, self.gamma0, self.f0, deg=True)
123 d_scalar_expected = real(self.theta0 / self.gamma0)
124 assert_almost_equal(d_scalar, d_scalar_expected)
125
126
127
128
129
130 class TestVoltageCurrentPropagation(unittest.TestCase):
131 def setUp(self):
132 pass
133
134 def test_d_zero(self):
135 """
136 Propagate voltage and current on a d=0 transmission line.
137 Voltage and current are of course equal.
138 """
139 gamma = array([1j])
140 d = 0
141 z0 = 50
142 v1 = 3
143 i1 = 2
144 theta = gamma * d
145
146 v2, i2 = rf.voltage_current_propagation(v1, i1, z0, theta)
147
148 assert_almost_equal(v2, v1)
149 assert_almost_equal(i2, i1)
150
151 def test_d_wavelength(self):
152 """
153 Propagate voltage and current on a d=lambda lossless transmission line.
154 Voltage and current are equal.
155 """
156 z0 = 50
157 rng = np.random.default_rng()
158 v1 = rng.random()
159 i1 = rng.random()
160 theta = 1j*2*pi
161
162 v2, i2 = rf.voltage_current_propagation(v1, i1, z0, theta)
163 assert_almost_equal(v2, v1)
164 assert_almost_equal(i2, i1)
165
166 def test_d_half_wavelength(self):
167 """
168 Propagate voltage and current on a d=lambda/2 lossless transmission line.
169 Voltage and current are inversed.
170 """
171 z0 = 50
172 rng = np.random.default_rng()
173 v1 = rng.random()
174 i1 = rng.random()
175 theta = 1j*pi
176
177 v2, i2 = rf.voltage_current_propagation(v1, i1, z0, theta)
178
179 assert_almost_equal(v2, -v1)
180 assert_almost_equal(i2, -i1)