Imported Upstream version 3.2.2
[debian/gnuradio] / gr-utils / src / python / usrp2_rx_cfile.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,2007,2008,2009 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 """
24 Read samples from the USRP2 and write to file formatted as binary
25 outputs single precision complex float values or complex short values 
26 (interleaved 16 bit signed short integers).
27 """
28
29 from gnuradio import gr, eng_notation
30 from gnuradio import usrp2
31 from gnuradio.eng_option import eng_option
32 from optparse import OptionParser
33 import sys
34
35 n2s = eng_notation.num_to_str
36
37 class rx_cfile_block(gr.top_block):
38
39     def __init__(self, options, filename):
40         gr.top_block.__init__(self)
41
42         # Create a USRP2 source
43         if options.output_shorts:
44             self._u = usrp2.source_16sc(options.interface, options.mac_addr)
45             self._sink = gr.file_sink(gr.sizeof_short*2, filename)
46         else:
47             self._u = usrp2.source_32fc(options.interface, options.mac_addr)
48             self._sink = gr.file_sink(gr.sizeof_gr_complex, filename)
49
50         # Set receiver decimation rate
51         self._u.set_decim(options.decim)
52
53         # Set receive daughterboard gain
54         if options.gain is None:
55             g = self._u.gain_range()
56             options.gain = float(g[0]+g[1])/2
57             print "Using mid-point gain of", options.gain, "(", g[0], "-", g[1], ")"
58         self._u.set_gain(options.gain)
59
60         # Set receive frequency
61         if options.lo_offset is not None:
62             self._u.set_lo_offset(options.lo_offset)
63
64         tr = self._u.set_center_freq(options.freq)
65         if tr == None:
66             sys.stderr.write('Failed to set center frequency\n')
67             raise SystemExit, 1
68
69         # Create head block if needed and wire it up
70         if options.nsamples is None:
71             self.connect(self._u, self._sink)
72         else:
73             if options.output_shorts:
74                 self._head = gr.head(gr.sizeof_short*2, int(options.nsamples))
75             else:
76                 self._head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
77
78             self.connect(self._u, self._head, self._sink)
79
80         input_rate = self._u.adc_rate()/self._u.decim()
81         
82         if options.verbose:
83             print "Network interface:", options.interface
84             print "USRP2 address:", self._u.mac_addr()
85             print "Using RX d'board id 0x%04X" % (self._u.daughterboard_id(),)
86             print "Rx gain:", options.gain
87             print "Rx baseband frequency:", n2s(tr.baseband_freq)
88             print "Rx DDC frequency:", n2s(tr.dxc_freq)
89             print "Rx residual frequency:", n2s(tr.residual_freq)
90             print "Rx decimation rate:", options.decim
91             print "Rx sample rate:", n2s(input_rate)
92             if options.nsamples is None:
93                 print "Receiving samples until Ctrl-C"
94             else:
95                 print "Receving", n2s(options.nsamples), "samples"
96             if options.output_shorts:
97                 print "Writing 16-bit complex shorts"
98             else:
99                 print "Writing 32-bit complex floats"
100             print "Output filename:", filename
101         
102 def get_options():
103     usage="%prog: [options] output_filename"
104     parser = OptionParser(option_class=eng_option, usage=usage)
105     parser.add_option("-e", "--interface", type="string", default="eth0",
106                       help="use specified Ethernet interface [default=%default]")
107     parser.add_option("-m", "--mac-addr", type="string", default="",
108                       help="use USRP2 at specified MAC address [default=None]")  
109     parser.add_option("-d", "--decim", type="int", default=16,
110                       help="set fgpa decimation rate to DECIM [default=%default]")
111     parser.add_option("-f", "--freq", type="eng_float", default=None,
112                       help="set frequency to FREQ", metavar="FREQ")
113     parser.add_option("-g", "--gain", type="eng_float", default=None,
114                       help="set gain in dB (default is midpoint)")
115     parser.add_option( "-s","--output-shorts", action="store_true", default=False,
116                       help="output interleaved shorts instead of complex floats")
117     parser.add_option("-N", "--nsamples", type="eng_float", default=None,
118                       help="number of samples to collect [default=+inf]")
119     parser.add_option("-v", "--verbose", action="store_true", default=False,
120                       help="verbose output")
121     parser.add_option("", "--lo-offset", type="eng_float", default=None,
122                       help="set daughterboard LO offset to OFFSET [default=hw default]")
123
124     (options, args) = parser.parse_args ()
125     if len(args) != 1:
126         parser.print_help()
127         raise SystemExit, 1
128     
129     if options.freq is None:
130         parser.print_help()
131         sys.stderr.write('You must specify the frequency with -f FREQ\n');
132         raise SystemExit, 1
133     
134     return (options, args[0])
135
136
137 if __name__ == '__main__':
138     (options, filename) = get_options()
139     tb = rx_cfile_block(options, filename)
140     
141     try:
142         tb.run()
143     except KeyboardInterrupt:
144         pass