Example #1
0
def nsdual(g, wins, nn, M=None):

    M = chkM(M,g)

    # Construct the diagonal of the frame operator matrix explicitly
    x = np.zeros((nn,), dtype=float)
    for gi,mii,sl in izip(g, M, wins):
        xa = np.square(np.fft.fftshift(gi))
        xa *= mii
        x[sl] += xa

        # could be more elegant...
#        (w1a,w1b),(w2a,w2b) = sl
#        x[w1a] += xa[:w1a.stop-w1a.start]
#        xa = xa[w1a.stop-w1a.start:]
#        x[w1b] += xa[:w1b.stop-w1b.start]
#        xa = xa[w1b.stop-w1b.start:]
#        x[w2a] += xa[:w2a.stop-w2a.start]
#        xa = xa[w2a.stop-w2a.start:]
#        x[w2b] += xa[:w2b.stop-w2b.start]
##        xa = xa[w1b.stop-w1b.start:]

    # Using the frame operator and the original window sequence, compute 
    # the dual window sequence
#    gd = [gi/N.fft.ifftshift(N.hstack((x[wi[0][0]],x[wi[0][1]],x[wi[1][0]],x[wi[1][1]]))) for gi,wi in izip(g,wins)]
    gd = [gi/np.fft.ifftshift(x[wi]) for gi,wi in izip(g,wins)]
    return gd
Example #2
0
def nsdual(g, wins, nn, M=None):

    M = chkM(M, g)

    # Construct the diagonal of the frame operator matrix explicitly
    x = np.zeros((nn, ), dtype=float)
    for gi, mii, sl in izip(g, M, wins):
        xa = np.square(np.fft.fftshift(gi))
        xa *= mii
        x[sl] += xa

        # could be more elegant...
#        (w1a,w1b),(w2a,w2b) = sl
#        x[w1a] += xa[:w1a.stop-w1a.start]
#        xa = xa[w1a.stop-w1a.start:]
#        x[w1b] += xa[:w1b.stop-w1b.start]
#        xa = xa[w1b.stop-w1b.start:]
#        x[w2a] += xa[:w2a.stop-w2a.start]
#        xa = xa[w2a.stop-w2a.start:]
#        x[w2b] += xa[:w2b.stop-w2b.start]
##        xa = xa[w1b.stop-w1b.start:]

# Using the frame operator and the original window sequence, compute
# the dual window sequence
#    gd = [gi/N.fft.ifftshift(N.hstack((x[wi[0][0]],x[wi[0][1]],x[wi[1][0]],x[wi[1][1]]))) for gi,wi in izip(g,wins)]
    gd = [gi / np.fft.ifftshift(x[wi]) for gi, wi in izip(g, wins)]
    return gd
Example #3
0
def nsgtf_sl(f_slices, g, wins, nn, M=None, real=False, reducedform=0, measurefft=False, multithreading=False):
    M = chkM(M,g)
    dtype = g[0].dtype
    
    fft = fftp(measure=measurefft, dtype=dtype)
    ifft = ifftp(measure=measurefft, dtype=dtype)
    
    if real:
        assert 0 <= reducedform <= 2
        sl = slice(reducedform,len(g)//2+1-reducedform)
    else:
        sl = slice(0,None)
    
    maxLg = max(int(ceil(float(len(gii))/mii))*mii for mii,gii in izip(M[sl],g[sl]))
    temp0 = None
    
    if multithreading and MP is not None:
        mmap = MP.Pool().map
    else:
        mmap = map

    loopparams = []
    for mii,gii,win_range in izip(M[sl],g[sl],wins[sl]):
        Lg = len(gii)
        col = int(ceil(float(Lg)/mii))
        assert col*mii >= Lg
        gi1 = gii[:(Lg+1)//2]
        gi2 = gii[-(Lg//2):]
        p = (mii,gii,gi1,gi2,win_range,Lg,col)
        loopparams.append(p)

    # main loop over slices
    for f in f_slices:
        Ls = len(f)
        
        # some preparation    
        ft = fft(f)

        if temp0 is None:
            # pre-allocate buffer (delayed because of dtype)
            temp0 = np.empty(maxLg, dtype=ft.dtype)
        
        # A small amount of zero-padding might be needed (e.g. for scale frames)
        if nn > Ls:
            ft = np.concatenate((ft, np.zeros(nn-Ls, dtype=ft.dtype)))
        
        # The actual transform
        c = nsgtf_loop(loopparams, ft, temp0)
            
        # TODO: if matrixform, perform "2D" FFT along one axis
        # this could also be nicely parallelized
        y = mmap(ifft,c)
        
        yield y
Example #4
0
def nsgtf_sl(f_slices,g,wins,nn,M=None,real=False,reducedform=0,measurefft=False):
    M = chkM(M,g)
    
    fft = fftp(measure=measurefft)
    ifft = ifftp(measure=measurefft)
    
    if real:
        assert 0 <= reducedform <= 2
        sl = slice(reducedform,len(g)//2+1-reducedform)
    else:
        sl = slice(0,None)
    
    maxLg = max(int(ceil(float(len(gii))/mii))*mii for mii,gii in izip(M[sl],g[sl]))
    temp0 = None
    
    
    loopparams = []
    for mii,gii,win_range in izip(M[sl],g[sl],wins[sl]):
        Lg = len(gii)
        col = int(ceil(float(Lg)/mii))
        assert col*mii >= Lg
        gi1 = gii[:(Lg+1)//2]
        gi2 = gii[-(Lg//2):]
        p = (mii,gii,gi1,gi2,win_range,Lg,col)
        loopparams.append(p)

    if True or T is None:
        def loop(temp0):
            c = [] # Initialization of the result
                
            # The actual transform
            # TODO: stuff loop into theano
            for mii,gii,gi1,gi2,win_range,Lg,col in loopparams:
    #            Lg = len(gii)            
                # if the number of time channels is too small (mii < Lg), aliasing is introduced
                # wrap around and sum up in the end (below)
    #            col = int(ceil(float(Lg)/mii)) # normally col == 1                        
    #            assert col*mii >= Lg                        
    
                temp = temp0[:col*mii]
    
                # original version
    #            t = ft[win_range]*N.fft.fftshift(N.conj(gii))
    #            temp[:(Lg+1)//2] = t[Lg//2:]  # if mii is odd, this is of length mii-mii//2
    #            temp[-(Lg//2):] = t[:Lg//2]  # if mii is odd, this is of length mii//2
    
                # modified version to avoid superfluous memory allocation
                t1 = temp[:(Lg+1)//2]
                t1[:] = gi1  # if mii is odd, this is of length mii-mii//2
                t2 = temp[-(Lg//2):]
                t2[:] = gi2  # if mii is odd, this is of length mii//2
    
                ftw = ft[win_range]
                t2 *= ftw[:Lg//2]
                t1 *= ftw[Lg//2:]
    
    #            (wh1a,wh1b),(wh2a,wh2b) = win_range
    #            t2[:wh1a.stop-wh1a.start] *= ft[wh1a]
    #            t2[wh1a.stop-wh1a.start:] *= ft[wh1b]
    #            t1[:wh2a.stop-wh2a.start] *= ft[wh2a]
    #            t1[wh2a.stop-wh2a.start:] *= ft[wh2b]
                
                temp[(Lg+1)//2:-(Lg//2)] = 0  # clear gap (if any)
                
                if col > 1:
                    temp = N.sum(temp.reshape((mii,-1)),axis=1)
                else:
                    temp = temp.copy()
     
                c.append(temp)
            return c
    else:
        raise RuntimeError("Theano support not implemented yet")

    for f in f_slices:
        Ls = len(f)
        
        # some preparation    
        ft = fft(f)

        if temp0 is None:
            # pre-allocate buffer (delayed because of dtype)
            temp0 = N.empty(maxLg,dtype=ft.dtype)
        
        # A small amount of zero-padding might be needed (e.g. for scale frames)
        if nn > Ls:
            ft = N.concatenate((ft,N.zeros(nn-Ls,dtype=ft.dtype)))
        
        # The actual transform
        c = loop(temp0)
            
        yield map(ifft,c)  # TODO: if matrixform, perform "2D" FFT along one axis
Example #5
0
def nsgtf_sl(f_slices,
             g,
             wins,
             nn,
             M=None,
             real=False,
             reducedform=0,
             measurefft=False):
    M = chkM(M, g)

    fft = fftp(measure=measurefft)
    ifft = ifftp(measure=measurefft)

    if real:
        assert 0 <= reducedform <= 2
        sl = slice(reducedform, len(g) // 2 + 1 - reducedform)
    else:
        sl = slice(0, None)

    maxLg = max(
        int(ceil(float(len(gii)) / mii)) * mii
        for mii, gii in izip(M[sl], g[sl]))
    temp0 = None

    loopparams = []
    for mii, gii, win_range in izip(M[sl], g[sl], wins[sl]):
        Lg = len(gii)
        col = int(ceil(float(Lg) / mii))
        assert col * mii >= Lg
        gi1 = gii[:(Lg + 1) // 2]
        gi2 = gii[-(Lg // 2):]
        p = (mii, gii, gi1, gi2, win_range, Lg, col)
        loopparams.append(p)

    if True or T is None:

        def loop(temp0):
            c = []  # Initialization of the result

            # The actual transform
            # TODO: stuff loop into theano
            for mii, gii, gi1, gi2, win_range, Lg, col in loopparams:
                #            Lg = len(gii)
                # if the number of time channels is too small (mii < Lg), aliasing is introduced
                # wrap around and sum up in the end (below)
                #            col = int(ceil(float(Lg)/mii)) # normally col == 1
                #            assert col*mii >= Lg

                temp = temp0[:col * mii]

                # original version
                #            t = ft[win_range]*N.fft.fftshift(N.conj(gii))
                #            temp[:(Lg+1)//2] = t[Lg//2:]  # if mii is odd, this is of length mii-mii//2
                #            temp[-(Lg//2):] = t[:Lg//2]  # if mii is odd, this is of length mii//2

                # modified version to avoid superfluous memory allocation
                t1 = temp[:(Lg + 1) // 2]
                t1[:] = gi1  # if mii is odd, this is of length mii-mii//2
                t2 = temp[-(Lg // 2):]
                t2[:] = gi2  # if mii is odd, this is of length mii//2

                ftw = ft[win_range]
                t2 *= ftw[:Lg // 2]
                t1 *= ftw[Lg // 2:]

                #            (wh1a,wh1b),(wh2a,wh2b) = win_range
                #            t2[:wh1a.stop-wh1a.start] *= ft[wh1a]
                #            t2[wh1a.stop-wh1a.start:] *= ft[wh1b]
                #            t1[:wh2a.stop-wh2a.start] *= ft[wh2a]
                #            t1[wh2a.stop-wh2a.start:] *= ft[wh2b]

                temp[(Lg + 1) // 2:-(Lg // 2)] = 0  # clear gap (if any)

                if col > 1:
                    temp = N.sum(temp.reshape((mii, -1)), axis=1)
                else:
                    temp = temp.copy()

                c.append(temp)
            return c
    else:
        raise RuntimeError("Theano support not implemented yet")

    for f in f_slices:
        Ls = len(f)

        # some preparation
        ft = fft(f)

        if temp0 is None:
            # pre-allocate buffer (delayed because of dtype)
            temp0 = N.empty(maxLg, dtype=ft.dtype)

        # A small amount of zero-padding might be needed (e.g. for scale frames)
        if nn > Ls:
            ft = N.concatenate((ft, N.zeros(nn - Ls, dtype=ft.dtype)))

        # The actual transform
        c = loop(temp0)

        yield map(ifft,
                  c)  # TODO: if matrixform, perform "2D" FFT along one axis