Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-examples / python / channel-coding / test_turbo_equalization.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 fsm_utils
10
11
12 def make_rx(fg,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     fg.connect (gnd,inter[0])
34     fg.connect (metrics_in,scale)
35     fg.connect (scale,(siso_in[0],1))
36
37     # connect the rest
38     for it in range(IT):
39       if it < IT-1:
40         fg.connect (metrics_in,(siso_in[it+1],1))
41         fg.connect (siso_in[it],deinter[it],(siso_out[it],1))
42         fg.connect (gnd,(siso_out[it],0))
43         fg.connect (siso_out[it],inter[it+1])
44         fg.connect (inter[it],(siso_in[it],0))
45       else:
46         fg.connect (siso_in[it],deinter[it],siso_out[it])
47         fg.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,dimensionality,tot_constellation,Es,N0,IT,seed):
53     fg = gr.flow_graph ()
54
55     # TX
56     src = gr.lfsr_32k_source_s()
57     src_head = gr.head (gr.sizeof_short,Kb/16) # packet size in shorts
58     s2fsmi = gr.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the iouter FSM input cardinality
59     enc_out = trellis.encoder_ss(fo,0) # initial state = 0
60     inter = trellis.permutation(interleaver.K(),interleaver.INTER(),1,gr.sizeof_short)
61     enc_in = trellis.encoder_ss(fi,0) # initial state = 0
62     # essentially here we implement the combination of modulation and channel as a memoryless modulation (the memory induced by the channel is hidden in the innner FSM)
63     mod = gr.chunks_to_symbols_sf(tot_constellation,dimensionality)
64
65     # CHANNEL
66     add = gr.add_ff()
67     noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
68     
69     # RX
70     (head,tail) = make_rx(fg,fo,fi,dimensionality,tot_constellation,K,interleaver,IT,Es,N0,trellis.TRELLIS_MIN_SUM) 
71     fsmi2s = gr.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts
72     dst = gr.check_lfsr_32k_s(); 
73     
74     fg.connect (src,src_head,s2fsmi,enc_out,inter,enc_in,mod)
75     fg.connect (mod,(add,0))
76     fg.connect (noise,(add,1))
77     fg.connect (add,head)
78     fg.connect (tail,fsmi2s,dst)
79     
80     fg.run()
81
82     ntotal = dst.ntotal ()
83     nright = dst.nright ()
84     runlength = dst.runlength ()
85     #print ntotal,nright,runlength 
86     
87     return (ntotal,ntotal-nright)
88
89
90
91
92 def main(args):
93     nargs = len (args)
94     if nargs == 3:
95         fname_out=args[0]
96         esn0_db=float(args[1])
97         rep=int(args[2])
98     else:
99         sys.stderr.write ('usage: test_turbo_equalization.py fsm_name_out Es/No_db  repetitions\n')
100         sys.exit (1)
101
102     # system parameters
103     Kb=64*16  # packet size in bits (multiple of 16)
104     modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations
105     channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels
106     fo=trellis.fsm(fname_out) # get the outer FSM specification from a file
107     fi=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically
108     if fo.O() != fi.I():
109         sys.stderr.write ('Incompatible cardinality between outer and inner FSM.\n')
110         sys.exit (1)
111     bitspersymbol = int(round(math.log(fo.I())/math.log(2))) # bits per FSM input symbol
112     K=Kb/bitspersymbol # packet size in trellis steps
113     interleaver=trellis.interleaver(K,666) # construct a random interleaver
114     tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1)
115     dimensionality = tot_channel[0]
116     tot_constellation = tot_channel[1]
117     if len(tot_constellation)/dimensionality != fi.O():
118         sys.stderr.write ('Incompatible FSM output cardinality and lookup table size.\n')
119         sys.exit (1)
120     N0=pow(10.0,-esn0_db/10.0); # noise variance
121     IT = 3 # number of turbo iterations
122
123     tot_s=0 # total number of transmitted shorts
124     terr_s=0 # total number of shorts in error
125     terr_p=0 # total number of packets in error
126
127     for i in range(rep):
128         (s,e)=run_test(fo,fi,interleaver,Kb,bitspersymbol,K,dimensionality,tot_constellation,1,N0,IT,-long(666+i)) # run experiment with different seed to get different noise realizations
129         tot_s=tot_s+s
130         terr_s=terr_s+e
131         terr_p=terr_p+(terr_s!=0)
132         if ((i+1)%10==0) : # display progress
133             print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
134     # estimate of the (short or bit) error rate
135     print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
136
137
138
139 if __name__ == '__main__':
140     main (sys.argv[1:])
141