Merged r9834:9855 from jcorgan/u2-wip into trunk. Catches up gr-usrp2 with the lates...
[debian/gnuradio] / gr-utils / src / python / usrp2_rx_cfile.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,2007,2008 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 (interleaved 16 bit signed short integers).
26
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             options.gain = 0 # Until gain range is implemented
58         self._u.set_gain(options.gain)
59
60         # Set receive frequency
61         tr = self._u.set_center_freq(options.freq)
62         if tr == None:
63             sys.stderr.write('Failed to set center frequency\n')
64             raise SystemExit, 1
65
66         # Create head block if needed wire it up
67         if options.nsamples is None:
68             self.connect(self._u, self._sink)
69         else:
70             if options.output_shorts:
71                 self._head = gr.head(gr.sizeof_short*2, int(options.nsamples))
72             else:
73                 self._head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
74
75             self.connect(self._u, self._head, self._sink)
76
77         #input_rate = self.u.adc_freq() / self.u.decim_rate()
78         input_rate = 100e6/options.decim
79         
80         if options.verbose:
81             print "Network interface:", options.interface
82             print "USRP2 address:", self._u.mac_addr()
83             #print "Using RX d'board %s" % (self._u.rx_name(),)
84             print "Rx gain:", options.gain
85             print "Rx baseband frequency:", n2s(tr.baseband_freq)
86             print "Rx DDC frequency:", n2s(tr.dxc_freq)
87             print "Rx residual frequency:", n2s(tr.residual_freq)
88             print "Rx decimation rate:", options.decim
89             print "Rx sample rate:", n2s(input_rate)
90             if options.nsamples is None:
91                 print "Receiving samples until Ctrl-C"
92             else:
93                 print "Receving", n2s(options.nsamples), "samples"
94             if options.output_shorts:
95                 print "Writing 16-bit complex shorts"
96             else:
97                 print "Writing 32-bit complex floats"
98             print "Output filename:", filename
99         
100 def get_options():
101     usage="%prog: [options] output_filename"
102     parser = OptionParser(option_class=eng_option, usage=usage)
103     parser.add_option("-e", "--interface", type="string", default="eth0",
104                       help="use specified Ethernet interface [default=%default]")
105     parser.add_option("-m", "--mac-addr", type="string", default="",
106                       help="use USRP2 at specified MAC address [default=None]")  
107     parser.add_option("-d", "--decim", type="int", default=16,
108                       help="set fgpa decimation rate to DECIM [default=%default]")
109     parser.add_option("-f", "--freq", type="eng_float", default=None,
110                       help="set frequency to FREQ", metavar="FREQ")
111     parser.add_option("-g", "--gain", type="eng_float", default=None,
112                       help="set gain in dB (default is midpoint)")
113     parser.add_option( "-s","--output-shorts", action="store_true", default=False,
114                       help="output interleaved shorts instead of complex floats")
115     parser.add_option("-N", "--nsamples", type="eng_float", default=None,
116                       help="number of samples to collect [default=+inf]")
117     parser.add_option("-v", "--verbose", action="store_true", default=False,
118                       help="verbose output")
119     (options, args) = parser.parse_args ()
120     if len(args) != 1:
121         parser.print_help()
122         raise SystemExit, 1
123     
124     if options.freq is None:
125         parser.print_help()
126         sys.stderr.write('You must specify the frequency with -f FREQ\n');
127         raise SystemExit, 1
128     
129     return (options, args[0])
130
131
132 if __name__ == '__main__':
133     (options, filename) = get_options()
134     tb = rx_cfile_block(options, filename)
135     
136     try:
137         tb.run()
138     except KeyboardInterrupt:
139         pass