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