Frǝd's Ham Radio Scratchpad - W6BSD

In [3]:
import numpy as np

from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

import tabulate

from ipywidgets import HTML

plt.style.use('ggplot')

x_size = 4

fig, ax = plt.subplots(figsize=((x_size + 1) * 2, 4))
ax.set_title("SWR along a (lossless) line")
ax.text(x_size + .05, -37, "https://0x9900.com/", color='gray', rotation=90)
ax.set_xlim(0, x_size)
ax.set_ylim(-40, 40)

l1, = ax.plot([], [], lw=2, color='g')
l2, = ax.plot([], [], lw=2, color='r')
l3, = ax.plot([], [], linestyle='dashed', lw=1, color='b')

ax.hlines(20, 0, x_size, color='k', linestyle=":")
ax.hlines(10, 0, x_size, color='k', linestyle=":")
ax.hlines(0, 0, x_size, color='gray', linestyle=":")
ax.text(4.02, 21, 'V-fwd', style='italic')
ax.text(4.02, 11, 'V-rev', style='italic')

def init():
    l1.set_data([], [])
    l2.set_data([], [])
    l3.set_data([], [])
    return l1, l2, l3

def animate(i):
    x = np.linspace(0, x_size, 200)
    y = np.sin(1.5 * np.pi * (x - 0.02 * i)) * 20
    z = np.sin(1.5 * np.pi * (x + 0.02 * i)) * 10
    l1.set_data(x, y)
    l2.set_data(x, z)
    l3.set_data(x, y+z)
    return l1, l2, l3

anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=40, blit=True)

file_name = 'vswr_wave_0.html'
with open(file_name, "w") as fdout:
    print(anim.to_html5_video(), file=fdout)
 

SWR along the line animation: vswr_wave_0.html

What about SWR

Mathematically we calculate SWR by determining the reflection coeficient gamma ($\Gamma$) which is a function of the load impedance ($Z_L$) and the source impedance ($Z_0$). These 2 impedances are complex number.

$$ \Gamma = \frac{Z_L - Z_0}{Z_L + Z_0} $$

or

$$ \Gamma = \frac{V_{rev}}{V_{fwd}} $$

or

$$ \Gamma = \frac{VSWR+1}{VSWR-1} $$

VSWR is them calculated using $\Gamma$ in the following equation:

$$ VSWR = \frac{1+|\Gamma|}{1-|\Gamma|} $$

In our example above we have the forward voltage is 20V, and the reverse voltage is 15V:

$$ \begin{align} \Gamma &= \frac{10}{20} && = 0.5 \\ \nonumber VSWR &= \frac{1+0.5}{1-0.5} && = 3 \end{align} $$

It is them possible to calculate the Return loss ($R_L$) using the equation: $$ R_L = 20 \log_{10}{\left(\frac{VSWR+1}{VSWR-1}\right)} $$

We can calculate the SWR from the Return Loss: $$ VSWR = \frac{10^{\frac{RL}{20}}+1}{10^{\frac{RL}{20}}-1} $$

The following equation is especially useful when using a directional power meter. It allows you to calculate the VSWR from the forward and reverse power.

$$ VSWR = \frac{1+\sqrt{\frac{P_{ref}}{P_{fwd}}}}{1-\sqrt{\frac{P_{ref}}{P_{fwd}}}} $$
In [15]:
from itertools import chain
x = []
y = []

swr_values = chain(range(1, 10), range(10, 20, 2), range(20, 55, 5))
table = [('vswr', '% Reflected PWR')]
for vswr in swr_values:
    rp = 100 * ((vswr-1)/(vswr+1))**2
    table.append((vswr, rp))
    x.append(rp)
    y.append(vswr)

print("Reflected power doesn't necessarily mean the power lost")
print(tabulate.tabulate(table, headers='firstrow', numalign="decimal", tablefmt="github", 
                        floatfmt=("2.1f", "6.2f")))
    
fig, ax = plt.subplots(figsize=(14, 6))
ax.set_title("VSWR and Reflected Power")
ax.text(max(*y) + .2, 3, "https://0x9900.com/", color='gray', rotation=90)
ax.set_xlabel("VSWR")
ax.set_ylabel("% Refected Power")
ax.set_ylim(0, 100)
ax.set_xlim(1, max(*y))
ax.plot(y, x)
plt.savefig('vswr.svg')
Reflected power doesn't necessarily mean the power lost
|   vswr |   % Reflected PWR |
|--------|-------------------|
|      1 |              0.00 |
|      2 |             11.11 |
|      3 |             25.00 |
|      4 |             36.00 |
|      5 |             44.44 |
|      6 |             51.02 |
|      7 |             56.25 |
|      8 |             60.49 |
|      9 |             64.00 |
|     10 |             66.94 |
|     12 |             71.60 |
|     14 |             75.11 |
|     16 |             77.85 |
|     18 |             80.06 |
|     20 |             81.86 |
|     25 |             85.21 |
|     30 |             87.51 |
|     35 |             89.20 |
|     40 |             90.48 |
|     45 |             91.49 |
|     50 |             92.31 |
In [16]:
x = []
y = []
table = [('RL (dB)', "VSWR")]
rl_values = chain(range(1, 11, 1), range(11, 30, 3), range(30, 49, 6))
for rl in rl_values:
    expo = 10**(rl/20)
    vswr = (expo+1)/(expo-1)
    table.append((rl, vswr))
    x.append(rl)
    y.append(vswr)

print(tabulate.tabulate(table, headers='firstrow', numalign="decimal", tablefmt="github", 
                        floatfmt=("%d", "2.3f")))
    
fig, ax = plt.subplots(figsize=(14, 6))
ax.set_title("Return Loss / VSWR")
ax.text(10.1, 1, "https://0x9900.com/", color='gray', rotation=90)
ax.set_xlabel("VSWR")
ax.set_ylabel("Return Loss (dB)")
ax.set_ylim(0, 40)
ax.set_xlim(1, 10)
ax.plot(y, x)
plt.savefig('vswr.svg')
|   RL (dB) |   VSWR |
|-----------|--------|
|         1 | 17.391 |
|         2 |  8.724 |
|         3 |  5.848 |
|         4 |  4.419 |
|         5 |  3.570 |
|         6 |  3.010 |
|         7 |  2.615 |
|         8 |  2.323 |
|         9 |  2.100 |
|        10 |  1.925 |
|        11 |  1.785 |
|        14 |  1.499 |
|        17 |  1.329 |
|        20 |  1.222 |
|        23 |  1.152 |
|        26 |  1.106 |
|        29 |  1.074 |
|        30 |  1.065 |
|        36 |  1.032 |
|        42 |  1.016 |
|        48 |  1.008 |
$$ \begin{align} |Z| &= \sqrt{R^2 + jX^2} \\ \nonumber \theta &= \arctan{\frac{X}{R}} \end{align} $$

Q Factor of an inductor

$$ Q=\frac{X_L}{R} $$

or

$$ \begin{align} Q &= \frac{2{\pi}fL}{R}\\ \nonumber &= \frac{2{\pi} \times 14.15 \times {10^6} \times 220 \times {10^{-9}}}{.05} \\ \nonumber &= 391 \end{align} $$

LC Q factor equations

Series LC

$$ Q = \frac{1}{R} \times \sqrt{\frac{L}{C}} $$

Parallel LC

$$ Q = R\times\sqrt{\frac{C}{L}} $$

Antenna Length

To determine the target length of the antenna elements, the relationship in Eq. (4) was used: $$ L_{target} = L_{measured} \times \frac{f_{measured}}{f_{target}} $$

Power: dBm -> Watt

$$ \begin{align} P_{mW} &= 10^{\left(\frac{P_{dBm}}{10}\right)} \\ \nonumber P_{W} &= 10^{-3} \times 10^{\left(\frac{P_{dBm}}{10}\right)} \end{align} $$

Power: Watt -> dBm

$$ \begin{align} P_{dBm} &= 10 \times \log_{10}(P_{W} \times 10^{-3}) \\ \nonumber &= 10 \times \log_{10}(P_{W}) + 30 \end{align} $$

Volt -> dBm

$$ P_{dBm} = 10 \times \log_{10}\left(\frac{V_{rms}*1000}{Z}\right) $$

dBm -> Voltage

$$ V = {\sqrt {R \times {\frac {10^{\frac{P_{dBm}}{10}}}{1000}}}} $$
In [75]:
import math

imp = 50
results = [[' ', 'dBm', 'Watt', 'Volt (PP)', 'Volt (RMS)', 'Amp']]
for dbm in (float(d) for d in range(64, -21, -1)):
    watt = 0.001 * 10**(dbm/10)
    volt = math.sqrt(imp*(10**(dbm/10)/1000))
    volt_pp = 2 * volt * math.sqrt(2)
    current = volt / imp
    marker = '->' if dbm % 10 == 0 else ' ' 
    results.append((marker, dbm, watt, volt_pp, volt, current))
    
table = tabulate.tabulate(results, headers='firstrow', tablefmt="github", 
                          floatfmt=("s", "3.0f", "3.3f", "3.3f", "3.3f", "3.4f"))
print(' ' + '-' * 23 + f' On a {imp}Ω load ' + '-' * 24)
print(table)
 ----------------------- On a 50Ω load ------------------------
|     |   dBm |     Watt |   Volt (PP) |   Volt (RMS) |    Amp |
|-----|-------|----------|-------------|--------------|--------|
|     |    64 | 2511.886 |    1002.374 |      354.393 | 7.0879 |
|     |    63 | 1995.262 |     893.367 |      315.853 | 6.3171 |
|     |    62 | 1584.893 |     796.214 |      281.504 | 5.6301 |
|     |    61 | 1258.925 |     709.627 |      250.891 | 5.0178 |
| ->  |    60 | 1000.000 |     632.456 |      223.607 | 4.4721 |
|     |    59 |  794.328 |     563.677 |      199.290 | 3.9858 |
|     |    58 |  630.957 |     502.377 |      177.617 | 3.5523 |
|     |    57 |  501.187 |     447.744 |      158.301 | 3.1660 |
|     |    56 |  398.107 |     399.052 |      141.086 | 2.8217 |
|     |    55 |  316.228 |     355.656 |      125.743 | 2.5149 |
|     |    54 |  251.189 |     316.979 |      112.069 | 2.2414 |
|     |    53 |  199.526 |     282.508 |       99.881 | 1.9976 |
|     |    52 |  158.489 |     251.785 |       89.019 | 1.7804 |
|     |    51 |  125.893 |     224.404 |       79.339 | 1.5868 |
| ->  |    50 |  100.000 |     200.000 |       70.711 | 1.4142 |
|     |    49 |   79.433 |     178.250 |       63.021 | 1.2604 |
|     |    48 |   63.096 |     158.866 |       56.167 | 1.1233 |
|     |    47 |   50.119 |     141.589 |       50.059 | 1.0012 |
|     |    46 |   39.811 |     126.191 |       44.615 | 0.8923 |
|     |    45 |   31.623 |     112.468 |       39.764 | 0.7953 |
|     |    44 |   25.119 |     100.237 |       35.439 | 0.7088 |
|     |    43 |   19.953 |      89.337 |       31.585 | 0.6317 |
|     |    42 |   15.849 |      79.621 |       28.150 | 0.5630 |
|     |    41 |   12.589 |      70.963 |       25.089 | 0.5018 |
| ->  |    40 |   10.000 |      63.246 |       22.361 | 0.4472 |
|     |    39 |    7.943 |      56.368 |       19.929 | 0.3986 |
|     |    38 |    6.310 |      50.238 |       17.762 | 0.3552 |
|     |    37 |    5.012 |      44.774 |       15.830 | 0.3166 |
|     |    36 |    3.981 |      39.905 |       14.109 | 0.2822 |
|     |    35 |    3.162 |      35.566 |       12.574 | 0.2515 |
|     |    34 |    2.512 |      31.698 |       11.207 | 0.2241 |
|     |    33 |    1.995 |      28.251 |        9.988 | 0.1998 |
|     |    32 |    1.585 |      25.179 |        8.902 | 0.1780 |
|     |    31 |    1.259 |      22.440 |        7.934 | 0.1587 |
| ->  |    30 |    1.000 |      20.000 |        7.071 | 0.1414 |
|     |    29 |    0.794 |      17.825 |        6.302 | 0.1260 |
|     |    28 |    0.631 |      15.887 |        5.617 | 0.1123 |
|     |    27 |    0.501 |      14.159 |        5.006 | 0.1001 |
|     |    26 |    0.398 |      12.619 |        4.462 | 0.0892 |
|     |    25 |    0.316 |      11.247 |        3.976 | 0.0795 |
|     |    24 |    0.251 |      10.024 |        3.544 | 0.0709 |
|     |    23 |    0.200 |       8.934 |        3.159 | 0.0632 |
|     |    22 |    0.158 |       7.962 |        2.815 | 0.0563 |
|     |    21 |    0.126 |       7.096 |        2.509 | 0.0502 |
| ->  |    20 |    0.100 |       6.325 |        2.236 | 0.0447 |
|     |    19 |    0.079 |       5.637 |        1.993 | 0.0399 |
|     |    18 |    0.063 |       5.024 |        1.776 | 0.0355 |
|     |    17 |    0.050 |       4.477 |        1.583 | 0.0317 |
|     |    16 |    0.040 |       3.991 |        1.411 | 0.0282 |
|     |    15 |    0.032 |       3.557 |        1.257 | 0.0251 |
|     |    14 |    0.025 |       3.170 |        1.121 | 0.0224 |
|     |    13 |    0.020 |       2.825 |        0.999 | 0.0200 |
|     |    12 |    0.016 |       2.518 |        0.890 | 0.0178 |
|     |    11 |    0.013 |       2.244 |        0.793 | 0.0159 |
| ->  |    10 |    0.010 |       2.000 |        0.707 | 0.0141 |
|     |     9 |    0.008 |       1.783 |        0.630 | 0.0126 |
|     |     8 |    0.006 |       1.589 |        0.562 | 0.0112 |
|     |     7 |    0.005 |       1.416 |        0.501 | 0.0100 |
|     |     6 |    0.004 |       1.262 |        0.446 | 0.0089 |
|     |     5 |    0.003 |       1.125 |        0.398 | 0.0080 |
|     |     4 |    0.003 |       1.002 |        0.354 | 0.0071 |
|     |     3 |    0.002 |       0.893 |        0.316 | 0.0063 |
|     |     2 |    0.002 |       0.796 |        0.282 | 0.0056 |
|     |     1 |    0.001 |       0.710 |        0.251 | 0.0050 |
| ->  |     0 |    0.001 |       0.632 |        0.224 | 0.0045 |
|     |    -1 |    0.001 |       0.564 |        0.199 | 0.0040 |
|     |    -2 |    0.001 |       0.502 |        0.178 | 0.0036 |
|     |    -3 |    0.001 |       0.448 |        0.158 | 0.0032 |
|     |    -4 |    0.000 |       0.399 |        0.141 | 0.0028 |
|     |    -5 |    0.000 |       0.356 |        0.126 | 0.0025 |
|     |    -6 |    0.000 |       0.317 |        0.112 | 0.0022 |
|     |    -7 |    0.000 |       0.283 |        0.100 | 0.0020 |
|     |    -8 |    0.000 |       0.252 |        0.089 | 0.0018 |
|     |    -9 |    0.000 |       0.224 |        0.079 | 0.0016 |
| ->  |   -10 |    0.000 |       0.200 |        0.071 | 0.0014 |
|     |   -11 |    0.000 |       0.178 |        0.063 | 0.0013 |
|     |   -12 |    0.000 |       0.159 |        0.056 | 0.0011 |
|     |   -13 |    0.000 |       0.142 |        0.050 | 0.0010 |
|     |   -14 |    0.000 |       0.126 |        0.045 | 0.0009 |
|     |   -15 |    0.000 |       0.112 |        0.040 | 0.0008 |
|     |   -16 |    0.000 |       0.100 |        0.035 | 0.0007 |
|     |   -17 |    0.000 |       0.089 |        0.032 | 0.0006 |
|     |   -18 |    0.000 |       0.080 |        0.028 | 0.0006 |
|     |   -19 |    0.000 |       0.071 |        0.025 | 0.0005 |
| ->  |   -20 |    0.000 |       0.063 |        0.022 | 0.0004 |
In [ ]: