merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / qam64.py
1 #
2 # Copyright 2005,2006 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 2, 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 # See gnuradio-examples/python/digital for examples
23
24 """
25 differential QPSK modulation and demodulation.
26 """
27
28 from gnuradio import gr, gru, modulation_utils
29 from math import pi, sqrt
30 import qam
31 import cmath
32 import Numeric
33 from pprint import pprint
34
35 # default values (used in __init__ and add_options)
36 _def_samples_per_symbol = 2
37 _def_excess_bw = 0.35
38 _def_gray_code = True
39 _def_verbose = False
40 _def_log = False
41
42 _def_costas_alpha = None
43 _def_gain_mu = 0.03
44 _def_mu = 0.05
45 _def_omega_relative_limit = 0.005
46
47
48 # /////////////////////////////////////////////////////////////////////////////
49 #                           QAM64 modulator
50 # /////////////////////////////////////////////////////////////////////////////
51
52 class qam64_mod(gr.hier_block):
53
54     def __init__(self, fg,
55                  samples_per_symbol=_def_samples_per_symbol,
56                  excess_bw=_def_excess_bw,
57                  gray_code=_def_gray_code,
58                  verbose=_def_verbose,
59                  log=_def_log):
60
61         """
62         Hierarchical block for RRC-filtered QPSK modulation.
63
64         The input is a byte stream (unsigned char) and the
65         output is the complex modulated signal at baseband.
66
67         @param fg: flow graph
68         @type fg: flow graph
69         @param samples_per_symbol: samples per symbol >= 2
70         @type samples_per_symbol: integer
71         @param excess_bw: Root-raised cosine filter excess bandwidth
72         @type excess_bw: float
73         @param gray_code: Tell modulator to Gray code the bits
74         @type gray_code: bool
75         @param verbose: Print information about modulator?
76         @type verbose: bool
77         @param debug: Print modualtion data to files?
78         @type debug: bool
79         """
80
81         self._fg = fg
82         self._samples_per_symbol = samples_per_symbol
83         self._excess_bw = excess_bw
84         self._gray_code = gray_code
85
86         if not isinstance(samples_per_symbol, int) or samples_per_symbol < 2:
87             raise TypeError, ("sbp must be an integer >= 2, is %d" % samples_per_symbol)
88
89         ntaps = 11 * samples_per_symbol
90  
91         arity = pow(2, self.bits_per_symbol())
92
93         # turn bytes into k-bit vectors
94         self.bytes2chunks = \
95           gr.packed_to_unpacked_bb(self.bits_per_symbol(), gr.GR_MSB_FIRST)
96
97         if self._gray_code:
98             self.symbol_mapper = gr.map_bb(qam.binary_to_gray[arity])
99         else:
100             self.symbol_mapper = gr.map_bb(qam.binary_to_ungray[arity])
101             
102         self.diffenc = gr.diff_encoder_bb(arity)
103
104         rot = 1.0
105         print "constellation with %d arity" % arity
106         rotated_const = map(lambda pt: pt * rot, qam.constellation[arity])
107         self.chunks2symbols = gr.chunks_to_symbols_bc(rotated_const)
108
109         # pulse shaping filter
110         self.rrc_taps = gr.firdes.root_raised_cosine(
111             self._samples_per_symbol, # gain  (sps since we're interpolating by sps)
112             self._samples_per_symbol, # sampling rate
113             1.0,                      # symbol rate
114             self._excess_bw,          # excess bandwidth (roll-off factor)
115             ntaps)
116
117         self.rrc_filter = gr.interp_fir_filter_ccf(self._samples_per_symbol, self.rrc_taps)
118
119         if verbose:
120             self._print_verbage()
121         
122         if log:
123             self._setup_logging()
124             
125         # Connect & Initialize base class
126         self._fg.connect(self.bytes2chunks, self.symbol_mapper, self.diffenc,
127                          self.chunks2symbols, self.rrc_filter)
128         gr.hier_block.__init__(self, self._fg, self.bytes2chunks, self.rrc_filter)
129
130     def samples_per_symbol(self):
131         return self._samples_per_symbol
132
133     def bits_per_symbol(self=None):   # staticmethod that's also callable on an instance
134         return 6
135     bits_per_symbol = staticmethod(bits_per_symbol)      # make it a static method.  RTFM
136
137     def _print_verbage(self):
138         print "bits per symbol = %d" % self.bits_per_symbol()
139         print "Gray code = %s" % self._gray_code
140         print "RRS roll-off factor = %f" % self._excess_bw
141
142     def _setup_logging(self):
143         print "Modulation logging turned on."
144         self._fg.connect(self.bytes2chunks,
145                          gr.file_sink(gr.sizeof_char, "bytes2chunks.dat"))
146         self._fg.connect(self.symbol_mapper,
147                          gr.file_sink(gr.sizeof_char, "graycoder.dat"))
148         self._fg.connect(self.diffenc,
149                          gr.file_sink(gr.sizeof_char, "diffenc.dat"))        
150         self._fg.connect(self.chunks2symbols,
151                          gr.file_sink(gr.sizeof_gr_complex, "chunks2symbols.dat"))
152         self._fg.connect(self.rrc_filter,
153                          gr.file_sink(gr.sizeof_gr_complex, "rrc_filter.dat"))
154
155     def add_options(parser):
156         """
157         Adds QAM modulation-specific options to the standard parser
158         """
159         parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
160                           help="set RRC excess bandwith factor [default=%default] (PSK)")
161         parser.add_option("", "--no-gray-code", dest="gray_code",
162                           action="store_false", default=_def_gray_code,
163                           help="disable gray coding on modulated bits (PSK)")
164     add_options=staticmethod(add_options)
165
166
167     def extract_kwargs_from_options(options):
168         """
169         Given command line options, create dictionary suitable for passing to __init__
170         """
171         return modulation_utils.extract_kwargs_from_options(qam64_mod.__init__,
172                                                             ('self', 'fg'), options)
173     extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
174
175
176 # /////////////////////////////////////////////////////////////////////////////
177 #                           QAM16 demodulator
178 #
179 # /////////////////////////////////////////////////////////////////////////////
180
181 class qam64_demod(gr.hier_block):
182
183     def __init__(self, fg,
184                  samples_per_symbol=_def_samples_per_symbol,
185                  excess_bw=_def_excess_bw,
186                  costas_alpha=_def_costas_alpha,
187                  gain_mu=_def_gain_mu,
188                  mu=_def_mu,
189                  omega_relative_limit=_def_omega_relative_limit,
190                  gray_code=_def_gray_code,
191                  verbose=_def_verbose,
192                  log=_def_log):
193
194         # do this
195         pass
196     
197     def bits_per_symbol(self=None):   # staticmethod that's also callable on an instance
198         return 6
199     bits_per_symbol = staticmethod(bits_per_symbol)      # make it a static method.  RTFM
200
201 #
202 # Add these to the mod/demod registry
203 #
204 # NOT READY TO BE USED YET -- ENABLE AT YOUR OWN RISK
205 #modulation_utils.add_type_1_mod('qam64', qam64_mod)
206 #modulation_utils.add_type_1_demod('qam16', qam16_demod)