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