3 from gnuradio import gr
4 from gnuradio import audio
5 from gnuradio import trellis
6 from gnuradio import eng_notation
12 def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed):
18 #for i in range(Kb-1*16): # last 16 bits = 0 to drive the final state to 0
19 #packet[i] = random.randint(0, 1) # random 0s and 1s
20 #src = gr.vector_source_s(packet,False)
21 src = gr.lfsr_32k_source_s()
22 src_head = gr.head (gr.sizeof_short,Kb/16) # packet size in shorts
23 #b2s = gr.unpacked_to_packed_ss(1,gr.GR_MSB_FIRST) # pack bits in shorts
24 s2fsmi = gr.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 = gr.chunks_to_symbols_sf(constellation,dimensionality)
31 noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
35 metrics = trellis.metrics_f(f.O(),dimensionality,constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi
36 va = trellis.viterbi_s(f,K,0,-1) # Put -1 if the Initial/Final states are not set.
37 fsmi2s = gr.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts
38 #s2b = gr.packed_to_unpacked_ss(1,gr.GR_MSB_FIRST) # unpack shorts to bits
39 #dst = gr.vector_sink_s();
40 dst = gr.check_lfsr_32k_s();
43 fg.connect (src,src_head,s2fsmi,enc,mod)
44 #fg.connect (src,b2s,s2fsmi,enc,mod)
45 fg.connect (mod,(add,0))
46 fg.connect (noise,(add,1))
47 fg.connect (add,metrics)
48 fg.connect (metrics,va,fsmi2s,dst)
49 #fg.connect (metrics,va,fsmi2s,s2b,dst)
54 # A bit of cheating: run the program once and print the
55 # final encoder state..
56 # Then put it as the last argument in the viterbi block
57 #print "final state = " , enc.ST()
59 ntotal = dst.ntotal ()
60 nright = dst.nright ()
61 runlength = dst.runlength ()
63 #if len(dst.data()) != ntotal:
64 #print "Error: not enough data\n"
66 #for i in range(ntotal):
67 #if packet[i]==dst.data()[i]:
71 return (ntotal,ntotal-nright)
80 esn0_db=float(args[1]) # Es/No in dB
81 rep=int(args[2]) # number of times the experiment is run to collect enough errors
83 sys.stderr.write ('usage: test_tcm.py fsm_fname Es/No_db repetitions\n')
87 f=trellis.fsm(fname) # get the FSM specification from a file (will hopefully be automated in the future...)
88 Kb=1024*16 # packet size in bits (make it multiple of 16 so it can be packed in a short)
89 bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol
90 K=Kb/bitspersymbol # packet size in trellis steps
91 modulation = fsm_utils.psk4 # see fsm_utlis.py for available predefined modulations
92 dimensionality = modulation[0]
93 constellation = modulation[1]
94 if len(constellation)/dimensionality != f.O():
95 sys.stderr.write ('Incompatible FSM output cardinality and modulation size.\n')
97 # calculate average symbol energy
99 for i in range(len(constellation)):
100 Es = Es + constellation[i]**2
101 Es = Es / (len(constellation)/dimensionality)
102 N0=Es/pow(10.0,esn0_db/10.0); # noise variance
109 (s,e)=run_test(f,Kb,bitspersymbol,K,dimensionality,constellation,N0,-long(666+i)) # run experiment with different seed to get different noise realizations
113 print i,s,e,tot_s,terr_s, '%e' % ((1.0*terr_s)/tot_s)
114 # estimate of the (short or bit) error rate
115 print tot_s,terr_s, '%e' % ((1.0*terr_s)/tot_s)
118 if __name__ == '__main__':