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).
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
15 class my_graph(gr.flow_graph):
18 gr.flow_graph.__init__(self)
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 ()
44 if options.freq is None:
46 sys.stderr.write('You must specify the frequency with -f FREQ\n');
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)
55 self.u = usrp.source_c(decim_rate=options.decim,fpga_filename=self.fpga_filename)
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)
61 self.u = usrp.source_c(decim_rate=options.decim)
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)
70 self.dst = gr.file_sink(gr.sizeof_gr_complex, filename)
71 if options.nsamples is None:
72 self.connect(self.u, self.dst)
74 if options.output_shorts:
75 self.head = gr.head(gr.sizeof_short, int(options.nsamples)*2)
77 self.head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
78 self.connect(self.u, self.head, self.dst)
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))
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))
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
95 self.subdev.set_gain(options.gain)
97 r = self.u.tune(0, self.subdev, options.freq)
99 sys.stderr.write('Failed to set frequency\n')
103 if __name__ == '__main__':
106 except KeyboardInterrupt: