Imported Upstream version 3.0
[debian/gnuradio] / gr-trellis / doc / test_viterbi_equalization1.py.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <programlisting>
3   1  #!/usr/bin/env python
4   2  
5   3  from gnuradio import gr
6   4  from gnuradio import audio
7   5  from gnuradio import trellis
8   6  from gnuradio import eng_notation
9   7  import math
10   8  import sys
11   9  import random
12  10  import fsm_utils
13  11  
14  12  def run_test (f,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,N0,seed):
15  13      fg = gr.flow_graph ()
16  14      L = len(channel)
17  15  
18  16      # TX
19  17      # this for loop is TOO slow in python!!!
20  18      packet = [0]*(K+2*L)
21  19      random.seed(seed)
22  20      for i in range(len(packet)):
23  21          packet[i] = random.randint(0, 2**bitspersymbol - 1) # random symbols
24  22      for i in range(L): # first/last L symbols set to 0
25  23          packet[i] = 0
26  24          packet[len(packet)-i-1] = 0
27  25      src = gr.vector_source_s(packet,False)
28  26      mod = gr.chunks_to_symbols_sf(modulation[1],modulation[0])
29  27  
30  28      # CHANNEL
31  29      isi = gr.fir_filter_fff(1,channel)
32  30      add = gr.add_ff()
33  31      noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
34  32      
35  33      # RX
36  34      skip = gr.skiphead(gr.sizeof_float, L) # skip the first L samples since you know they are coming from the L zero symbols
37  35      #metrics = trellis.metrics_f(f.O(),dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi
38  36      #va = trellis.viterbi_s(f,K+L,0,0) # Put -1 if the Initial/Final states are not set.
39  37      va = trellis.viterbi_combined_s(f,K+L,0,0,dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # using viterbi_combined_s instead of metrics_f/viterbi_s allows larger packet lengths because metrics_f is complaining for not being able to allocate large buffers. This is due to the large f.O() in this application...
40  38      dst = gr.vector_sink_s()
41  39  
42  40      fg.connect (src,mod)
43  41      fg.connect (mod,isi,(add,0))
44  42      fg.connect (noise,(add,1))
45  43      #fg.connect (add,metrics)
46  44      #fg.connect (metrics,va,dst)
47  45      fg.connect (add,skip,va,dst)
48  46  
49  47      fg.run()
50  48  
51  49      data = dst.data() 
52  50      ntotal = len(data) - L
53  51      nright=0
54  52      for i in range(ntotal):
55  53          if packet[i+L]==data[i]:
56  54              nright=nright+1
57  55          #else:
58  56              #print &quot;Error in &quot;, i
59  57      
60  58      return (ntotal,ntotal-nright)
61  59  
62  60  
63  61  def main(args):
64  62      nargs = len (args)
65  63      if nargs == 2:
66  64          esn0_db=float(args[0])
67  65          rep=int(args[1])
68  66      else:
69  67          sys.stderr.write (&apos;usage: test_viterbi_equalization1.py Es/No_db  repetitions\n&apos;)
70  68          sys.exit (1)
71  69  
72  70      # system parameters
73  71      Kb=2048  # packet size in bits
74  72      modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations
75  73      channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels
76  74      f=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically
77  75      bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol
78  76      K=Kb/bitspersymbol # packet size in trellis steps
79  77  
80  78      tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1)
81  79      dimensionality = tot_channel[0]
82  80      tot_constellation = tot_channel[1]
83  81      N0=pow(10.0,-esn0_db/10.0); # noise variance
84  82      if len(tot_constellation)/dimensionality != f.O():
85  83          sys.stderr.write (&apos;Incompatible FSM output cardinality and lookup table size.\n&apos;)
86  84          sys.exit (1)
87  85  
88  86      tot_s=0 # total number of transmitted shorts
89  87      terr_s=0 # total number of shorts in error
90  88      terr_p=0 # total number of packets in error
91  89  
92  90      for i in range(rep):
93  91          (s,e)=run_test(f,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,N0,-long(666+i)) # run experiment with different seed to get different data and noise realizations
94  92          tot_s=tot_s+s
95  93          terr_s=terr_s+e
96  94          terr_p=terr_p+(terr_s!=0)
97  95          if ((i+1)%100==0) : # display progress
98  96              print i+1,terr_p, &apos;%.2e&apos; % ((1.0*terr_p)/(i+1)),tot_s,terr_s, &apos;%.2e&apos; % ((1.0*terr_s)/tot_s)
99  97      # estimate of the (short or symbol) error rate
100  98      print rep,terr_p, &apos;%.2e&apos; % ((1.0*terr_p)/(i+1)),tot_s,terr_s, &apos;%.2e&apos; % ((1.0*terr_s)/tot_s)
101  99  
102 100  
103 101  if __name__ == &apos;__main__&apos;:
104 102      main (sys.argv[1:])
105 </programlisting>