Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / multi-antenna / multi_file.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(gr.flow_graph):
20
21     def __init__(self):
22         gr.flow_graph.__init__(self)
23
24         parser = OptionParser (option_class=eng_option)
25         #parser.add_option("-S", "--subdev", type="subdev", default=(0, None),
26         #                  help="select USRP Rx side A or B (default=A)")
27         parser.add_option("-d", "--decim", type="int", default=128,
28                           help="set fgpa decimation rate to DECIM [default=%default]")
29         parser.add_option("-f", "--freq", type="eng_float", default=146.585e6,
30                           help="set frequency to FREQ [default=%default])", metavar="FREQ")
31         parser.add_option("-g", "--gain", type="eng_float", default=20,
32                           help="set gain in dB [default=%default]")
33         parser.add_option("-F", "--filter", action="store_true", default=True,
34                           help="Enable channel filter")
35         parser.add_option("-o", "--output", type="string", default=None,
36                           help="set output basename")
37         (options, args) = parser.parse_args()
38
39         if len(args) != 0:
40             parser.print_help()
41             raise SystemExit
42
43         if options.output is None:
44             parser.print_help()
45             sys.stderr.write("You must provide an output filename base with -o OUTPUT\n")
46             raise SystemExit
47         else:
48             basename = options.output
49
50         nchan = 4
51         nsecs = 4.0
52
53         if options.filter:
54             sw_decim = 4
55         else:
56             sw_decim = 1
57
58         self.u = usrp.source_c(0, options.decim, fpga_filename="std_4rx_0tx.rbf")
59         if self.u.nddc() < nchan:
60             sys.stderr.write('This code requires an FPGA build with %d DDCs.  This FPGA has only %d.\n' % (
61                 nchan, self.u.nddc()))
62             raise SystemExit
63                              
64         if not self.u.set_nchannels(nchan):
65             sys.stderr.write('set_nchannels(%d) failed\n' % (nchan,))
66             raise SystemExit
67         
68         input_rate = self.u.adc_freq() / self.u.decim_rate()
69         print "USB data rate   = %s" % (eng_notation.num_to_str(input_rate),)
70         sink_data_rate = input_rate/sw_decim
71         print "Scope data rate = %s" % (eng_notation.num_to_str(sink_data_rate),)
72
73         self.subdev = self.u.db[0] + self.u.db[1]
74
75         if (len(self.subdev) != 4 or
76             self.u.db[0][0].dbid() != usrp_dbid.BASIC_RX or
77             self.u.db[1][0].dbid() != usrp_dbid.BASIC_RX):
78             sys.stderr.write('This code requires a Basic Rx board on Sides A & B\n')
79             sys.exit(1)
80
81         self.u.set_mux(gru.hexint(0xf3f2f1f0))
82
83         # collect 1 second worth of data
84         limit = int(nsecs * input_rate * nchan)
85         print "limit = ", limit
86         head = gr.head(gr.sizeof_gr_complex, limit)
87
88         # deinterleave four channels from FPGA
89         di = gr.deinterleave(gr.sizeof_gr_complex)
90
91         self.connect(self.u, head, di)
92         
93         # taps for channel filter
94         chan_filt_coeffs = optfir.low_pass (1,           # gain
95                                             input_rate,  # sampling rate
96                                             80e3,        # passband cutoff
97                                             115e3,       # stopband cutoff
98                                             0.1,         # passband ripple
99                                             60)          # stopband attenuation
100         #print len(chan_filt_coeffs)
101
102         for i in range(nchan):
103
104             sink = gr.file_sink(gr.sizeof_gr_complex,
105                                 basename + ("-%s-%d.dat" % (eng_notation.num_to_str(sink_data_rate), i)))
106             if options.filter:
107                 chan_filt = gr.fir_filter_ccf(sw_decim, chan_filt_coeffs)
108                 self.connect((di, i), chan_filt, sink)
109             else:
110                 self.connect((di, i), sink)
111
112
113         self.set_gain(options.gain)
114         self.set_freq(options.freq)
115
116     def set_gain(self, gain):
117         for i in range(len(self.subdev)):
118             self.subdev[i].set_gain(gain)
119
120     def set_freq(self, target_freq):
121         ok = True
122         for i in range(len(self.subdev)):
123             r = usrp.tune(self.u, i, self.subdev[i], target_freq)
124             if not r:
125                 ok = False
126                 print "set_freq: failed to set subdev[%d] freq to %f" % (
127                     i, target_freq)
128
129         return ok
130
131
132 def main ():
133     my_graph().run()
134
135 if __name__ == '__main__':
136     main ()