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