Old/Tutorials/k0SDR/Tutorial00: usrp1_rx_cfile.py

File usrp1_rx_cfile.py, 3.4 KB (added by wenyuan, 18 years ago)
Line 
1#!/usr/bin/env python
2
3"""
4Read samples from the USRP and write to file formatted as binary
5single-precision complex values.
6
7"""
8
9from gnuradio import gr, eng_notation
10from gnuradio import usrp
11from gnuradio.eng_option import eng_option
12from optparse import OptionParser
13import sys
14
15class 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("-u", "--usrp", type="int", default=0,
25 help="set the usrp board to receive signal [default=%default]")
26 parser.add_option("-d", "--decim", type="int", default=16,
27 help="set fgpa decimation rate to DECIM [default=%default]")
28 parser.add_option("-f", "--freq", type="eng_float", default=None,
29 help="set frequency to FREQ", metavar="FREQ")
30 parser.add_option("-g", "--gain", type="eng_float", default=None,
31 help="set gain in dB (default is midpoint)")
32 parser.add_option("-N", "--nsamples", type="eng_float", default=None,
33 help="number of samples to collect [default=+inf]")
34 (options, args) = parser.parse_args ()
35 if len(args) != 1:
36 parser.print_help()
37 raise SystemExit, 1
38 filename = args[0]
39
40 if options.freq is None:
41 parser.print_help()
42 sys.stderr.write('You must specify the frequency with -f FREQ\n');
43 raise SystemExit, 1
44
45 # build the graph
46 self.u = usrp.source_c(which=options.usrp,decim_rate=options.decim)
47 self.dst = gr.file_sink(gr.sizeof_gr_complex, filename)
48 if options.nsamples is None:
49 self.connect(self.u, self.dst)
50 else:
51 self.head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
52 self.connect(self.u, self.head, self.dst)
53
54 if options.rx_subdev_spec is None:
55 options.rx_subdev_spec = usrp.pick_rx_subdevice(self.u)
56 m = usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec)
57 print "mux = %#04x" % (m,)
58 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
59 #self.u.set_mux(0xf0)
60 #self.u.set_pga(options.usrp,10)
61
62 # determine the daughterboard subdevice we're using
63 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
64 print "subdev", options.rx_subdev_spec
65 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
66 input_rate = self.u.adc_freq() / self.u.decim_rate()
67 print "USB sample rate %s" % (eng_notation.num_to_str(input_rate))
68
69 if options.gain is None:
70 # if no gain was specified, use the mid-point in dB
71 g = self.subdev.gain_range()
72 print g[0]," ",g[1], " ",g[2]," ",float(g[0]+g[1])/2
73 options.gain = float(g[0]+g[1])/2
74 self.subdev.set_gain(options.gain)
75 print "gain is:",options.gain
76
77 r = self.u.tune(0, self.subdev, options.freq)
78 if not r:
79 sys.stderr.write('Failed to set frequency\n')
80 raise SystemExit, 1
81
82
83if __name__ == '__main__':
84 try:
85 my_graph().run()
86 except KeyboardInterrupt:
87 pass