shun_iwasawa a35b8f
#!/usr/bin/env python
shun_iwasawa a35b8f
shun_iwasawa a35b8f
from Numeric import *
shun_iwasawa a35b8f
from FFT import *
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def make_random(len):
shun_iwasawa a35b8f
    import random
shun_iwasawa a35b8f
    res=[]
shun_iwasawa a35b8f
    for i in range(int(len)):
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
    return res
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def slowfilter(sig,h):
shun_iwasawa a35b8f
    translen = len(h)-1
shun_iwasawa a35b8f
    return convolve(sig,h)[translen:-translen]
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def nextpow2(x):
shun_iwasawa a35b8f
    return 2 ** math.ceil(math.log(x)/math.log(2))
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def fastfilter(sig,h,nfft=None):
shun_iwasawa a35b8f
    if nfft is None:
shun_iwasawa a35b8f
        nfft = int( nextpow2( 2*len(h) ) )
shun_iwasawa a35b8f
    H = fft( h , nfft )
shun_iwasawa a35b8f
    scraplen = len(h)-1
shun_iwasawa a35b8f
    keeplen = nfft-scraplen
shun_iwasawa a35b8f
    res=[]
shun_iwasawa a35b8f
    isdone = 0
shun_iwasawa a35b8f
    lastidx = nfft
shun_iwasawa a35b8f
    idx0 = 0
shun_iwasawa a35b8f
    while not isdone:
shun_iwasawa a35b8f
        idx1 = idx0 + nfft
shun_iwasawa a35b8f
        if idx1 >= len(sig):
shun_iwasawa a35b8f
            idx1 = len(sig)
shun_iwasawa a35b8f
            lastidx = idx1-idx0
shun_iwasawa a35b8f
            if lastidx <= scraplen:
shun_iwasawa a35b8f
                break
shun_iwasawa a35b8f
            isdone = 1
shun_iwasawa a35b8f
        Fss = fft(sig[idx0:idx1],nfft)
shun_iwasawa a35b8f
        fm = Fss * H
shun_iwasawa a35b8f
        m = inverse_fft(fm)
shun_iwasawa a35b8f
        res.append( m[scraplen:lastidx] )
shun_iwasawa a35b8f
        idx0 += keeplen
shun_iwasawa a35b8f
    return concatenate( res )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def main():
shun_iwasawa a35b8f
    import sys
shun_iwasawa a35b8f
    from getopt import getopt
shun_iwasawa a35b8f
    opts,args = getopt(sys.argv[1:],'rn:l:')
shun_iwasawa a35b8f
    opts=dict(opts)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    siglen = int(opts.get('-l',1e4 ) )
shun_iwasawa a35b8f
    hlen =50 
shun_iwasawa a35b8f
 
shun_iwasawa a35b8f
    nfft = int(opts.get('-n',128) )
shun_iwasawa a35b8f
    usereal = opts.has_key('-r')
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    print 'nfft=%d'%nfft
shun_iwasawa a35b8f
    # make a signal
shun_iwasawa a35b8f
    sig = make_random( siglen )
shun_iwasawa a35b8f
    # make an impulse response
shun_iwasawa a35b8f
    h = make_random( hlen )
shun_iwasawa a35b8f
    #h=[1]*2+[0]*3
shun_iwasawa a35b8f
    if usereal:
shun_iwasawa a35b8f
        sig=[c.real for c in sig]
shun_iwasawa a35b8f
        h=[c.real for c in h]
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    # perform MAC filtering
shun_iwasawa a35b8f
    yslow = slowfilter(sig,h)
shun_iwasawa a35b8f
    #print '<yslow>',yslow,'</yslow>'
shun_iwasawa a35b8f
    #yfast = fastfilter(sig,h,nfft)
shun_iwasawa a35b8f
    yfast = utilfastfilter(sig,h,nfft,usereal)
shun_iwasawa a35b8f
    #print yfast
shun_iwasawa a35b8f
    print 'len(yslow)=%d'%len(yslow)
shun_iwasawa a35b8f
    print 'len(yfast)=%d'%len(yfast)
shun_iwasawa a35b8f
    diff = yslow-yfast
shun_iwasawa a35b8f
    snr = 10*log10( abs( vdot(yslow,yslow) / vdot(diff,diff) ) )
shun_iwasawa a35b8f
    print 'snr=%s' % snr
shun_iwasawa a35b8f
    if snr < 10.0:
shun_iwasawa a35b8f
        print 'h=',h
shun_iwasawa a35b8f
        print 'sig=',sig[:5],'...'
shun_iwasawa a35b8f
        print 'yslow=',yslow[:5],'...'
shun_iwasawa a35b8f
        print 'yfast=',yfast[:5],'...'
shun_iwasawa a35b8f
shun_iwasawa a35b8f
def utilfastfilter(sig,h,nfft,usereal):
shun_iwasawa a35b8f
    import compfft
shun_iwasawa a35b8f
    import os
shun_iwasawa a35b8f
    open( 'sig.dat','w').write( compfft.dopack(sig,'f',not usereal) )
shun_iwasawa a35b8f
    open( 'h.dat','w').write( compfft.dopack(h,'f',not usereal) )
shun_iwasawa a35b8f
    if usereal: 
shun_iwasawa a35b8f
        util = './fastconvr' 
shun_iwasawa a35b8f
    else:
shun_iwasawa a35b8f
        util = './fastconv'
shun_iwasawa a35b8f
    cmd = 'time %s -n %d -i sig.dat -h h.dat -o out.dat' % (util, nfft)
shun_iwasawa a35b8f
    print cmd
shun_iwasawa a35b8f
    ec  = os.system(cmd)
shun_iwasawa a35b8f
    print 'exited->',ec
shun_iwasawa a35b8f
    return compfft.dounpack(open('out.dat').read(),'f',not usereal)
shun_iwasawa a35b8f
shun_iwasawa a35b8f
if __name__ == "__main__":
shun_iwasawa a35b8f
    main()