Imported Upstream version 3.2.2
[debian/gnuradio] / gr-trellis / doc / test_tcm.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,dimensionality,constellation,N0,seed):
15  13      tb = gr.top_block ()
16  14  
17  15      # TX
18  16      src = gr.lfsr_32k_source_s()
19  17      src_head = gr.head (gr.sizeof_short,Kb/16) # packet size in shorts
20  18      s2fsmi = gr.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality
21  19      enc = trellis.encoder_ss(f,0) # initial state = 0
22  20      mod = gr.chunks_to_symbols_sf(constellation,dimensionality)
23  21  
24  22      # CHANNEL
25  23      add = gr.add_ff()
26  24      noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
27  25  
28  26      # RX
29  27      metrics = trellis.metrics_f(f.O(),dimensionality,constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi
30  28      va = trellis.viterbi_s(f,K,0,-1) # Put -1 if the Initial/Final states are not set.
31  29      fsmi2s = gr.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts
32  30      dst = gr.check_lfsr_32k_s(); 
33  31  
34  32      tb.connect (src,src_head,s2fsmi,enc,mod)
35  33      tb.connect (mod,(add,0))
36  34      tb.connect (noise,(add,1))
37  35      tb.connect (add,metrics)
38  36      tb.connect (metrics,va,fsmi2s,dst)
39  37      
40  38      tb.run()
41  39      
42  40      # A bit of cheating: run the program once and print the 
43  41      # final encoder state.
44  42      # Then put it as the last argument in the viterbi block
45  43      #print &quot;final state = &quot; , enc.ST()
46  44  
47  45      ntotal = dst.ntotal ()
48  46      nright = dst.nright ()
49  47      runlength = dst.runlength ()
50  48      return (ntotal,ntotal-nright)
51  49  
52  50  
53  51  def main(args):
54  52      nargs = len (args)
55  53      if nargs == 3:
56  54          fname=args[0]
57  55          esn0_db=float(args[1]) # Es/No in dB
58  56          rep=int(args[2]) # number of times the experiment is run to collect enough errors
59  57      else:
60  58          sys.stderr.write (&apos;usage: test_tcm.py fsm_fname Es/No_db  repetitions\n&apos;)
61  59          sys.exit (1)
62  60  
63  61      # system parameters
64  62      f=trellis.fsm(fname) # get the FSM specification from a file
65  63      Kb=1024*16  # packet size in bits (make it multiple of 16 so it can be packed in a short)
66  64      bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol
67  65      K=Kb/bitspersymbol # packet size in trellis steps
68  66      modulation = fsm_utils.psk4 # see fsm_utlis.py for available predefined modulations
69  67      dimensionality = modulation[0]
70  68      constellation = modulation[1] 
71  69      if len(constellation)/dimensionality != f.O():
72  70          sys.stderr.write (&apos;Incompatible FSM output cardinality and modulation size.\n&apos;)
73  71          sys.exit (1)
74  72      # calculate average symbol energy
75  73      Es = 0
76  74      for i in range(len(constellation)):
77  75          Es = Es + constellation[i]**2
78  76      Es = Es / (len(constellation)/dimensionality)
79  77      N0=Es/pow(10.0,esn0_db/10.0); # noise variance
80  78      
81  79      tot_s=0
82  80      terr_s=0
83  81      for i in range(rep):
84  82          (s,e)=run_test(f,Kb,bitspersymbol,K,dimensionality,constellation,N0,-long(666+i)) # run experiment with different seed to get different noise realizations
85  83          tot_s=tot_s+s
86  84          terr_s=terr_s+e
87  85          if (i%100==0):
88  86              print i,s,e,tot_s,terr_s, &apos;%e&apos; % ((1.0*terr_s)/tot_s)
89  87      # estimate of the (short) error rate
90  88      print tot_s,terr_s, &apos;%e&apos; % ((1.0*terr_s)/tot_s)
91  89  
92  90  
93  91  if __name__ == &apos;__main__&apos;:
94  92      main (sys.argv[1:])
95 </programlisting>