Imported Upstream version 3.2.2
[debian/gnuradio] / gr-trellis / src / examples / test_turbo_equalization1.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr
4 from gnuradio import audio
5 from gnuradio import trellis
6 from gnuradio import eng_notation
7 import math
8 import sys
9 import random
10 import fsm_utils
11
12 def make_rx(tb,fo,fi,dimensionality,tot_constellation,K,interleaver,IT,Es,N0,type):
13     metrics_in = trellis.metrics_f(fi.O(),dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for innner SISO
14     scale = gr.multiply_const_ff(1.0/N0)
15     gnd = gr.vector_source_f([0],True);
16
17     inter=[]
18     deinter=[]
19     siso_in=[]
20     siso_out=[]
21
22     # generate all blocks
23     for it in range(IT):
24       inter.append( trellis.permutation(interleaver.K(),interleaver.INTER(),fi.I(),gr.sizeof_float) )
25       siso_in.append( trellis.siso_f(fi,K,0,-1,True,False,type) )
26       deinter.append( trellis.permutation(interleaver.K(),interleaver.DEINTER(),fi.I(),gr.sizeof_float) )
27       if it < IT-1:
28         siso_out.append( trellis.siso_f(fo,K,0,-1,False,True,type) )
29       else:
30         siso_out.append( trellis.viterbi_s(fo,K,0,-1) ) # no soft outputs needed
31
32     # connect first stage
33     tb.connect (gnd,inter[0])
34     tb.connect (metrics_in,scale)
35     tb.connect (scale,(siso_in[0],1))
36
37     # connect the rest
38     for it in range(IT):
39       if it < IT-1:
40         tb.connect (scale,(siso_in[it+1],1))
41         tb.connect (siso_in[it],deinter[it],(siso_out[it],1))
42         tb.connect (gnd,(siso_out[it],0))
43         tb.connect (siso_out[it],inter[it+1])
44         tb.connect (inter[it],(siso_in[it],0))
45       else:
46         tb.connect (siso_in[it],deinter[it],siso_out[it])
47         tb.connect (inter[it],(siso_in[it],0))
48
49     return (metrics_in,siso_out[IT-1])
50
51
52 def run_test (fo,fi,interleaver,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,Es,N0,IT,seed):
53     tb = gr.top_block ()
54     L = len(channel)
55
56     # TX
57     # this for loop is TOO slow in python!!!
58     packet = [0]*(K)
59     random.seed(seed)
60     for i in range(len(packet)):
61         packet[i] = random.randint(0, 2**bitspersymbol - 1) # random symbols
62     src = gr.vector_source_s(packet,False)
63     enc_out = trellis.encoder_ss(fo,0) # initial state = 0
64     inter = trellis.permutation(interleaver.K(),interleaver.INTER(),1,gr.sizeof_short)
65     mod = gr.chunks_to_symbols_sf(modulation[1],modulation[0])
66
67     # CHANNEL
68     isi = gr.fir_filter_fff(1,channel)
69     add = gr.add_ff()
70     noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
71     
72     # RX
73     (head,tail) = make_rx(tb,fo,fi,dimensionality,tot_constellation,K,interleaver,IT,Es,N0,trellis.TRELLIS_MIN_SUM) 
74     dst = gr.vector_sink_s(); 
75     
76     tb.connect (src,enc_out,inter,mod)
77     tb.connect (mod,isi,(add,0))
78     tb.connect (noise,(add,1))
79     tb.connect (add,head)
80     tb.connect (tail,dst)
81     
82     tb.run()
83
84     data = dst.data()
85     ntotal = len(data)
86     nright=0
87     for i in range(ntotal):
88         if packet[i]==data[i]:
89             nright=nright+1
90         #else:
91             #print "Error in ", i
92  
93     return (ntotal,ntotal-nright)
94
95
96
97
98 def main(args):
99     nargs = len (args)
100     if nargs == 3:
101         fname_out=args[0]
102         esn0_db=float(args[1])
103         rep=int(args[2])
104     else:
105         sys.stderr.write ('usage: test_turbo_equalization.py fsm_name_out Es/No_db  repetitions\n')
106         sys.exit (1)
107
108     # system parameters
109     Kb=64*16  # packet size in bits (multiple of 16)
110     modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations
111     channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels
112     fo=trellis.fsm(fname_out) # get the outer FSM specification from a file
113     fi=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically
114     if fo.O() != fi.I():
115         sys.stderr.write ('Incompatible cardinality between outer and inner FSM.\n')
116         sys.exit (1)
117     bitspersymbol = int(round(math.log(fo.I())/math.log(2))) # bits per FSM input symbol
118     K=Kb/bitspersymbol # packet size in trellis steps
119     interleaver=trellis.interleaver(K,666) # construct a random interleaver
120     tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1)
121     dimensionality = tot_channel[0]
122     tot_constellation = tot_channel[1]
123     if len(tot_constellation)/dimensionality != fi.O():
124         sys.stderr.write ('Incompatible FSM output cardinality and lookup table size.\n')
125         sys.exit (1)
126     N0=pow(10.0,-esn0_db/10.0); # noise variance
127     IT = 3 # number of turbo iterations
128
129     tot_s=0 # total number of transmitted shorts
130     terr_s=0 # total number of shorts in error
131     terr_p=0 # total number of packets in error
132
133     for i in range(rep):
134         (s,e)=run_test(fo,fi,interleaver,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,1,N0,IT,-long(666+i)) # run experiment with different seed to get different noise realizations
135         tot_s=tot_s+s
136         terr_s=terr_s+e
137         terr_p=terr_p+(terr_s!=0)
138         if ((i+1)%10==0) : # display progress
139             print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
140     # estimate of the (short or bit) error rate
141     print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
142
143
144
145 if __name__ == '__main__':
146     main (sys.argv[1:])
147