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