Skip to content

Instantly share code, notes, and snippets.

@edy555 edy555/vna_probe.py
Created Mar 30, 2013

Embed
What would you like to do?
VNAからgnuradioにより測定値を得るスクリプト
#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Vna Probe
# Generated: Sat Jan 12 03:07:08 2013
##################################################
from pylibftdi import BitBangDevice
from vna import vna
import pylab as plb
import skrf as rf
from gnuradio import audio
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
import gnuradio.gr.gr_threading as _threading
import time
from optparse import OptionParser
class vna_probe(gr.top_block):
def __init__(self):
gr.top_block.__init__(self)
##################################################
# Variables
##################################################
self.signal_freq = signal_freq = 5000
self.samp_rate = samp_rate = 48000
self.bw = bw = 200
##################################################
# Blocks
##################################################
self.gr_probe_ref = gr.probe_signal_f()
self.gr_probe_mag = gr.probe_signal_f()
self.gr_probe_re = gr.probe_signal_f()
self.gr_probe_im = gr.probe_signal_f()
self.gr_divide_xx_0 = gr.divide_cc(1)
self.gr_complex_to_mag_ref = gr.complex_to_mag(1)
self.gr_complex_to_mag_0 = gr.complex_to_mag(1)
self.gr_complex_to_arg_0 = gr.complex_to_arg(1)
self.gr_complex_to_float = gr.complex_to_float(1)
self.band_pass_filter_0_0 = gr.fir_filter_fcc(1, firdes.complex_band_pass(
1, samp_rate, signal_freq-bw/2, signal_freq+bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
self.band_pass_filter_0 = gr.fir_filter_fcc(1, firdes.complex_band_pass(
1, samp_rate, signal_freq-bw/2, signal_freq+bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
self.audio_source_0 = audio.source(samp_rate, "", True)
##################################################
# Connections
##################################################
self.connect((self.band_pass_filter_0_0, 0), (self.gr_complex_to_mag_0, 0))
self.connect((self.gr_complex_to_mag_0, 0), (self.gr_probe_mag, 0))
self.connect((self.gr_divide_xx_0, 0), (self.gr_complex_to_float, 0))
self.connect((self.band_pass_filter_0_0, 0), (self.gr_divide_xx_0, 0))
self.connect((self.band_pass_filter_0, 0), (self.gr_divide_xx_0, 1))
self.connect((self.audio_source_0, 1), (self.band_pass_filter_0_0, 0))
self.connect((self.audio_source_0, 0), (self.band_pass_filter_0, 0))
self.connect((self.gr_complex_to_float, 0), (self.gr_probe_re, 0))
self.connect((self.gr_complex_to_float, 1), (self.gr_probe_im, 0))
self.connect((self.band_pass_filter_0, 0), (self.gr_complex_to_mag_ref, 0))
self.connect((self.gr_complex_to_mag_ref, 0), (self.gr_probe_ref, 0))
def get_signal_freq(self):
return self.signal_freq
def set_signal_freq(self, signal_freq):
self.signal_freq = signal_freq
self.band_pass_filter_0.set_taps(firdes.complex_band_pass(1, self.samp_rate, self.signal_freq-self.bw/2, self.signal_freq+self.bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
self.band_pass_filter_0_0.set_taps(firdes.complex_band_pass(1, self.samp_rate, self.signal_freq-self.bw/2, self.signal_freq+self.bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.band_pass_filter_0.set_taps(firdes.complex_band_pass(1, self.samp_rate, self.signal_freq-self.bw/2, self.signal_freq+self.bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
self.band_pass_filter_0_0.set_taps(firdes.complex_band_pass(1, self.samp_rate, self.signal_freq-self.bw/2, self.signal_freq+self.bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
def get_bw(self):
return self.bw
def set_bw(self, bw):
self.bw = bw
self.band_pass_filter_0.set_taps(firdes.complex_band_pass(1, self.samp_rate, self.signal_freq-self.bw/2, self.signal_freq+self.bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
self.band_pass_filter_0_0.set_taps(firdes.complex_band_pass(1, self.samp_rate, self.signal_freq-self.bw/2, self.signal_freq+self.bw/2, 100, firdes.WIN_BLACKMAN, 6.76))
class top_block_runner(_threading.Thread):
def __init__(self, tb):
_threading.Thread.__init__(self)
self.setDaemon(1)
self.tb = tb
self.done = False
self.start()
def run(self):
self.tb.run()
self.done = True
def get_calibration(dir):
my_ideals = rf.load_all_touchstones('ideals')
my_measured = rf.load_all_touchstones(dir)
cal = rf.Calibration(
ideals = [my_ideals[k] for k in ['short','open','load']],
measured = [my_measured[k] for k in ['short','open','load']],
type = 'one port')
cal.run()
return cal
if __name__ == '__main__':
parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="make lots of messages [default]")
parser.add_option("-m", "--mul", dest="mul", default=16,
help="set PLL multiplier [default: %default]")
parser.add_option("-x", metavar="FREQ", dest="crystal", default=25e6,
help="set crystal frequency to FREQ [default: %default]")
parser.add_option("-s", metavar="FREQ", dest="start", type="float", default=0e6,
help="sweep start FREQ [default: %default]")
parser.add_option("-e", metavar="FREQ", dest="end", type="float", default=2000e6,
help="sweep stop FREQ [default: %default]")
parser.add_option("-n", metavar="STEP", dest="step", type="int", default=200,
help="sweep STEPs [default: %default]")
parser.add_option("-i", metavar="SEC", dest="interval", type="float", default=0.1,
help="set measuring interval [default: %default]")
parser.add_option("-t", action="store_true", dest="thru", default=False,
help="select thrugh signal(s21) [default: %default]")
parser.add_option("-c", metavar="CONF", dest="conf", type="int", default=0,
help="force select dds configuration [default: 0 (auto)]")
parser.add_option("-o", metavar="OUTPUT", dest="name", type="string", default=None,
help="name of output [default: None]")
parser.add_option("-k", metavar="CAL", dest="cal", type="string", default=None,
help="name of calibration [default: None]")
parser.add_option("-p", action="store_true", dest="plot", default=False,
help="plot smith chart")
(options, args) = parser.parse_args()
tb = vna_probe()
runner = top_block_runner(tb)
with BitBangDevice(direction=0xff, sync=False) as dev:
v = vna(dev)
v.select_signal(options.thru)
v.select_conf(options.conf)
name = options.name or 'measured'
freqs = rf.Frequency(options.start, options.end, options.step, 'hz')
if options.verbose:
print freqs
cal = None
if options.cal:
cal = get_calibration(options.cal)
s = []
for freq in freqs.f:
v.set_frequency(freq)
time.sleep(options.interval)
re = tb.gr_probe_re.level()
im = tb.gr_probe_im.level()
x = re + im * 1.j
s.append(x)
if options.verbose:
print "%d" % freq,
print "%f" % re,
print "%f" % im,
print "%f" % (tb.gr_probe_mag.level()),
print "%f" % (tb.gr_probe_ref.level())
z0 = [50.]
measured = rf.Network(frequency=freqs, s=s, z0=z0, name=name)
measured.write_touchstone()
measured = rf.Network('measured.s1p')
if cal:
dut = cal.apply_cal(measured)
else:
dut = measured
if options.name:
dut.write_touchstone(options.name)
if options.plot:
dut.plot_s_smith()
plb.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.