|
| 1 | +######################################################################################## |
| 2 | +## |
| 3 | +## TESTS FOR |
| 4 | +## 'amplifier.py' |
| 5 | +## |
| 6 | +######################################################################################## |
| 7 | + |
| 8 | +# IMPORTS ============================================================================== |
| 9 | + |
| 10 | +import unittest |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +from pathsim_rf import RFAmplifier |
| 14 | +from pathsim_rf.amplifier import _dbm_to_vpeak |
| 15 | + |
| 16 | + |
| 17 | +# TESTS ================================================================================ |
| 18 | + |
| 19 | +class TestRFAmplifier(unittest.TestCase): |
| 20 | + """Test the RFAmplifier block.""" |
| 21 | + |
| 22 | + # -- initialisation ---------------------------------------------------------------- |
| 23 | + |
| 24 | + def test_init_default(self): |
| 25 | + """Test default initialization.""" |
| 26 | + amp = RFAmplifier() |
| 27 | + self.assertEqual(amp.gain, 20.0) |
| 28 | + self.assertIsNone(amp.IIP3) |
| 29 | + self.assertIsNone(amp.P1dB) |
| 30 | + self.assertEqual(amp.Z0, 50.0) |
| 31 | + |
| 32 | + def test_init_custom(self): |
| 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) |
| 51 | + |
| 52 | + def test_init_validation(self): |
| 53 | + """Test input validation.""" |
| 54 | + with self.assertRaises(ValueError): |
| 55 | + RFAmplifier(Z0=0) |
| 56 | + with self.assertRaises(ValueError): |
| 57 | + RFAmplifier(Z0=-50) |
| 58 | + |
| 59 | + def test_port_labels(self): |
| 60 | + """Test port label definitions.""" |
| 61 | + self.assertEqual(RFAmplifier.input_port_labels["rf_in"], 0) |
| 62 | + self.assertEqual(RFAmplifier.output_port_labels["rf_out"], 0) |
| 63 | + |
| 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 |
| 70 | + amp.update(None) |
| 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) |
| 80 | + |
| 81 | + def test_linear_negative_input(self): |
| 82 | + """Linear mode works with negative inputs.""" |
| 83 | + amp = RFAmplifier(gain=20.0) |
| 84 | + amp.inputs[0] = -0.05 |
| 85 | + amp.update(None) |
| 86 | + self.assertAlmostEqual(amp.outputs[0], -0.5) |
| 87 | + |
| 88 | + def test_linear_zero_input(self): |
| 89 | + """Zero input produces zero output.""" |
| 90 | + amp = RFAmplifier(gain=20.0) |
| 91 | + amp.inputs[0] = 0.0 |
| 92 | + amp.update(None) |
| 93 | + self.assertAlmostEqual(amp.outputs[0], 0.0) |
| 94 | + |
| 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 |
| 113 | + amp.update(None) |
| 114 | + linear_out = amp._a1 * x_in |
| 115 | + self.assertLess(amp.outputs[0], linear_out) |
| 116 | + |
| 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 |
| 121 | + amp.update(None) |
| 122 | + self.assertAlmostEqual(amp.outputs[0], amp._y_sat) |
| 123 | + |
| 124 | + def test_ip3_symmetry(self): |
| 125 | + """Nonlinear response is odd-symmetric.""" |
| 126 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
| 127 | + |
| 128 | + amp.inputs[0] = 1e3 |
| 129 | + amp.update(None) |
| 130 | + pos = amp.outputs[0] |
| 131 | + |
| 132 | + amp.inputs[0] = -1e3 |
| 133 | + amp.update(None) |
| 134 | + neg = amp.outputs[0] |
| 135 | + |
| 136 | + self.assertAlmostEqual(pos, -neg) |
| 137 | + |
| 138 | + def test_ip3_zero(self): |
| 139 | + """Zero input gives zero output with IP3.""" |
| 140 | + amp = RFAmplifier(gain=20.0, IIP3=10.0) |
| 141 | + amp.inputs[0] = 0.0 |
| 142 | + amp.update(None) |
| 143 | + self.assertAlmostEqual(amp.outputs[0], 0.0) |
| 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 | + |
| 159 | + |
| 160 | +# RUN TESTS LOCALLY ==================================================================== |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + unittest.main(verbosity=2) |
0 commit comments