c@174: #!/usr/bin/env python c@174: c@174: # use FFTPACK as a baseline c@174: import FFT c@174: from Numeric import * c@174: import math c@174: import random c@174: import sys c@174: import struct c@174: import fft c@174: c@174: pi=math.pi c@174: e=math.e c@174: j=complex(0,1) c@174: lims=(-32768,32767) c@174: c@174: def randbuf(n,cpx=1): c@174: res = array( [ random.uniform( lims[0],lims[1] ) for i in range(n) ] ) c@174: if cpx: c@174: res = res + j*randbuf(n,0) c@174: return res c@174: c@174: def main(): c@174: from getopt import getopt c@174: import popen2 c@174: opts,args = getopt( sys.argv[1:],'u:n:Rt:' ) c@174: opts=dict(opts) c@174: exitcode=0 c@174: c@174: util = opts.get('-u','./kf_float') c@174: c@174: try: c@174: dims = [ int(d) for d in opts['-n'].split(',')] c@174: cpx = opts.get('-R') is None c@174: fmt=opts.get('-t','f') c@174: except KeyError: c@174: sys.stderr.write(""" c@174: usage: compfft.py c@174: -n d1[,d2,d3...] : FFT dimension(s) c@174: -u utilname : see sample_code/fftutil.c, default = ./kf_float c@174: -R : real-optimized version\n""") c@174: sys.exit(1) c@174: c@174: x = fft.make_random( dims ) c@174: c@174: cmd = '%s -n %s ' % ( util, ','.join([ str(d) for d in dims]) ) c@174: if cpx: c@174: xout = FFT.fftnd(x) c@174: xout = reshape(xout,(size(xout),)) c@174: else: c@174: cmd += '-R ' c@174: xout = FFT.real_fft(x) c@174: c@174: proc = popen2.Popen3( cmd , bufsize=len(x) ) c@174: c@174: proc.tochild.write( dopack( x , fmt ,cpx ) ) c@174: proc.tochild.close() c@174: xoutcomp = dounpack( proc.fromchild.read( ) , fmt ,1 ) c@174: #xoutcomp = reshape( xoutcomp , dims ) c@174: c@174: sig = xout * conjugate(xout) c@174: sigpow = sum( sig ) c@174: c@174: diff = xout-xoutcomp c@174: noisepow = sum( diff * conjugate(diff) ) c@174: c@174: snr = 10 * math.log10(abs( sigpow / noisepow ) ) c@174: if snr<100: c@174: print xout c@174: print xoutcomp c@174: exitcode=1 c@174: print 'NFFT=%s,SNR = %f dB' % (str(dims),snr) c@174: sys.exit(exitcode) c@174: c@174: def dopack(x,fmt,cpx): c@174: x = reshape( x, ( size(x),) ) c@174: if cpx: c@174: s = ''.join( [ struct.pack('ff',c.real,c.imag) for c in x ] ) c@174: else: c@174: s = ''.join( [ struct.pack('f',c) for c in x ] ) c@174: return s c@174: c@174: def dounpack(x,fmt,cpx): c@174: uf = fmt * ( len(x) / 4 ) c@174: s = struct.unpack(uf,x) c@174: if cpx: c@174: return array(s[::2]) + array( s[1::2] )*j c@174: else: c@174: return array(s ) c@174: c@174: if __name__ == "__main__": c@174: main()