Merged r4518:5130 from developer branch n4hy/ofdm into trunk, passes distcheck.
[debian/gnuradio] / gnuradio-examples / python / ofdm / receive_path.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006 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, gru, blks
24 from gnuradio import usrp
25 from gnuradio import eng_notation
26 import copy
27 import sys
28
29 # from current dir
30 from pick_bitrate import pick_rx_bitrate
31 import ofdm
32
33 # /////////////////////////////////////////////////////////////////////////////
34 #                              receive path
35 # /////////////////////////////////////////////////////////////////////////////
36
37 class receive_path(gr.hier_block):
38     def __init__(self, fg, rx_callback, options):
39
40         options = copy.copy(options)    # make a copy so we can destructively modify
41
42         self._verbose     = options.verbose
43         self._log         = options.log
44         self._rx_callback = rx_callback      # this callback is fired when there's a packet available
45
46         if 0:
47             
48             chan_coeffs = gr.firdes.low_pass (1.0,                  # gain
49                                               2.0,                  # sampling rate
50                                               bw,                   # midpoint of trans. band
51                                               1*bw,               # width of trans. band
52                                               gr.firdes.WIN_KAISER)   # filter type 
53         else:
54             chan_coeffs = [ 1.0, 0.0, 0 ] 
55         self.chan_filt = gr.fft_filter_ccc(1, chan_coeffs)
56
57         self.ofdm_demod = ofdm.ofdm_demod(fg, options)
58         
59         # receiver
60         self.packet_receiver = \
61             blks.demod_ofdm_pkts(fg,
62                                  self.ofdm_demod,
63                                  access_code=None,
64                                  callback=self._rx_callback,
65                                  threshold=-1)
66
67         # Carrier Sensing Blocks
68         alpha = 0.001
69         thresh = 30   # in dB, will have to adjust
70         self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha)
71         fg.connect(self.chan_filt, self.probe)
72
73         # Display some information about the setup
74         if self._verbose:
75             self._print_verbage()
76         
77         fg.connect(self.chan_filt, self.packet_receiver)
78         #fg.connect(self.chan_filt, gr.file_sink(gr.sizeof_gr_complex, "ofdmrx_chflt.dat"))
79         gr.hier_block.__init__(self, fg, self.chan_filt, None)
80
81     def carrier_sensed(self):
82         """
83         Return True if we think carrier is present.
84         """
85         #return self.probe.level() > X
86         return self.probe.unmuted()
87
88     def carrier_threshold(self):
89         """
90         Return current setting in dB.
91         """
92         return self.probe.threshold()
93
94     def set_carrier_threshold(self, threshold_in_db):
95         """
96         Set carrier threshold.
97
98         @param threshold_in_db: set detection threshold
99         @type threshold_in_db:  float (dB)
100         """
101         self.probe.set_threshold(threshold_in_db)
102     
103         
104     def add_options(normal, expert):
105         """
106         Adds receiver-specific options to the Options Parser
107         """
108         normal.add_option("-v", "--verbose", action="store_true", default=False)
109         expert.add_option("", "--log", action="store_true", default=False,
110                           help="Log all parts of flow graph to files (CAUTION: lots of data)")
111
112     # Make a static method to call before instantiation
113     add_options = staticmethod(add_options)
114
115
116     def _print_verbage(self):
117         """
118         Prints information about the receive path
119         """
120         print "Using RX d'board %s"    % (self.subdev.side_and_name(),)
121         print "Rx gain:         %g"    % (self.gain,)
122         print "modulation:      %s"    % (self._demod_class.__name__)
123         print "bitrate:         %sb/s" % (eng_notation.num_to_str(self._bitrate))
124         print "samples/symbol:  %3d"   % (self._samples_per_symbol)
125         print "decim:           %3d"   % (self._decim)