RigExpert AA-30 Zero用のPythonプログラム

AA-30 Zeroは、RigExpert社のローコストなアンテナ・アナライザーです。

私の以前のプログラム は、C, C++, AWK, gnuplotの混ぜこぜだったのですが、これはPythonで書かれたバージョンです。

# aa30zero.py

import serial
import time
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("freq", help="center freq in kHz, example 7000000")
parser.add_argument("bw", help="band width in kHz, example 2000000")
parser.add_argument("ndiv", help="no. of divisions, example 100")

args = parser.parse_args()
print('!! freq = ', args.freq)
print('!! bw = ', args.bw)
print('!! ndiv = ', args.ndiv)

ser = serial.Serial('/dev/cu.usbmodem14701', 38400, timeout=1)
print('!! ', ser)

time.sleep(2)
ser.write(b'ver'+b'\x0a')

time.sleep(1)
ser.write(b'fq'+args.freq.encode('ASCII')+b'\x0a')

time.sleep(1)
ser.write(b'sw'+args.bw.encode('ASCII')+b'\x0a')

for i in range(3):
    line = ser.readline()
    print('!! ', line)

time.sleep(1)
ser.write(b'frx'+args.ndiv.encode('ASCII')+b'\x0a')

print('# MHz S RI R 50')
z0 = complex(50.0, 0.0)

for i in range(int(args.ndiv)+1):
    line = ser.readline().decode(encoding='utf-8').rstrip().split(',')
    freq = float(line[0])
    z = complex(float(line[1]), float(line[2]))
    rho = (z-z0)/(z+z0)
    print('! ', freq, z.real, z.imag)
    print(freq, rho.real, rho.imag)

for i in range(1):
    line = ser.readline()
    print('!! ', line)

ser.close()

最初のプログラムは、AA-30 Zeroを制御して、touchstoneファイルを生成します。

PythonのライブラリpySerialを用いて、デバイスと通信を行います。

$ python aa30zero.py 5200000 1000000 100 | tee touchstone.s1p
 
$ cat touchstone.s1p
!! freq =  5200000
!! bw =  1000000
!! ndiv =  100
!!  Serial<id=0x10fab2860, open=True>(port='/dev/cu.usbmodem14701', baudrate=38400, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)
!!  b'AA-30 ZERO 150\r\n'
!!  b'OK\r\n'
!!  b'OK\r\n'
# MHz S RI R 50
!  4.7 26.910389 -45.76308
4.7 0.0397565169109296 -0.5713623335864779
...

$ python scikit-rf.py

二番目のプログラムは、touchstoneファイルを読み出してスクリーン上にグラフを幾つか表示します。

# scikit-rf.py

import matplotlib.pyplot as plt
import skrf as rf
from pylab import xkcd
import numpy as np

xkcd()
plt.style.use('ggplot')
plt.figure(figsize=(8, 18))

nwk = rf.Network('touchstone.s1p')

index = np.argmin(nwk.s_db)
fpeak = nwk.f[index] / 1000000 # fvector in Hz
rlmax = nwk.s_db[index, 0, 0]

plt.subplot(4, 1, 1)
nwk.plot_s_db(label='S11 [dB]', lw=3)
plt.title('Return Loss max = {0:.2f} dB at {1} [MHz]'.format(-rlmax, fpeak))
plt.grid(True)

plt.subplot(4, 1, 2)
nwk.plot_z_re(label='Real{Z}', color='blue', lw=3)
plt.grid(True)

plt.subplot(4, 1, 3)
nwk.plot_z_im(label='Imag{Z}', color='orange', lw=3)
plt.grid(True)

plt.subplot(4, 1, 4)
nwk.plot_s_smith(label='S11', draw_labels=True, color='green', lw=3)

plt.show()

scikit-rfというライブラリのおかげで、このプログラムは非常にシンプルです。