Imported Upstream version 3.2.2
[debian/gnuradio] / gr-utils / src / python / usrp_rx_cfile.py
1 #!/usr/bin/env python
2
3 """
4 Read samples from the USRP and write to file formatted as binary
5 outputs single precision complex float values or complex short values (interleaved 16 bit signed short integers).
6
7 """
8
9 from gnuradio import gr, eng_notation
10 from gnuradio import audio
11 from gnuradio import usrp
12 from gnuradio.eng_option import eng_option
13 from optparse import OptionParser
14 import sys
15
16 class my_top_block(gr.top_block):
17
18     def __init__(self):
19         gr.top_block.__init__(self)
20
21         usage="%prog: [options] output_filename"
22         parser = OptionParser(option_class=eng_option, usage=usage)
23         parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
24                           help="select USRP Rx side A or B (default=A)")
25         parser.add_option("-d", "--decim", type="int", default=16,
26                           help="set fgpa decimation rate to DECIM [default=%default]")
27         parser.add_option("-f", "--freq", type="eng_float", default=None,
28                           help="set frequency to FREQ", metavar="FREQ")
29         parser.add_option("-g", "--gain", type="eng_float", default=None,
30                           help="set gain in dB (default is midpoint)")
31         parser.add_option("-8", "--width-8", action="store_true", default=False,
32                           help="Enable 8-bit samples across USB")
33         parser.add_option( "--no-hb", action="store_true", default=False,
34                           help="don't use halfband filter in usrp")
35         parser.add_option( "-s","--output-shorts", action="store_true", default=False,
36                           help="output interleaved shorts in stead of complex floats")
37         parser.add_option("-N", "--nsamples", type="eng_float", default=None,
38                           help="number of samples to collect [default=+inf]")
39         (options, args) = parser.parse_args ()
40         if len(args) != 1:
41             parser.print_help()
42             raise SystemExit, 1
43         filename = args[0]
44
45         if options.freq is None:
46             parser.print_help()
47             sys.stderr.write('You must specify the frequency with -f FREQ\n');
48             raise SystemExit, 1
49
50         # build the graph
51         if options.no_hb or (options.decim<8):
52           self.fpga_filename="std_4rx_0tx.rbf" #Min decimation of this firmware is 4. contains 4 Rx paths without halfbands and 0 tx paths.
53           if options.output_shorts:
54             self.u = usrp.source_s(decim_rate=options.decim,fpga_filename=self.fpga_filename)
55           else:
56             self.u = usrp.source_c(decim_rate=options.decim,fpga_filename=self.fpga_filename)
57         else:
58           #standard fpga firmware "std_2rxhb_2tx.rbf" contains 2 Rx paths with halfband filters and 2 tx paths (the default) min decimation 8
59           if options.output_shorts:
60             self.u = usrp.source_s(decim_rate=options.decim)
61           else:
62             self.u = usrp.source_c(decim_rate=options.decim)
63         if options.width_8:
64             sample_width = 8
65             sample_shift = 8
66             format = self.u.make_format(sample_width, sample_shift)
67             r = self.u.set_format(format)
68         if options.output_shorts:
69           self.dst = gr.file_sink(gr.sizeof_short, filename)
70         else:
71           self.dst = gr.file_sink(gr.sizeof_gr_complex, filename)
72         if options.nsamples is None:
73             self.connect(self.u, self.dst)
74         else:
75             if options.output_shorts:
76               self.head = gr.head(gr.sizeof_short, int(options.nsamples)*2)
77             else:
78               self.head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
79             self.connect(self.u, self.head, self.dst)
80
81         if options.rx_subdev_spec is None:
82             options.rx_subdev_spec = usrp.pick_rx_subdevice(self.u)
83         self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
84
85         # determine the daughterboard subdevice we're using
86         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
87         print "Using RX d'board %s" % (self.subdev.side_and_name(),)
88         input_rate = self.u.adc_freq() / self.u.decim_rate()
89         print "USB sample rate %s" % (eng_notation.num_to_str(input_rate))
90
91         if options.gain is None:
92             # if no gain was specified, use the mid-point in dB
93             g = self.subdev.gain_range()
94             options.gain = float(g[0]+g[1])/2
95
96         self.subdev.set_gain(options.gain)
97
98         r = self.u.tune(0, self.subdev, options.freq)
99         if not r:
100             sys.stderr.write('Failed to set frequency\n')
101             raise SystemExit, 1
102
103
104 if __name__ == '__main__':
105     try:
106         my_top_block().run()
107     except KeyboardInterrupt:
108         pass