Imported Upstream version 3.2.2
[debian/gnuradio] / gr-trellis / src / examples / test_viterbi_equalization.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,tot_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     # 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)
21
22     # CHANNEL
23     add = gr.add_ff()
24     noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
25     
26     # RX
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(); 
31     
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)
37     
38     tb.run()
39
40     ntotal = dst.ntotal ()
41     nright = dst.nright ()
42     runlength = dst.runlength ()
43     #print ntotal,nright,runlength 
44     
45     return (ntotal,ntotal-nright)
46
47
48
49
50 def main(args):
51     nargs = len (args)
52     if nargs == 2:
53         esn0_db=float(args[0])
54         rep=int(args[1])
55     else:
56         sys.stderr.write ('usage: test_viterbi_equalization.py Es/No_db  repetitions\n')
57         sys.exit (1)
58
59     # system parameters
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
66
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')
73         sys.exit (1)
74
75
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
79
80     for i in range(rep):
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
82         tot_s=tot_s+s
83         terr_s=terr_s+e
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)
89
90
91
92 if __name__ == '__main__':
93     main (sys.argv[1:])
94