GNUradio Python Programming KC Huang WINLAB Outlines Python Introduction (cid:135) Understanding & Using GNU Radio (cid:135) What is GNU Radio architecture? (cid:132) How to program in GNUradio? (Python and (cid:132) C++) An example: dial-tone (cid:135) Useful Resources (cid:135) WINLAB Python - running Why Python? (cid:135) Object-oriented (cid:132) Free (cid:132) Mixable (python/c++) (cid:132) Python scripts can be written in text files (cid:135) with the suffix .py Example: (cid:132) $ python script.py (cid:135) This will simply execute the script and return to the (cid:135) terminal afterwards WINLAB Python - format Module: a python file containing definitions and (cid:135) statements from pick_bitrate import pick_tx_bitrate (cid:132) (from file import function) from gnuradio import gr, (or *) (cid:132) (from package import subpackage or all) Some modules are built-in e.g. sys (import sys) (cid:132) Indentation: it is Python’s way of grouping (cid:135) statements Example: (cid:132) while b < 10: (cid:135) print b return Body of loop has to be indented by the same amount (cid:135) to indicate statements of the loop WINLAB Python – function & class (1) Function definitions have the following basic (cid:135) structure: def func(args): return values Regardless of the arguments, (including the case of no (cid:132) arguments) a function call must end with parentheses Example: (cid:132) def f1(x): return x*(x-1) f1(3) = 6 WINLAB Python – function & class (2) Classes (cid:135) class ClassName: <statement-1> . . . <statement-N> Objects (cid:135) x = ClassName() creates a new instance of this class and assigns the object to the variable x Initial state: for instantiation and parameter pass (cid:132) def __init__(self): <statement-1> WINLAB Python – function & class (3) class cs_mac(object): def __init__(self, tun_fd, verbose=False): self.tun_fd = tun_fd self.verbose = verbose self.fg = None def main_loop(self): min_delay = 0.001 # seconds while 1: payload = os.read(self.tun_fd, 10*1024) if not payload: self.fg.send_pkt(eof=True) break if self.verbose: print "Tx: len(payload) = %4d" % (len(payload),) delay = min_delay while self.fg.carrier_sensed(): sys.stderr.write('B') time.sleep(delay) if delay < 0.050: delay = delay * 2 # exponential back-off self.fg.send_pkt(payload) WINLAB GNUradio Architecture Sender User-defined RF USB FPGA DAC Code Front end mother board daughter board PC USRP GNU radio has provided some useful APIs (cid:135) What we are interested in at this time is how to (cid:135) use the existing modules that has been provided in GNU radio project to communicate between two end systems WINLAB GNUradio Architecture - software How these modules co-work? (cid:136) Signal processing block and flow-graph (cid:136) C++: Extensive library of signal processing blocks (cid:135) (cid:131) Performance-critical modules Python: Environment for composing blocks (cid:135) (cid:131) Glue to connect modules (cid:131) Non performance-critical modules Signal Processing Block (cid:136) Source: No input (cid:129) noise_source, signal_source, usrp_source (cid:129) Sink: No outputs (cid:129) audio_alsa_sink,usrp_sink (cid:129) Processing blocks: one or more inputs/outputs (cid:129) WINLAB GNUradio Architecture – software(2) V2 V2 C++ C++ C++ C++ Sink C++ C++ V1 V1 V3 V3 Source At python level, what we need to do is always just to draw a diagram showing the signal flow from the source to the sink in our mind. WINLAB
Description: