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
16 class my_top_block(gr.top_block):
19 gr.top_block.__init__(self)
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 ()
45 if options.freq is None:
47 sys.stderr.write('You must specify the frequency with -f FREQ\n');
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)
56 self.u = usrp.source_c(decim_rate=options.decim,fpga_filename=self.fpga_filename)
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)
62 self.u = usrp.source_c(decim_rate=options.decim)
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)
71 self.dst = gr.file_sink(gr.sizeof_gr_complex, filename)
72 if options.nsamples is None:
73 self.connect(self.u, self.dst)
75 if options.output_shorts:
76 self.head = gr.head(gr.sizeof_short, int(options.nsamples)*2)
78 self.head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
79 self.connect(self.u, self.head, self.dst)
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))
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))
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
96 self.subdev.set_gain(options.gain)
98 r = self.u.tune(0, self.subdev, options.freq)
100 sys.stderr.write('Failed to set frequency\n')
104 if __name__ == '__main__':
107 except KeyboardInterrupt: