Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / multi-antenna / multi_scope.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr, gru, eng_notation
4 from gnuradio import usrp
5 from gnuradio.eng_option import eng_option
6 from gnuradio import eng_notation
7 from gnuradio import optfir
8 from optparse import OptionParser
9 from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
10 import wx
11 import usrp_dbid
12 import time
13 import os.path
14 import sys
15
16 # required FPGA that can do 4 rx channels.
17
18
19 class my_graph(stdgui.gui_flow_graph):
20
21     def __init__(self, frame, panel, vbox, argv):
22         stdgui.gui_flow_graph.__init__(self)
23
24         self.frame = frame
25         self.panel = panel
26
27         parser = OptionParser (option_class=eng_option)
28         #parser.add_option("-S", "--subdev", type="subdev", default=(0, None),
29         #                  help="select USRP Rx side A or B (default=A)")
30         parser.add_option("-d", "--decim", type="int", default=128,
31                           help="set fgpa decimation rate to DECIM [default=%default]")
32         parser.add_option("-f", "--freq", type="eng_float", default=146.585e6,
33                           help="set frequency to FREQ [default=%default])", metavar="FREQ")
34         parser.add_option("-g", "--gain", type="eng_float", default=20,
35                           help="set gain in dB [default=%default]")
36         parser.add_option("-F", "--filter", action="store_true", default=True,
37                           help="Enable channel filter")
38         (options, args) = parser.parse_args()
39
40         if len(args) != 0:
41             parser.print_help()
42             raise SystemExit
43
44         nchan = 4
45
46         if options.filter:
47             sw_decim = 4
48         else:
49             sw_decim = 1
50
51         self.u = usrp.source_c(0, options.decim, fpga_filename="std_4rx_0tx.rbf")
52         if self.u.nddc() < nchan:
53             sys.stderr.write('This code requires an FPGA build with %d DDCs.  This FPGA has only %d.\n' % (
54                 nchan, self.u.nddc()))
55             raise SystemExit
56                              
57         if not self.u.set_nchannels(nchan):
58             sys.stderr.write('set_nchannels(%d) failed\n' % (nchan,))
59             raise SystemExit
60         
61         input_rate = self.u.adc_freq() / self.u.decim_rate()
62         print "USB data rate   = %s" % (eng_notation.num_to_str(input_rate),)
63         print "Scope data rate = %s" % (eng_notation.num_to_str(input_rate/sw_decim),)
64
65         self.subdev = self.u.db[0] + self.u.db[1]
66
67         if (len(self.subdev) != 4 or
68             self.u.db[0][0].dbid() != usrp_dbid.BASIC_RX or
69             self.u.db[1][0].dbid() != usrp_dbid.BASIC_RX):
70             sys.stderr.write('This code requires a Basic Rx board on Sides A & B\n')
71             sys.exit(1)
72
73         self.u.set_mux(gru.hexint(0xf3f2f1f0))
74
75         # deinterleave four channels from FPGA
76         di = gr.deinterleave(gr.sizeof_gr_complex)
77
78         self.connect(self.u, di)
79         
80         # our destination (8 float inputs)
81         self.scope = scopesink.scope_sink_f(self, panel, sample_rate=input_rate/sw_decim)
82
83         # taps for channel filter
84         chan_filt_coeffs = optfir.low_pass (1,           # gain
85                                             input_rate,  # sampling rate
86                                             80e3,        # passband cutoff
87                                             115e3,       # stopband cutoff
88                                             0.1,         # passband ripple
89                                             60)          # stopband attenuation
90         #print len(chan_filt_coeffs)
91
92         # bust the deinterleaved complex channels into floats
93         for i in range(nchan):
94
95             if options.filter:
96                 chan_filt = gr.fir_filter_ccf(sw_decim, chan_filt_coeffs)
97                 c2f = gr.complex_to_float()
98                 self.connect((di, i), chan_filt, c2f)
99             else:
100                 c2f = gr.complex_to_float()
101                 self.connect((di, i), c2f)
102
103             self.connect((c2f, 0), (self.scope, 2*i + 0))
104             self.connect((c2f, 1), (self.scope, 2*i + 1))
105
106
107         self._build_gui(vbox)
108
109         self.set_gain(options.gain)
110         self.set_freq(options.freq)
111
112     def set_gain(self, gain):
113         for i in range(len(self.subdev)):
114             self.subdev[i].set_gain(gain)
115
116     def set_freq(self, target_freq):
117         ok = True
118         for i in range(len(self.subdev)):
119             r = usrp.tune(self.u, i, self.subdev[i], target_freq)
120             if not r:
121                 ok = False
122                 print "set_freq: failed to set subdev[%d] freq to %f" % (
123                     i, target_freq)
124
125         return ok
126
127
128     def _build_gui(self, vbox):
129         vbox.Add(self.scope.win, 10, wx.EXPAND)
130         
131
132
133 def main ():
134     app = stdgui.stdapp(my_graph, "Multi Scope", nstatus=1)
135     app.MainLoop()
136
137 if __name__ == '__main__':
138     main ()