SHARE
TWEET

Untitled

a guest Mar 8th, 2012 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Copyright 2005,2006,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. """
  23. PSK modulation and demodulation.
  24. """
  25.  
  26. from math import pi, log, sqrt
  27. from cmath import exp
  28.  
  29. from gnuradio.digital import digital_swig
  30. from gnuradio.digital import modulation_utils
  31. from gnuradio.digital.utils import mod_codes, gray_code
  32. from mod_new.generic_mod_demod_new import generic_mod, generic_demod
  33.  
  34. # Default number of points in constellation.
  35. _def_constellation_points = 4
  36. # The default encoding (e.g. gray-code, set-partition)
  37. _def_mod_code = mod_codes.GRAY_CODE
  38.  
  39. def create_encodings(mod_code, arity):
  40.     post_diff_code = None
  41.     if mod_code not in mod_codes.codes:
  42.         raise ValueError('That modulation code does not exist.')
  43.     if mod_code == mod_codes.GRAY_CODE:
  44.         pre_diff_code = gray_code.gray_code(arity)
  45.     elif mod_code == mod_codes.SET_PARTITION_CODE:
  46.         pre_diff_code = set_partition_code.set_partition_code(arity)
  47.     elif mod_code == mod_codes.NO_CODE:
  48.         pre_diff_code = []
  49.     else:
  50.         raise ValueError('That modulation code is not implemented for this constellation.')
  51.     return (pre_diff_code, post_diff_code)
  52.    
  53. # /////////////////////////////////////////////////////////////////////////////
  54. #                           PSK constellation
  55. # /////////////////////////////////////////////////////////////////////////////
  56.  
  57. def psk_constellation(m=_def_constellation_points, mod_code=_def_mod_code):
  58.     """
  59.    Creates a PSK constellation object.
  60.    """
  61.     k = log(m) / log(2.0)
  62.     if (k != int(k)):
  63.         raise StandardError('Number of constellation points must be a power of two.')
  64.     if m==2:
  65.         points = [-1,1]
  66.     elif m==4:
  67.         points = [(2.0/sqrt(2.0))*exp(2*pi*(0+1j)*i/m+(pi/m)*(0+1j)) for i in range(0,m)]
  68.     #points = [0.707+0.707j,-0.707+0.707j,-0.707-0.707j,0.707-0.707j]
  69.     #points = [1+1j,-1+1j,-1-1j,1-1j]
  70.     pre_diff_code, post_diff_code = create_encodings(mod_code, m)
  71.     if post_diff_code is not None:
  72.         inverse_post_diff_code = mod_codes.invert_code(post_diff_code)
  73.         points = [points[x] for x in inverse_post_diff_code]
  74.     constellation = digital_swig.constellation_psk(points, pre_diff_code, m)
  75.     return constellation
  76.  
  77. # /////////////////////////////////////////////////////////////////////////////
  78. #                           PSK modulator
  79. # /////////////////////////////////////////////////////////////////////////////
  80.  
  81. class psk_mod(generic_mod):
  82.  
  83.     def __init__(self, constellation_points=_def_constellation_points,
  84.                  mod_code=_def_mod_code,
  85.                  *args, **kwargs):
  86.  
  87.         """
  88.         Hierarchical block for RRC-filtered PSK modulation.
  89.  
  90.         The input is a byte stream (unsigned char) and the
  91.         output is the complex modulated signal at baseband.
  92.  
  93.        See generic_mod block for list of parameters.
  94.         """
  95.  
  96.         constellation = psk_constellation(constellation_points, mod_code)
  97.         super(psk_mod, self).__init__(constellation, *args, **kwargs)
  98.  
  99. # /////////////////////////////////////////////////////////////////////////////
  100. #                           PSK demodulator
  101. #
  102. # /////////////////////////////////////////////////////////////////////////////
  103.  
  104. class psk_demod(generic_demod):
  105.  
  106.     def __init__(self, constellation_points=_def_constellation_points,
  107.                  mod_code=_def_mod_code,
  108.                  *args, **kwargs):
  109.  
  110.         """
  111.         Hierarchical block for RRC-filtered PSK modulation.
  112.  
  113.         The input is a byte stream (unsigned char) and the
  114.         output is the complex modulated signal at baseband.
  115.  
  116.        See generic_demod block for list of parameters.
  117.        """
  118.  
  119.         constellation = psk_constellation(constellation_points, mod_code)
  120.         super(psk_demod, self).__init__(constellation, *args, **kwargs)
  121.  
  122. #
  123. # Add these to the mod/demod registry
  124. #
  125. modulation_utils.add_type_1_mod('psk', psk_mod)
  126. modulation_utils.add_type_1_demod('psk', psk_demod)
  127. modulation_utils.add_type_1_constellation('psk', psk_constellation)
  128.  
  129. #print psk_constellation(2)
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