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