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_utils2
29 from math import pi, sqrt, ceil
32 from pprint import pprint
34 # default values (used in __init__ and add_options)
35 _def_samples_per_symbol = 2
41 _def_freq_alpha = 0.010
42 _def_phase_alpha = 0.1
43 _def_timing_alpha = 0.100
44 _def_timing_beta = 0.010
45 _def_timing_max_dev = 1.5
48 # /////////////////////////////////////////////////////////////////////////////
50 # /////////////////////////////////////////////////////////////////////////////
52 class dbpsk2_mod(gr.hier_block2):
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 differential BPSK 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 baud >= 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 log: Log modulation data to files?
78 gr.hier_block2.__init__(self, "dbpsk_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 self._samples_per_symbol < 2:
87 raise TypeError, ("sbp must be an integer >= 2, is %d" % self._samples_per_symbol)
89 arity = pow(2,self.bits_per_symbol())
91 # turn bytes into k-bit vectors
93 gr.packed_to_unpacked_bb(self.bits_per_symbol(), gr.GR_MSB_FIRST)
96 self.symbol_mapper = gr.map_bb(psk.binary_to_gray[arity])
98 self.symbol_mapper = gr.map_bb(psk.binary_to_ungray[arity])
100 self.diffenc = gr.diff_encoder_bb(arity)
102 self.chunks2symbols = gr.chunks_to_symbols_bc(psk.constellation[arity])
104 # pulse shaping filter
106 ntaps = nfilts * 11 * int(self._samples_per_symbol) # make nfilts filters of ntaps each
107 self.rrc_taps = gr.firdes.root_raised_cosine(
109 nfilts, # sampling rate based on 32 filters in resampler
111 self._excess_bw, # excess bandwidth (roll-off factor)
113 self.rrc_filter = gr.pfb_arb_resampler_ccf(self._samples_per_symbol, self.rrc_taps)
116 self.connect(self, self.bytes2chunks, self.symbol_mapper, self.diffenc,
117 self.chunks2symbols, self.rrc_filter, self)
120 self._print_verbage()
123 self._setup_logging()
126 def samples_per_symbol(self):
127 return self._samples_per_symbol
129 def bits_per_symbol(self=None): # static method that's also callable on an instance
131 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
133 def add_options(parser):
135 Adds DBPSK modulation-specific options to the standard parser
137 parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
138 help="set RRC excess bandwith factor [default=%default]")
139 parser.add_option("", "--no-gray-code", dest="gray_code",
140 action="store_false", default=True,
141 help="disable gray coding on modulated bits (PSK)")
142 add_options=staticmethod(add_options)
144 def extract_kwargs_from_options(options):
146 Given command line options, create dictionary suitable for passing to __init__
148 return modulation_utils2.extract_kwargs_from_options(dbpsk2_mod.__init__,
150 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
153 def _print_verbage(self):
155 print "bits per symbol: %d" % self.bits_per_symbol()
156 print "Gray code: %s" % self._gray_code
157 print "RRC roll-off factor: %.2f" % self._excess_bw
159 def _setup_logging(self):
160 print "Modulation logging turned on."
161 self.connect(self.bytes2chunks,
162 gr.file_sink(gr.sizeof_char, "tx_bytes2chunks.dat"))
163 self.connect(self.symbol_mapper,
164 gr.file_sink(gr.sizeof_char, "tx_graycoder.dat"))
165 self.connect(self.diffenc,
166 gr.file_sink(gr.sizeof_char, "tx_diffenc.dat"))
167 self.connect(self.chunks2symbols,
168 gr.file_sink(gr.sizeof_gr_complex, "tx_chunks2symbols.dat"))
169 self.connect(self.rrc_filter,
170 gr.file_sink(gr.sizeof_gr_complex, "tx_rrc_filter.dat"))
173 # /////////////////////////////////////////////////////////////////////////////
176 # Differentially coherent detection of differentially encoded BPSK
177 # /////////////////////////////////////////////////////////////////////////////
179 class dbpsk2_demod(gr.hier_block2):
182 samples_per_symbol=_def_samples_per_symbol,
183 excess_bw=_def_excess_bw,
184 freq_alpha=_def_freq_alpha,
185 phase_alpha=_def_phase_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,
193 Hierarchical block for RRC-filtered differential BPSK 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 samples_per_symbol: samples per symbol >= 2
199 @type samples_per_symbol: float
200 @param excess_bw: Root-raised cosine filter excess bandwidth
201 @type excess_bw: float
202 @param freq_alpha: loop filter gain for frequency recovery
203 @type freq_alpha: float
204 @param phase_alpha: loop filter gain for phase/fine frequency recovery
205 @type phase_alpha: float
206 @param timing_alpha: loop alpha gain for timing recovery
207 @type timing_alpha: float
208 @param timing_max: timing loop maximum rate deviations
209 @type timing_max: float
210 @param gray_code: Tell modulator to Gray code the bits
211 @type gray_code: bool
212 @param verbose: Print information about modulator?
214 @param log: Print modualtion data to files?
216 @param sync_out: Output a sync signal on :1?
219 if sync_out: io_sig_out = gr.io_signaturev(2, 2, (gr.sizeof_char, gr.sizeof_gr_complex))
220 else: io_sig_out = gr.io_signature(1, 1, gr.sizeof_char)
222 gr.hier_block2.__init__(self, "dqpsk2_demod",
223 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
224 io_sig_out) # Output signature
226 self._samples_per_symbol = samples_per_symbol
227 self._excess_bw = excess_bw
228 self._freq_alpha = freq_alpha
229 self._freq_beta = 0.10*self._freq_alpha
230 self._phase_alpha = phase_alpha
231 self._timing_alpha = timing_alpha
232 self._timing_beta = _def_timing_beta
233 self._timing_max_dev=timing_max_dev
234 self._gray_code = gray_code
236 if samples_per_symbol < 2:
237 raise TypeError, "samples_per_symbol must be >= 2, is %r" % (samples_per_symbol,)
239 arity = pow(2,self.bits_per_symbol())
241 # Automatic gain control
242 self.agc = gr.agc2_cc(0.6e-1, 1e-3, 1, 1, 100)
243 #self.agc = gr.feedforward_agc_cc(16, 1.0)
245 # Frequency correction
246 self.freq_recov = gr.fll_band_edge_cc(self._samples_per_symbol, self._excess_bw,
247 11*int(self._samples_per_symbol),
248 self._freq_alpha, self._freq_beta)
250 # symbol timing recovery with RRC data filter
252 ntaps = 11 * int(self._samples_per_symbol*nfilts)
253 taps = gr.firdes.root_raised_cosine(nfilts, nfilts,
254 1.0/float(self._samples_per_symbol),
255 self._excess_bw, ntaps)
256 self.time_recov = gr.pfb_clock_sync_ccf(self._samples_per_symbol,
258 taps, nfilts, nfilts/2, self._timing_max_dev)
259 self.time_recov.set_beta(self._timing_beta)
261 # Perform phase / fine frequency correction
262 self._phase_beta = 0.25 * self._phase_alpha * self._phase_alpha
263 # Allow a frequency swing of +/- half of the sample rate
267 self.phase_recov = gr.costas_loop_cc(self._phase_alpha,
271 # Do differential decoding based on phase change of symbols
272 self.diffdec = gr.diff_phasor_cc()
274 # find closest constellation point
276 rotated_const = map(lambda pt: pt * rot, psk.constellation[arity])
277 self.slicer = gr.constellation_decoder_cb(rotated_const, range(arity))
280 self.symbol_mapper = gr.map_bb(psk.gray_to_binary[arity])
282 self.symbol_mapper = gr.map_bb(psk.ungray_to_binary[arity])
284 # unpack the k bit vector into a stream of bits
285 self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbol())
288 self._print_verbage()
291 self._setup_logging()
294 self.connect(self, self.agc,
295 self.freq_recov, self.time_recov, self.phase_recov,
296 self.diffdec, self.slicer, self.symbol_mapper, self.unpack, self)
297 if sync_out: self.connect(self.time_recov, (self, 1))
299 def samples_per_symbol(self):
300 return self._samples_per_symbol
302 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
304 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM
306 def _print_verbage(self):
307 print "\nDemodulator:"
308 print "bits per symbol: %d" % self.bits_per_symbol()
309 print "Gray code: %s" % self._gray_code
310 print "RRC roll-off factor: %.2f" % self._excess_bw
311 print "FLL gain: %.2e" % self._freq_alpha
312 print "Timing alpha gain: %.2e" % self._timing_alpha
313 print "Timing beta gain: %.2e" % self._timing_beta
314 print "Timing max dev: %.2f" % self._timing_max_dev
315 print "Phase track alpha: %.2e" % self._phase_alpha
316 print "Phase track beta: %.2e" % self._phase_beta
318 def _setup_logging(self):
319 print "Modulation logging turned on."
320 self.connect(self.agc,
321 gr.file_sink(gr.sizeof_gr_complex, "rx_agc.dat"))
322 self.connect(self.freq_recov,
323 gr.file_sink(gr.sizeof_gr_complex, "rx_freq_recov.dat"))
324 self.connect(self.time_recov,
325 gr.file_sink(gr.sizeof_gr_complex, "rx_time_recov.dat"))
326 self.connect(self.phase_recov,
327 gr.file_sink(gr.sizeof_gr_complex, "rx_phase_recov.dat"))
328 self.connect(self.diffdec,
329 gr.file_sink(gr.sizeof_gr_complex, "rx_diffdec.dat"))
330 self.connect(self.slicer,
331 gr.file_sink(gr.sizeof_char, "rx_slicer.dat"))
332 self.connect(self.symbol_mapper,
333 gr.file_sink(gr.sizeof_char, "rx_symbol_mapper.dat"))
334 self.connect(self.unpack,
335 gr.file_sink(gr.sizeof_char, "rx_unpack.dat"))
337 def add_options(parser):
339 Adds DBPSK demodulation-specific options to the standard parser
341 parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
342 help="set RRC excess bandwith factor [default=%default] (PSK)")
343 parser.add_option("", "--no-gray-code", dest="gray_code",
344 action="store_false", default=_def_gray_code,
345 help="disable gray coding on modulated bits (PSK)")
346 parser.add_option("", "--freq-alpha", type="float", default=_def_freq_alpha,
347 help="set frequency lock loop alpha gain value [default=%default] (PSK)")
348 parser.add_option("", "--phase-alpha", type="float", default=_def_phase_alpha,
349 help="set phase tracking loop alpha value [default=%default] (PSK)")
350 parser.add_option("", "--timing-alpha", type="float", default=_def_timing_alpha,
351 help="set timing symbol sync loop gain alpha value [default=%default] (GMSK/PSK)")
352 parser.add_option("", "--timing-beta", type="float", default=_def_timing_beta,
353 help="set timing symbol sync loop gain beta value [default=%default] (GMSK/PSK)")
354 parser.add_option("", "--timing-max-dev", type="float", default=_def_timing_max_dev,
355 help="set timing symbol sync loop maximum deviation [default=%default] (GMSK/PSK)")
356 add_options=staticmethod(add_options)
358 def extract_kwargs_from_options(options):
360 Given command line options, create dictionary suitable for passing to __init__
362 return modulation_utils2.extract_kwargs_from_options(
363 dbpsk2_demod.__init__, ('self',), options)
364 extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
366 # Add these to the mod/demod registry
368 modulation_utils2.add_type_1_mod('dbpsk2', dbpsk2_mod)
369 modulation_utils2.add_type_1_demod('dbpsk2', dbpsk2_demod)