2d3d648a00b1779438fc8067e2a44f7d1dc4a821
[debian/gnuradio] / gnuradio-examples / python / digital / usrp_receive_path.py
1 #
2 # Copyright 2009 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 from gnuradio import gr
23 from gnuradio import usrp_options
24 import receive_path
25 from pick_bitrate2 import pick_rx_bitrate
26 from gnuradio import eng_notation
27
28 def add_freq_option(parser):
29     """
30     Hackery that has the -f / --freq option set both tx_freq and rx_freq
31     """
32     def freq_callback(option, opt_str, value, parser):
33         parser.values.rx_freq = value
34         parser.values.tx_freq = value
35
36     if not parser.has_option('--freq'):
37         parser.add_option('-f', '--freq', type="eng_float",
38                           action="callback", callback=freq_callback,
39                           help="set Tx and/or Rx frequency to FREQ [default=%default]",
40                           metavar="FREQ")
41
42 def add_options(parser, expert):
43     add_freq_option(parser)
44     usrp_options.add_rx_options(parser)
45     receive_path.receive_path.add_options(parser, expert)
46     expert.add_option("", "--rx-freq", type="eng_float", default=None,
47                           help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
48     parser.add_option("-v", "--verbose", action="store_true", default=False)
49
50 class usrp_receive_path(gr.hier_block2):
51
52     def __init__(self, demod_class, rx_callback, options):
53         '''
54         See below for what options should hold
55         '''
56         gr.hier_block2.__init__(self, "usrp_receive_path",
57                 gr.io_signature(0, 0, 0),                    # Input signature
58                 gr.io_signature(0, 0, 0)) # Output signature
59         if options.rx_freq is None:
60             sys.stderr.write("-f FREQ or --freq FREQ or --rx-freq FREQ must be specified\n")
61             raise SystemExit
62         rx_path = receive_path.receive_path(demod_class, rx_callback, options)
63         for attr in dir(rx_path): #forward the methods
64             if not attr.startswith('_') and not hasattr(self, attr):
65                 setattr(self, attr, getattr(rx_path, attr))
66
67         #setup usrp
68         self._demod_class = demod_class
69         self._setup_usrp_source(options)
70
71         # Set up resampler based on rate determined by _setup_usrp_source
72         rs_taps = gr.firdes.low_pass_2(32, 32, 0.45, 0.1, 60)
73         self.resampler = gr.pfb_arb_resampler_ccf(self.rs_rate, rs_taps)
74
75         #connect
76         self.connect(self.u, self.resampler, rx_path)
77
78     def _setup_usrp_source(self, options):
79         self.u = usrp_options.create_usrp_source(options)
80         adc_rate = self.u.adc_rate()
81         self.rs_rate = options.bitrate
82         if options.verbose:
83             print 'USRP Source:', self.u
84
85             
86         #(self._bitrate, self._samples_per_symbol, self._decim) = \
87         #                pick_rx_bitrate(options.bitrate, self._demod_class.bits_per_symbol(), \
88         #                                options.samples_per_symbol, options.decim, adc_rate,  \
89         #                                self.u.get_decim_rates())
90         (self._bitrate, self._samples_per_symbol, self._decim) = \
91                         pick_rx_bitrate(options.bitrate, self._demod_class.bits_per_symbol(), \
92                                         adc_rate, self.u.get_decim_rates())
93
94         print "USRP Decimation: ", self._decim
95         print "Samples Per Symbol: ", self._samples_per_symbol
96
97         self.u.set_decim(self._decim)
98
99         if not self.u.set_center_freq(options.rx_freq):
100             print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.rx_freq))
101             raise ValueError, eng_notation.num_to_str(options.rx_freq)