flowchart
每个gnuradio的程序都是一个通信系统,是一个流图。
from gnuradio import gr, blocks, filter, analog
class my_topblock(gr.top_block):
def __init__(self):
gr.top_block.__init__(self)
amp = 1
taps = filter.firdes.low_pass(1, 1, 0.1, 0.01)
self.src = analog.noise_source_c(analog.GR_GAUSSIAN, amp)
self.flt = filter.fir_filter_ccf(1, taps)
self.snk = blocks.null_sink(gr.sizeof_gr_complex)
self.connect(self.src, self.flt, self.snk)
if __name__ == "__main__":
tb = my_topblock()
#tb.run()
tb.start()
tb.wait()
__init__初始化之后,tb.start()开始运行,tb.wait()相当于thread.join(),等待tb运行结束才加入新的线程。
或者使用tb.run(),启动流图。
运行时更新flowchart
在通信系统运行的时候,经常需要根据输入信号改变系统的状态,这时候需要更新流图。更新流图有三步:
- 锁定,停止运行,处理数据
- 更新
- 解锁
from gnuradio import gr, analog, blocks
import time
class mytb(gr.top_block):
def __init__(self):
gr.top_block.__init__(self)
self.src0 = analog.noise_source_c(analog.GR_GAUSSIAN, 1)
self.src1 = analog.noise_source_c(analog.GR_GAUSSIAN, 1)
self.add = blocks.add_cc()
self.sub = blocks.sub_cc()
self.head = blocks.head(gr.sizeof_gr_complex, 1000000)
self.snk = blocks.file_sink(gr.sizeof_gr_complex, "output.32fc")
self.connect(self.src0, (self.add,0))
self.connect(self.src1, (self.add,1))
self.connect(self.add, self.head)
self.connect(self.head, self.snk)
def main():
tb = mytb()
tb.start()
time.sleep(0.01)
# Stop flowgraph and disconnect the add block
tb.lock()
tb.disconnect(tb.add, tb.head)
tb.disconnect(tb.src0, (tb.add,0))
tb.disconnect(tb.src1, (tb.add,1))
# Connect the sub block and restart
tb.connect(tb.sub, tb.head)
tb.connect(tb.src0, (tb.sub,0))
tb.connect(tb.src1, (tb.sub,1))
tb.unlock()
tb.wait()
if __name__ == "__main__":
main()