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