root / gnuradio-core / src / python / gnuradio / blks2impl / generic_mod_demod.py @ e4df34e7
History | View | Annotate | Download (16.5 kB)
| 1 | #
|
|---|---|
| 2 | # Copyright 2005,2006,2007,2009 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 | Generic modulation and demodulation.
|
| 26 | """
|
| 27 | |
| 28 | from gnuradio import gr |
| 29 | from gnuradio.modulation_utils2 import extract_kwargs_from_options_for_class |
| 30 | from gnuradio.utils import mod_codes |
| 31 | |
| 32 | # default values (used in __init__ and add_options)
|
| 33 | _def_samples_per_symbol = 2
|
| 34 | _def_excess_bw = 0.35
|
| 35 | _def_verbose = False
|
| 36 | _def_log = False
|
| 37 | |
| 38 | # Frequency correction
|
| 39 | _def_freq_alpha = 0.010
|
| 40 | # Symbol timing recovery
|
| 41 | _def_timing_alpha = 0.100
|
| 42 | _def_timing_beta = 0.010
|
| 43 | _def_timing_max_dev = 1.5
|
| 44 | # Fine frequency / Phase correction
|
| 45 | _def_phase_alpha = 0.1
|
| 46 | # Number of points in constellation
|
| 47 | _def_constellation_points = 16
|
| 48 | # Whether differential coding is used.
|
| 49 | _def_differential = True
|
| 50 | |
| 51 | def add_common_options(parser): |
| 52 | """
|
| 53 | Sets options common to both modulator and demodulator.
|
| 54 | """ |
| 55 | parser.add_option("-p", "--constellation-points", type="int", default=_def_constellation_points, |
| 56 | help="set the number of constellation points (must be a power of 2 (power of 4 for QAM) [default=%default]")
|
| 57 | parser.add_option("", "--differential", action="store_true", dest="differential", default=True, |
| 58 | help="use differential encoding [default=%default]")
|
| 59 | parser.add_option("", "--not-differential", action="store_false", dest="differential", |
| 60 | help="do not use differential encoding [default=%default]")
|
| 61 | parser.add_option("", "--mod-code", type="choice", choices=mod_codes.codes, |
| 62 | default=mod_codes.NO_CODE, |
| 63 | help="Select modulation code from: %s [default=%%default]"
|
| 64 | % (', '.join(mod_codes.codes),))
|
| 65 | parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw, |
| 66 | help="set RRC excess bandwith factor [default=%default]")
|
| 67 | |
| 68 | |
| 69 | # /////////////////////////////////////////////////////////////////////////////
|
| 70 | # Generic modulator
|
| 71 | # /////////////////////////////////////////////////////////////////////////////
|
| 72 | |
| 73 | class generic_mod(gr.hier_block2): |
| 74 | |
| 75 | def __init__(self, constellation, |
| 76 | differential=_def_differential, |
| 77 | samples_per_symbol=_def_samples_per_symbol, |
| 78 | excess_bw=_def_excess_bw, |
| 79 | verbose=_def_verbose, |
| 80 | log=_def_log): |
| 81 | """
|
| 82 | Hierarchical block for RRC-filtered differential generic modulation.
|
| 83 | |
| 84 | The input is a byte stream (unsigned char) and the
|
| 85 | output is the complex modulated signal at baseband.
|
| 86 |
|
| 87 | @param constellation: determines the modulation type
|
| 88 | @type constellation: gnuradio.gr.gr_constellation
|
| 89 | @param samples_per_symbol: samples per baud >= 2
|
| 90 | @type samples_per_symbol: integer
|
| 91 | @param excess_bw: Root-raised cosine filter excess bandwidth
|
| 92 | @type excess_bw: float
|
| 93 | @param verbose: Print information about modulator?
|
| 94 | @type verbose: bool
|
| 95 | @param log: Log modulation data to files?
|
| 96 | @type log: bool
|
| 97 | """ |
| 98 | |
| 99 | gr.hier_block2.__init__(self, "generic_mod", |
| 100 | gr.io_signature(1, 1, gr.sizeof_char), # Input signature |
| 101 | gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature |
| 102 | |
| 103 | self._constellation = constellation.base()
|
| 104 | self._samples_per_symbol = samples_per_symbol
|
| 105 | self._excess_bw = excess_bw
|
| 106 | self._differential = differential
|
| 107 | |
| 108 | if not isinstance(self._samples_per_symbol, int) or self._samples_per_symbol < 2: |
| 109 | raise TypeError, ("sbp must be an integer >= 2, is %d" % self._samples_per_symbol) |
| 110 | |
| 111 | ntaps = 11 * self._samples_per_symbol |
| 112 | |
| 113 | arity = pow(2,self.bits_per_symbol()) |
| 114 | |
| 115 | # turn bytes into k-bit vectors
|
| 116 | self.bytes2chunks = \
|
| 117 | gr.packed_to_unpacked_bb(self.bits_per_symbol(), gr.GR_MSB_FIRST)
|
| 118 | |
| 119 | if self._constellation.apply_pre_diff_code(): |
| 120 | self.symbol_mapper = gr.map_bb(self._constellation.pre_diff_code()) |
| 121 | |
| 122 | if differential:
|
| 123 | self.diffenc = gr.diff_encoder_bb(arity)
|
| 124 | |
| 125 | self.chunks2symbols = gr.chunks_to_symbols_bc(self._constellation.points()) |
| 126 | |
| 127 | # pulse shaping filter
|
| 128 | self.rrc_taps = gr.firdes.root_raised_cosine(
|
| 129 | self._samples_per_symbol, # gain (samples_per_symbol since we're |
| 130 | # interpolating by samples_per_symbol)
|
| 131 | self._samples_per_symbol, # sampling rate |
| 132 | 1.0, # symbol rate |
| 133 | self._excess_bw, # excess bandwidth (roll-off factor) |
| 134 | ntaps) |
| 135 | self.rrc_filter = gr.interp_fir_filter_ccf(self._samples_per_symbol, |
| 136 | self.rrc_taps)
|
| 137 | |
| 138 | # Connect
|
| 139 | blocks = [self, self.bytes2chunks] |
| 140 | if self._constellation.apply_pre_diff_code(): |
| 141 | blocks.append(self.symbol_mapper)
|
| 142 | if differential:
|
| 143 | blocks.append(self.diffenc)
|
| 144 | blocks += [self.chunks2symbols, self.rrc_filter, self] |
| 145 | self.connect(*blocks)
|
| 146 | |
| 147 | if verbose:
|
| 148 | self._print_verbage()
|
| 149 | |
| 150 | if log:
|
| 151 | self._setup_logging()
|
| 152 | |
| 153 | |
| 154 | def samples_per_symbol(self): |
| 155 | return self._samples_per_symbol |
| 156 | |
| 157 | def bits_per_symbol(self): # static method that's also callable on an instance |
| 158 | return self._constellation.bits_per_symbol() |
| 159 | |
| 160 | def add_options(parser): |
| 161 | """
|
| 162 | Adds generic modulation options to the standard parser
|
| 163 | """ |
| 164 | add_common_options(parser) |
| 165 | add_options=staticmethod(add_options)
|
| 166 | |
| 167 | def extract_kwargs_from_options(cls, options): |
| 168 | """
|
| 169 | Given command line options, create dictionary suitable for passing to __init__
|
| 170 | """ |
| 171 | return extract_kwargs_from_options_for_class(cls, options)
|
| 172 | extract_kwargs_from_options=classmethod(extract_kwargs_from_options)
|
| 173 | |
| 174 | |
| 175 | def _print_verbage(self): |
| 176 | print "\nModulator:" |
| 177 | print "bits per symbol: %d" % self.bits_per_symbol() |
| 178 | print "RRC roll-off factor: %.2f" % self._excess_bw |
| 179 | |
| 180 | def _setup_logging(self): |
| 181 | print "Modulation logging turned on." |
| 182 | self.connect(self.bytes2chunks, |
| 183 | gr.file_sink(gr.sizeof_char, "tx_bytes2chunks.dat"))
|
| 184 | if self._constellation.apply_pre_diff_code(): |
| 185 | self.connect(self.symbol_mapper, |
| 186 | gr.file_sink(gr.sizeof_char, "tx_symbol_mapper.dat"))
|
| 187 | if self._differential: |
| 188 | self.connect(self.diffenc, |
| 189 | gr.file_sink(gr.sizeof_char, "tx_diffenc.dat"))
|
| 190 | self.connect(self.chunks2symbols, |
| 191 | gr.file_sink(gr.sizeof_gr_complex, "tx_chunks2symbols.dat"))
|
| 192 | self.connect(self.rrc_filter, |
| 193 | gr.file_sink(gr.sizeof_gr_complex, "tx_rrc_filter.dat"))
|
| 194 | |
| 195 | |
| 196 | # /////////////////////////////////////////////////////////////////////////////
|
| 197 | # Generic demodulator
|
| 198 | #
|
| 199 | # Differentially coherent detection of differentially encoded generically
|
| 200 | # modulated signal.
|
| 201 | # /////////////////////////////////////////////////////////////////////////////
|
| 202 | |
| 203 | class generic_demod(gr.hier_block2): |
| 204 | |
| 205 | def __init__(self, constellation, |
| 206 | samples_per_symbol=_def_samples_per_symbol, |
| 207 | differential=_def_differential, |
| 208 | excess_bw=_def_excess_bw, |
| 209 | freq_alpha=_def_freq_alpha, |
| 210 | timing_alpha=_def_timing_alpha, |
| 211 | timing_max_dev=_def_timing_max_dev, |
| 212 | phase_alpha=_def_phase_alpha, |
| 213 | verbose=_def_verbose, |
| 214 | log=_def_log): |
| 215 | """
|
| 216 | Hierarchical block for RRC-filtered differential generic demodulation.
|
| 217 | |
| 218 | The input is the complex modulated signal at baseband.
|
| 219 | The output is a stream of bits packed 1 bit per byte (LSB)
|
| 220 | |
| 221 | @param constellation: determines the modulation type
|
| 222 | @type constellation: gnuradio.gr.gr_constellation
|
| 223 | @param samples_per_symbol: samples per symbol >= 2
|
| 224 | @type samples_per_symbol: float
|
| 225 | @param excess_bw: Root-raised cosine filter excess bandwidth
|
| 226 | @type excess_bw: float
|
| 227 | @param freq_alpha: loop filter gain for frequency recovery
|
| 228 | @type freq_alpha: float
|
| 229 | @param timing_alpha: loop alpha gain for timing recovery
|
| 230 | @type timing_alpha: float
|
| 231 | @param timing_max_dev: timing loop maximum rate deviations
|
| 232 | @type timing_max_dev: float
|
| 233 | @param phase_alpha: loop filter gain in phase loop
|
| 234 | @type phase_alphas: float
|
| 235 | @param verbose: Print information about modulator?
|
| 236 | @type verbose: bool
|
| 237 | @param debug: Print modualtion data to files?
|
| 238 | @type debug: bool
|
| 239 | """ |
| 240 | |
| 241 | gr.hier_block2.__init__(self, "generic_demod", |
| 242 | gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature |
| 243 | gr.io_signature(1, 1, gr.sizeof_char)) # Output signature |
| 244 | |
| 245 | self._constellation = constellation.base()
|
| 246 | self._samples_per_symbol = samples_per_symbol
|
| 247 | self._excess_bw = excess_bw
|
| 248 | self._phase_alpha = phase_alpha
|
| 249 | self._freq_alpha = freq_alpha
|
| 250 | self._freq_beta = 0.10*self._freq_alpha |
| 251 | self._timing_alpha = timing_alpha
|
| 252 | self._timing_beta = _def_timing_beta
|
| 253 | self._timing_max_dev=timing_max_dev
|
| 254 | self._differential = differential
|
| 255 | |
| 256 | if not isinstance(self._samples_per_symbol, int) or self._samples_per_symbol < 2: |
| 257 | raise TypeError, ("sbp must be an integer >= 2, is %d" % self._samples_per_symbol) |
| 258 | |
| 259 | arity = pow(2,self.bits_per_symbol()) |
| 260 | |
| 261 | # Automatic gain control
|
| 262 | self.agc = gr.agc2_cc(0.6e-1, 1e-3, 1, 1, 100) |
| 263 | |
| 264 | # Frequency correction
|
| 265 | self.freq_recov = gr.fll_band_edge_cc(self._samples_per_symbol, self._excess_bw, |
| 266 | 11*int(self._samples_per_symbol), |
| 267 | self._freq_alpha, self._freq_beta) |
| 268 | |
| 269 | # symbol timing recovery with RRC data filter
|
| 270 | nfilts = 32
|
| 271 | ntaps = 11 * int(self._samples_per_symbol*nfilts) |
| 272 | taps = gr.firdes.root_raised_cosine(nfilts, nfilts, |
| 273 | 1.0/float(self._samples_per_symbol), |
| 274 | self._excess_bw, ntaps)
|
| 275 | self.time_recov = gr.pfb_clock_sync_ccf(self._samples_per_symbol, |
| 276 | self._timing_alpha,
|
| 277 | taps, nfilts, nfilts/2, self._timing_max_dev) |
| 278 | self.time_recov.set_beta(self._timing_beta) |
| 279 | |
| 280 | #self._phase_beta = 0.25 * self._phase_alpha * self._phase_alpha
|
| 281 | self._phase_beta = 0.25 * self._phase_alpha * self._phase_alpha |
| 282 | fmin = -0.25
|
| 283 | fmax = 0.25
|
| 284 | |
| 285 | self.receiver = gr.constellation_receiver_cb(
|
| 286 | self._constellation,
|
| 287 | self._phase_alpha, self._phase_beta, |
| 288 | fmin, fmax) |
| 289 | |
| 290 | # Do differential decoding based on phase change of symbols
|
| 291 | if differential:
|
| 292 | self.diffdec = gr.diff_decoder_bb(arity)
|
| 293 | |
| 294 | if self._constellation.apply_pre_diff_code(): |
| 295 | self.symbol_mapper = gr.map_bb(
|
| 296 | mod_codes.invert_code(self._constellation.pre_diff_code()))
|
| 297 | |
| 298 | # unpack the k bit vector into a stream of bits
|
| 299 | self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbol()) |
| 300 | |
| 301 | if verbose:
|
| 302 | self._print_verbage()
|
| 303 | |
| 304 | if log:
|
| 305 | self._setup_logging()
|
| 306 | |
| 307 | # Connect and Initialize base class
|
| 308 | blocks = [self, self.agc, self.freq_recov, self.time_recov, self.receiver] |
| 309 | if differential:
|
| 310 | blocks.append(self.diffdec)
|
| 311 | if self._constellation.apply_pre_diff_code(): |
| 312 | blocks.append(self.symbol_mapper)
|
| 313 | blocks += [self.unpack, self] |
| 314 | self.connect(*blocks)
|
| 315 | |
| 316 | def samples_per_symbol(self): |
| 317 | return self._samples_per_symbol |
| 318 | |
| 319 | def bits_per_symbol(self): # staticmethod that's also callable on an instance |
| 320 | return self._constellation.bits_per_symbol() |
| 321 | |
| 322 | def _print_verbage(self): |
| 323 | print "\nDemodulator:" |
| 324 | print "bits per symbol: %d" % self.bits_per_symbol() |
| 325 | print "RRC roll-off factor: %.2f" % self._excess_bw |
| 326 | print "FLL gain: %.2e" % self._freq_alpha |
| 327 | print "Timing alpha gain: %.2e" % self._timing_alpha |
| 328 | print "Timing beta gain: %.2e" % self._timing_beta |
| 329 | print "Timing max dev: %.2f" % self._timing_max_dev |
| 330 | print "Phase track alpha: %.2e" % self._phase_alpha |
| 331 | print "Phase track beta: %.2e" % self._phase_beta |
| 332 | |
| 333 | def _setup_logging(self): |
| 334 | print "Modulation logging turned on." |
| 335 | self.connect(self.agc, |
| 336 | gr.file_sink(gr.sizeof_gr_complex, "rx_agc.dat"))
|
| 337 | self.connect((self.freq_recov, 0), |
| 338 | gr.file_sink(gr.sizeof_gr_complex, "rx_freq_recov.dat"))
|
| 339 | self.connect((self.freq_recov, 1), |
| 340 | gr.file_sink(gr.sizeof_float, "rx_freq_recov_freq.dat"))
|
| 341 | self.connect((self.freq_recov, 2), |
| 342 | gr.file_sink(gr.sizeof_float, "rx_freq_recov_phase.dat"))
|
| 343 | self.connect((self.freq_recov, 3), |
| 344 | gr.file_sink(gr.sizeof_gr_complex, "rx_freq_recov_error.dat"))
|
| 345 | self.connect((self.time_recov, 0), |
| 346 | gr.file_sink(gr.sizeof_gr_complex, "rx_time_recov.dat"))
|
| 347 | self.connect((self.time_recov, 1), |
| 348 | gr.file_sink(gr.sizeof_float, "rx_time_recov_error.dat"))
|
| 349 | self.connect((self.time_recov, 2), |
| 350 | gr.file_sink(gr.sizeof_float, "rx_time_recov_rate.dat"))
|
| 351 | self.connect((self.time_recov, 3), |
| 352 | gr.file_sink(gr.sizeof_float, "rx_time_recov_phase.dat"))
|
| 353 | self.connect((self.receiver, 0), |
| 354 | gr.file_sink(gr.sizeof_char, "rx_receiver.dat"))
|
| 355 | self.connect((self.receiver, 1), |
| 356 | gr.file_sink(gr.sizeof_float, "rx_receiver_error.dat"))
|
| 357 | self.connect((self.receiver, 2), |
| 358 | gr.file_sink(gr.sizeof_float, "rx_receiver_phase.dat"))
|
| 359 | self.connect((self.receiver, 3), |
| 360 | gr.file_sink(gr.sizeof_float, "rx_receiver_freq.dat"))
|
| 361 | if self._differential: |
| 362 | self.connect(self.diffdec, |
| 363 | gr.file_sink(gr.sizeof_char, "rx_diffdec.dat"))
|
| 364 | if self._constellation.apply_pre_diff_code(): |
| 365 | self.connect(self.symbol_mapper, |
| 366 | gr.file_sink(gr.sizeof_char, "rx_symbol_mapper.dat"))
|
| 367 | self.connect(self.unpack, |
| 368 | gr.file_sink(gr.sizeof_char, "rx_unpack.dat"))
|
| 369 | |
| 370 | def add_options(parser): |
| 371 | """
|
| 372 | Adds generic demodulation options to the standard parser
|
| 373 | """ |
| 374 | # Add options shared with modulator.
|
| 375 | add_common_options(parser) |
| 376 | # Add options specific to demodulator.
|
| 377 | parser.add_option("", "--freq-alpha", type="float", default=_def_freq_alpha, |
| 378 | help="set frequency lock loop alpha gain value [default=%default]")
|
| 379 | parser.add_option("", "--phase-alpha", type="float", default=_def_phase_alpha, |
| 380 | help="set phase tracking loop alpha value [default=%default]")
|
| 381 | parser.add_option("", "--timing-alpha", type="float", default=_def_timing_alpha, |
| 382 | help="set timing symbol sync loop gain alpha value [default=%default]")
|
| 383 | parser.add_option("", "--timing-beta", type="float", default=_def_timing_beta, |
| 384 | help="set timing symbol sync loop gain beta value [default=%default]")
|
| 385 | parser.add_option("", "--timing-max-dev", type="float", default=_def_timing_max_dev, |
| 386 | help="set timing symbol sync loop maximum deviation [default=%default]")
|
| 387 | add_options=staticmethod(add_options)
|
| 388 | |
| 389 | def extract_kwargs_from_options(cls, options): |
| 390 | """
|
| 391 | Given command line options, create dictionary suitable for passing to __init__
|
| 392 | """ |
| 393 | return extract_kwargs_from_options_for_class(cls, options)
|
| 394 | extract_kwargs_from_options=classmethod(extract_kwargs_from_options)
|
| 395 |