root / gnuradio-examples / python / channel-coding / test_viterbi_equalization1.py @ 87a17aaa
History | View | Annotate | Download (3.9 kB)
| 1 | 180347e4 | anastas | #!/usr/bin/env python
|
|---|---|---|---|
| 2 | 180347e4 | anastas | |
| 3 | 180347e4 | anastas | from gnuradio import gr |
| 4 | 180347e4 | anastas | from gnuradio import audio |
| 5 | 180347e4 | anastas | from gnuradio import trellis |
| 6 | 180347e4 | anastas | from gnuradio import eng_notation |
| 7 | 180347e4 | anastas | import math |
| 8 | 180347e4 | anastas | import sys |
| 9 | 180347e4 | anastas | import random |
| 10 | 180347e4 | anastas | import fsm_utils |
| 11 | 180347e4 | anastas | |
| 12 | 180347e4 | anastas | def run_test (f,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,N0,seed): |
| 13 | 180347e4 | anastas | fg = gr.flow_graph () |
| 14 | 180347e4 | anastas | L = len(channel)
|
| 15 | 180347e4 | anastas | |
| 16 | 180347e4 | anastas | # TX
|
| 17 | 180347e4 | anastas | # this for loop is TOO slow in python!!!
|
| 18 | 180347e4 | anastas | packet = [0]*(K+2*L) |
| 19 | 87a17aaa | anastas | random.seed(seed) |
| 20 | 180347e4 | anastas | for i in range(len(packet)): |
| 21 | 180347e4 | anastas | packet[i] = random.randint(0, 2**bitspersymbol - 1) # random symbols |
| 22 | 180347e4 | anastas | for i in range(L): # first/last L symbols set to 0 |
| 23 | 180347e4 | anastas | packet[i] = 0
|
| 24 | 180347e4 | anastas | packet[len(packet)-i-1] = 0 |
| 25 | 180347e4 | anastas | src = gr.vector_source_s(packet,False)
|
| 26 | 180347e4 | anastas | mod = gr.chunks_to_symbols_sf(modulation[1],modulation[0]) |
| 27 | 180347e4 | anastas | |
| 28 | 180347e4 | anastas | # CHANNEL
|
| 29 | 180347e4 | anastas | isi = gr.fir_filter_fff(1,channel)
|
| 30 | 180347e4 | anastas | add = gr.add_ff() |
| 31 | 180347e4 | anastas | noise = gr.noise_source_f(gr.GR_GAUSSIAN,math.sqrt(N0/2),seed)
|
| 32 | 180347e4 | anastas | |
| 33 | 180347e4 | anastas | # RX
|
| 34 | 87a17aaa | anastas | skip = gr.skiphead(gr.sizeof_float, L) # skip the first L samples since you know they are coming from the L zero symbols
|
| 35 | 180347e4 | anastas | #metrics = trellis.metrics_f(f.O(),dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi
|
| 36 | 87a17aaa | anastas | #va = trellis.viterbi_s(f,K+L,-1,0) # Put -1 if the Initial/Final states are not set.
|
| 37 | 87a17aaa | anastas | va = trellis.viterbi_combined_s(f,K+L,0,0,dimensionality,tot_constellation,trellis.TRELLIS_EUCLIDEAN) # using viterbi_combined_s instead of metrics_f/viterbi_s allows larger packet lengths because metrics_f is complaining for not being able to allocate large buffers. This is due to the large f.O() in this application... |
| 38 | 180347e4 | anastas | dst = gr.vector_sink_s() |
| 39 | 180347e4 | anastas | |
| 40 | 180347e4 | anastas | fg.connect (src,mod) |
| 41 | 180347e4 | anastas | fg.connect (mod,isi,(add,0))
|
| 42 | 180347e4 | anastas | fg.connect (noise,(add,1))
|
| 43 | 180347e4 | anastas | #fg.connect (add,metrics)
|
| 44 | 180347e4 | anastas | #fg.connect (metrics,va,dst)
|
| 45 | 87a17aaa | anastas | fg.connect (add,skip,va,dst) |
| 46 | 180347e4 | anastas | |
| 47 | 180347e4 | anastas | fg.run() |
| 48 | 180347e4 | anastas | |
| 49 | 180347e4 | anastas | data = dst.data() |
| 50 | 87a17aaa | anastas | ntotal = len(data) - L
|
| 51 | 180347e4 | anastas | nright=0
|
| 52 | 180347e4 | anastas | for i in range(ntotal): |
| 53 | 87a17aaa | anastas | if packet[i+L]==data[i]:
|
| 54 | 180347e4 | anastas | nright=nright+1
|
| 55 | 180347e4 | anastas | #else:
|
| 56 | 180347e4 | anastas | #print "Error in ", i
|
| 57 | 180347e4 | anastas | |
| 58 | 180347e4 | anastas | return (ntotal,ntotal-nright)
|
| 59 | 180347e4 | anastas | |
| 60 | 180347e4 | anastas | |
| 61 | 180347e4 | anastas | def main(args): |
| 62 | 180347e4 | anastas | nargs = len (args)
|
| 63 | 180347e4 | anastas | if nargs == 2: |
| 64 | 180347e4 | anastas | esn0_db=float(args[0]) |
| 65 | 180347e4 | anastas | rep=int(args[1]) |
| 66 | 180347e4 | anastas | else:
|
| 67 | 180347e4 | anastas | sys.stderr.write ('usage: test_viterbi_equalization1.py Es/No_db repetitions\n')
|
| 68 | 180347e4 | anastas | sys.exit (1)
|
| 69 | 180347e4 | anastas | |
| 70 | 180347e4 | anastas | # system parameters
|
| 71 | 180347e4 | anastas | Kb=128*16 # packet size in bits (multiple of 16) |
| 72 | 180347e4 | anastas | modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations
|
| 73 | 180347e4 | anastas | channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels
|
| 74 | 180347e4 | anastas | f=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically |
| 75 | 180347e4 | anastas | bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol |
| 76 | 180347e4 | anastas | K=Kb/bitspersymbol # packet size in trellis steps
|
| 77 | 180347e4 | anastas | |
| 78 | 180347e4 | anastas | tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1) |
| 79 | 180347e4 | anastas | dimensionality = tot_channel[0]
|
| 80 | 180347e4 | anastas | tot_constellation = tot_channel[1]
|
| 81 | 180347e4 | anastas | N0=pow(10.0,-esn0_db/10.0); # noise variance |
| 82 | 180347e4 | anastas | if len(tot_constellation)/dimensionality != f.O(): |
| 83 | 180347e4 | anastas | sys.stderr.write ('Incompatible FSM output cardinality and lookup table size.\n')
|
| 84 | 180347e4 | anastas | sys.exit (1)
|
| 85 | 180347e4 | anastas | |
| 86 | 180347e4 | anastas | tot_s=0 # total number of transmitted shorts |
| 87 | 180347e4 | anastas | terr_s=0 # total number of shorts in error |
| 88 | 180347e4 | anastas | terr_p=0 # total number of packets in error |
| 89 | 180347e4 | anastas | |
| 90 | 180347e4 | anastas | for i in range(rep): |
| 91 | 180347e4 | anastas | (s,e)=run_test(f,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,N0,-long(666+i)) # run experiment with different seed to get different noise realizations |
| 92 | 180347e4 | anastas | tot_s=tot_s+s |
| 93 | 180347e4 | anastas | terr_s=terr_s+e |
| 94 | 180347e4 | anastas | terr_p=terr_p+(terr_s!=0)
|
| 95 | 180347e4 | anastas | if ((i+1)%100==0) : # display progress |
| 96 | 180347e4 | anastas | print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s) |
| 97 | 180347e4 | anastas | # estimate of the (short or symbol) error rate
|
| 98 | 180347e4 | anastas | print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s) |
| 99 | 180347e4 | anastas | |
| 100 | 180347e4 | anastas | |
| 101 | 180347e4 | anastas | |
| 102 | 180347e4 | anastas | if __name__ == '__main__': |
| 103 | 180347e4 | anastas | main (sys.argv[1:]) |