2 # Copyright 2005,2006 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 2, 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 differential 8PSK modulation and demodulation.
28 from gnuradio import gr, gru, modulation_utils
29 from math import pi, sqrt
33 from pprint import pprint
35 # default values (used in __init__ and add_options)
36 _def_samples_per_symbol = 3
42 _def_costas_alpha = 0.175
45 _def_omega_relative_limit = 0.005
48 # /////////////////////////////////////////////////////////////////////////////
50 # /////////////////////////////////////////////////////////////////////////////
52 class d8psk_mod(gr.hier_block):
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,
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.
68 @param samples_per_symbol: samples per symbol >= 2
69 @type samples_per_symbol: integer
70 @param excess_bw: Root-raised cosine filter excess bandwidth
71 @type excess_bw: float
72 @param gray_code: Tell modulator to Gray code the bits
74 @param verbose: Print information about modulator?
76 @param debug: Print modualtion data to files?
81 self._samples_per_symbol = samples_per_symbol
82 self._excess_bw = excess_bw
83 self._gray_code = gray_code
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)
88 ntaps = 11 * samples_per_symbol
90 arity = pow(2,self.bits_per_symbol())
92 # turn bytes into k-bit vectors
94 gr.packed_to_unpacked_bb(self.bits_per_symbol(), gr.GR_MSB_FIRST)
97 self.symbol_mapper = gr.map_bb(psk.binary_to_gray[arity])
99 self.symbol_mapper = gr.map_bb(psk.binary_to_ungray[arity])
101 self.diffenc = gr.diff_encoder_bb(arity)
104 rotated_const = map(lambda pt: pt * rot, psk.constellation[arity])
105 self.chunks2symbols = gr.chunks_to_symbols_bc(rotated_const)
107 # pulse shaping filter
108 self.rrc_taps = gr.firdes.root_raised_cosine(
109 self._samples_per_symbol, # gain (sps since we're interpolating by sps)
110 self._samples_per_symbol, # sampling rate
112 self._excess_bw, # excess bandwidth (roll-off factor)
115 self.rrc_filter = gr.interp_fir_filter_ccf(self._samples_per_symbol, self.rrc_taps)
118 self._print_verbage()
121 self._setup_logging()
123 # Connect & Initialize base class
124 self._fg.connect(self.bytes2chunks, self.symbol_mapper, self.diffenc,
125 self.chunks2symbols, self.rrc_filter)
126 gr.hier_block.__init__(self, self._fg, self.bytes2chunks, self.rrc_filter)
128 def samples_per_symbol(self):
129 return self._samples_per_symbol
131 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
133 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
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 "RS roll-off factor = %f" % self._excess_bw
140 def _setup_logging(self):
141 print "Modulation logging turned on."
142 self._fg.connect(self.bytes2chunks,
143 gr.file_sink(gr.sizeof_char, "tx_bytes2chunks.dat"))
144 self._fg.connect(self.symbol_mapper,
145 gr.file_sink(gr.sizeof_char, "tx_graycoder.dat"))
146 self._fg.connect(self.diffenc,
147 gr.file_sink(gr.sizeof_char, "tx_diffenc.dat"))
148 self._fg.connect(self.chunks2symbols,
149 gr.file_sink(gr.sizeof_gr_complex, "tx_chunks2symbols.dat"))
150 self._fg.connect(self.rrc_filter,
151 gr.file_sink(gr.sizeof_gr_complex, "tx_rrc_filter.dat"))
153 def add_options(parser):
155 Adds 8PSK modulation-specific options to the standard parser
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)
165 def extract_kwargs_from_options(options):
167 Given command line options, create dictionary suitable for passing to __init__
169 return modulation_utils.extract_kwargs_from_options(d8psk_mod.__init__,
170 ('self', 'fg'), options)
171 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
174 # /////////////////////////////////////////////////////////////////////////////
177 # Differentially coherent detection of differentially encoded 8psk
178 # /////////////////////////////////////////////////////////////////////////////
180 class d8psk_demod(gr.hier_block):
182 def __init__(self, fg,
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 Hierarchical block for RRC-filtered DQPSK demodulation
195 The input is the complex modulated signal at baseband.
196 The output is a stream of bits packed 1 bit per byte (LSB)
198 @param fg: flow graph
200 @param samples_per_symbol: samples per symbol >= 2
201 @type samples_per_symbol: float
202 @param excess_bw: Root-raised cosine filter excess bandwidth
203 @type excess_bw: float
204 @param costas_alpha: loop filter gain
205 @type costas_alphas: float
206 @param gain_mu: for M&M block
208 @param mu: for M&M block
210 @param omega_relative_limit: for M&M block
211 @type omega_relative_limit: float
212 @param gray_code: Tell modulator to Gray code the bits
213 @type gray_code: bool
214 @param verbose: Print information about modulator?
216 @param debug: Print modualtion data to files?
221 self._samples_per_symbol = samples_per_symbol
222 self._excess_bw = excess_bw
223 self._costas_alpha = costas_alpha
224 self._mm_gain_mu = gain_mu
226 self._mm_omega_relative_limit = omega_relative_limit
227 self._gray_code = gray_code
229 if samples_per_symbol < 2:
230 raise TypeError, "sbp must be >= 2, is %d" % samples_per_symbol
232 arity = pow(2,self.bits_per_symbol())
234 # Automatic gain control
235 scale = (1.0/16384.0)
236 self.pre_scaler = gr.multiply_const_cc(scale) # scale the signal from full-range to +-1
237 #self.agc = gr.agc_cc(1e-2, 1, 1, 100)
238 self.agc = gr.agc2_cc(1e-1, 1e-2, 1, 1, 100)
239 #self.agc = gr.feedforward_agc_cc(16, 1.0)
242 ntaps = 11 * samples_per_symbol
243 self.rrc_taps = gr.firdes.root_raised_cosine(
245 self._samples_per_symbol, # sampling rate
247 self._excess_bw, # excess bandwidth (roll-off factor)
249 self.rrc_filter=gr.interp_fir_filter_ccf(1, self.rrc_taps)
251 # symbol clock recovery
252 self._mm_omega = self._samples_per_symbol
253 self._mm_gain_omega = .25 * self._mm_gain_mu * self._mm_gain_mu
254 self._costas_beta = 0.25 * self._costas_alpha * self._costas_alpha
258 self.receiver=gr.mpsk_receiver_cc(arity, 0,
259 self._costas_alpha, self._costas_beta,
261 self._mm_mu, self._mm_gain_mu,
262 self._mm_omega, self._mm_gain_omega,
263 self._mm_omega_relative_limit)
265 # Perform Differential decoding on the constellation
266 self.diffdec = gr.diff_phasor_cc()
268 # find closest constellation point
270 rotated_const = map(lambda pt: pt * rot, psk.constellation[arity])
271 self.slicer = gr.constellation_decoder_cb(rotated_const, range(arity))
274 self.symbol_mapper = gr.map_bb(psk.gray_to_binary[arity])
276 self.symbol_mapper = gr.map_bb(psk.ungray_to_binary[arity])
279 # unpack the k bit vector into a stream of bits
280 self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbol())
283 self._print_verbage()
286 self._setup_logging()
288 # Connect & Initialize base class
289 self._fg.connect(self.pre_scaler, self.agc, self.rrc_filter, self.receiver,
290 self.diffdec, self.slicer, self.symbol_mapper, self.unpack)
291 gr.hier_block.__init__(self, self._fg, self.pre_scaler, self.unpack)
293 def samples_per_symbol(self):
294 return self._samples_per_symbol
296 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
298 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
300 def _print_verbage(self):
301 print "\nDemodulator:"
302 print "bits per symbol: %d" % self.bits_per_symbol()
303 print "Gray code: %s" % self._gray_code
304 print "RRC roll-off factor: %.2f" % self._excess_bw
305 print "Costas Loop alpha: %.2e" % self._costas_alpha
306 print "Costas Loop beta: %.2e" % self._costas_beta
307 print "M&M mu: %.2f" % self._mm_mu
308 print "M&M mu gain: %.2e" % self._mm_gain_mu
309 print "M&M omega: %.2f" % self._mm_omega
310 print "M&M omega gain: %.2e" % self._mm_gain_omega
311 print "M&M omega limit: %.2f" % self._mm_omega_relative_limit
314 def _setup_logging(self):
315 print "Modulation logging turned on."
316 self._fg.connect(self.pre_scaler,
317 gr.file_sink(gr.sizeof_gr_complex, "rx_prescaler.dat"))
318 self._fg.connect(self.agc,
319 gr.file_sink(gr.sizeof_gr_complex, "rx_agc.dat"))
320 self._fg.connect(self.rrc_filter,
321 gr.file_sink(gr.sizeof_gr_complex, "rx_rrc_filter.dat"))
322 self._fg.connect(self.receiver,
323 gr.file_sink(gr.sizeof_gr_complex, "rx_receiver.dat"))
324 self._fg.connect(self.diffdec,
325 gr.file_sink(gr.sizeof_gr_complex, "rx_diffdec.dat"))
326 self._fg.connect(self.slicer,
327 gr.file_sink(gr.sizeof_char, "rx_slicer.dat"))
328 self._fg.connect(self.symbol_mapper,
329 gr.file_sink(gr.sizeof_char, "rx_gray_decoder.dat"))
330 self._fg.connect(self.unpack,
331 gr.file_sink(gr.sizeof_char, "rx_unpack.dat"))
333 def add_options(parser):
335 Adds modulation-specific options to the standard parser
337 parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
338 help="set RRC excess bandwith factor [default=%default] (PSK)")
339 parser.add_option("", "--no-gray-code", dest="gray_code",
340 action="store_false", default=_def_gray_code,
341 help="disable gray coding on modulated bits (PSK)")
342 parser.add_option("", "--costas-alpha", type="float", default=_def_costas_alpha,
343 help="set Costas loop alpha value [default=%default] (PSK)")
344 parser.add_option("", "--gain-mu", type="float", default=_def_gain_mu,
345 help="set M&M symbol sync loop gain mu value [default=%default] (PSK)")
346 parser.add_option("", "--mu", type="float", default=_def_mu,
347 help="set M&M symbol sync loop mu value [default=%default] (PSK)")
348 add_options=staticmethod(add_options)
350 def extract_kwargs_from_options(options):
352 Given command line options, create dictionary suitable for passing to __init__
354 return modulation_utils.extract_kwargs_from_options(
355 d8psk_demod.__init__, ('self', 'fg'), options)
356 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
360 # Add these to the mod/demod registry
362 # NOT READY TO BE USED YET -- ENABLE AT YOUR OWN RISK
363 #modulation_utils.add_type_1_mod('d8psk', d8psk_mod)
364 #modulation_utils.add_type_1_demod('d8psk', d8psk_demod)