2 # Copyright 2005,2006,2007 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 # See gnuradio-examples/python/digital for examples
25 QAM256 modulation and demodulation.
28 from gnuradio import gr, gru, modulation_utils
29 from math import pi, sqrt
32 from pprint import pprint
34 # default values (used in __init__ and add_options)
35 _def_samples_per_symbol = 2
41 _def_costas_alpha = None
44 _def_omega_relative_limit = 0.005
47 # /////////////////////////////////////////////////////////////////////////////
49 # /////////////////////////////////////////////////////////////////////////////
51 class qam256_mod(gr.hier_block2):
54 samples_per_symbol=_def_samples_per_symbol,
55 excess_bw=_def_excess_bw,
56 gray_code=_def_gray_code,
61 Hierarchical block for RRC-filtered QPSK modulation.
63 The input is a byte stream (unsigned char) and the
64 output is the complex modulated signal at baseband.
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
72 @param verbose: Print information about modulator?
74 @param debug: Print modualtion data to files?
78 gr.hier_block2.__init__(self, "qam256_mod",
79 gr.io_signature(1, 1, gr.sizeof_char), # Input signature
80 gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
82 self._samples_per_symbol = samples_per_symbol
83 self._excess_bw = excess_bw
84 self._gray_code = gray_code
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)
89 ntaps = 11 * samples_per_symbol
91 arity = pow(2, self.bits_per_symbol())
93 # turn bytes into k-bit vectors
95 gr.packed_to_unpacked_bb(self.bits_per_symbol(), gr.GR_MSB_FIRST)
98 self.symbol_mapper = gr.map_bb(qam.binary_to_gray[arity])
100 self.symbol_mapper = gr.map_bb(qam.binary_to_ungray[arity])
102 self.diffenc = gr.diff_encoder_bb(arity)
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)
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
114 self._excess_bw, # excess bandwidth (roll-off factor)
117 self.rrc_filter = gr.interp_fir_filter_ccf(self._samples_per_symbol, self.rrc_taps)
120 self._print_verbage()
123 self._setup_logging()
126 self.connect(self, self.bytes2chunks, self.symbol_mapper, self.diffenc,
127 self.chunks2symbols, self.rrc_filter, self)
129 def samples_per_symbol(self):
130 return self._samples_per_symbol
132 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
134 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
136 def _print_verbage(self):
137 print "bits per symbol = %d" % self.bits_per_symbol()
138 print "Gray code = %s" % self._gray_code
139 print "RRS roll-off factor = %f" % self._excess_bw
141 def _setup_logging(self):
142 print "Modulation logging turned on."
143 self.connect(self.bytes2chunks,
144 gr.file_sink(gr.sizeof_char, "bytes2chunks.dat"))
145 self.connect(self.symbol_mapper,
146 gr.file_sink(gr.sizeof_char, "graycoder.dat"))
147 self.connect(self.diffenc,
148 gr.file_sink(gr.sizeof_char, "diffenc.dat"))
149 self.connect(self.chunks2symbols,
150 gr.file_sink(gr.sizeof_gr_complex, "chunks2symbols.dat"))
151 self.connect(self.rrc_filter,
152 gr.file_sink(gr.sizeof_gr_complex, "rrc_filter.dat"))
154 def add_options(parser):
156 Adds QAM modulation-specific options to the standard parser
158 parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
159 help="set RRC excess bandwith factor [default=%default] (PSK)")
160 parser.add_option("", "--no-gray-code", dest="gray_code",
161 action="store_false", default=_def_gray_code,
162 help="disable gray coding on modulated bits (PSK)")
163 add_options=staticmethod(add_options)
166 def extract_kwargs_from_options(options):
168 Given command line options, create dictionary suitable for passing to __init__
170 return modulation_utils.extract_kwargs_from_options(qam256_mod.__init__,
172 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
175 # /////////////////////////////////////////////////////////////////////////////
178 # /////////////////////////////////////////////////////////////////////////////
180 class qam256_demod(gr.hier_block2):
183 samples_per_symbol=_def_samples_per_symbol,
184 excess_bw=_def_excess_bw,
185 costas_alpha=_def_costas_alpha,
186 gain_mu=_def_gain_mu,
188 omega_relative_limit=_def_omega_relative_limit,
189 gray_code=_def_gray_code,
190 verbose=_def_verbose,
193 gr.hier_block2.__init__(self, "qam256_demod",
194 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
195 gr.io_signature(1, 1, gr.sizeof_char)) # Output signature
200 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
202 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
205 # Add these to the mod/demod registry
207 # NOT READY TO BE USED YET -- ENABLE AT YOUR OWN RISK
208 #modulation_utils.add_type_1_mod('qam256', qam256_mod)
209 #modulation_utils.add_type_1_demod('qam256', qam256_demod)