c@174: #!/usr/bin/env python c@174: c@174: import math c@174: import sys c@174: import os c@174: import random c@174: import struct c@174: import popen2 c@174: import getopt c@174: import numpy c@174: c@174: pi=math.pi c@174: e=math.e c@174: j=complex(0,1) c@174: c@174: doreal=0 c@174: c@174: datatype = os.environ.get('DATATYPE','float') c@174: c@174: util = '../tools/fft_' + datatype c@174: minsnr=90 c@174: if datatype == 'double': c@174: fmt='d' c@174: elif datatype=='int16_t': c@174: fmt='h' c@174: minsnr=10 c@174: elif datatype=='int32_t': c@174: fmt='i' c@174: elif datatype=='simd': c@174: fmt='4f' c@174: sys.stderr.write('testkiss.py does not yet test simd') c@174: sys.exit(0) c@174: elif datatype=='float': c@174: fmt='f' c@174: else: c@174: sys.stderr.write('unrecognized datatype %s\n' % datatype) c@174: sys.exit(1) c@174: c@174: c@174: def dopack(x,cpx=1): c@174: x = numpy.reshape( x, ( numpy.size(x),) ) c@174: c@174: if cpx: c@174: s = ''.join( [ struct.pack(fmt*2,c.real,c.imag) for c in x ] ) c@174: else: c@174: s = ''.join( [ struct.pack(fmt,c.real) for c in x ] ) c@174: return s c@174: c@174: def dounpack(x,cpx): c@174: uf = fmt * ( len(x) / struct.calcsize(fmt) ) c@174: s = struct.unpack(uf,x) c@174: if cpx: c@174: return numpy.array(s[::2]) + numpy.array( s[1::2] )*j c@174: else: c@174: return numpy.array(s ) c@174: c@174: def make_random(dims=[1]): c@174: res = [] c@174: for i in range(dims[0]): c@174: if len(dims)==1: c@174: r=random.uniform(-1,1) c@174: if doreal: c@174: res.append( r ) c@174: else: c@174: i=random.uniform(-1,1) c@174: res.append( complex(r,i) ) c@174: else: c@174: res.append( make_random( dims[1:] ) ) c@174: return numpy.array(res) c@174: c@174: def flatten(x): c@174: ntotal = numpy.size(x) c@174: return numpy.reshape(x,(ntotal,)) c@174: c@174: def randmat( ndims ): c@174: dims=[] c@174: for i in range( ndims ): c@174: curdim = int( random.uniform(2,5) ) c@174: if doreal and i==(ndims-1): c@174: curdim = int(curdim/2)*2 # force even last dimension if real c@174: dims.append( curdim ) c@174: return make_random(dims ) c@174: c@174: def test_fft(ndims): c@174: x=randmat( ndims ) c@174: c@174: c@174: if doreal: c@174: xver = numpy.fft.rfftn(x) c@174: else: c@174: xver = numpy.fft.fftn(x) c@174: c@174: open('/tmp/fftexp.dat','w').write(dopack( flatten(xver) , True ) ) c@174: c@174: x2=dofft(x,doreal) c@174: err = xver - x2 c@174: errf = flatten(err) c@174: xverf = flatten(xver) c@174: errpow = numpy.vdot(errf,errf)+1e-10 c@174: sigpow = numpy.vdot(xverf,xverf)+1e-10 c@174: snr = 10*math.log10(abs(sigpow/errpow) ) c@174: print 'SNR (compared to NumPy) : %.1fdB' % float(snr) c@174: c@174: if snr