Houston, we have a trunk.
[debian/gnuradio] / gnuradio-examples / python / gmsk2 / 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., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
21
22
23 from gnuradio import gr, gru, blks
24 from gnuradio import usrp
25
26 # from current dir
27 from pick_bitrate import pick_rx_bitrate
28
29 # /////////////////////////////////////////////////////////////////////////////
30 #                              receive path
31 # /////////////////////////////////////////////////////////////////////////////
32
33 class receive_path(gr.hier_block):
34     def __init__(self, fg, demod_class, rx_subdev_spec,
35                  bitrate, decim, spb,
36                  rx_callback, options, demod_kwargs):
37
38         self.u = usrp.source_c (fusb_block_size=options.fusb_block_size,
39                                 fusb_nblocks=options.fusb_nblocks)
40         adc_rate = self.u.adc_rate()
41
42         (self._bitrate, self._spb, self._decim) = \
43             pick_rx_bitrate(bitrate, demod_class.bits_per_baud(), spb, decim, adc_rate)
44
45         self.u.set_decim_rate(self._decim)
46         sw_decim = 1
47
48         if rx_subdev_spec is None:
49             rx_subdev_spec = usrp.pick_rx_subdevice(self.u)
50         self.subdev = usrp.selected_subdev(self.u, rx_subdev_spec)
51         print "Using RX d'board %s" % (self.subdev.side_and_name(),)
52
53         self.u.set_mux(usrp.determine_rx_mux_value(self.u, rx_subdev_spec))
54
55         # Create filter to get actual channel we want
56         chan_coeffs = gr.firdes.low_pass (1.0,                  # gain
57                                           sw_decim * self._spb, # sampling rate
58                                           1.0,                  # midpoint of trans. band
59                                           0.1,                  # width of trans. band
60                                           gr.firdes.WIN_HANN)   # filter type 
61
62         print "len(rx_chan_coeffs) =", len(chan_coeffs)
63
64         # Decimating Channel filter
65         # complex in and out, float taps
66         self.chan_filt = gr.fft_filter_ccc(sw_decim, chan_coeffs)
67         #self.chan_filt = gr.fir_filter_ccf(sw_decim, chan_coeffs)
68
69         # receiver
70         self.packet_receiver = \
71             blks.demod_pkts(fg,
72                             demod_class(fg, spb=self._spb, **demod_kwargs),
73                             access_code=None,
74                             callback=rx_callback,
75                             threshold=-1)
76
77         fg.connect(self.u, self.chan_filt, self.packet_receiver)
78         gr.hier_block.__init__(self, fg, None, None)
79
80         g = self.subdev.gain_range()
81         #self.set_gain((g[0] + g[1])/2)        # set gain to midpoint
82         self.set_gain(g[1])                    # set gain to max
83         self.set_auto_tr(True)                 # enable Auto Transmit/Receive switching
84
85         # Carrier Sensing Blocks
86         alpha = 0.001
87         thresh = 30   # in dB, will have to adjust
88         self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha)
89         fg.connect(self.chan_filt, self.probe)
90
91     def set_freq(self, target_freq):
92         """
93         Set the center frequency we're interested in.
94
95         @param target_freq: frequency in Hz
96         @rypte: bool
97
98         Tuning is a two step process.  First we ask the front-end to
99         tune as close to the desired frequency as it can.  Then we use
100         the result of that operation and our target_frequency to
101         determine the value for the digital up converter.
102         """
103         r = self.u.tune(0, self.subdev, target_freq)
104         if r:
105             return True
106
107         return False
108
109     def set_gain(self, gain):
110         if gain is None:
111             r = self.subdev.gain_range()
112             gain = (r[0] + r[1])/2               # set gain to midpoint
113         self.gain = gain
114         return self.subdev.set_gain(gain)
115
116     def set_auto_tr(self, enable):
117         return self.subdev.set_auto_tr(enable)
118         
119     def bitrate(self):
120         return self._bitrate
121
122     def spb(self):
123         return self._spb
124
125     def decim(self):
126         return self._decim
127
128
129     def carrier_sensed(self):
130         """
131         Return True if we think carrier is present.
132         """
133         #return self.probe.level() > X
134         return self.probe.unmuted()
135
136     def carrier_threshold(self):
137         """
138         Return current setting in dB.
139         """
140         return self.probe.threshold()
141
142     def set_carrier_threshold(self, threshold_in_db):
143         """
144         Set carrier threshold.
145
146         @param threshold_in_db: set detection threshold
147         @type threshold_in_db:  float (dB)
148         """
149         self.probe.set_threshold(threshold_in_db)
150