3 from gnuradio import gr
4 from gnuradio import audio
5 from gnuradio import trellis
6 from gnuradio import eng_notation
11 def run_test (f,Kb,bitspersymbol,K,dimensionality,tot_constellation,N0,seed):
15 src = gr.lfsr_32k_source_s()
16 src_head = gr.head (gr.sizeof_short,Kb/16) # packet size in shorts
17 s2fsmi = gr.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality
18 enc = trellis.encoder_ss(f,0) # initial state = 0
19 # essentially here we implement the combination of modulation and channel as a memoryless modulation (the memory induced by the channel is hidden in the FSM)
20 mod = gr.chunks_to_symbols_sf(tot_constellation,dimensionality)
24 noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
27 metrics = trellis.metrics_f(f.O(),dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi
28 va = trellis.viterbi_s(f,K,0,-1) # Put -1 if the Initial/Final states are not set.
29 fsmi2s = gr.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts
30 dst = gr.check_lfsr_32k_s();
32 tb.connect (src,src_head,s2fsmi,enc,mod)
33 tb.connect (mod,(add,0))
34 tb.connect (noise,(add,1))
35 tb.connect (add,metrics)
36 tb.connect (metrics,va,fsmi2s,dst)
40 ntotal = dst.ntotal ()
41 nright = dst.nright ()
42 runlength = dst.runlength ()
43 #print ntotal,nright,runlength
45 return (ntotal,ntotal-nright)
53 esn0_db=float(args[0])
56 sys.stderr.write ('usage: test_viterbi_equalization.py Es/No_db repetitions\n')
60 Kb=128*16 # packet size in bits (multiple of 16)
61 modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations
62 channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels
63 f=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically
64 bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol
65 K=Kb/bitspersymbol # packet size in trellis steps
67 tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1)
68 dimensionality = tot_channel[0]
69 tot_constellation = tot_channel[1]
70 N0=pow(10.0,-esn0_db/10.0); # noise variance
71 if len(tot_constellation)/dimensionality != f.O():
72 sys.stderr.write ('Incompatible FSM output cardinality and lookup table size.\n')
76 tot_s=0 # total number of transmitted shorts
77 terr_s=0 # total number of shorts in error
78 terr_p=0 # total number of packets in error
81 (s,e)=run_test(f,Kb,bitspersymbol,K,dimensionality,tot_constellation,N0,-long(666+i)) # run experiment with different seed to get different noise realizations
84 terr_p=terr_p+(terr_s!=0)
85 if ((i+1)%100==0) : # display progress
86 print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
87 # estimate of the (short or bit) error rate
88 print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
92 if __name__ == '__main__':