Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blks2impl / qam.py
1 #
2 # Copyright 2005,2006 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 from math import pi, sqrt
23 import math
24
25 # These constellations are generated for Gray coding when symbos [1, ..., m] are used
26 # Mapping to Gray coding is therefore unnecessary
27
28 def make_constellation(m):
29     # number of bits/symbol (log2(M))
30     k = int(math.log10(m) / math.log10(2.0))
31
32     coeff = 1
33     const_map = []
34     for i in range(m):
35         a = (i&(0x01 << k-1)) >> k-1
36         b = (i&(0x01 << k-2)) >> k-2
37         bits_i = [((i&(0x01 << k-j-1)) >> k-j-1) for j in range(2, k, 2)]
38         bits_q = [((i&(0x01 << k-j-1)) >> k-j-1) for j in range(3, k, 2)]
39
40         ss = 0
41         ll = len(bits_i)
42         for ii in range(ll):
43             rr = 0
44             for jj in range(ll-ii):
45                 rr = abs(bits_i[jj] - rr)
46             ss += rr*pow(2.0, ii+1)
47         re = (2*a-1)*(ss+1)
48         
49         ss = 0
50         ll = len(bits_q)
51         for ii in range(ll):
52             rr = 0
53             for jj in range(ll-ii):
54                 rr = abs(bits_q[jj] - rr)
55             ss += rr*pow(2.0, ii+1)
56         im = (2*b-1)*(ss+1)
57
58         a = max(re, im)
59         if a > coeff:
60             coeff = a
61         const_map.append(complex(re, im))
62
63     norm_map = [complex(i.real/coeff, i.imag/coeff) for i in const_map]
64     return norm_map
65         
66 # Common definition of constellations for Tx and Rx
67 constellation = {
68     4 :  make_constellation(4),           # QAM4 (QPSK)
69     8 :  make_constellation(8),           # QAM8
70     16:  make_constellation(16),          # QAM16
71     64:  make_constellation(64),          # QAM64
72     256: make_constellation(256)          # QAM256
73     }
74
75 # -----------------------
76 # Do Gray code
77 # -----------------------
78 # binary to gray coding
79 binary_to_gray = {
80     4 : range(4),
81     8 : range(8),
82     16: range(16),
83     64: range(64),
84     256: range(256)
85     }
86    
87 # gray to binary
88 gray_to_binary = {
89     4 : range(4),
90     8 : range(8),
91     16: range(16),
92     64: range(64),
93     256: range(256)
94     }
95
96 # -----------------------
97 # Don't Gray code
98 # -----------------------
99 # identity mapping
100 binary_to_ungray = {
101     4 : range(4),
102     8 : range(8),
103     16: range(16),
104     64: range(64)
105     }
106     
107 # identity mapping
108 ungray_to_binary = {
109     4 : range(4),
110     8 : range(8),
111     16: range(16),
112     64: range(64)
113     }