SHARE
TWEET

generic_mod_demod_new v2

artynet Mar 11th, 2012 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Copyright 2005,2006,2007,2009,2011 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.digital.modulation_utils import extract_kwargs_from_options_for_class
  30. from gnuradio.digital.utils import mod_codes
  31. from gnuradio.digital import digital_swig,digital
  32. import math
  33.  
  34. ## default values (used in __init__ and add_options)
  35. #_def_samples_per_symbol = 2
  36. #_def_excess_bw = 0.35
  37. #_def_verbose = False
  38. #_def_log = False
  39. _def_ber_mode=False
  40. ## Frequency correction
  41. #_def_freq_bw = 2*math.pi/100.0
  42. ## Symbol timing recovery
  43. #_def_timing_bw = 2*math.pi/100.0
  44. #_def_timing_max_dev = 1.5
  45. ## Fine frequency / Phase correction
  46. #_def_phase_bw = 2*math.pi/100.0
  47. ## Number of points in constellation
  48. #_def_constellation_points = 16
  49. ## Whether differential coding is used.
  50. #_def_differential = False
  51.  
  52. def add_common_options(parser):
  53.     """
  54.    Sets options common to both modulator and demodulator.
  55.    """
  56.     parser.add_option("-p", "--constellation-points", type="int", default=_def_constellation_points,
  57.                       help="set the number of constellation points (must be a power of 2 for psk, power of 4 for QAM) [default=%default]")
  58.     parser.add_option("", "--non-differential", action="store_false",
  59.                       dest="differential",
  60.                       help="do not use differential encoding [default=False]")
  61.     parser.add_option("", "--differential", action="store_true",
  62.                       dest="differential", default=True,
  63.                       help="use differential encoding [default=%default]")
  64.     parser.add_option("", "--mod-code", type="choice", choices=mod_codes.codes,
  65.                       default=mod_codes.NO_CODE,
  66.                       help="Select modulation code from: %s [default=%%default]"
  67.                             % (', '.join(mod_codes.codes),))
  68.     parser.add_option("", "--excess-bw", type="float", default=_def_excess_bw,
  69.                       help="set RRC excess bandwith factor [default=%default]")
  70.    
  71.  
  72. # /////////////////////////////////////////////////////////////////////////////
  73. #                             Generic modulator
  74. # /////////////////////////////////////////////////////////////////////////////
  75.  
  76. class generic_mod(gr.hier_block2):
  77.  
  78.     def __init__(self, constellation,
  79.                  gray_coded=True,):
  80.                  #samples_per_symbol=_def_samples_per_symbol,
  81.                  #differential=_def_differential,
  82.                  #excess_bw=_def_excess_bw,
  83.                  #gray_coded=True,
  84.                  #verbose=_def_verbose,
  85.                  #log=_def_log
  86.                  #):
  87.         """
  88.         Hierarchical block for RRC-filtered differential generic modulation.
  89.  
  90.         The input is a byte stream (unsigned char) and the
  91.         output is the complex modulated signal at baseband.
  92.        
  93.         @param constellation: determines the modulation type
  94.         @type constellation: gnuradio.digital.gr_constellation
  95.         @param samples_per_symbol: samples per baud >= 2
  96.         @type samples_per_symbol: float
  97.         @param excess_bw: Root-raised cosine filter excess bandwidth
  98.         @type excess_bw: float
  99.        @param gray_coded: turn gray coding on/off
  100.        @type gray_coded: bool
  101.        @param verbose: Print information about modulator?
  102.        @type verbose: bool
  103.        @param log: Log modulation data to files?
  104.        @type log: bool
  105.         """
  106.  
  107.         gr.hier_block2.__init__(self, "generic_mod",
  108.                                 gr.io_signature(1, 1, gr.sizeof_char),       # Input signature
  109.                                 gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
  110.  
  111.         self._constellation = constellation.base()
  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 gray_coded == True:
  120.             #self.symbol_mapper = gr.map_bb(self._constellation.pre_diff_code())
  121.        
  122.         self.symbol_mapper = gr.map_bb([0,1,3,2])
  123.        
  124.         digital_swig.constellation_receiver_cb
  125.        
  126.  
  127.  
  128.  
  129.  
  130.         self.chunks2symbols = gr.chunks_to_symbols_bc(self._constellation.points())
  131.  
  132.  
  133.         # Connect
  134.         blocks = [self, self.bytes2chunks]
  135.         if gray_coded == True:
  136.             blocks.append(self.symbol_mapper)
  137.         #if differential:
  138.             #blocks.append(self.diffenc)
  139.         blocks += [self.chunks2symbols, self]
  140.         self.connect(*blocks)
  141.  
  142.  
  143.  
  144.     def bits_per_symbol(self):   # static method that's also callable on an instance
  145.         return self._constellation.bits_per_symbol()
  146.  
  147.  
  148.              
  149.  
  150. # /////////////////////////////////////////////////////////////////////////////
  151. #                             Generic demodulator
  152. #
  153. #      Differentially coherent detection of differentially encoded generically
  154. #      modulated signal.
  155. # /////////////////////////////////////////////////////////////////////////////
  156.  
  157. class generic_demod(gr.hier_block2):
  158.  
  159.     def __init__(self, constellation,
  160.                  gray_coded=True,
  161.                  ber_mode=_def_ber_mode):
  162.                  #samples_per_symbol=_def_samples_per_symbol,
  163.                  #differential=_def_differential,
  164.                  #excess_bw=_def_excess_bw,
  165.                  #gray_coded=True,
  166.                  #freq_bw=_def_freq_bw,
  167.                  #timing_bw=_def_timing_bw,
  168.                  #phase_bw=_def_phase_bw,
  169.                  #verbose=_def_verbose,
  170.                  #log=_def_log):
  171.         """
  172.         Hierarchical block for RRC-filtered differential generic demodulation.
  173.        generic_demod
  174.        
  175.         The input is the complex modulated signal at baseband.
  176.         The output is a stream of bits packed 1 bit per byte (LSB)
  177.  
  178.         @param constellation: determines the modulat:
  179.            #self.diffdec = gr.diff_decoder_bb(arity)
  180.  
  181.        if gray_coded == True:
  182.            #self.symbol_mapper = gr.map_bb(
  183.                #mod_codes.invert_code(self._constellation.pre_diff_code()))
  184.             self.symbol_mapper = gr.map_bb([0,1,3,2])
  185.  
  186.        # unpack the k bit vector into a stream of bits
  187.        self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbolion type
  188.         @type constellation: gnuradio.digital.gr_constellation
  189.         @param samples_per_symbol: samples per symbol >= 2
  190.         @type samples_per_symbol: float
  191.         @param excess_bw: Root-raised cosine filter excess bandwidth
  192.         @type excess_bw: float
  193.        @param gray_coded: turn gray coding on/off
  194.        @type gray_coded: bool
  195.        @param freq_bw: loop filter lock-in bandwidth
  196.        @type freq_bw: float
  197.        @param timing_bw: timing recovery loop lock-in bandwidth
  198.        @type timing_bw: float
  199.        @param phase_bw: phase recovery loop bandwidth
  200.        @type phase_bw: float
  201.        @param verbose: Print information about modulator?
  202.        @type verbose: bool
  203.        @param debug: Print modualtion data to files?
  204.        @type debug: bool
  205.         """
  206.        
  207.         gr.hier_block2.__init__(self, "generic_demod",
  208.                                 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
  209.                                 gr.io_signature(1, 1, gr.sizeof_char))       # Output signature
  210.                                
  211.         self._constellation = constellation.base()
  212.         #self._samples_per_symbol = samples_per_symbol
  213.         #self._excess_bw = excess_bw
  214.         #self._phase_bw = phase_bw
  215.         #self._freq_bw = freq_bw
  216.         #self._timing_bw = timing_bw
  217.         #self._timing_max_dev= _def_timing_max_dev
  218.         #self._differential = differential
  219.  
  220.  
  221.         arity = pow(2,self.bits_per_symbol())
  222.  
  223.  
  224.         # unpack the k bit:
  225.             #self.diffdec = gr.diff_decoder_bb(arity)
  226.  
  227.         if gray_coded:
  228.             self.symbol_mapper = gr.map_bb(mod_codes.invert_code(self._constellation.pre_diff_code()))
  229.            
  230.         self.symbol_mapper_points = gr.map_bb(range(arity))
  231.         #fmin = 0.25
  232.         #fmax = 0.25
  233.         #phase_bw = 2*math.pi/100.0
  234.            
  235.         #self.receiver = digital_swig.constellation_receiver_cb(
  236.             #self._constellation, phase_bw,
  237.             #fmin, fmax)
  238.  
  239.        
  240.         self.receiver2 = digital_swig.constellation_decoder_cb(self._constellation)
  241.  
  242.         # unpack the k bit vector into a stream of bits
  243.         self.unpack = gr.unpack_k_bits_bb(self.bits_per_symbol()) #ber mode
  244.  
  245.    
  246.                  
  247.         blocks = [self, self.receiver2]
  248.         #if differential:
  249.             #blocks.append(self.diffdec)
  250.         if self._constellation.apply_pre_diff_code():
  251.             blocks.append(self.symbol_mapper)
  252.         if ber_mode :
  253.             blocks.append(self.unpack)
  254.         blocks += [self]
  255.         self.connect(*blocks)
  256.  
  257.     def samples_per_symbol(self):
  258.         return self._samples_per_symbol
  259.  
  260.     def bits_per_symbol(self):   # staticmethod that's also callable on an instance
  261.         return self._constellation.bits_per_symbol()
  262.  
  263.     #def _print_verbage(self):
  264.         #print "\nDemodulator:"
  265.         #print "bits per symbol:     %d"   % self.bits_per_symbol()
  266.         #print "RRC roll-off factor: %.2f" % self._excess_bw
  267.         #print "FLL bandwidth:       %.2e" % self._freq_bw
  268.         #print "Timing bandwidth:    %.2e" % self._timing_bw
  269.         #print "Phase bandwidth:     %.2e" % self._phase_bw
  270.  
  271.     #def _setup_logging(self):
  272.         #print "Modulation logging turned on."
  273.         #self.connect(self.agc,
  274.                      #gr.file_sink(gr.sizeof_gr_complex, "rx_agc.32fc"))
  275.         #self.connect((self.freq_recov, 0),
  276.                      #gr.file_sink(gr.sizeof_gr_complex, "rx_freq_recov.32fc"))
  277.         #self.connect((self.freq_recov, 1),
  278.                      #gr.file_sink(gr.sizeof_float, "rx_freq_recov_freq.32f"))
  279.         #self.connect((self.freq_recov, 2),
  280.                      #gr.file_sink(gr.sizeof_float, "rx_freq_recov_phase.32f"))
  281.         #self.connect((self.freq_recov, 3),
  282.                      #gr.file_sink(gr.sizeof_float, "rx_freq_recov_error.32f"))
  283.         #self.connect((self.time_recov, 0),
  284.                      #gr.file_sink(gr.sizeof_gr_complex, "rx_time_recov.32fc"))
  285.         #self.connect((self.time_recov, 1),
  286.                      #gr.file_sink(gr.sizeof_float, "rx_time_recov_error.32f"))
  287.         #self.connect((self.time_recov, 2),
  288.                      #gr.file_sink(gr.sizeof_float, "rx_time_recov_rate.32f"))
  289.         #self.connect((self.time_recov, 3),
  290.                      #gr.file_sink(gr.sizeof_float, "rx_time_recov_phase.32f"))
  291.         #self.connect((self.receiver, 0),
  292.                      #gr.file_sink(gr.sizeof_char, "rx_receiver.8b"))
  293.         #self.connect((self.receiver, 1),
  294.                      #gr.file_sink(gr.sizeof_float, "rx_receiver_error.32f"))
  295.         #self.connect((self.receiver, 2),
  296.                      #gr.file_sink(gr.sizeof_float, "rx_receiver_phase.32f"))
  297.         #self.connect((self.receiver, 3),
  298.                      #gr.file_sink(gr.sizeof_float, "rx_receiver_freq.32f"))
  299.         #if self._differential:
  300.             #self.connect(self.diffdec,
  301.                          #gr.file_sink(gr.sizeof_char, "rx_diffdec.8b"))
  302.         #if self._constellation.apply_pre_diff_code():
  303.             #self.connect(self.symbol_mapper,
  304.                          #gr.file_sink(gr.sizeof_char, "rx_symbol_mapper.8b"))
  305.         #self.connect(self.unpack,
  306.                      #gr.file_sink(gr.sizeof_char, "rx_unpack.8b"))
  307.        
  308.     #def add_options(parser):
  309.         #"""
  310.         #Adds generic demodulation options to the standard parser
  311.         #"""
  312.         ## Add options shared with modulator.
  313.         #add_common_options(parser)
  314.         ## Add options specific to demodulator.
  315.         #parser.add_option("", "--freq-bw", type="float", default=_def_freq_bw,
  316.                           #help="set frequency lock loop lock-in bandwidth [default=%default]")
  317.         #parser.add_option("", "--phase-bw", type="float", default=_def_phase_bw,
  318.                           #help="set phase tracking loop lock-in bandwidth [default=%default]")
  319.         #parser.add_option("", "--timing-bw", type="float", default=_def_timing_bw,
  320.                           #help="set timing symbol sync loop gain lock-in bandwidth [default=%default]")
  321.     #add_options=staticmethod(add_options)
  322.    
  323.     #def extract_kwargs_from_options(cls, options):
  324.         #"""
  325.         #Given command line options, create dictionary suitable for passing to __init__
  326.         #"""
  327.         #return extract_kwargs_from_options_for_class(cls, options)
  328.     #extract_kwargs_from_options=classmethod(extract_kwargs_from_options)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top