shun_iwasawa a35b8f
#!/usr/bin/env python
shun_iwasawa a35b8f
shun_iwasawa a35b8f
import math
shun_iwasawa a35b8f
import sys
shun_iwasawa a35b8f
import random
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
shun_iwasawa a35b8f
def fft(f,inv):
shun_iwasawa a35b8f
    n=len(f)
shun_iwasawa a35b8f
    if n==1:
shun_iwasawa a35b8f
        return f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for p in 2,3,5:
shun_iwasawa a35b8f
        if n%p==0:
shun_iwasawa a35b8f
            break
shun_iwasawa a35b8f
    else:
shun_iwasawa a35b8f
        raise Exception('%s not factorable ' % n)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    m = n/p
shun_iwasawa a35b8f
    Fout=[]
shun_iwasawa a35b8f
    for q in range(p): # 0,1
shun_iwasawa a35b8f
        fp = f[q::p]  # every p'th time sample
shun_iwasawa a35b8f
        Fp = fft( fp ,inv)
shun_iwasawa a35b8f
        Fout.extend( Fp )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for u in range(m):
shun_iwasawa a35b8f
        scratch = Fout[u::m] # u to end in strides of m
shun_iwasawa a35b8f
        for q1 in range(p):
shun_iwasawa a35b8f
            k = q1*m + u  # indices to Fout above that became scratch
shun_iwasawa a35b8f
            Fout[ k ] = scratch[0] # cuz e**0==1 in loop below
shun_iwasawa a35b8f
            for q in range(1,p):
shun_iwasawa a35b8f
                if inv:
shun_iwasawa a35b8f
                    t = e ** ( j*2*pi*k*q/n )
shun_iwasawa a35b8f
                else:                    
shun_iwasawa a35b8f
                    t = e ** ( -j*2*pi*k*q/n )
shun_iwasawa a35b8f
                Fout[ k ] += scratch[q] * t
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    return Fout
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def rifft(F):
shun_iwasawa a35b8f
    N = len(F) - 1
shun_iwasawa a35b8f
    Z = [0] * (N)
shun_iwasawa a35b8f
    for k in range(N):
shun_iwasawa a35b8f
        Fek = ( F[k] + F[-k-1].conjugate() )
shun_iwasawa a35b8f
        Fok = ( F[k] - F[-k-1].conjugate() ) * e ** (j*pi*k/N)
shun_iwasawa a35b8f
        Z[k] = Fek + j*Fok
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    fp = fft(Z , 1)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    f = []
shun_iwasawa a35b8f
    for c in fp:
shun_iwasawa a35b8f
        f.append(c.real)
shun_iwasawa a35b8f
        f.append(c.imag)
shun_iwasawa a35b8f
    return f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def real_fft( f,inv ):
shun_iwasawa a35b8f
    if inv:
shun_iwasawa a35b8f
        return rifft(f)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    N = len(f) / 2
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    res = f[::2]
shun_iwasawa a35b8f
    ims = f[1::2]
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    fp = [ complex(r,i) for r,i in zip(res,ims) ]
shun_iwasawa a35b8f
    print 'fft input ', fp
shun_iwasawa a35b8f
    Fp = fft( fp ,0 )
shun_iwasawa a35b8f
    print 'fft output ', Fp
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    F = [ complex(0,0) ] * ( N+1 )
shun_iwasawa a35b8f
    
shun_iwasawa a35b8f
    F[0] = complex( Fp[0].real + Fp[0].imag , 0 ) 
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for k in range(1,N/2+1):
shun_iwasawa a35b8f
        tw = e ** ( -j*pi*(.5+float(k)/N ) )
shun_iwasawa a35b8f
        
shun_iwasawa a35b8f
        F1k = Fp[k] + Fp[N-k].conjugate()
shun_iwasawa a35b8f
        F2k = Fp[k] - Fp[N-k].conjugate()
shun_iwasawa a35b8f
        F2k *= tw
shun_iwasawa a35b8f
        F[k] = ( F1k + F2k ) * .5
shun_iwasawa a35b8f
        F[N-k] = ( F1k - F2k ).conjugate() * .5
shun_iwasawa a35b8f
        #F[N-k] = ( F1kp + e ** ( -j*pi*(.5+float(N-k)/N ) ) * F2kp ) * .5
shun_iwasawa a35b8f
        #F[N-k] = ( F1k.conjugate() - tw.conjugate() * F2k.conjugate() ) * .5
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    F[N] = complex( Fp[0].real - Fp[0].imag , 0 ) 
shun_iwasawa a35b8f
    return F
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def main():
shun_iwasawa a35b8f
    #fft_func = fft
shun_iwasawa a35b8f
    fft_func = real_fft
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    tvec = [0.309655,0.815653,0.768570,0.591841,0.404767,0.637617,0.007803,0.012665]
shun_iwasawa a35b8f
    Ftvec = [ complex(r,i) for r,i in zip(
shun_iwasawa a35b8f
                [3.548571,-0.378761,-0.061950,0.188537,-0.566981,0.188537,-0.061950,-0.378761],
shun_iwasawa a35b8f
                [0.000000,-1.296198,-0.848764,0.225337,0.000000,-0.225337,0.848764,1.296198] ) ]
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    F = fft_func( tvec,0 )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    nerrs= 0
shun_iwasawa a35b8f
    for i in range(len(Ftvec)/2 + 1):
shun_iwasawa a35b8f
        if abs( F[i] - Ftvec[i] )> 1e-5:
shun_iwasawa a35b8f
            print 'F[%d]: %s != %s' % (i,F[i],Ftvec[i])
shun_iwasawa a35b8f
            nerrs += 1
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    print '%d errors in forward fft' % nerrs
shun_iwasawa a35b8f
    if nerrs:
shun_iwasawa a35b8f
        return
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    trec = fft_func( F , 1 )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for i in range(len(trec) ):
shun_iwasawa a35b8f
        trec[i] /= len(trec)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for i in range(len(tvec) ):
shun_iwasawa a35b8f
        if abs( trec[i] - tvec[i] )> 1e-5:
shun_iwasawa a35b8f
            print 't[%d]: %s != %s' % (i,tvec[i],trec[i])
shun_iwasawa a35b8f
            nerrs += 1
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    print '%d errors in reverse fft' % nerrs
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def make_random(dims=[1]):
shun_iwasawa a35b8f
    import Numeric 
shun_iwasawa a35b8f
    res = []
shun_iwasawa a35b8f
    for i in range(dims[0]):
shun_iwasawa a35b8f
        if len(dims)==1:
shun_iwasawa a35b8f
            r=random.uniform(-1,1)
shun_iwasawa a35b8f
            i=random.uniform(-1,1)
shun_iwasawa a35b8f
            res.append( complex(r,i) )
shun_iwasawa a35b8f
        else:
shun_iwasawa a35b8f
            res.append( make_random( dims[1:] ) )
shun_iwasawa a35b8f
    return Numeric.array(res)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def flatten(x):
shun_iwasawa a35b8f
    import Numeric
shun_iwasawa a35b8f
    ntotal = Numeric.product(Numeric.shape(x))
shun_iwasawa a35b8f
    return Numeric.reshape(x,(ntotal,))
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def randmat( ndims ):
shun_iwasawa a35b8f
    dims=[]
shun_iwasawa a35b8f
    for i in range( ndims ):
shun_iwasawa a35b8f
        curdim = int( random.uniform(2,4) )
shun_iwasawa a35b8f
        dims.append( curdim )
shun_iwasawa a35b8f
    return make_random(dims )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def test_fftnd(ndims=3):
shun_iwasawa a35b8f
    import FFT
shun_iwasawa a35b8f
    import Numeric
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    x=randmat( ndims )
shun_iwasawa a35b8f
    print 'dimensions=%s' % str( Numeric.shape(x) )
shun_iwasawa a35b8f
    #print 'x=%s' %str(x)
shun_iwasawa a35b8f
    xver = FFT.fftnd(x)
shun_iwasawa a35b8f
    x2=myfftnd(x)
shun_iwasawa a35b8f
    err = xver - x2
shun_iwasawa a35b8f
    errf = flatten(err)
shun_iwasawa a35b8f
    xverf = flatten(xver)
shun_iwasawa a35b8f
    errpow = Numeric.vdot(errf,errf)+1e-10
shun_iwasawa a35b8f
    sigpow = Numeric.vdot(xverf,xverf)+1e-10
shun_iwasawa a35b8f
    snr = 10*math.log10(abs(sigpow/errpow) )
shun_iwasawa a35b8f
    if snr<80:
shun_iwasawa a35b8f
        print xver
shun_iwasawa a35b8f
        print x2
shun_iwasawa a35b8f
    print 'SNR=%sdB' % str( snr )
shun_iwasawa a35b8f
 
shun_iwasawa a35b8f
def myfftnd(x):
shun_iwasawa a35b8f
    import Numeric
shun_iwasawa a35b8f
    xf = flatten(x)
shun_iwasawa a35b8f
    Xf = fftndwork( xf , Numeric.shape(x) )
shun_iwasawa a35b8f
    return Numeric.reshape(Xf,Numeric.shape(x) )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def fftndwork(x,dims):
shun_iwasawa a35b8f
    import Numeric
shun_iwasawa a35b8f
    dimprod=Numeric.product( dims )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for k in range( len(dims) ):
shun_iwasawa a35b8f
        cur_dim=dims[ k ]
shun_iwasawa a35b8f
        stride=dimprod/cur_dim
shun_iwasawa a35b8f
        next_x = [complex(0,0)]*len(x)
shun_iwasawa a35b8f
        for i in range(stride):
shun_iwasawa a35b8f
            next_x[i*cur_dim:(i+1)*cur_dim] = fft(x[i:(i+cur_dim)*stride:stride],0)
shun_iwasawa a35b8f
        x = next_x
shun_iwasawa a35b8f
    return x
shun_iwasawa a35b8f
shun_iwasawa a35b8f
if __name__ == "__main__":
shun_iwasawa a35b8f
    try:
shun_iwasawa a35b8f
        nd = int(sys.argv[1])
shun_iwasawa a35b8f
    except:
shun_iwasawa a35b8f
        nd=None
shun_iwasawa a35b8f
    if nd:    
shun_iwasawa a35b8f
        test_fftnd( nd )
shun_iwasawa a35b8f
    else:    
shun_iwasawa a35b8f
        sys.exit(0)