diff options
author | Douglas Anderson <danderson@ntia.doc.gov> | 2017-02-12 15:52:19 -0800 |
---|---|---|
committer | Johnathan Corgan <johnathan@corganlabs.com> | 2017-02-26 18:21:22 -0800 |
commit | 9e625c4821f4c63421b3d3747c0c4f358fef6c5f (patch) | |
tree | 41dedbe053417be7314cdce15d64fbbb89db4d8d /gr-trellis | |
parent | e5aabcc6a4a9335f3ef8abf5f89104b626e9364d (diff) |
python3: update non-GRC components to use python2 or python3
Diffstat (limited to 'gr-trellis')
-rw-r--r--[-rwxr-xr-x] | gr-trellis/doc/make_numbered_listing.py | 1 | ||||
-rw-r--r-- | gr-trellis/doc/test_tcm.py | 23 | ||||
-rw-r--r--[-rwxr-xr-x] | gr-trellis/doc/test_viterbi_equalization1.py | 19 | ||||
-rw-r--r--[-rwxr-xr-x] | gr-trellis/examples/python/test_tcm.py | 27 | ||||
-rw-r--r-- | gr-trellis/python/trellis/CMakeLists.txt | 2 | ||||
-rw-r--r-- | gr-trellis/python/trellis/__init__.py | 5 | ||||
-rw-r--r--[-rwxr-xr-x] | gr-trellis/python/trellis/fsm_utils.py | 56 | ||||
-rw-r--r--[-rwxr-xr-x] | gr-trellis/python/trellis/qa_trellis.py | 14 | ||||
-rw-r--r-- | gr-trellis/swig/trellis_swig.py.in | 6 |
9 files changed, 85 insertions, 68 deletions
diff --git a/gr-trellis/doc/make_numbered_listing.py b/gr-trellis/doc/make_numbered_listing.py index c295dc8763..09a82cfaa4 100755..100644 --- a/gr-trellis/doc/make_numbered_listing.py +++ b/gr-trellis/doc/make_numbered_listing.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import unicode_literals import sys import os, os.path from optparse import OptionParser diff --git a/gr-trellis/doc/test_tcm.py b/gr-trellis/doc/test_tcm.py index 61ab00f1c8..e9a0ba59eb 100644 --- a/gr-trellis/doc/test_tcm.py +++ b/gr-trellis/doc/test_tcm.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function +from __future__ import division +from __future__ import unicode_literals from gnuradio import gr from gnuradio import audio from gnuradio import trellis, digital, blocks @@ -20,14 +23,14 @@ def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): # TX src = blocks.lfsr_32k_source_s() - src_head = blocks.head (gr.sizeof_short,Kb/16) # packet size in shorts + src_head = blocks.head (gr.sizeof_short,Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f,0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() - noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) + noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0 / 2),seed) # RX metrics = trellis.metrics_f(f.O(),dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi @@ -67,31 +70,31 @@ def main(args): # system parameters f=trellis.fsm(fname) # get the FSM specification from a file Kb=1024*16 # packet size in bits (make it multiple of 16 so it can be packed in a short) - bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol - K=Kb/bitspersymbol # packet size in trellis steps + bitspersymbol = int(round(math.log(f.I()) / math.log(2))) # bits per FSM input symbol + K=Kb / bitspersymbol # packet size in trellis steps modulation = fsm_utils.psk4 # see fsm_utlis.py for available predefined modulations dimensionality = modulation[0] constellation = modulation[1] - if len(constellation)/dimensionality != f.O(): + if len(constellation) / dimensionality != f.O(): sys.stderr.write ('Incompatible FSM output cardinality and modulation size.\n') sys.exit (1) # calculate average symbol energy Es = 0 for i in range(len(constellation)): Es = Es + constellation[i]**2 - Es = Es / (len(constellation)/dimensionality) - N0=Es/pow(10.0,esn0_db/10.0); # noise variance + Es = Es / (old_div(len(constellation,dimensionality))) + N0=Es / pow(10.0,old_div(esn0_db,10.0)); # noise variance tot_s=0 terr_s=0 for i in range(rep): - (s,e)=run_test(f,Kb,bitspersymbol,K,dimensionality,constellation,N0,-long(666+i)) # run experiment with different seed to get different noise realizations + (s,e)=run_test(f,Kb,bitspersymbol,K,dimensionality,constellation,N0,-int(666+i)) # run experiment with different seed to get different noise realizations tot_s=tot_s+s terr_s=terr_s+e if (i%100==0): - print i,s,e,tot_s,terr_s, '%e' % ((1.0*terr_s)/tot_s) + print(i,s,e,tot_s,terr_s, '%e' % ((1.0*terr_s) / tot_s)) # estimate of the (short) error rate - print tot_s,terr_s, '%e' % ((1.0*terr_s)/tot_s) + print(tot_s,terr_s, '%e' % ((1.0*terr_s) / tot_s)) if __name__ == '__main__': diff --git a/gr-trellis/doc/test_viterbi_equalization1.py b/gr-trellis/doc/test_viterbi_equalization1.py index c1a831d0bb..95cb119edf 100755..100644 --- a/gr-trellis/doc/test_viterbi_equalization1.py +++ b/gr-trellis/doc/test_viterbi_equalization1.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function +from __future__ import division +from __future__ import unicode_literals from gnuradio import gr from gnuradio import audio from gnuradio import trellis, digital, filter, blocks @@ -34,7 +37,7 @@ def run_test (f,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constel # CHANNEL isi = filter.fir_filter_fff(1,channel) add = blocks.add_ff() - noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) + noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0 / 2),seed) # RX skip = blocks.skiphead(gr.sizeof_float, L) # skip the first L samples since you know they are coming from the L zero symbols @@ -78,14 +81,14 @@ def main(args): modulation = fsm_utils.pam4 # see fsm_utlis.py for available predefined modulations channel = fsm_utils.c_channel # see fsm_utlis.py for available predefined test channels f=trellis.fsm(len(modulation[1]),len(channel)) # generate the FSM automatically - bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol - K=Kb/bitspersymbol # packet size in trellis steps + bitspersymbol = int(round(math.log(f.I()) / math.log(2))) # bits per FSM input symbol + K=Kb / bitspersymbol # packet size in trellis steps tot_channel = fsm_utils.make_isi_lookup(modulation,channel,True) # generate the lookup table (normalize energy to 1) dimensionality = tot_channel[0] tot_constellation = tot_channel[1] - N0=pow(10.0,-esn0_db/10.0); # noise variance - if len(tot_constellation)/dimensionality != f.O(): + N0=pow(10.0,-esn0_db / 10.0); # noise variance + if len(tot_constellation) / dimensionality != f.O(): sys.stderr.write ('Incompatible FSM output cardinality and lookup table size.\n') sys.exit (1) @@ -94,14 +97,14 @@ def main(args): terr_p=0 # total number of packets in error for i in range(rep): - (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 data and noise realizations + (s,e)=run_test(f,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,N0,-int(666+i)) # run experiment with different seed to get different data and noise realizations tot_s=tot_s+s terr_s=terr_s+e terr_p=terr_p+(terr_s!=0) if ((i+1)%100==0) : # display progress - print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s) + print(i+1,terr_p, '%.2e' % ((1.0*terr_p) / (i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s) / tot_s)) # estimate of the (short or symbol) error rate - print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s)/tot_s) + print(rep,terr_p, '%.2e' % ((1.0*terr_p) / (i+1)),tot_s,terr_s, '%.2e' % ((1.0*terr_s) / tot_s)) if __name__ == '__main__': diff --git a/gr-trellis/examples/python/test_tcm.py b/gr-trellis/examples/python/test_tcm.py index dfc565616e..e2eb87fcd7 100755..100644 --- a/gr-trellis/examples/python/test_tcm.py +++ b/gr-trellis/examples/python/test_tcm.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function +from __future__ import division +from __future__ import unicode_literals from gnuradio import gr from gnuradio import trellis, digital, blocks from gnuradio import eng_notation @@ -33,7 +36,7 @@ def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): # CHANNEL add = blocks.add_ff() - noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),long(seed)) + noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0 / 2),int(seed)) # RX va = trellis.viterbi_combined_fs(f,K,0,0,dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # Put -1 if the Initial/Final states are not set. @@ -56,7 +59,7 @@ def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): #print "final state = " , enc.ST() if len(dst.data()) != len(packet): - print "Error: not enough data:", len(dst.data()), len(packet) + print("Error: not enough data:", len(dst.data()), len(packet)) ntotal=len(packet) nwrong = sum(abs(packet-numpy.array(dst.data()))); return (ntotal,nwrong,abs(packet-numpy.array(dst.data()))) @@ -73,7 +76,7 @@ def main(): (options, args) = parser.parse_args () if len(args) != 0: parser.print_help() - raise SystemExit, 1 + raise SystemExit(1) fname=options.fsm_file esn0_db=float(options.esn0) @@ -84,20 +87,20 @@ def main(): # alternatively you can specify the fsm from its generator matrix #f=trellis.fsm(1,2,[5,7]) Kb=1024*16 # packet size in bits (make it multiple of 16 so it can be packed in a short) - bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol - K=Kb/bitspersymbol # packet size in trellis steps + bitspersymbol = int(round(math.log(f.I()) / math.log(2))) # bits per FSM input symbol + K=Kb / bitspersymbol # packet size in trellis steps modulation = fsm_utils.psk4 # see fsm_utlis.py for available predefined modulations dimensionality = modulation[0] constellation = modulation[1] - if len(constellation)/dimensionality != f.O(): + if len(constellation) / dimensionality != f.O(): sys.stderr.write ('Incompatible FSM output cardinality and modulation size.\n') sys.exit (1) # calculate average symbol energy Es = 0 for i in range(len(constellation)): Es = Es + constellation[i]**2 - Es = Es / (len(constellation)/dimensionality) - N0=Es/pow(10.0,esn0_db/10.0); # calculate noise variance + Es = Es / (old_div(len(constellation,dimensionality))) + N0=Es / pow(10.0,old_div(esn0_db,10.0)); # calculate noise variance tot_b=0 # total number of transmitted bits terr_b=0 # total number of bits in error @@ -108,14 +111,14 @@ def main(): terr_b=terr_b+e terr_p=terr_p+(e!=0) if ((i+1)%100==0) : # display progress - print i+1,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_b,terr_b, '%.2e' % ((1.0*terr_b)/tot_b) + print(i+1,terr_p, '%.2e' % ((1.0*terr_p) / (i+1)),tot_b,terr_b, '%.2e' % ((1.0*terr_b) / tot_b)) if e!=0: - print "rep=",i, e + print("rep=",i, e) for k in range(Kb): if pattern[k]!=0: - print k + print(k) # estimate of the bit error rate - print rep,terr_p, '%.2e' % ((1.0*terr_p)/(i+1)),tot_b,terr_b, '%.2e' % ((1.0*terr_b)/tot_b) + print(rep,terr_p, '%.2e' % ((1.0*terr_p) / (i+1)),tot_b,terr_b, '%.2e' % ((1.0*terr_b) / tot_b)) diff --git a/gr-trellis/python/trellis/CMakeLists.txt b/gr-trellis/python/trellis/CMakeLists.txt index 10fd9d5c0e..94a160b310 100644 --- a/gr-trellis/python/trellis/CMakeLists.txt +++ b/gr-trellis/python/trellis/CMakeLists.txt @@ -44,6 +44,6 @@ if(ENABLE_TESTING) file(GLOB py_qa_test_files "qa_*.py") foreach(py_qa_test_file ${py_qa_test_files}) get_filename_component(py_qa_test_name ${py_qa_test_file} NAME_WE) - GR_ADD_TEST(${py_qa_test_name} ${QA_PYTHON_EXECUTABLE} ${PYTHON_DASH_B} ${py_qa_test_file}) + GR_ADD_TEST(${py_qa_test_name} ${QA_PYTHON_EXECUTABLE} -B ${py_qa_test_file}) endforeach(py_qa_test_file) endif(ENABLE_TESTING) diff --git a/gr-trellis/python/trellis/__init__.py b/gr-trellis/python/trellis/__init__.py index a6b5ed0038..445a381424 100644 --- a/gr-trellis/python/trellis/__init__.py +++ b/gr-trellis/python/trellis/__init__.py @@ -21,15 +21,16 @@ ''' Blocks and utilities for trellis coding and related. ''' +from __future__ import unicode_literals # The presence of this file turns this directory into a Python package import os try: - from trellis_swig import * + from .trellis_swig import * except ImportError: dirname, filename = os.path.split(os.path.abspath(__file__)) __path__.append(os.path.join(dirname, "..", "..", "swig")) - from trellis_swig import * + from .trellis_swig import * # import any pure python here diff --git a/gr-trellis/python/trellis/fsm_utils.py b/gr-trellis/python/trellis/fsm_utils.py index 72aa1d3660..efc526c0e7 100755..100644 --- a/gr-trellis/python/trellis/fsm_utils.py +++ b/gr-trellis/python/trellis/fsm_utils.py @@ -20,11 +20,13 @@ # Boston, MA 02110-1301, USA. # +from __future__ import print_function +from __future__ import division +from __future__ import unicode_literals -import re import math import sys -import operator + import numpy #from gnuradio import trellis @@ -32,7 +34,7 @@ import numpy try: import scipy.linalg except ImportError: - print "Error: Program requires scipy (see: www.scipy.org)." + print("Error: Program requires scipy (see: www.scipy.org).") sys.exit(1) @@ -43,13 +45,13 @@ except ImportError: # to base 'base' (most significant symbol first). ###################################################################### def dec2base(num,base,l): - s=range(l) + s=list(range(l)) n=num for i in range(l): s[l-i-1]=n%base - n=int(n/base) + n=int(n / base) if n!=0: - print 'Number ', num, ' requires more than ', l, 'digits.' + print('Number ', num, ' requires more than ', l, 'digits.') return s @@ -84,9 +86,9 @@ def make_isi_lookup(mod,channel,normalize): for i in range(len(channel)): p = p + channel[i]**2 for i in range(len(channel)): - channel[i] = channel[i]/math.sqrt(p) + channel[i] = channel[i] / math.sqrt(p) - lookup=range(len(constellation)**len(channel)) + lookup=list(range(len(constellation)**len(channel))) for o in range(len(constellation)**len(channel)): ss=dec2base(o,len(constellation),len(channel)) ll=0 @@ -109,11 +111,11 @@ def make_isi_lookup(mod,channel,normalize): ###################################################################### def make_cpm_signals(K,P,M,L,q,frac): - Q=numpy.size(q)/L - h=(1.0*K)/P + Q=numpy.size(q) / L + h=(1.0*K) / P f0=-h*(M-1)/2 dt=0.0; # maybe start at t=0.5 - t=(dt+numpy.arange(0,Q))/Q + t=(dt+numpy.arange(0 / Q),Q) qq=numpy.zeros(Q) for m in range(L): qq=qq + q[m*Q:m*Q+Q] @@ -122,46 +124,46 @@ def make_cpm_signals(K,P,M,L,q,frac): X=(M**L)*P PSI=numpy.empty((X,Q)) for x in range(X): - xv=dec2base(x/P,M,L) + xv=dec2base(x / P,M,L) xv=numpy.append(xv, x%P) qq1=numpy.zeros(Q) for m in range(L): qq1=qq1+xv[m]*q[m*Q:m*Q+Q] psi=2*math.pi*h*xv[-1]+4*math.pi*h*qq1+w - #print psi + #print(psi) PSI[x]=psi PSI = numpy.transpose(PSI) SS=numpy.exp(1j*PSI) # contains all signals as columns - #print SS + #print(SS) # Now we need to orthogonalize the signals F = scipy.linalg.orth(SS) # find an orthonormal basis for SS - #print numpy.dot(numpy.transpose(F.conjugate()),F) # check for orthonormality + #print(numpy.dot(numpy.transpose(F.conjugate()),F) # check for orthonormality) S = numpy.dot(numpy.transpose(F.conjugate()),SS) - #print F - #print S + #print(F) + #print(S) # We only want to keep those dimensions that contain most # of the energy of the overall constellation (eg, frac=0.9 ==> 90%) # evaluate mean energy in each dimension - E=numpy.sum(numpy.absolute(S)**2,axis=1)/Q - E=E/numpy.sum(E) - #print E + E=numpy.sum(numpy.absolute(S)**2, axis=1) / Q + E=E / numpy.sum(E) + #print(E) Es = -numpy.sort(-E) Esi = numpy.argsort(-E) - #print Es - #print Esi + #print(Es) + #print(Esi) Ecum=numpy.cumsum(Es) - #print Ecum + #print(Ecum) v0=numpy.searchsorted(Ecum,frac) N = v0+1 - #print v0 - #print Esi[0:v0+1] + #print(v0) + #print(Esi[0:v0+1]) Ff=numpy.transpose(numpy.transpose(F)[Esi[0:v0+1]]) - #print Ff + #print(Ff) Sf = S[Esi[0:v0+1]] - #print Sf + #print(Sf) return (f0,SS,S,F,Sf,Ff,N) diff --git a/gr-trellis/python/trellis/qa_trellis.py b/gr-trellis/python/trellis/qa_trellis.py index 86c740a730..f248832204 100755..100644 --- a/gr-trellis/python/trellis/qa_trellis.py +++ b/gr-trellis/python/trellis/qa_trellis.py @@ -20,6 +20,8 @@ # Boston, MA 02110-1301, USA. # +from __future__ import division + import math import os @@ -71,7 +73,7 @@ class test_trellis (gr_unittest.TestCase): Runs some coding/decoding tests with a few different FSM specs. """ - for name, args in fsm_args.items(): + for name, args in list(fsm_args.items()): constellation = constells[args[2]] fsms = trellis.fsm(*args) noise = 0.1 @@ -85,7 +87,7 @@ class trellis_tb(gr.top_block): """ A simple top block for use testing gr-trellis. """ - def __init__(self, constellation, f, N0=0.25, seed=-666L): + def __init__(self, constellation, f, N0=0.25, seed=-666): """ constellation - a constellation object used for modulation. f - a finite state machine specification used for coding. @@ -96,14 +98,14 @@ class trellis_tb(gr.top_block): # packet size in bits (make it multiple of 16 so it can be packed in a short) packet_size = 1024*16 # bits per FSM input symbol - bitspersymbol = int(round(math.log(f.I())/math.log(2))) # bits per FSM input symbol + bitspersymbol = int(round(math.log(f.I()) / math.log(2))) # bits per FSM input symbol # packet size in trellis steps - K = packet_size/bitspersymbol + K = packet_size // bitspersymbol # TX src = blocks.lfsr_32k_source_s() # packet size in shorts - src_head = blocks.head(gr.sizeof_short, packet_size/16) + src_head = blocks.head(gr.sizeof_short, packet_size // 16) # unpack shorts to symbols compatible with the FSM input cardinality s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol, gr.GR_MSB_FIRST) # initial FSM state = 0 @@ -112,7 +114,7 @@ class trellis_tb(gr.top_block): # CHANNEL add = blocks.add_cc() - noise = analog.noise_source_c(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) + noise = analog.noise_source_c(analog.GR_GAUSSIAN,math.sqrt(N0 / 2),seed) # RX # data preprocessing to generate metrics for Viterbi diff --git a/gr-trellis/swig/trellis_swig.py.in b/gr-trellis/swig/trellis_swig.py.in index fac5f631e2..f49e04e0a3 100644 --- a/gr-trellis/swig/trellis_swig.py.in +++ b/gr-trellis/swig/trellis_swig.py.in @@ -19,5 +19,7 @@ # Boston, MA 02110-1301, USA. # -from trellis_swig0 import * -from trellis_swig1 import * +from __future__ import absolute_import + +from .trellis_swig0 import * +from .trellis_swig1 import * |