merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-examples / python / digital / receive_path_lb.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 eng_notation
25 import copy
26 import sys
27
28 # /////////////////////////////////////////////////////////////////////////////
29 #                              receive path
30 # /////////////////////////////////////////////////////////////////////////////
31
32 class receive_path(gr.hier_block):
33     def __init__(self, fg, demod_class, rx_callback, options):
34         
35         options = copy.copy(options)    # make a copy so we can destructively modify
36
37         self._verbose            = options.verbose
38         self._bitrate            = options.bitrate         # desired bit rate
39         self._samples_per_symbol = options.samples_per_symbol  # desired samples/symbol
40
41         self._rx_callback   = rx_callback      # this callback is fired when there's a packet available
42         self._demod_class   = demod_class      # the demodulator_class we're using
43
44         # Get demod_kwargs
45         demod_kwargs = self._demod_class.extract_kwargs_from_options(options)
46
47         # Design filter to get actual channel we want
48         sw_decim = 1
49         chan_coeffs = gr.firdes.low_pass (1.0,                  # gain
50                                           sw_decim * self._samples_per_symbol, # sampling rate
51                                           1.0,                  # midpoint of trans. band
52                                           0.5,                  # width of trans. band
53                                           gr.firdes.WIN_HANN)   # filter type
54         self.channel_filter = gr.fft_filter_ccc(sw_decim, chan_coeffs)
55         
56         # receiver
57         self.packet_receiver = \
58             blks.demod_pkts(fg,
59                             self._demod_class(fg, **demod_kwargs),
60                             access_code=None,
61                             callback=self._rx_callback,
62                             threshold=-1)
63
64         # Carrier Sensing Blocks
65         alpha = 0.001
66         thresh = 30   # in dB, will have to adjust
67         self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha)
68
69         # Display some information about the setup
70         if self._verbose:
71             self._print_verbage()
72
73         # connect the channel input filter to the carrier power detector
74         fg.connect(self.channel_filter, self.probe)
75
76         # connect channel filter to the packet receiver
77         fg.connect(self.channel_filter, self.packet_receiver)
78
79         gr.hier_block.__init__(self, fg, self.channel_filter, None)
80
81     def bitrate(self):
82         return self._bitrate
83
84     def samples_per_symbol(self):
85         return self._samples_per_symbol
86
87     def carrier_sensed(self):
88         """
89         Return True if we think carrier is present.
90         """
91         #return self.probe.level() > X
92         return self.probe.unmuted()
93
94     def carrier_threshold(self):
95         """
96         Return current setting in dB.
97         """
98         return self.probe.threshold()
99
100     def set_carrier_threshold(self, threshold_in_db):
101         """
102         Set carrier threshold.
103
104         @param threshold_in_db: set detection threshold
105         @type threshold_in_db:  float (dB)
106         """
107         self.probe.set_threshold(threshold_in_db)
108     
109         
110     def add_options(normal, expert):
111         """
112         Adds receiver-specific options to the Options Parser
113         """
114         if not normal.has_option("--bitrate"):
115             normal.add_option("-r", "--bitrate", type="eng_float", default=100e3,
116                               help="specify bitrate [default=%default].")
117         normal.add_option("", "--show-rx-gain-range", action="store_true", default=False, 
118                           help="print min and max Rx gain available on selected daughterboard")
119         normal.add_option("-v", "--verbose", action="store_true", default=False)
120         expert.add_option("-S", "--samples-per-symbol", type="int", default=2,
121                           help="set samples/symbol [default=%default]")
122         expert.add_option("", "--log", action="store_true", default=False,
123                           help="Log all parts of flow graph to files (CAUTION: lots of data)")
124
125     # Make a static method to call before instantiation
126     add_options = staticmethod(add_options)
127
128
129     def _print_verbage(self):
130         """
131         Prints information about the receive path
132         """
133         print "\nReceive Path:"
134         print "modulation:      %s"    % (self._demod_class.__name__)
135         print "bitrate:         %sb/s" % (eng_notation.num_to_str(self._bitrate))
136         print "samples/symbol:  %3d"   % (self._samples_per_symbol)