Imported Upstream version 3.2.2
[debian/gnuradio] / gr-trellis / src / examples / test_tcm_combined.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):
12     tb = gr.top_block ()
13
14     # TX
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     mod = gr.chunks_to_symbols_sf(constellation,dimensionality)
20
21
22     # CHANNEL
23     add = gr.add_ff()
24     noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
25
26     
27     # RX
28     va = trellis.viterbi_combined_fs(f,K,0,-1,dimensionality,constellation,trellis.TRELLIS_EUCLIDEAN) # 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(); 
31     
32
33     tb.connect (src,src_head,s2fsmi,enc,mod)
34     tb.connect (mod,(add,0))
35     tb.connect (noise,(add,1))
36     tb.connect (add,va,fsmi2s,dst)
37     
38
39     tb.run()
40     
41     # A bit of cheating: run the program once and print the 
42     # final encoder state..
43     # Then put it as the last argument in the viterbi block
44     #print "final state = " , enc.ST()
45
46     ntotal = dst.ntotal ()
47     nright = dst.nright ()
48     runlength = dst.runlength ()
49     
50     return (ntotal,ntotal-nright)
51
52
53
54
55 def main(args):
56     nargs = len (args)
57     if nargs == 3:
58         fname=args[0]
59         esn0_db=float(args[1]) # Es/No in dB
60         rep=int(args[2]) # number of times the experiment is run to collect enough errors
61     else:
62         sys.stderr.write ('usage: test_tcm_combined.py fsm_fname  Es/No_db  repetitions\n')
63         sys.exit (1)
64
65     # system parameters
66     f=trellis.fsm(fname) # get the FSM specification from a file (will hopefully be automated in the future...)
67     Kb=1024*16  # packet size in bits (make it multiple of 16)
68     bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol
69     K=Kb/bitspersymbol # packet size in trellis steps
70     modulation = fsm_utils.psk4 # see fsm_utils.py for available predefined modulations
71     dimensionality = modulation[0]
72     constellation = modulation[1] 
73     if len(constellation)/dimensionality != f.O():
74         sys.stderr.write ('Incompatible FSM output cardinality and modulation size.\n')
75         sys.exit (1)
76     # calculate average symbol energy
77     Es = 0
78     for i in range(len(constellation)):
79         Es = Es + constellation[i]**2
80     Es = Es / (len(constellation)/dimensionality)
81     N0=Es/pow(10.0,esn0_db/10.0); # noise variance
82
83     tot_s=0 # total number of transmitted shorts
84     terr_s=0 # total number of shorts in error
85     terr_p=0 # total number of packets in error
86     for i in range(rep):
87         (s,e)=run_test(f,Kb,bitspersymbol,K,dimensionality,constellation,N0,-long(666+i)) # run experiment with different seed to get different noise realizations
88         tot_s=tot_s+s
89         terr_s=terr_s+e
90         terr_p=terr_p+(terr_s!=0)
91         if ((i+1)%100==0) : # display progress
92             print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
93     # estimate of the (short or bit) error rate
94     print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s)
95
96
97
98 if __name__ == '__main__':
99     main (sys.argv[1:])
100