3 # Copyright 2004 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
31 from gnuradio import trellis
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):
47 print 'Number ', num, ' requires more than ', l, 'digits.'
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 ######################################################################
58 for i in range(len(s)):
64 ######################################################################
65 # Generate a new FSM representing n stages through the original FSM
66 ######################################################################
73 for s in range(f.S()):
75 ii=dec2base(i,f.I(),n)
79 oo[k]=f.OS()[ns*f.I()+ii[k]]
80 ns=f.NS()[ns*f.I()+ii[k]]
83 osm[s*I+i]=base2dec(oo,f.O())
86 f=trellis.fsm(I,S,O,nsm,osm)
92 ######################################################################
93 # Automatically generate the lookup table that maps the FSM outputs
94 # to channel inputs corresponding to a channel 'channel' and a modulation
95 # 'mod'. Optional normalization of channel to unit energy.
96 # This table is used by the 'metrics' block to translate
97 # channel outputs to metrics for use with the Viterbi algorithm.
98 # Limitations: currently supports only one-dimensional modulations.
99 ######################################################################
100 def make_isi_lookup(mod,channel,normalize):
102 constellation = mod[1]
106 for i in range(len(channel)):
107 p = p + channel[i]**2
108 for i in range(len(channel)):
109 channel[i] = channel[i]/math.sqrt(p)
111 lookup=range(len(constellation)**len(channel))
112 for o in range(len(constellation)**len(channel)):
113 ss=dec2base(o,len(constellation),len(channel))
115 for i in range(len(channel)):
116 ll=ll+constellation[ss[i]]*channel[i]
125 ######################################################################
126 # Automatically generate the signals appropriate for CPM
128 # This decomposition is based on the paper by B. Rimoldi
129 # "A decomposition approach to CPM", IEEE Trans. Info Theory, March 1988
130 # See also my own notes at http://www.eecs.umich.edu/~anastas/docs/cpm.pdf
131 ######################################################################
132 def make_cpm_signals(K,P,M,L,q,frac):
137 dt=0.0; # maybe start at t=0.5
138 t=(dt+numpy.arange(0,Q))/Q
142 w=math.pi*h*(M-1)*t-2*math.pi*h*(M-1)*qq+math.pi*h*(L-1)*(M-1)
145 PSI=numpy.empty((X,Q))
148 xv=numpy.append(xv, x%P)
151 qq1=qq1+xv[m]*q[m*Q:m*Q+Q]
152 psi=2*math.pi*h*xv[-1]+4*math.pi*h*qq1+w
155 PSI = numpy.transpose(PSI)
156 SS=numpy.exp(1j*PSI) # contains all signals as columns
160 # Now we need to orthogonalize the signals
161 F = scipy.linalg.orth(SS) # find an orthonormal basis for SS
162 #print numpy.dot(numpy.transpose(F.conjugate()),F) # check for orthonormality
163 S = numpy.dot(numpy.transpose(F.conjugate()),SS)
167 # We only want to keep those dimensions that contain most
168 # of the energy of the overall constellation (eg, frac=0.9 ==> 90%)
169 # evaluate mean energy in each dimension
170 E=numpy.sum(numpy.absolute(S)**2,axis=1)/Q
174 Esi = numpy.argsort(-E)
177 Ecum=numpy.cumsum(Es)
179 v0=numpy.searchsorted(Ecum,frac)
183 Ff=numpy.transpose(numpy.transpose(F)[Esi[0:v0+1]])
189 return (f0,SS,S,F,Sf,Ff,N)
195 ######################################################################
196 # A list of common modulations.
197 # Format: (dimensionality,constellation)
198 ######################################################################
200 pam4 = (1,[-3, -1, 3, 1]) # includes Gray mapping
201 pam8 = (1,[-7, -5, -3, -1, 1, 3, 5, 7])
206 -1, 0]) # includes Gray mapping
207 psk8=(2,[math.cos(2*math.pi*0/8), math.sin(2*math.pi*0/8), \
208 math.cos(2*math.pi*1/8), math.sin(2*math.pi*1/8), \
209 math.cos(2*math.pi*2/8), math.sin(2*math.pi*2/8), \
210 math.cos(2*math.pi*3/8), math.sin(2*math.pi*3/8), \
211 math.cos(2*math.pi*4/8), math.sin(2*math.pi*4/8), \
212 math.cos(2*math.pi*5/8), math.sin(2*math.pi*5/8), \
213 math.cos(2*math.pi*6/8), math.sin(2*math.pi*6/8), \
214 math.cos(2*math.pi*7/8), math.sin(2*math.pi*7/8)])
218 orth4=(4,[1, 0, 0, 0, \
223 ######################################################################
224 # A list of channels to be tested
225 ######################################################################
227 # C test channel (J. Proakis, Digital Communications, McGraw-Hill Inc., 2001)
228 c_channel = [0.227, 0.460, 0.688, 0.460, 0.227]
239 if __name__ == '__main__':
240 f1=trellis.fsm('fsm_files/awgn1o2_4.fsm')
241 #f2=trellis.fsm('fsm_files/awgn2o3_4.fsm')
242 #print f1.I(), f1.S(), f1.O()
245 #print f2.I(), f2.S(), f2.O()
248 ##f1.write_trellis_svg('f1.svg',4)
249 #f2.write_trellis_svg('f2.svg',4)
250 #f=fsm_concatenate(f1,f2)
253 #print "----------\n"
254 #print f.I(), f.S(), f.O()
257 #f.write_trellis_svg('f.svg',4)
259 q=numpy.arange(0,8)/(2.0*8)
260 (f0,SS,S,F,Sf,Ff,N) = make_cpm_signals(1,2,2,1,q,0.99)