Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / multi-antenna / multi_fft.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
81
82         # taps for channel filter
83         chan_filt_coeffs = optfir.low_pass (1,           # gain
84                                             input_rate,  # sampling rate
85                                             80e3,        # passband cutoff
86                                             115e3,       # stopband cutoff
87                                             0.1,         # passband ripple
88                                             60)          # stopband attenuation
89         #print len(chan_filt_coeffs)
90
91         for i in range(nchan):
92             scope = fftsink.fft_sink_c(self, panel, sample_rate=input_rate/sw_decim,
93                                        title="Input %d" % (i,),
94                                        ref_level=80, y_per_div=20)
95             vbox.Add(scope.win, 10, wx.EXPAND)
96
97             if options.filter:
98                 chan_filt = gr.fir_filter_ccf(sw_decim, chan_filt_coeffs)
99                 self.connect((di, i), chan_filt, scope)
100             else:
101                 self.connect((di, i), scope)
102
103
104         self.set_gain(options.gain)
105         self.set_freq(options.freq)
106
107     def set_gain(self, gain):
108         for i in range(len(self.subdev)):
109             self.subdev[i].set_gain(gain)
110
111     def set_freq(self, target_freq):
112         ok = True
113         for i in range(len(self.subdev)):
114             r = usrp.tune(self.u, i, self.subdev[i], target_freq)
115             if not r:
116                 ok = False
117                 print "set_freq: failed to set subdev[%d] freq to %f" % (
118                     i, target_freq)
119
120         return ok
121
122
123 def main ():
124     app = stdgui.stdapp(my_graph, "Multi Scope", nstatus=1)
125     app.MainLoop()
126
127 if __name__ == '__main__':
128     main ()