this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

I had to fix some python3 incompatibilities and realized how embarrassing the code was. I refactored to make it look a little more like it was written by someone who knows Python.

+82 -105
+82 -105
test/testkiss.py
··· 4 4 # 5 5 # SPDX-License-Identifier: BSD-3-Clause 6 6 # See COPYING file for more information. 7 - from __future__ import division,print_function 7 + from __future__ import absolute_import, division, print_function 8 8 import math 9 9 import sys 10 10 import os 11 11 import random 12 12 import struct 13 13 import getopt 14 - import numpy 15 - 16 - pi=math.pi 17 - e=math.e 18 - 19 - doreal=0 14 + import numpy as np 20 15 21 - datatype = os.environ.get('DATATYPE','float') 16 + po = math.pi 17 + e = math.e 18 + do_real = False 19 + datatype = os.environ.get('DATATYPE', 'float') 22 20 23 21 util = '../tools/fft_' + datatype 24 - minsnr=90 22 + minsnr = 90 25 23 if datatype == 'double': 26 - fmt='d' 27 - elif datatype=='int16_t': 28 - fmt='h' 29 - minsnr=10 30 - elif datatype=='int32_t': 31 - fmt='i' 32 - elif datatype=='simd': 33 - fmt='4f' 24 + dtype = np.float64 25 + elif datatype == 'float': 26 + dtype = np.float32 27 + elif datatype == 'int16_t': 28 + dtype = np.int16 29 + minsnr = 10 30 + elif datatype == 'int32_t': 31 + dtype = np.int32 32 + elif datatype == 'simd': 34 33 sys.stderr.write('testkiss.py does not yet test simd') 35 34 sys.exit(0) 36 - elif datatype=='float': 37 - fmt='f' 38 35 else: 39 - sys.stderr.write('unrecognized datatype %s\n' % datatype) 36 + sys.stderr.write('unrecognized datatype {0}\n'.format(datatype)) 40 37 sys.exit(1) 41 - 42 38 43 - def dopack(x,cpx=1): 44 - x = numpy.reshape( x, ( numpy.size(x),) ) 45 - 46 - if cpx: 47 - s = ''.join( [ struct.pack(fmt*2,c.real,c.imag) for c in x ] ) 39 + def dopack(x): 40 + if np.iscomplexobj(x): 41 + x = x.astype(np.complex128).view(np.float64) 48 42 else: 49 - s = ''.join( [ struct.pack(fmt,c.real) for c in x ] ) 50 - return s 43 + x = x.astype(np.float64) 44 + return x.astype(dtype).tobytes() 51 45 52 - def dounpack(x,cpx): 53 - uf = fmt * ( len(x) // struct.calcsize(fmt) ) 54 - s = struct.unpack(uf,x) 46 + def dounpack(x, cpx): 47 + x = np.frombuffer(x, dtype).astype(np.float64) 55 48 if cpx: 56 - return numpy.array(s[::2]) + numpy.array( s[1::2] )*1j 49 + x = x[::2] + 1j * x[1::2] 50 + return x 51 + 52 + def make_random(shape): 53 + 'create random uniform (-1,1) data of the given shape' 54 + if do_real: 55 + return np.random.uniform(-1, 1, shape) 57 56 else: 58 - return numpy.array(s ) 57 + return (np.random.uniform(-1, 1, shape) + 1j * np.random.uniform(-1, 1, shape)) 59 58 60 - def make_random(dims=[1]): 61 - res = [] 62 - for i in range(dims[0]): 63 - if len(dims)==1: 64 - r=random.uniform(-1,1) 65 - if doreal: 66 - res.append( r ) 67 - else: 68 - i=random.uniform(-1,1) 69 - res.append( complex(r,i) ) 70 - else: 71 - res.append( make_random( dims[1:] ) ) 72 - return numpy.array(res) 59 + def randmat(ndim): 60 + 'create a random multidimensional array in range (-1,1)' 61 + dims = np.random.randint(2, 5, ndim) 62 + if do_real: 63 + dims[-1] = (dims[-1] // 2) * 2 # force even last dimension if real 64 + return make_random(dims) 73 65 74 - def flatten(x): 75 - ntotal = numpy.size(x) 76 - return numpy.reshape(x,(ntotal,)) 66 + def test_fft(ndim): 67 + x = randmat(ndim) 77 68 78 - def randmat( ndims ): 79 - dims=[] 80 - for i in range( ndims ): 81 - curdim = int( random.uniform(2,5) ) 82 - if doreal and i==(ndims-1): 83 - curdim = int(curdim/2)*2 # force even last dimension if real 84 - dims.append( curdim ) 85 - return make_random(dims ) 69 + if do_real: 70 + xver = np.fft.rfftn(x) 71 + else: 72 + xver = np.fft.fftn(x) 86 73 87 - def test_fft(ndims): 88 - x=randmat( ndims ) 89 - 90 - if doreal: 91 - xver = numpy.fft.rfftn(x) 92 - else: 93 - xver = numpy.fft.fftn(x) 94 - 95 - x2=dofft(x,doreal) 74 + x2 = dofft(x, do_real) 96 75 err = xver - x2 97 - errf = flatten(err) 98 - xverf = flatten(xver) 99 - errpow = numpy.vdot(errf,errf)+1e-10 100 - sigpow = numpy.vdot(xverf,xverf)+1e-10 101 - snr = 10*math.log10(abs(sigpow/errpow) ) 102 - print( 'SNR (compared to NumPy) : {0:.1f}dB'.format( float(snr) ) ) 76 + errf = err.ravel() 77 + xverf = xver.ravel() 78 + errpow = np.vdot(errf, errf) + 1e-10 79 + sigpow = np.vdot(xverf, xverf) + 1e-10 80 + snr = 10 * math.log10(abs(sigpow / errpow)) 81 + print('SNR (compared to NumPy) : {0:.1f}dB'.format(float(snr))) 103 82 104 - if snr<minsnr: 105 - print( 'xver=',xver ) 106 - print( 'x2=',x2) 107 - print( 'err',err) 83 + if snr < minsnr: 84 + print('xver=', xver) 85 + print('x2=', x2) 86 + print('err', err) 108 87 sys.exit(1) 109 - 110 - def dofft(x,isreal): 111 - dims=list( numpy.shape(x) ) 112 - x = flatten(x) 88 + 89 + def dofft(x, isreal): 90 + dims = list(np.shape(x)) 91 + x = x.ravel() 113 92 114 - scale=1 115 - if datatype=='int16_t': 93 + scale = 1 94 + if datatype == 'int16_t': 116 95 x = 32767 * x 117 96 scale = len(x) / 32767.0 118 - elif datatype=='int32_t': 97 + elif datatype == 'int32_t': 119 98 x = 2147483647.0 * x 120 99 scale = len(x) / 2147483647.0 121 100 122 - cmd='%s -n ' % util 101 + cmd = util + ' -n ' 123 102 cmd += ','.join([str(d) for d in dims]) 124 - if doreal: 103 + if do_real: 125 104 cmd += ' -R ' 126 105 127 - print( cmd) 106 + print(cmd) 128 107 129 - from subprocess import Popen,PIPE 130 - p = Popen(cmd,shell=True,stdin=PIPE,stdout=PIPE ) 108 + from subprocess import Popen, PIPE 109 + p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) 131 110 132 - p.stdin.write( dopack( x , isreal==False ) ) 111 + p.stdin.write(dopack(x)) 133 112 p.stdin.close() 134 113 135 - res = dounpack( p.stdout.read() , 1 ) 136 - if doreal: 137 - dims[-1] = int( dims[-1]/2 ) + 1 114 + res = dounpack(p.stdout.read(), 1) 115 + if do_real: 116 + dims[-1] = (dims[-1] // 2) + 1 138 117 139 118 res = scale * res 140 119 141 120 p.wait() 142 - return numpy.reshape(res,dims) 121 + return np.reshape(res, dims) 143 122 144 123 def main(): 145 - opts,args = getopt.getopt(sys.argv[1:],'r') 146 - opts=dict(opts) 124 + opts, args = getopt.getopt(sys.argv[1:], 'r') 125 + opts = dict(opts) 126 + global do_real 127 + do_real = '-r' in opts 128 + if do_real: 129 + print('Testing multi-dimensional real FFTs') 130 + else: 131 + print('Testing multi-dimensional FFTs') 147 132 148 - global doreal 149 - doreal = opts.has_key('-r') 133 + for dim in range(1, 4): 134 + test_fft(dim) 150 135 151 - if doreal: 152 - print( 'Testing multi-dimensional real FFTs') 153 - else: 154 - print( 'Testing multi-dimensional FFTs') 155 - 156 - for dim in range(1,4): 157 - test_fft( dim ) 158 136 159 137 if __name__ == "__main__": 160 138 main() 161 -