merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / psk.py
index fdb6c9e6933f9b4a0e5ebf2350b95a0787e44dae..58677b29d0dbe2b6dbd281d2ad80cc9dea02bf05 100644 (file)
 # Boston, MA 02110-1301, USA.
 # 
 
-from math import pi, sqrt
-import cmath
+from math import pi, sqrt, log10
+import math, cmath
 
+# The following algorithm generates Gray coded constellations for M-PSK for M=[2,4,8]
+def make_gray_constellation(m):
+    # number of bits/symbol (log2(M))
+    k = int(log10(m) / log10(2.0))
+
+    coeff = 1
+    const_map = []
+    bits = [0]*3
+    for i in range(m):
+        # get a vector of the k bits to use in this mapping
+        bits[3-k:3] = [((i&(0x01 << k-j-1)) >> k-j-1) for j in range(k)]
+
+        theta = -(2*bits[0]-1)*(2*pi/m)*(bits[0]+abs(bits[1]-bits[2])+2*bits[1])
+        re = math.cos(theta)
+        im = math.sin(theta)
+        const_map.append(complex(re, im))   # plug it into the constellation
+    
+    # return the constellation; by default, it is normalized
+    return const_map
+
+# This makes a constellation that increments around the unit circle
 def make_constellation(m):
     return [cmath.exp(i * 2 * pi / m * 1j) for i in range(m)]
-        
+
 # Common definition of constellations for Tx and Rx
 constellation = {
     2 : make_constellation(2),           # BPSK
@@ -35,18 +56,18 @@ constellation = {
 # -----------------------
 # Do Gray code
 # -----------------------
-# binary to gray coding
+# binary to gray coding -- constellation does Gray coding
 binary_to_gray = {
-    2 : (0, 1),
-    4 : (0, 1, 3, 2),
-    8 : (0, 1, 3, 2, 7, 6, 4, 5)
+    2 : range(2),
+    4 : [0,1,3,2],
+    8 : [0, 1, 3, 2, 7, 6, 4, 5]
     }
-   
+
 # gray to binary
 gray_to_binary = {
-    2 : (0, 1),
-    4 : (0, 1, 3, 2),
-    8 : (0, 1, 3, 2, 6, 7, 5, 4)
+    2 : range(2),
+    4 : [0,1,3,2],
+    8 : [0, 1, 3, 2, 6, 7, 5, 4]
     }
 
 # -----------------------
@@ -54,14 +75,14 @@ gray_to_binary = {
 # -----------------------
 # identity mapping
 binary_to_ungray = {
-    2 : (0, 1),
-    4 : (0, 1, 2, 3),
-    8 : (0, 1, 2, 3, 4, 5, 6, 7)
+    2 : range(2),
+    4 : range(4),
+    8 : range(8)
     }
-    
+
 # identity mapping
 ungray_to_binary = {
-    2 : (0, 1),
-    4 : (0, 1, 2, 3),
-    8 : (0, 1, 2, 3, 4, 5, 6, 7)
+    2 : range(2),
+    4 : range(4),
+    8 : range(8)
     }