root / gnuradio-core / src / python / gnuradio / blks2impl / dbpsk2.py @ 3bac2fa5
History | View | Annotate | Download (15.3 kB)
| 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 BPSK modulation and demodulation.
|
| 26 | """
|
| 27 | |
| 28 | from gnuradio import gr, gru, modulation_utils |
| 29 | from math import pi, sqrt, ceil |
| 30 | import psk |
| 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_freq_alpha = 4e-3
|
| 42 | _def_costas_alpha = 0.1
|
| 43 | _def_timing_alpha = 0.100
|
| 44 | _def_timing_beta = 0.010
|
| 45 | _def_timing_max_dev = 1.5
|
| 46 | |
| 47 | |
| 48 | # /////////////////////////////////////////////////////////////////////////////
|
| 49 | # DBPSK modulator
|
| 50 | # /////////////////////////////////////////////////////////////////////////////
|
| 51 | |
| 52 | class dbpsk2_mod(gr.hier_block2): |
| 53 | |
| 54 | def __init__(self, |
| 55 | samples_per_symbol=_def_samples_per_symbol, |
| 56 | excess_bw=_def_excess_bw, |
| 57 | gray_code=_def_gray_code, |
| 58 | verbose=_def_verbose, |
| 59 | log=_def_log): |
| 60 | """
|
| 61 | Hierarchical block for RRC-filtered differential BPSK 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 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
|
| 71 | @type gray_code: bool
|
| 72 | @param verbose: Print information about modulator?
|
| 73 | @type verbose: bool
|
| 74 | @param log: Log modulation data to files?
|
| 75 | @type log: bool
|
| 76 | """ |
| 77 | |
| 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 |
| 81 | |
| 82 | self._samples_per_symbol = samples_per_symbol
|
| 83 | self._excess_bw = excess_bw
|
| 84 | self._gray_code = gray_code
|
| 85 | |
| 86 | if not isinstance(self._samples_per_symbol, int) or self._samples_per_symbol < 2: |
| 87 | raise TypeError, ("sbp must be an integer >= 2, is %d" % self._samples_per_symbol) |
| 88 | |
| 89 | arity = pow(2,self.bits_per_symbol()) |
| 90 | |
| 91 | # turn bytes into k-bit vectors
|
| 92 | self.bytes2chunks = \
|
| 93 | gr.packed_to_unpacked_bb(self.bits_per_symbol(), gr.GR_MSB_FIRST)
|
| 94 | |
| 95 | if self._gray_code: |
| 96 | self.symbol_mapper = gr.map_bb(psk.binary_to_gray[arity])
|
| 97 | else:
|
| 98 | self.symbol_mapper = gr.map_bb(psk.binary_to_ungray[arity])
|
| 99 | |
| 100 | self.diffenc = gr.diff_encoder_bb(arity)
|
| 101 | |
| 102 | self.chunks2symbols = gr.chunks_to_symbols_bc(psk.constellation[arity])
|
| 103 | |
| 104 | # pulse shaping filter
|
| 105 | nfilts = 32
|
| 106 | ntaps = nfilts * 11 * self._samples_per_symbol # make nfilts filters of ntaps each |
| 107 | self.rrc_taps = gr.firdes.root_raised_cosine(
|
| 108 | nfilts, # gain
|
| 109 | nfilts, # sampling rate based on 32 filters in resampler
|
| 110 | 1.0, # symbol rate |
| 111 | self._excess_bw, # excess bandwidth (roll-off factor) |
| 112 | ntaps) |
| 113 | self.rrc_filter = gr.pfb_arb_resampler_ccf(self._samples_per_symbol, self.rrc_taps) |
| 114 | |
| 115 | # Connect
|
| 116 | self.connect(self, self.bytes2chunks, self.symbol_mapper, self.diffenc, |
| 117 | self.chunks2symbols, self.rrc_filter, self) |
| 118 | |
| 119 | if verbose:
|
| 120 | self._print_verbage()
|
| 121 | |
| 122 | if log:
|
| 123 | self._setup_logging()
|
| 124 | |
| 125 | |
| 126 | def samples_per_symbol(self): |
| 127 | return self._samples_per_symbol |
| 128 | |
| 129 | def bits_per_symbol(self=None): # static method that's also callable on an instance |
| 130 | return 1 |
| 131 | bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM |
| 132 | |
| 133 | def add_options(parser): |
| 134 | """
|
| 135 | Adds DBPSK modulation-specific options to the standard parser
|
| 136 | """ |
| 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)
|
| 143 | |
| 144 | def extract_kwargs_from_options(options): |
| 145 | """
|
| 146 | Given command line options, create dictionary suitable for passing to __init__
|
| 147 | """ |
| 148 | return modulation_utils.extract_kwargs_from_options(dbpsk2_mod.__init__,
|
| 149 | ('self',), options)
|
| 150 | extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
|
| 151 | |
| 152 | |
| 153 | def _print_verbage(self): |
| 154 | print "\nModulator:" |
| 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 |
| 158 | |
| 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"))
|
| 171 | |
| 172 | |
| 173 | # /////////////////////////////////////////////////////////////////////////////
|
| 174 | # DBPSK demodulator
|
| 175 | #
|
| 176 | # Differentially coherent detection of differentially encoded BPSK
|
| 177 | # /////////////////////////////////////////////////////////////////////////////
|
| 178 | |
| 179 | class dbpsk2_demod(gr.hier_block2): |
| 180 | |
| 181 | def __init__(self, |
| 182 | samples_per_symbol=_def_samples_per_symbol, |
| 183 | excess_bw=_def_excess_bw, |
| 184 | freq_alpha=_def_freq_alpha, |
| 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, |
| 190 | log=_def_log, |
| 191 | sync_out=False):
|
| 192 | """
|
| 193 | Hierarchical block for RRC-filtered differential BPSK demodulation
|
| 194 | |
| 195 | The input is the complex modulated signal at baseband.
|
| 196 | The output is a stream of bits packed 1 bit per byte (LSB)
|
| 197 | |
| 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 costas_alpha: loop filter gain for phase/fine frequency recovery
|
| 205 | @type costas_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?
|
| 213 | @type verbose: bool
|
| 214 | @param log: Print modualtion data to files?
|
| 215 | @type log: bool
|
| 216 | @param sync_out: Output a sync signal on :1?
|
| 217 | @type sync_out: bool
|
| 218 | """ |
| 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) |
| 221 | |
| 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
|
| 225 | |
| 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.25*self._freq_alpha**2 |
| 230 | self._costas_alpha = costas_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
|
| 235 | |
| 236 | if samples_per_symbol < 2: |
| 237 | raise TypeError, "samples_per_symbol must be >= 2, is %r" % (samples_per_symbol,) |
| 238 | |
| 239 | arity = pow(2,self.bits_per_symbol()) |
| 240 | |
| 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)
|
| 244 | |
| 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) |
| 249 | |
| 250 | # symbol timing recovery with RRC data filter
|
| 251 | nfilts = 32
|
| 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, |
| 257 | self._timing_alpha,
|
| 258 | taps, nfilts, nfilts/2, self._timing_max_dev) |
| 259 | self.time_recov.set_beta(self._timing_beta) |
| 260 | |
| 261 | # Perform phase / fine frequency correction
|
| 262 | self._costas_beta = 0.25 * self._costas_alpha * self._costas_alpha |
| 263 | # Allow a frequency swing of +/- half of the sample rate
|
| 264 | fmin = -0.5
|
| 265 | fmax = 0.5
|
| 266 | |
| 267 | self.phase_recov = gr.costas_loop_cc(self._costas_alpha, |
| 268 | self._costas_beta,
|
| 269 | fmax, fmin, arity) |
| 270 | |
| 271 | # Do differential decoding based on phase change of symbols
|
| 272 | self.diffdec = gr.diff_phasor_cc()
|
| 273 | |
| 274 | # find closest constellation point
|
| 275 | rot = 1
|
| 276 | rotated_const = map(lambda pt: pt * rot, psk.constellation[arity]) |
| 277 | self.slicer = gr.constellation_decoder_cb(rotated_const, range(arity)) |
| 278 | |
| 279 | if self._gray_code: |
| 280 | self.symbol_mapper = gr.map_bb(psk.gray_to_binary[arity])
|
| 281 | else:
|
| 282 | self.symbol_mapper = gr.map_bb(psk.ungray_to_binary[arity])
|
| 283 | |
| 284 | # unpack the k bit vector into a stream of bits
|
| 285 | self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbol()) |
| 286 | |
| 287 | if verbose:
|
| 288 | self._print_verbage()
|
| 289 | |
| 290 | if log:
|
| 291 | self._setup_logging()
|
| 292 | |
| 293 | # Connect
|
| 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)) |
| 298 | |
| 299 | def samples_per_symbol(self): |
| 300 | return self._samples_per_symbol |
| 301 | |
| 302 | def bits_per_symbol(self=None): # staticmethod that's also callable on an instance |
| 303 | return 1 |
| 304 | bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method. RTFM |
| 305 | |
| 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: %.2f" % self._freq_alpha |
| 312 | print "Costas Loop alpha: %.2f" % self._costas_alpha |
| 313 | print "Costas Loop beta: %.2f" % self._costas_beta |
| 314 | print "Timing alpha gain: %.2f" % self._timing_alpha |
| 315 | print "Timing beta gain: %.2f" % self._timing_beta |
| 316 | print "Timing max dev: %.2f" % self._timing_max_dev |
| 317 | |
| 318 | def _setup_logging(self): |
| 319 | print "Modulation logging turned on." |
| 320 | self.connect(self.pre_scaler, |
| 321 | gr.file_sink(gr.sizeof_gr_complex, "rx_prescaler.dat"))
|
| 322 | self.connect(self.agc, |
| 323 | gr.file_sink(gr.sizeof_gr_complex, "rx_agc.dat"))
|
| 324 | self.connect(self.rrc_filter, |
| 325 | gr.file_sink(gr.sizeof_gr_complex, "rx_rrc_filter.dat"))
|
| 326 | self.connect(self.clock_recov, |
| 327 | gr.file_sink(gr.sizeof_gr_complex, "rx_clock_recov.dat"))
|
| 328 | self.connect(self.time_recov, |
| 329 | gr.file_sink(gr.sizeof_gr_complex, "rx_time_recov.dat"))
|
| 330 | self.connect(self.diffdec, |
| 331 | gr.file_sink(gr.sizeof_gr_complex, "rx_diffdec.dat"))
|
| 332 | self.connect(self.slicer, |
| 333 | gr.file_sink(gr.sizeof_char, "rx_slicer.dat"))
|
| 334 | self.connect(self.symbol_mapper, |
| 335 | gr.file_sink(gr.sizeof_char, "rx_symbol_mapper.dat"))
|
| 336 | self.connect(self.unpack, |
| 337 | gr.file_sink(gr.sizeof_char, "rx_unpack.dat"))
|
| 338 | |
| 339 | def add_options(parser): |
| 340 | """
|
| 341 | Adds DBPSK demodulation-specific options to the standard parser
|
| 342 | """ |
| 343 | parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw, |
| 344 | help="set RRC excess bandwith factor [default=%default] (PSK)")
|
| 345 | parser.add_option("", "--no-gray-code", dest="gray_code", |
| 346 | action="store_false", default=_def_gray_code,
|
| 347 | help="disable gray coding on modulated bits (PSK)")
|
| 348 | parser.add_option("", "--freq-alpha", type="float", default=_def_freq_alpha, |
| 349 | help="set frequency lock loop alpha gain value [default=%default] (PSK)")
|
| 350 | parser.add_option("", "--costas-alpha", type="float", default=None, |
| 351 | help="set Costas loop alpha value [default=%default] (PSK)")
|
| 352 | parser.add_option("", "--gain-alpha", type="float", default=_def_timing_alpha, |
| 353 | help="set timing symbol sync loop gain alpha value [default=%default] (GMSK/PSK)")
|
| 354 | parser.add_option("", "--gain-beta", type="float", default=_def_timing_beta, |
| 355 | help="set timing symbol sync loop gain beta value [default=%default] (GMSK/PSK)")
|
| 356 | parser.add_option("", "--timing-max-dev", type="float", default=_def_timing_max_dev, |
| 357 | help="set timing symbol sync loop maximum deviation [default=%default] (GMSK/PSK)")
|
| 358 | add_options=staticmethod(add_options)
|
| 359 | |
| 360 | def extract_kwargs_from_options(options): |
| 361 | """
|
| 362 | Given command line options, create dictionary suitable for passing to __init__
|
| 363 | """ |
| 364 | return modulation_utils.extract_kwargs_from_options(
|
| 365 | dbpsk2_demod.__init__, ('self',), options)
|
| 366 | extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
|
| 367 | #
|
| 368 | # Add these to the mod/demod registry
|
| 369 | #
|
| 370 | modulation_utils.add_type_1_mod('dbpsk2', dbpsk2_mod)
|
| 371 | modulation_utils.add_type_1_demod('dbpsk2', dbpsk2_demod)
|