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