|
11 | 11 | import numpy as np |
12 | 12 |
|
13 | 13 | from pathsim_rf import RFAmplifier |
| 14 | +from pathsim_rf.amplifier import _dbm_to_vpeak |
14 | 15 |
|
15 | 16 |
|
16 | 17 | # TESTS ================================================================================ |
17 | 18 |
|
18 | 19 | class TestRFAmplifier(unittest.TestCase): |
19 | 20 | """Test the RFAmplifier block.""" |
20 | 21 |
|
| 22 | + # -- initialisation ---------------------------------------------------------------- |
| 23 | + |
21 | 24 | def test_init_default(self): |
22 | 25 | """Test default initialization.""" |
23 | 26 | amp = RFAmplifier() |
24 | | - self.assertEqual(amp.gain, 10.0) |
25 | | - self.assertIsNone(amp.saturation) |
| 27 | + self.assertEqual(amp.gain, 20.0) |
| 28 | + self.assertIsNone(amp.IIP3) |
| 29 | + self.assertIsNone(amp.P1dB) |
| 30 | + self.assertEqual(amp.Z0, 50.0) |
26 | 31 |
|
27 | 32 | def test_init_custom(self): |
28 | | - """Test custom initialization.""" |
29 | | - amp = RFAmplifier(gain=20.0, saturation=5.0) |
30 | | - self.assertEqual(amp.gain, 20.0) |
31 | | - self.assertEqual(amp.saturation, 5.0) |
| 33 | + """Test custom initialization with IIP3.""" |
| 34 | + amp = RFAmplifier(gain=15.0, IIP3=10.0, Z0=75.0) |
| 35 | + self.assertEqual(amp.gain, 15.0) |
| 36 | + self.assertEqual(amp.IIP3, 10.0) |
| 37 | + self.assertAlmostEqual(amp.P1dB, 10.0 - 9.6) |
| 38 | + self.assertEqual(amp.Z0, 75.0) |
| 39 | + |
| 40 | + def test_init_P1dB_derives_IIP3(self): |
| 41 | + """P1dB without IIP3 derives IIP3 = P1dB + 9.6.""" |
| 42 | + amp = RFAmplifier(P1dB=0.0) |
| 43 | + self.assertAlmostEqual(amp.IIP3, 9.6) |
| 44 | + self.assertAlmostEqual(amp.P1dB, 0.0) |
| 45 | + |
| 46 | + def test_IIP3_takes_precedence(self): |
| 47 | + """IIP3 takes precedence over P1dB when both given.""" |
| 48 | + amp = RFAmplifier(P1dB=0.0, IIP3=15.0) |
| 49 | + self.assertEqual(amp.IIP3, 15.0) |
| 50 | + self.assertAlmostEqual(amp.P1dB, 15.0 - 9.6) |
32 | 51 |
|
33 | 52 | def test_init_validation(self): |
34 | 53 | """Test input validation.""" |
35 | 54 | with self.assertRaises(ValueError): |
36 | | - RFAmplifier(gain=0) |
37 | | - with self.assertRaises(ValueError): |
38 | | - RFAmplifier(gain=-1) |
| 55 | + RFAmplifier(Z0=0) |
39 | 56 | with self.assertRaises(ValueError): |
40 | | - RFAmplifier(saturation=0) |
41 | | - with self.assertRaises(ValueError): |
42 | | - RFAmplifier(saturation=-1) |
| 57 | + RFAmplifier(Z0=-50) |
43 | 58 |
|
44 | 59 | def test_port_labels(self): |
45 | 60 | """Test port label definitions.""" |
46 | 61 | self.assertEqual(RFAmplifier.input_port_labels["rf_in"], 0) |
47 | 62 | self.assertEqual(RFAmplifier.output_port_labels["rf_out"], 0) |
48 | 63 |
|
49 | | - def test_linear_gain(self): |
50 | | - """Linear mode: output = gain * input.""" |
51 | | - amp = RFAmplifier(gain=5.0) |
52 | | - amp.inputs[0] = 2.0 |
| 64 | + # -- linear mode ------------------------------------------------------------------- |
| 65 | + |
| 66 | + def test_linear_gain_dB(self): |
| 67 | + """20 dB gain = voltage factor of 10.""" |
| 68 | + amp = RFAmplifier(gain=20.0) |
| 69 | + amp.inputs[0] = 0.1 |
53 | 70 | amp.update(None) |
54 | | - self.assertAlmostEqual(amp.outputs[0], 10.0) |
| 71 | + self.assertAlmostEqual(amp.outputs[0], 1.0) |
| 72 | + |
| 73 | + def test_linear_6dB(self): |
| 74 | + """6 dB gain ≈ voltage factor of ~2.""" |
| 75 | + amp = RFAmplifier(gain=6.0) |
| 76 | + amp.inputs[0] = 1.0 |
| 77 | + amp.update(None) |
| 78 | + expected = 10.0 ** (6.0 / 20.0) # 1.9953 |
| 79 | + self.assertAlmostEqual(amp.outputs[0], expected, places=4) |
55 | 80 |
|
56 | 81 | def test_linear_negative_input(self): |
57 | 82 | """Linear mode works with negative inputs.""" |
58 | | - amp = RFAmplifier(gain=3.0) |
59 | | - amp.inputs[0] = -4.0 |
| 83 | + amp = RFAmplifier(gain=20.0) |
| 84 | + amp.inputs[0] = -0.05 |
60 | 85 | amp.update(None) |
61 | | - self.assertAlmostEqual(amp.outputs[0], -12.0) |
| 86 | + self.assertAlmostEqual(amp.outputs[0], -0.5) |
62 | 87 |
|
63 | 88 | def test_linear_zero_input(self): |
64 | 89 | """Zero input produces zero output.""" |
65 | | - amp = RFAmplifier(gain=10.0) |
| 90 | + amp = RFAmplifier(gain=20.0) |
66 | 91 | amp.inputs[0] = 0.0 |
67 | 92 | amp.update(None) |
68 | 93 | self.assertAlmostEqual(amp.outputs[0], 0.0) |
69 | 94 |
|
70 | | - def test_saturation_small_signal(self): |
71 | | - """With saturation, small signals are approximately linear.""" |
72 | | - amp = RFAmplifier(gain=10.0, saturation=100.0) |
73 | | - amp.inputs[0] = 0.01 # small signal: gain*input/sat = 0.001 |
| 95 | + # -- nonlinear (IP3) mode ---------------------------------------------------------- |
| 96 | + |
| 97 | + def test_ip3_small_signal_linear(self): |
| 98 | + """Small signals are approximately linear even with IP3.""" |
| 99 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
| 100 | + # tiny input well below compression |
| 101 | + amp.inputs[0] = 1e-6 |
| 102 | + amp.update(None) |
| 103 | + expected_linear = amp._a1 * 1e-6 |
| 104 | + self.assertAlmostEqual(amp.outputs[0], expected_linear, places=10) |
| 105 | + |
| 106 | + def test_ip3_compression(self): |
| 107 | + """Near IP3 the output compresses below linear gain.""" |
| 108 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
| 109 | + A_iip3 = _dbm_to_vpeak(10.0, 50.0) |
| 110 | + # drive at half the IIP3 voltage — should see compression |
| 111 | + x_in = A_iip3 * 0.5 |
| 112 | + amp.inputs[0] = x_in |
74 | 113 | amp.update(None) |
75 | | - # tanh(x) ≈ x for small x, so output ≈ gain * input |
76 | | - self.assertAlmostEqual(amp.outputs[0], 10.0 * 0.01, places=3) |
| 114 | + linear_out = amp._a1 * x_in |
| 115 | + self.assertLess(amp.outputs[0], linear_out) |
77 | 116 |
|
78 | | - def test_saturation_large_signal(self): |
79 | | - """With saturation, large signals are clipped to saturation level.""" |
80 | | - amp = RFAmplifier(gain=100.0, saturation=5.0) |
81 | | - amp.inputs[0] = 1000.0 # heavily driven |
| 117 | + def test_ip3_saturation_clip(self): |
| 118 | + """Output is clipped at the gain compression peak for large signals.""" |
| 119 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
| 120 | + amp.inputs[0] = 1e3 # way beyond saturation |
82 | 121 | amp.update(None) |
83 | | - # tanh(large) ≈ 1, so output ≈ saturation |
84 | | - self.assertAlmostEqual(amp.outputs[0], 5.0, places=3) |
| 122 | + self.assertAlmostEqual(amp.outputs[0], amp._y_sat) |
85 | 123 |
|
86 | | - def test_saturation_symmetry(self): |
87 | | - """Saturation is symmetric for positive and negative inputs.""" |
88 | | - amp = RFAmplifier(gain=100.0, saturation=5.0) |
| 124 | + def test_ip3_symmetry(self): |
| 125 | + """Nonlinear response is odd-symmetric.""" |
| 126 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
89 | 127 |
|
90 | | - amp.inputs[0] = 1000.0 |
| 128 | + amp.inputs[0] = 1e3 |
91 | 129 | amp.update(None) |
92 | 130 | pos = amp.outputs[0] |
93 | 131 |
|
94 | | - amp.inputs[0] = -1000.0 |
| 132 | + amp.inputs[0] = -1e3 |
95 | 133 | amp.update(None) |
96 | 134 | neg = amp.outputs[0] |
97 | 135 |
|
98 | 136 | self.assertAlmostEqual(pos, -neg) |
99 | 137 |
|
100 | | - def test_saturation_zero(self): |
101 | | - """Zero input gives zero output even with saturation.""" |
102 | | - amp = RFAmplifier(gain=10.0, saturation=5.0) |
| 138 | + def test_ip3_zero(self): |
| 139 | + """Zero input gives zero output with IP3.""" |
| 140 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
103 | 141 | amp.inputs[0] = 0.0 |
104 | 142 | amp.update(None) |
105 | 143 | self.assertAlmostEqual(amp.outputs[0], 0.0) |
106 | 144 |
|
| 145 | + # -- helper ------------------------------------------------------------------------ |
| 146 | + |
| 147 | + def test_dbm_to_vpeak(self): |
| 148 | + """Verify dBm to peak voltage conversion.""" |
| 149 | + # 0 dBm = 1 mW into 50 Ohm -> V_rms = sqrt(0.001*50) = 0.2236 |
| 150 | + # V_peak = V_rms * sqrt(2) = 0.3162 |
| 151 | + v = _dbm_to_vpeak(0.0, 50.0) |
| 152 | + self.assertAlmostEqual(v, np.sqrt(2.0 * 50.0 * 1e-3), places=6) |
| 153 | + |
| 154 | + def test_dbm_to_vpeak_30dBm(self): |
| 155 | + """30 dBm = 1 W -> V_peak = sqrt(2*50*1) = 10.0 V.""" |
| 156 | + v = _dbm_to_vpeak(30.0, 50.0) |
| 157 | + self.assertAlmostEqual(v, 10.0, places=4) |
| 158 | + |
107 | 159 |
|
108 | 160 | # RUN TESTS LOCALLY ==================================================================== |
109 | 161 |
|
|
0 commit comments