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