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