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