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