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 differential BPSK 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 = 0.1
42 _def_timing_alpha = 0.100
43 _def_timing_beta = 0.010
44 _def_timing_max_dev = 1.5
47 # /////////////////////////////////////////////////////////////////////////////
49 # /////////////////////////////////////////////////////////////////////////////
51 class dbpsk2_mod(gr.hier_block2):
54 samples_per_symbol=_def_samples_per_symbol,
55 excess_bw=_def_excess_bw,
56 gray_code=_def_gray_code,
60 Hierarchical block for RRC-filtered differential BPSK modulation.
62 The input is a byte stream (unsigned char) and the
63 output is the complex modulated signal at baseband.
65 @param samples_per_symbol: samples per baud >= 2
66 @type samples_per_symbol: integer
67 @param excess_bw: Root-raised cosine filter excess bandwidth
68 @type excess_bw: float
69 @param gray_code: Tell modulator to Gray code the bits
71 @param verbose: Print information about modulator?
73 @param log: Log modulation data to files?
77 gr.hier_block2.__init__(self, "dbpsk_mod",
78 gr.io_signature(1, 1, gr.sizeof_char), # Input signature
79 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
85 if not isinstance(self._samples_per_symbol, int) or self._samples_per_symbol < 2:
86 raise TypeError, ("sbp must be an integer >= 2, is %d" % self._samples_per_symbol)
88 ntaps = 11 * self._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)
103 self.chunks2symbols = gr.chunks_to_symbols_bc(psk.constellation[arity])
105 # pulse shaping filter
106 self.rrc_taps = gr.firdes.root_raised_cosine(
107 self._samples_per_symbol, # gain (samples_per_symbol since we're
108 # interpolating by samples_per_symbol)
109 self._samples_per_symbol, # sampling rate
111 self._excess_bw, # excess bandwidth (roll-off factor)
113 self.rrc_filter = gr.interp_fir_filter_ccf(self._samples_per_symbol,
117 self.connect(self, self.bytes2chunks, self.symbol_mapper, self.diffenc,
118 self.chunks2symbols, self.rrc_filter, self)
121 self._print_verbage()
124 self._setup_logging()
127 def samples_per_symbol(self):
128 return self._samples_per_symbol
130 def bits_per_symbol(self=None): # static method that's also callable on an instance
132 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
134 def add_options(parser):
136 Adds DBPSK modulation-specific options to the standard parser
138 parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
139 help="set RRC excess bandwith factor [default=%default]")
140 parser.add_option("", "--no-gray-code", dest="gray_code",
141 action="store_false", default=True,
142 help="disable gray coding on modulated bits (PSK)")
143 add_options=staticmethod(add_options)
145 def extract_kwargs_from_options(options):
147 Given command line options, create dictionary suitable for passing to __init__
149 return modulation_utils.extract_kwargs_from_options(dbpsk2_mod.__init__,
151 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
154 def _print_verbage(self):
156 print "bits per symbol: %d" % self.bits_per_symbol()
157 print "Gray code: %s" % self._gray_code
158 print "RRC roll-off factor: %.2f" % self._excess_bw
160 def _setup_logging(self):
161 print "Modulation logging turned on."
162 self.connect(self.bytes2chunks,
163 gr.file_sink(gr.sizeof_char, "tx_bytes2chunks.dat"))
164 self.connect(self.symbol_mapper,
165 gr.file_sink(gr.sizeof_char, "tx_graycoder.dat"))
166 self.connect(self.diffenc,
167 gr.file_sink(gr.sizeof_char, "tx_diffenc.dat"))
168 self.connect(self.chunks2symbols,
169 gr.file_sink(gr.sizeof_gr_complex, "tx_chunks2symbols.dat"))
170 self.connect(self.rrc_filter,
171 gr.file_sink(gr.sizeof_gr_complex, "tx_rrc_filter.dat"))
174 # /////////////////////////////////////////////////////////////////////////////
177 # Differentially coherent detection of differentially encoded BPSK
178 # /////////////////////////////////////////////////////////////////////////////
180 class dbpsk2_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 timing_alpha=_def_timing_alpha,
187 timing_max_dev=_def_timing_max_dev,
188 gray_code=_def_gray_code,
189 verbose=_def_verbose,
192 Hierarchical block for RRC-filtered differential BPSK demodulation
194 The input is the complex modulated signal at baseband.
195 The output is a stream of bits packed 1 bit per byte (LSB)
197 @param samples_per_symbol: samples per symbol >= 2
198 @type samples_per_symbol: float
199 @param excess_bw: Root-raised cosine filter excess bandwidth
200 @type excess_bw: float
201 @param costas_alpha: loop filter gain
202 @type costas_alpha: float
203 @param timing_alpha: timing loop alpha gain
204 @type timing_alpha: float
205 @param timing_max: timing loop maximum rate deviations
206 @type timing_max: float
207 @param gray_code: Tell modulator to Gray code the bits
208 @type gray_code: bool
209 @param verbose: Print information about modulator?
211 @param debug: Print modualtion data to files?
215 gr.hier_block2.__init__(self, "dbpsk2_demod",
216 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
217 gr.io_signature(1, 1, gr.sizeof_char)) # Output signature
219 self._samples_per_symbol = samples_per_symbol
220 self._excess_bw = excess_bw
221 self._costas_alpha = costas_alpha
222 self._timing_alpha = timing_alpha
223 self._timing_beta = _def_timing_beta
224 self._timing_max_dev=timing_max_dev
225 self._gray_code = gray_code
227 if samples_per_symbol < 2:
228 raise TypeError, "samples_per_symbol must be >= 2, is %r" % (samples_per_symbol,)
230 arity = pow(2,self.bits_per_symbol())
232 # Automatic gain control
233 self.agc = gr.agc2_cc(0.6e-1, 1e-3, 1, 1, 100)
234 #self.agc = gr.feedforward_agc_cc(16, 1.0)
236 self._costas_beta = 0.25 * self._costas_alpha * self._costas_alpha
237 # Allow a frequency swing of +/- half of the sample rate
241 self.clock_recov = gr.costas_loop_cc(self._costas_alpha,
245 # symbol timing recovery with RRC data filter
247 ntaps = 11 * samples_per_symbol*nfilts
248 taps = gr.firdes.root_raised_cosine(nfilts, nfilts, 1.0/float(self._samples_per_symbol), self._excess_bw, ntaps)
249 self.time_recov = gr.pfb_clock_sync_ccf(self._samples_per_symbol,
251 taps, nfilts, nfilts/2, self._timing_max_dev)
252 self.time_recov.set_beta(self._timing_beta)
254 # Do differential decoding based on phase change of symbols
255 self.diffdec = gr.diff_phasor_cc()
257 # find closest constellation point
259 rotated_const = map(lambda pt: pt * rot, psk.constellation[arity])
260 self.slicer = gr.constellation_decoder_cb(rotated_const, range(arity))
263 self.symbol_mapper = gr.map_bb(psk.gray_to_binary[arity])
265 self.symbol_mapper = gr.map_bb(psk.ungray_to_binary[arity])
267 # unpack the k bit vector into a stream of bits
268 self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbol())
271 self._print_verbage()
274 self._setup_logging()
277 self.connect(self, self.agc,
280 self.diffdec, self.slicer, self.symbol_mapper, self.unpack, self)
282 def samples_per_symbol(self):
283 return self._samples_per_symbol
285 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
287 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
289 def _print_verbage(self):
290 print "\nDemodulator:"
291 print "bits per symbol: %d" % self.bits_per_symbol()
292 print "Gray code: %s" % self._gray_code
293 print "RRC roll-off factor: %.2f" % self._excess_bw
294 print "Costas Loop alpha: %.2f" % self._costas_alpha
295 print "Costas Loop beta: %.2f" % self._costas_beta
296 print "Timing alpha gain: %.2f" % self._timing_alpha
297 print "Timing beta gain: %.2f" % self._timing_beta
298 print "Timing max dev: %.2f" % self._timing_max_dev
300 def _setup_logging(self):
301 print "Modulation logging turned on."
302 self.connect(self.pre_scaler,
303 gr.file_sink(gr.sizeof_gr_complex, "rx_prescaler.dat"))
304 self.connect(self.agc,
305 gr.file_sink(gr.sizeof_gr_complex, "rx_agc.dat"))
306 self.connect(self.rrc_filter,
307 gr.file_sink(gr.sizeof_gr_complex, "rx_rrc_filter.dat"))
308 self.connect(self.clock_recov,
309 gr.file_sink(gr.sizeof_gr_complex, "rx_clock_recov.dat"))
310 self.connect(self.time_recov,
311 gr.file_sink(gr.sizeof_gr_complex, "rx_time_recov.dat"))
312 self.connect(self.diffdec,
313 gr.file_sink(gr.sizeof_gr_complex, "rx_diffdec.dat"))
314 self.connect(self.slicer,
315 gr.file_sink(gr.sizeof_char, "rx_slicer.dat"))
316 self.connect(self.symbol_mapper,
317 gr.file_sink(gr.sizeof_char, "rx_symbol_mapper.dat"))
318 self.connect(self.unpack,
319 gr.file_sink(gr.sizeof_char, "rx_unpack.dat"))
321 def add_options(parser):
323 Adds DBPSK demodulation-specific options to the standard parser
325 parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
326 help="set RRC excess bandwith factor [default=%default] (PSK)")
327 parser.add_option("", "--no-gray-code", dest="gray_code",
328 action="store_false", default=_def_gray_code,
329 help="disable gray coding on modulated bits (PSK)")
330 parser.add_option("", "--costas-alpha", type="float", default=None,
331 help="set Costas loop alpha value [default=%default] (PSK)")
332 parser.add_option("", "--gain-alpha", type="float", default=_def_timing_alpha,
333 help="set timing symbol sync loop gain alpha value [default=%default] (GMSK/PSK)")
334 parser.add_option("", "--gain-beta", type="float", default=_def_timing_beta,
335 help="set timing symbol sync loop gain beta value [default=%default] (GMSK/PSK)")
336 parser.add_option("", "--timing-max-dev", type="float", default=_def_timing_max_dev,
337 help="set timing symbol sync loop maximum deviation [default=%default] (GMSK/PSK)")
338 add_options=staticmethod(add_options)
340 def extract_kwargs_from_options(options):
342 Given command line options, create dictionary suitable for passing to __init__
344 return modulation_utils.extract_kwargs_from_options(
345 dbpsk2_demod.__init__, ('self',), options)
346 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
348 # Add these to the mod/demod registry
350 modulation_utils.add_type_1_mod('dbpsk2', dbpsk2_mod)
351 modulation_utils.add_type_1_demod('dbpsk2', dbpsk2_demod)