3 from gnuradio import gr
4 from gnuradio import audio
5 from gnuradio import trellis
6 from gnuradio import eng_notation
12 def make_rx(fg,fo,fi,dimensionality,tot_constellation,K,interleaver,IT,Es,N0,type):
13 metrics_in = trellis.metrics_f(fi.O(),dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for innner SISO
14 scale = gr.multiply_const_ff(1.0/N0)
15 gnd = gr.vector_source_f([0],True);
24 inter.append( trellis.permutation(interleaver.K(),interleaver.INTER(),fi.I(),gr.sizeof_float) )
25 siso_in.append( trellis.siso_f(fi,K,0,-1,True,False,type) )
26 deinter.append( trellis.permutation(interleaver.K(),interleaver.DEINTER(),fi.I(),gr.sizeof_float) )
28 siso_out.append( trellis.siso_f(fo,K,0,-1,False,True,type) )
30 siso_out.append( trellis.viterbi_s(fo,K,0,-1) ) # no soft outputs needed
33 fg.connect (gnd,inter[0])
34 fg.connect (metrics_in,scale)
35 fg.connect (scale,(siso_in[0],1))
40 fg.connect (metrics_in,(siso_in[it+1],1))
41 fg.connect (siso_in[it],deinter[it],(siso_out[it],1))
42 fg.connect (gnd,(siso_out[it],0))
43 fg.connect (siso_out[it],inter[it+1])
44 fg.connect (inter[it],(siso_in[it],0))
46 fg.connect (siso_in[it],deinter[it],siso_out[it])
47 fg.connect (inter[it],(siso_in[it],0))
49 return (metrics_in,siso_out[IT-1])
52 def run_test (fo,fi,interleaver,Kb,bitspersymbol,K,dimensionality,tot_constellation,Es,N0,IT,seed):
56 src = gr.lfsr_32k_source_s()
57 src_head = gr.head (gr.sizeof_short,Kb/16) # packet size in shorts
58 s2fsmi = gr.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the iouter FSM input cardinality
59 enc_out = trellis.encoder_ss(fo,0) # initial state = 0
60 inter = trellis.permutation(interleaver.K(),interleaver.INTER(),1,gr.sizeof_short)
61 enc_in = trellis.encoder_ss(fi,0) # initial state = 0
62 # essentially here we implement the combination of modulation and channel as a memoryless modulation (the memory induced by the channel is hidden in the innner FSM)
63 mod = gr.chunks_to_symbols_sf(tot_constellation,dimensionality)
67 noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
70 (head,tail) = make_rx(fg,fo,fi,dimensionality,tot_constellation,K,interleaver,IT,Es,N0,trellis.TRELLIS_MIN_SUM)
71 fsmi2s = gr.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts
72 dst = gr.check_lfsr_32k_s();
74 fg.connect (src,src_head,s2fsmi,enc_out,inter,enc_in,mod)
75 fg.connect (mod,(add,0))
76 fg.connect (noise,(add,1))
78 fg.connect (tail,fsmi2s,dst)
82 ntotal = dst.ntotal ()
83 nright = dst.nright ()
84 runlength = dst.runlength ()
85 #print ntotal,nright,runlength
87 return (ntotal,ntotal-nright)
96 esn0_db=float(args[1])
99 sys.stderr.write ('usage: test_turbo_equalization.py fsm_name_out Es/No_db repetitions\n')
103 Kb=64*16 # packet size in bits (multiple of 16)
104 modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations
105 channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels
106 fo=trellis.fsm(fname_out) # get the outer FSM specification from a file
107 fi=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically
109 sys.stderr.write ('Incompatible cardinality between outer and inner FSM.\n')
111 bitspersymbol = int(round(math.log(fo.I())/math.log(2))) # bits per FSM input symbol
112 K=Kb/bitspersymbol # packet size in trellis steps
113 interleaver=trellis.interleaver(K,666) # construct a random interleaver
114 tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1)
115 dimensionality = tot_channel[0]
116 tot_constellation = tot_channel[1]
117 if len(tot_constellation)/dimensionality != fi.O():
118 sys.stderr.write ('Incompatible FSM output cardinality and lookup table size.\n')
120 N0=pow(10.0,-esn0_db/10.0); # noise variance
121 IT = 3 # number of turbo iterations
123 tot_s=0 # total number of transmitted shorts
124 terr_s=0 # total number of shorts in error
125 terr_p=0 # total number of packets in error
128 (s,e)=run_test(fo,fi,interleaver,Kb,bitspersymbol,K,dimensionality,tot_constellation,1,N0,IT,-long(666+i)) # run experiment with different seed to get different noise realizations
131 terr_p=terr_p+(terr_s!=0)
132 if ((i+1)%10==0) : # display progress
133 print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
134 # estimate of the (short or bit) error rate
135 print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
139 if __name__ == '__main__':