Imported Upstream version 3.2.2
[debian/gnuradio] / gr-sounder / src / python / usrp_sounder.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 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 from gnuradio import gr
24 from gnuradio.sounder import sounder
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28 import numpy
29 import sys
30
31 n2s = eng_notation.num_to_str
32
33 def main():
34     parser = OptionParser(option_class=eng_option)
35     parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
36                       help="select USRP Rx side A or B")
37     parser.add_option("-g", "--gain", type="eng_float", default=None,
38                       help="set gain in dB (default is midpoint)")
39     parser.add_option("-f", "--frequency", type="eng_float", default=0.0,
40                       help="set frequency to FREQ in Hz, default is %default", metavar="FREQ")
41     parser.add_option("-d", "--degree", type="int", default=12,
42                       help="set sounding sequence degree (2-12), default is %default,")
43     parser.add_option("-a", "--amplitude", type="int", default=4096,
44                       help="set waveform amplitude, default is %default,")
45     parser.add_option("-t", "--transmit", action="store_true", default=False,
46                       help="enable sounding transmitter")
47     parser.add_option("-r", "--receive", action="store_true", default=False,
48                       help="enable sounding receiver")
49     parser.add_option("-l", "--loopback", action="store_true", default=False,
50                       help="enable digital loopback, default is disabled")
51     parser.add_option("-v", "--verbose", action="store_true", default=False,
52                       help="enable verbose output, default is disabled")
53     parser.add_option("-D", "--debug", action="store_true", default=False,
54                       help="enable debugging output, default is disabled")
55     parser.add_option("-F", "--filename", default=None,
56                       help="log received impulse responses to file")
57     parser.add_option("", "--alpha", type="eng_float", default=1.0,
58                       help="smoothing factor (0.0-1.0), default is %default (none)")
59                       
60     (options, args) = parser.parse_args()
61
62     if len(args) != 0 or not (options.transmit | options.receive):
63         parser.print_help()
64         sys.exit(1)
65
66     if options.receive and (options.filename == None):
67         print "Must supply filename when receiving."
68         sys.exit(1)
69
70     if options.degree > 12 or options.degree < 2:
71         print "PN code degree must be between 2 and 12"
72         sys.exit(1)
73
74     length = int(2**options.degree-1)
75     if options.verbose:
76         print "Using PN code degree of", options.degree, "length", length
77         if options.loopback == False:
78             print "Sounding frequency range is", n2s(options.frequency-16e6), "to", n2s(options.frequency+16e6)
79         if options.filename != None:
80             print "Logging impulse records to file: ", options.filename
81             
82     msgq = gr.msg_queue()
83     s = sounder(transmit=options.transmit,receive=options.receive,
84                 loopback=options.loopback,rx_subdev_spec=options.rx_subdev_spec,
85                 frequency=options.frequency,rx_gain=options.gain,
86                 degree=options.degree,length=length,alpha=options.alpha,
87                 msgq=msgq,verbose=options.verbose,ampl=options.amplitude,
88                 debug=options.debug)
89     s.start()
90
91     if options.receive:
92         f = open(options.filename, "wb")
93         print "Enter CTRL-C to stop."
94         try:
95             while (1):
96                 msg = msgq.delete_head()
97                 if msg.type() == 1:
98                     break
99                 rec = msg.to_string()[:length*gr.sizeof_gr_complex]
100                 if options.debug:
101                     print "Received impulse vector of length", len(rec)
102
103                 f.write(rec)
104                 
105         except KeyboardInterrupt:
106             pass
107     else:
108         if options.transmit:
109             raw_input("Press return to exit.")
110         
111 if __name__ == "__main__":
112     main()