示例#1
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
示例#2
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
示例#3
0
def nsigtf_sl(cseq,gd,wins,nn,Ls=None,real=False,reducedform=0,measurefft=False):
    cseq = iter(cseq)

    fft = fftp(measure=measurefft)
    ifft = irfftp(measure=measurefft) if real else ifftp(measure=measurefft)
    
    if real:
        ln = len(gd)//2+1-reducedform*2
        fftsymm = lambda c: N.hstack((c[0],c[-1:0:-1])).conj()
        if reducedform:
            # no coefficients for f=0 and f=fs/2
            symm = lambda fc: chain(fc,imap(fftsymm,fc[::-1]))
            sl = lambda x: chain(x[reducedform:len(gd)//2+1-reducedform],x[len(gd)//2+reducedform:len(gd)+1-reducedform])
        else:
            symm = lambda fc: chain(fc,imap(fftsymm,fc[-2:0:-1]))
            sl = lambda x: x
    else:
        ln = len(gd)
        symm = lambda fc: fc
        sl = lambda x: x
        
    maxLg = max(len(gdii) for gdii in sl(gd))

    # get first slice
    c0 = cseq.next()

    fr = N.empty(nn,dtype=c0[0].dtype)  # Initialize output
    temp0 = N.empty(maxLg,dtype=fr.dtype)  # pre-allocation
    
    loopparams = []
    for gdii,win_range in izip(sl(gd),sl(wins)):
        Lg = len(gdii)
        temp = temp0[:Lg]
        wr1 = win_range[:(Lg)//2]
        wr2 = win_range[-((Lg+1)//2):]
#        wr1,wr2 = win_range
        sl1 = slice(None,(Lg+1)//2)
        sl2 = slice(-(Lg//2),None)
        p = (gdii,wr1,wr2,sl1,sl2,temp)
        loopparams.append(p)

    if True or T is None:
        def loop(fr,fc):
            # The overlap-add procedure including multiplication with the synthesis windows
            # TODO: stuff loop into theano
            for t,(gdii,wr1,wr2,sl1,sl2,temp) in izip(symm(fc),loopparams):
                t1 = temp[sl1]
                t2 = temp[sl2]
                t1[:] = t[sl1]
                t2[:] = t[sl2]
                temp *= gdii
                temp *= len(t)
    
                fr[wr1] += t2
                fr[wr2] += t1
    
    #            wr1a,wr1b = wr1
    #            fr[wr1a] += t2[:wr1a.stop-wr1a.start]
    #            fr[wr1b] += t2[wr1a.stop-wr1a.start:]
    #            wr2a,wr2b = wr2
    #            fr[wr2a] += t1[:wr2a.stop-wr2a.start]
    #            fr[wr2b] += t1[wr2a.stop-wr2a.start:]
    else:
        raise RuntimeError("Theano support not implemented yet")

    for c in chain((c0,),cseq):
        assert len(c) == ln

        fr[:] = 0.
        fc = map(fft,c)  # do transforms on coefficients - TODO: for matrixform we could do a FFT on the whole matrix along one axis
        
        # The overlap-add procedure including multiplication with the synthesis windows
        loop(fr,fc)

        ftr = fr[:nn//2+1] if real else fr

        sig = ifft(ftr,outn=nn)

        sig = sig[:Ls] # Truncate the signal to original length (if given)

        yield sig
示例#4
0
def nsigtf_sl(cseq,
              gd,
              wins,
              nn,
              Ls=None,
              real=False,
              reducedform=0,
              measurefft=False):
    cseq = iter(cseq)

    fft = fftp(measure=measurefft)
    ifft = irfftp(measure=measurefft) if real else ifftp(measure=measurefft)

    if real:
        ln = len(gd) // 2 + 1 - reducedform * 2
        fftsymm = lambda c: N.hstack((c[0], c[-1:0:-1])).conj()
        if reducedform:
            # no coefficients for f=0 and f=fs/2
            symm = lambda fc: chain(fc, imap(fftsymm, fc[::-1]))
            sl = lambda x: chain(
                x[reducedform:len(gd) // 2 + 1 - reducedform], x[len(
                    gd) // 2 + reducedform:len(gd) + 1 - reducedform])
        else:
            symm = lambda fc: chain(fc, imap(fftsymm, fc[-2:0:-1]))
            sl = lambda x: x
    else:
        ln = len(gd)
        symm = lambda fc: fc
        sl = lambda x: x

    maxLg = max(len(gdii) for gdii in sl(gd))

    # get first slice
    c0 = cseq.next()

    fr = N.empty(nn, dtype=c0[0].dtype)  # Initialize output
    temp0 = N.empty(maxLg, dtype=fr.dtype)  # pre-allocation

    loopparams = []
    for gdii, win_range in izip(sl(gd), sl(wins)):
        Lg = len(gdii)
        temp = temp0[:Lg]
        wr1 = win_range[:(Lg) // 2]
        wr2 = win_range[-((Lg + 1) // 2):]
        #        wr1,wr2 = win_range
        sl1 = slice(None, (Lg + 1) // 2)
        sl2 = slice(-(Lg // 2), None)
        p = (gdii, wr1, wr2, sl1, sl2, temp)
        loopparams.append(p)

    if True or T is None:

        def loop(fr, fc):
            # The overlap-add procedure including multiplication with the synthesis windows
            # TODO: stuff loop into theano
            for t, (gdii, wr1, wr2, sl1, sl2,
                    temp) in izip(symm(fc), loopparams):
                t1 = temp[sl1]
                t2 = temp[sl2]
                t1[:] = t[sl1]
                t2[:] = t[sl2]
                temp *= gdii
                temp *= len(t)

                fr[wr1] += t2
                fr[wr2] += t1

    #            wr1a,wr1b = wr1
    #            fr[wr1a] += t2[:wr1a.stop-wr1a.start]
    #            fr[wr1b] += t2[wr1a.stop-wr1a.start:]
    #            wr2a,wr2b = wr2
    #            fr[wr2a] += t1[:wr2a.stop-wr2a.start]
    #            fr[wr2b] += t1[wr2a.stop-wr2a.start:]
    else:
        raise RuntimeError("Theano support not implemented yet")

    for c in chain((c0, ), cseq):
        assert len(c) == ln

        fr[:] = 0.
        fc = map(
            fft, c
        )  # do transforms on coefficients - TODO: for matrixform we could do a FFT on the whole matrix along one axis

        # The overlap-add procedure including multiplication with the synthesis windows
        loop(fr, fc)

        ftr = fr[:nn // 2 + 1] if real else fr

        sig = ifft(ftr, outn=nn)

        sig = sig[:Ls]  # Truncate the signal to original length (if given)

        yield sig