Imported Upstream version 3.2.2
[debian/gnuradio] / gr-trellis / src / examples / fsm_utils.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 Free Software Foundation, Inc.
4 #
5 # This file is part of GNU Radio
6 #
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11 #
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21 #
22
23
24 import re
25 import math
26 import sys
27 import operator
28 import numpy
29 import scipy.linalg
30
31 from gnuradio import trellis
32
33
34
35 ######################################################################
36 # Decimal to any base conversion.
37 # Convert 'num' to a list of 'l' numbers representing 'num'
38 # to base 'base' (most significant symbol first).
39 ######################################################################
40 def dec2base(num,base,l):
41     s=range(l)
42     n=num
43     for i in range(l):
44         s[l-i-1]=n%base
45         n=int(n/base)
46     if n!=0:
47         print 'Number ', num, ' requires more than ', l, 'digits.'
48     return s
49
50
51 ######################################################################
52 # Conversion from any base to decimal.
53 # Convert a list 's' of symbols to a decimal number
54 # (most significant symbol first)
55 ######################################################################
56 def base2dec(s,base):
57     num=0
58     for i in range(len(s)):
59         num=num*base+s[i]
60     return num
61
62
63
64
65 ######################################################################
66 # Automatically generate the lookup table that maps the FSM outputs
67 # to channel inputs corresponding to a channel 'channel' and a modulation
68 # 'mod'. Optional normalization of channel to unit energy.
69 # This table is used by the 'metrics' block to translate
70 # channel outputs to metrics for use with the Viterbi algorithm. 
71 # Limitations: currently supports only one-dimensional modulations.
72 ######################################################################
73 def make_isi_lookup(mod,channel,normalize):
74     dim=mod[0]
75     constellation = mod[1]
76
77     if normalize:
78         p = 0
79         for i in range(len(channel)):
80             p = p + channel[i]**2
81         for i in range(len(channel)):
82             channel[i] = channel[i]/math.sqrt(p)
83
84     lookup=range(len(constellation)**len(channel))
85     for o in range(len(constellation)**len(channel)):
86         ss=dec2base(o,len(constellation),len(channel))
87         ll=0
88         for i in range(len(channel)):
89             ll=ll+constellation[ss[i]]*channel[i]
90         lookup[o]=ll
91     return (1,lookup)
92
93
94
95
96
97
98 ######################################################################
99 # Automatically generate the signals appropriate for CPM
100 # decomposition. 
101 # This decomposition is based on the paper by B. Rimoldi
102 # "A decomposition approach to CPM", IEEE Trans. Info Theory, March 1988
103 # See also my own notes at http://www.eecs.umich.edu/~anastas/docs/cpm.pdf
104 ######################################################################
105 def make_cpm_signals(K,P,M,L,q,frac):
106
107     Q=numpy.size(q)/L
108     h=(1.0*K)/P
109     f0=-h*(M-1)/2
110     dt=0.0; # maybe start at t=0.5
111     t=(dt+numpy.arange(0,Q))/Q
112     qq=numpy.zeros(Q)
113     for m in range(L):
114        qq=qq + q[m*Q:m*Q+Q]
115     w=math.pi*h*(M-1)*t-2*math.pi*h*(M-1)*qq+math.pi*h*(L-1)*(M-1)
116     
117     X=(M**L)*P
118     PSI=numpy.empty((X,Q))
119     for x in range(X):
120        xv=dec2base(x/P,M,L)
121        xv=numpy.append(xv, x%P)
122        qq1=numpy.zeros(Q)
123        for m in range(L):
124           qq1=qq1+xv[m]*q[m*Q:m*Q+Q]
125        psi=2*math.pi*h*xv[-1]+4*math.pi*h*qq1+w
126        #print psi
127        PSI[x]=psi
128     PSI = numpy.transpose(PSI)
129     SS=numpy.exp(1j*PSI) # contains all signals as columns
130     #print SS
131    
132
133     # Now we need to orthogonalize the signals 
134     F = scipy.linalg.orth(SS) # find an orthonormal basis for SS
135     #print numpy.dot(numpy.transpose(F.conjugate()),F) # check for orthonormality
136     S = numpy.dot(numpy.transpose(F.conjugate()),SS)
137     #print F
138     #print S
139
140     # We only want to keep those dimensions that contain most
141     # of the energy of the overall constellation (eg, frac=0.9 ==> 90%)
142     # evaluate mean energy in each dimension
143     E=numpy.sum(numpy.absolute(S)**2,axis=1)/Q
144     E=E/numpy.sum(E)
145     #print E
146     Es = -numpy.sort(-E)
147     Esi = numpy.argsort(-E)
148     #print Es
149     #print Esi
150     Ecum=numpy.cumsum(Es)
151     #print Ecum
152     v0=numpy.searchsorted(Ecum,frac)
153     N = v0+1
154     #print v0
155     #print Esi[0:v0+1]
156     Ff=numpy.transpose(numpy.transpose(F)[Esi[0:v0+1]])
157     #print Ff
158     Sf = S[Esi[0:v0+1]]
159     #print Sf
160     
161
162     return (f0,SS,S,F,Sf,Ff,N)
163     #return f0
164     
165
166
167
168 ######################################################################
169 # A list of common modulations.
170 # Format: (dimensionality,constellation)
171 ######################################################################
172 pam2 = (1,[-1, 1])
173 pam4 = (1,[-3, -1, 3, 1])               # includes Gray mapping
174 pam8 = (1,[-7, -5, -3, -1, 1, 3, 5, 7])
175
176 psk4=(2,[1, 0, \
177          0, 1, \
178          0, -1,\
179         -1, 0])                         # includes Gray mapping
180 psk8=(2,[math.cos(2*math.pi*0/8), math.sin(2*math.pi*0/8),  \
181          math.cos(2*math.pi*1/8), math.sin(2*math.pi*1/8),  \
182          math.cos(2*math.pi*2/8), math.sin(2*math.pi*2/8),  \
183          math.cos(2*math.pi*3/8), math.sin(2*math.pi*3/8),  \
184          math.cos(2*math.pi*4/8), math.sin(2*math.pi*4/8),  \
185          math.cos(2*math.pi*5/8), math.sin(2*math.pi*5/8),  \
186          math.cos(2*math.pi*6/8), math.sin(2*math.pi*6/8),  \
187          math.cos(2*math.pi*7/8), math.sin(2*math.pi*7/8)])
188
189 orth2 = (2,[1, 0, \
190             0, 1])
191 orth4=(4,[1, 0, 0, 0, \
192           0, 1, 0, 0, \
193           0, 0, 1, 0, \
194           0, 0, 0, 1])
195
196 ######################################################################
197 # A list of channels to be tested
198 ######################################################################
199
200 # C test channel (J. Proakis, Digital Communications, McGraw-Hill Inc., 2001)
201 c_channel = [0.227, 0.460, 0.688, 0.460, 0.227]
202
203
204
205
206
207
208
209
210
211
212 if __name__ == '__main__':
213     f1=trellis.fsm('fsm_files/awgn1o2_4.fsm')
214     #f2=trellis.fsm('fsm_files/awgn2o3_4.fsm')
215     #print f1.I(), f1.S(), f1.O()
216     #print f1.NS()
217     #print f1.OS()
218     #print f2.I(), f2.S(), f2.O()
219     #print f2.NS()
220     #print f2.OS()
221     ##f1.write_trellis_svg('f1.svg',4)
222     #f2.write_trellis_svg('f2.svg',4)
223     #f=fsm_concatenate(f1,f2)
224     #f=fsm_radix(f1,2)
225
226     #print "----------\n"
227     #print f.I(), f.S(), f.O()
228     #print f.NS()
229     #print f.OS()
230     #f.write_trellis_svg('f.svg',4)
231
232     q=numpy.arange(0,8)/(2.0*8)
233     (f0,SS,S,F,Sf,Ff,N) = make_cpm_signals(1,2,2,1,q,0.99)
234