72ebeccfa65893985b9c6b127b12720f75cdcd00
[debian/gnuradio] / gnuradio-examples / python / hier / sounder / usrp_sounder_rx.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 2, 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, eng_notation
24 from gnuradio.eng_option import eng_option
25 from optparse import OptionParser
26 from usrp_source import usrp_source_c
27 from sounder_rx import sounder_rx
28
29 n2s = eng_notation.num_to_str
30
31 class usrp_sounder_rx(gr.hier_block2):
32     def __init__(self, options):
33         gr.hier_block2.__init__(self, "usrp_sounder_rx",
34                                 gr.io_signature(0,0,0),
35                                 gr.io_signature(0,0,0))
36
37         self._options = options
38         self._u = usrp_source_c(0,
39                                 self._options.rx_subdev_spec,
40                                 self._options.gain,
41                                 self._options.chip_rate,
42                                 self._options.freq,
43                                 self._options.cal,
44                                 self._options.verbose)
45         self._options.chip_rate = self._u._if_rate
46         self._length = 2**self._options.degree-1
47         self._receiver = sounder_rx(self._options.chip_rate,
48                                     self._options.degree,
49                                     self._options.verbose)
50
51         samples = 100 * self._length**2
52
53         self.define_component("usrp", self._u)
54         self.define_component("head", gr.head(gr.sizeof_gr_complex, samples))
55         self.define_component("rx",   self._receiver)
56         self.define_component("c2m",  gr.complex_to_mag())
57         self.define_component("s2v",  gr.stream_to_vector(gr.sizeof_float, self._length))
58         self.define_component("lpf",  gr.single_pole_iir_filter_ff(self._options.alpha, self._length))
59         self.define_component("v2s",  gr.vector_to_stream(gr.sizeof_float, self._length))
60         self.define_component("sink", gr.file_sink(gr.sizeof_float, "impulse.dat"))
61
62         self.connect("usrp", 0, "head", 0)
63         self.connect("head", 0, "rx", 0)
64         self.connect("rx", 0, "c2m", 0)
65         self.connect("c2m", 0, "s2v", 0)
66         self.connect("s2v", 0, "lpf", 0)
67         self.connect("lpf", 0, "v2s", 0)
68         self.connect("v2s", 0, "sink", 0)
69
70         if self._options.verbose:
71             print "Chip rate is", n2s(self._options.chip_rate), "chips/sec"
72             print "Resolution is", n2s(1.0/self._options.chip_rate), "sec"
73             print "Using PN code of degree", self._options.degree
74
75 def main():
76     parser = OptionParser(option_class=eng_option)
77     parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
78                       help="select USRP Rx side A or B (default=first found)")
79     parser.add_option("-f", "--freq", type="eng_float", default=0.0,
80                       help="set center frequency (default=%default)")
81     parser.add_option("-c", "--cal", type="eng_float", default=0.0,
82                       help="set frequency calibration offset (default=%default)")
83     parser.add_option("-v", "--verbose", action="store_true", default=False,
84                       help="print extra debugging info")
85     parser.add_option("-d", "--degree", type="int", default=10,
86                       help="set PN code degree (length=2**degree-1, default=%default)")
87     parser.add_option("-r", "--chip-rate", type="eng_float", default=8e6,
88                       help="set sounder chip rate (default=%default)")
89     parser.add_option("-g", "--gain", type="eng_float", default=None,
90                       help="set receiver gain (default=%default)")
91     parser.add_option("", "--alpha", type="eng_float", default=1.0,
92                       help="set smoothing constant (default=%default)")
93     (options, args) = parser.parse_args()
94     if len(args) != 0:
95         parser.print_help()
96         sys.exit(1)
97
98     top_block = usrp_sounder_rx(options)
99     runtime = gr.runtime(top_block)
100
101     try:
102         runtime.run()
103     except KeyboardInterrupt:
104         pass
105
106 if __name__ == '__main__':
107     main ()