Пример #1
0
def makewnd(sl_len, tr_area):
    hhop = sl_len // 4
    htr = tr_area // 2
    # build window function within one slice (centered with transition areas around sl_len/4 and 3*sl_len/4
    w = hannwin(2 * tr_area)  # window is shifted
    tw = np.empty(sl_len, dtype=float)
    tw[:hhop - htr] = 0
    tw[hhop - htr:hhop + htr] = w[tr_area:]
    tw[hhop + htr:3 * hhop - htr] = 1
    tw[3 * hhop - htr:3 * hhop + htr] = w[:tr_area]
    tw[3 * hhop + htr:] = 0
    return tw
Пример #2
0
def makewnd(sl_len, tr_area):
    hhop = sl_len//4
    htr = tr_area//2
    # build window function within one slice (centered with transition areas around sl_len/4 and 3*sl_len/4    
    w = hannwin(2*tr_area)  # window is shifted
    tw = np.empty(sl_len, dtype=float)
    tw[:hhop-htr] = 0
    tw[hhop-htr:hhop+htr] = w[tr_area:]
    tw[hhop+htr:3*hhop-htr] = 1
    tw[3*hhop-htr:3*hhop+htr] = w[:tr_area]
    tw[3*hhop+htr:] = 0
    return tw
Пример #3
0
def nsgfwin(fmin, fmax, bins, sr, Ls, min_win=4):

    nf = sr / 2

    if fmax > nf:
        fmax = nf

    b = N.ceil(N.log2(fmax / fmin)) + 1

    if not _isseq(bins):
        bins = N.ones(b, dtype=int) * bins
    elif len(bins) < b:
        # TODO: test this branch!
        bins[bins <= 0] = 1
        bins = N.concatenate(
            (bins, N.ones(b - len(bins), dtype=int) * N.min(bins)))

    fbas = []
    for kk, bkk in enumerate(bins):
        r = N.arange(kk * bkk, (kk + 1) * bkk, dtype=float)
        # TODO: use N.logspace instead
        fbas.append(2**(r / bkk) * fmin)
    fbas = N.concatenate(fbas)

    if fbas[N.min(N.where(fbas >= fmax))] >= nf:
        fbas = fbas[:N.max(N.where(fbas < fmax)) + 1]
    else:
        # TODO: test this branch!
        fbas = fbas[:N.min(N.where(fbas >= fmax)) + 1]

    lbas = len(fbas)
    fbas = N.concatenate(((0., ), fbas, (nf, ), sr - fbas[::-1]))
    fbas *= float(Ls) / sr

    # TODO: put together with array indexing
    M = N.empty(fbas.shape, int)
    M[0] = N.round(2. * fmin * Ls / sr)
    for k in xrange(1, 2 * lbas + 1):
        M[k] = N.round(fbas[k + 1] - fbas[k - 1])
    M[-1] = N.round(Ls - fbas[-2])

    M = N.clip(M, min_win, N.inf).astype(int)
    g = [hannwin(m) for m in M]

    fbas[lbas] = (fbas[lbas - 1] + fbas[lbas + 1]) / 2
    fbas[lbas + 2] = Ls - fbas[lbas]
    rfbas = N.round(fbas).astype(int)

    return g, rfbas, M
Пример #4
0
def nsgfwin(fmin,fmax,bins,sr,Ls,min_win=4):

    nf = sr/2
    
    if fmax > nf:
        fmax = nf
    
    b = N.ceil(N.log2(fmax/fmin))+1

    if not _isseq(bins):
        bins = N.ones(b,dtype=int)*bins
    elif len(bins) < b:
        # TODO: test this branch!
        bins[bins <= 0] = 1
        bins = N.concatenate((bins,N.ones(b-len(bins),dtype=int)*N.min(bins)))
    
    fbas = []
    for kk,bkk in enumerate(bins):
        r = N.arange(kk*bkk,(kk+1)*bkk,dtype=float)
        # TODO: use N.logspace instead
        fbas.append(2**(r/bkk)*fmin)
    fbas = N.concatenate(fbas)

    if fbas[N.min(N.where(fbas>=fmax))] >= nf:
        fbas = fbas[:N.max(N.where(fbas<fmax))+1]
    else:
        # TODO: test this branch!
        fbas = fbas[:N.min(N.where(fbas>=fmax))+1]
    
    lbas = len(fbas)
    fbas = N.concatenate(((0.,),fbas,(nf,),sr-fbas[::-1]))
    fbas *= float(Ls)/sr
    
    # TODO: put together with array indexing
    M = N.empty(fbas.shape,int)
    M[0] = N.round(2.*fmin*Ls/sr)
    for k in xrange(1,2*lbas+1):
        M[k] = N.round(fbas[k+1]-fbas[k-1])
    M[-1] = N.round(Ls-fbas[-2])
    
    M = N.clip(M,min_win,N.inf).astype(int)    
    g = [hannwin(m) for m in M]
    
    fbas[lbas] = (fbas[lbas-1]+fbas[lbas+1])/2
    fbas[lbas+2] = Ls-fbas[lbas]
    rfbas = N.round(fbas).astype(int)
    
    return g,rfbas,M
Пример #5
0
def unslicing(frec_sliced, sl_len, tr_area, dtype=float, usewindow=True):
    hhop = sl_len // 4
    islices = slicequads(frec_sliced, hhop)

    if usewindow:
        tr_area2 = min(2 * hhop - tr_area, 2 * tr_area)
        htr = tr_area // 2
        htr2 = tr_area2 // 2
        hw = hannwin(tr_area2)
        tw = np.zeros(sl_len, dtype=dtype)
        tw[max(hhop - htr - htr2, 0):hhop - htr] = hw[htr2:]
        tw[hhop - htr:3 * hhop + htr] = 1
        tw[3 * hhop + htr:min(3 * hhop + htr + htr2, sl_len)] = hw[:htr2]
        tw = [tw[o:o + hhop] for o in xrange(0, sl_len, hhop)]
    else:
        tw = cycle((1, ))

    # get first slice to deduce channels
    firstquad = islices.next()

    chns = len(firstquad[0])  # number of channels in first quad

    islices = chain((firstquad, ), islices)

    output = [np.zeros((chns, hhop), dtype=dtype) for _ in xrange(4)]

    for quad in islices:
        for osl, isl, w in izip(output, quad, tw):
            # in a piecewise manner add slices to output stream
            osl[:] += isl * w
        for _ in range(2):
            # absolutely first two should be padding (and discarded by the receiver)
            yield output.pop(0)
            output.append(np.zeros((chns, hhop), dtype=dtype))

    for _ in range(2):
        # absolutely last two should be padding (and discarded by the receiver)
        yield output.pop(0)
Пример #6
0
def unslicing(frec_sliced, sl_len, tr_area, dtype=float, usewindow=True):
    hhop = sl_len//4    
    islices = slicequads(frec_sliced, hhop)
    
    if usewindow:
        tr_area2 = min(2*hhop-tr_area, 2*tr_area)
        htr = tr_area//2
        htr2 = tr_area2//2
        hw = hannwin(tr_area2)
        tw = np.zeros(sl_len, dtype=dtype)
        tw[max(hhop-htr-htr2, 0):hhop-htr] = hw[htr2:]
        tw[hhop-htr:3*hhop+htr] = 1
        tw[3*hhop+htr:min(3*hhop+htr+htr2, sl_len)] = hw[:htr2]
        tw = [tw[o:o+hhop] for o in xrange(0, sl_len, hhop)]
    else:
        tw = cycle((1,))
        
    # get first slice to deduce channels
    firstquad = islices.next()
    
    chns = len(firstquad[0]) # number of channels in first quad
    
    islices = chain((firstquad,), islices)
    
    output = [np.zeros((chns,hhop), dtype=dtype) for _ in xrange(4)]
    
    for quad in islices:
        for osl,isl,w in izip(output, quad, tw):
            # in a piecewise manner add slices to output stream 
            osl[:] += isl*w
        for _ in range(2):
            # absolutely first two should be padding (and discarded by the receiver)
            yield output.pop(0)
            output.append(np.zeros((chns,hhop), dtype=dtype))

    for _ in range(2):
        # absolutely last two should be padding (and discarded by the receiver)
        yield output.pop(0)
Пример #7
0
def nsgfwin(f, q, sr, Ls, sliced=True, min_win=4, Qvar=1, dowarn=True):
    nf = sr / 2.

    lim = N.argmax(f > 0)
    if lim != 0:
        # f partly <= 0
        f = f[lim:]
        q = q[lim:]

    lim = N.argmax(f >= nf)
    if lim != 0:
        # f partly >= nf
        f = f[:lim]
        q = q[:lim]

    assert len(f) == len(q)
    assert N.all((f[1:] - f[:-1]) > 0)  # frequencies must be increasing
    assert N.all(q > 0)  # all q must be > 0

    qneeded = f * (Ls / (8. * sr))
    if N.any(q >= qneeded) and dowarn:
        warn("Q-factor too high for frequencies %s" %
             ",".join("%.2f" % fi for fi in f[q >= qneeded]))

    fbas = f
    lbas = len(fbas)

    frqs = N.concatenate(((0., ), fbas, (nf, )))

    fbas = N.concatenate((frqs, sr - frqs[-2:0:-1]))

    # at this point: fbas.... frequencies in Hz

    fbas *= float(Ls) / sr

    #    print "fbas",fbas

    # Omega[k] in the paper
    if sliced:
        M = N.zeros(fbas.shape, float)
        M[0] = 2 * fbas[1]
        M[1] = fbas[1] / q[0]  #(2**(1./bins[0])-2**(-1./bins[0]))
        for k in chain(xrange(2, lbas), (lbas + 1, )):
            M[k] = fbas[k + 1] - fbas[k - 1]
        M[lbas] = fbas[lbas] / q[lbas -
                                 1]  #(2**(1./bins[-1])-2**(-1./bins[-1]))
        #        M[lbas+1] = fbas[lbas]/q[lbas-1] #(2**(1./bins[-1])-2**(-1./bins[-1]))
        M[lbas + 2:2 * (lbas + 1)] = M[lbas:0:-1]
        #        M[-1] = M[1]
        M *= Qvar / 4.
        M = N.round(M).astype(int)
        M *= 4
    else:
        M = N.zeros(fbas.shape, int)
        M[0] = N.round(2 * fbas[1])
        for k in xrange(1, 2 * lbas + 1):
            M[k] = N.round(fbas[k + 1] - fbas[k - 1])
        M[-1] = N.round(Ls - fbas[-2])

    N.clip(M, min_win, N.inf, out=M)

    #    print "M",list(M)

    if sliced:
        g = [blackharr(m) for m in M]
    else:
        g = [hannwin(m) for m in M]

    if sliced:
        for kk in (1, lbas + 2):
            if M[kk - 1] > M[kk]:
                g[kk - 1] = N.ones(M[kk - 1], dtype=g[kk - 1].dtype)
                g[kk - 1][M[kk - 1] // 2 - M[kk] // 2:M[kk - 1] // 2 +
                          ceil(M[kk] / 2.)] = hannwin(M[kk])

        rfbas = N.round(fbas / 2.).astype(int) * 2
    else:
        fbas[lbas] = (fbas[lbas - 1] + fbas[lbas + 1]) / 2
        fbas[lbas + 2] = Ls - fbas[lbas]
        rfbas = N.round(fbas).astype(int)


#    print "rfbas",rfbas
#    print "g",g

    return g, rfbas, M
Пример #8
0
def nsgfwin_new(f, q, sr, Ls, sliced=True, min_win=4, Qvar=1, dowarn=True):
    nf = sr / 2.

    lim = np.argmax(f > 0)
    if lim != 0:
        # f partly <= 0
        f = f[lim:]
        q = q[lim:]

    lim = np.argmax(f >= nf)
    if lim != 0:
        # f partly >= nf
        f = f[:lim]
        q = q[:lim]

    assert len(f) == len(q)
    assert np.all((f[1:] - f[:-1]) > 0)  # frequencies must be increasing
    assert np.all(q > 0)  # all q must be > 0

    qneeded = f * (Ls / (8. * sr))
    if np.any(q >= qneeded) and dowarn:
        warn("Q-factor too high for frequencies %s" %
             ",".join("%.2f" % fi for fi in f[q >= qneeded]))

    fbas = f
    lbas = len(fbas)

    frqs = np.concatenate(((0., ), fbas, (nf, )))

    fbas = np.concatenate((frqs, sr - frqs[-2:0:-1]))

    # at this point: fbas.... frequencies in Hz

    fbas *= float(Ls) / sr

    #    print "fbas",fbas

    # Omega[k] in the paper
    if sliced:
        M = np.zeros(fbas.shape, dtype=float)
        M[0] = 2 * fbas[1]
        M[1] = fbas[1] / q[0]  #(2**(1./bins[0])-2**(-1./bins[0]))
        for k in chain(xrange(2, lbas), (lbas + 1, )):
            M[k] = fbas[k + 1] - fbas[k - 1]
        M[lbas] = fbas[lbas] / q[lbas -
                                 1]  #(2**(1./bins[-1])-2**(-1./bins[-1]))
        #        M[lbas+1] = fbas[lbas]/q[lbas-1] #(2**(1./bins[-1])-2**(-1./bins[-1]))
        M[lbas + 2:2 * (lbas + 1)] = M[lbas:0:-1]
#        M[-1] = M[1]

###        M *= Qvar/4.
###        M = N.round(M).astype(int)
###        M *= 4
    else:
        M = np.zeros(fbas.shape, dtype=int)
        M[0] = np.round(2 * fbas[1])
        for k in xrange(1, 2 * lbas + 1):
            M[k] = np.round(fbas[k + 1] - fbas[k - 1])
        M[-1] = np.round(Ls - fbas[-2])

    np.clip(M, min_win, np.inf, out=M)

    if sliced:
        ###        g = [blackharr(m) for m in M]

        rfbas = np.concatenate(
            (np.floor(fbas[:lbas + 2]), np.ceil(fbas[lbas + 2:])))
        corr_shift = fbas - rfbas

        #        shift = N.concatenate(([(-rfbas[-1])%Ls],N.diff(rfbas)))

        g, M = np.array([
            blackharrcw(m * Qvar, csh) for m, csh in izip(M, corr_shift)
        ]).T  ####
        #        M = N.ceil(M/4).astype(int)*4  # bailing out for some strange reason....
        M = np.array([ceil(m / 4) * 4 for m in M], dtype=int)
    else:
        g = [hannwin(m) for m in M]

    if sliced:
        for kk in (1, lbas + 2):
            if M[kk - 1] > M[kk]:
                g[kk - 1] = np.ones(M[kk - 1], dtype=g[kk - 1].dtype)
                g[kk - 1][M[kk - 1] // 2 - M[kk] // 2:M[kk - 1] // 2 +
                          ceil(M[kk] / 2.)] = hannwin(M[kk])

        rfbas = np.round(fbas / 2.).astype(int) * 2
    else:
        fbas[lbas] = (fbas[lbas - 1] + fbas[lbas + 1]) / 2
        fbas[lbas + 2] = Ls - fbas[lbas]
        rfbas = np.round(fbas).astype(int)

#    print "rfbas",rfbas
#    print "g",g

    return g, rfbas, M
Пример #9
0
def nsgfwin(f,q,sr,Ls,sliced=True,min_win=4,Qvar=1,dowarn=True):
    nf = sr/2.

    lim = N.argmax(f > 0)
    if lim != 0:
        # f partly <= 0 
        f = f[lim:]
        q = q[lim:]
            
    lim = N.argmax(f >= nf)
    if lim != 0:
        # f partly >= nf 
        f = f[:lim]
        q = q[:lim]
    
    assert len(f) == len(q)
    assert N.all((f[1:]-f[:-1]) > 0)  # frequencies must be increasing
    assert N.all(q > 0)  # all q must be > 0
    
    qneeded = f*(Ls/(8.*sr))
    if N.any(q >= qneeded) and dowarn:
        warn("Q-factor too high for frequencies %s"%",".join("%.2f"%fi for fi in f[q >= qneeded]))
    
    fbas = f
    lbas = len(fbas)
    
    frqs = N.concatenate(((0.,),fbas,(nf,)))
    
    fbas = N.concatenate((frqs,sr-frqs[-2:0:-1]))

    # at this point: fbas.... frequencies in Hz
    
    fbas *= float(Ls)/sr
    
#    print "fbas",fbas
    
    # Omega[k] in the paper
    if sliced:
        M = N.zeros(fbas.shape,float)
        M[0] = 2*fbas[1]
        M[1] = fbas[1]/q[0] #(2**(1./bins[0])-2**(-1./bins[0]))
        for k in chain(xrange(2,lbas),(lbas+1,)):
            M[k] = fbas[k+1]-fbas[k-1]
        M[lbas] = fbas[lbas]/q[lbas-1] #(2**(1./bins[-1])-2**(-1./bins[-1]))
#        M[lbas+1] = fbas[lbas]/q[lbas-1] #(2**(1./bins[-1])-2**(-1./bins[-1]))
        M[lbas+2:2*(lbas+1)] = M[lbas:0:-1]
#        M[-1] = M[1]
        M *= Qvar/4.
        M = N.round(M).astype(int)
        M *= 4        
    else:
        M = N.zeros(fbas.shape,int)
        M[0] = N.round(2*fbas[1])
        for k in xrange(1,2*lbas+1):
            M[k] = N.round(fbas[k+1]-fbas[k-1])
        M[-1] = N.round(Ls-fbas[-2])
        
    N.clip(M,min_win,N.inf,out=M)

#    print "M",list(M)
    
    if sliced: 
        g = [blackharr(m) for m in M]
    else:
        g = [hannwin(m) for m in M]
    
    if sliced:
        for kk in (1,lbas+2):
            if M[kk-1] > M[kk]:
                g[kk-1] = N.ones(M[kk-1],dtype=g[kk-1].dtype)
                g[kk-1][M[kk-1]//2-M[kk]//2:M[kk-1]//2+ceil(M[kk]/2.)] = hannwin(M[kk])
        
        rfbas = N.round(fbas/2.).astype(int)*2
    else:
        fbas[lbas] = (fbas[lbas-1]+fbas[lbas+1])/2
        fbas[lbas+2] = Ls-fbas[lbas]
        rfbas = N.round(fbas).astype(int)
        
#    print "rfbas",rfbas
#    print "g",g    
    
    return g,rfbas,M
Пример #10
0
def nsgfwin_new(f, q, sr, Ls, sliced=True, min_win=4, Qvar=1, dowarn=True):
    nf = sr/2.

    lim = np.argmax(f > 0)
    if lim != 0:
        # f partly <= 0 
        f = f[lim:]
        q = q[lim:]
            
    lim = np.argmax(f >= nf)
    if lim != 0:
        # f partly >= nf 
        f = f[:lim]
        q = q[:lim]
    
    assert len(f) == len(q)
    assert np.all((f[1:]-f[:-1]) > 0)  # frequencies must be increasing
    assert np.all(q > 0)  # all q must be > 0
    
    qneeded = f*(Ls/(8.*sr))
    if np.any(q >= qneeded) and dowarn:
        warn("Q-factor too high for frequencies %s"%",".join("%.2f"%fi for fi in f[q >= qneeded]))
    
    fbas = f
    lbas = len(fbas)
    
    frqs = np.concatenate(((0.,),fbas,(nf,)))
    
    fbas = np.concatenate((frqs,sr-frqs[-2:0:-1]))

    # at this point: fbas.... frequencies in Hz
    
    fbas *= float(Ls)/sr
    
#    print "fbas",fbas
    
    # Omega[k] in the paper
    if sliced:
        M = np.zeros(fbas.shape, dtype=float)
        M[0] = 2*fbas[1]
        M[1] = fbas[1]/q[0] #(2**(1./bins[0])-2**(-1./bins[0]))
        for k in chain(xrange(2,lbas),(lbas+1,)):
            M[k] = fbas[k+1]-fbas[k-1]
        M[lbas] = fbas[lbas]/q[lbas-1] #(2**(1./bins[-1])-2**(-1./bins[-1]))
#        M[lbas+1] = fbas[lbas]/q[lbas-1] #(2**(1./bins[-1])-2**(-1./bins[-1]))
        M[lbas+2:2*(lbas+1)] = M[lbas:0:-1]
#        M[-1] = M[1]

###        M *= Qvar/4.
###        M = N.round(M).astype(int)
###        M *= 4        
    else:
        M = np.zeros(fbas.shape, dtype=int)
        M[0] = np.round(2*fbas[1])
        for k in xrange(1, 2*lbas+1):
            M[k] = np.round(fbas[k+1]-fbas[k-1])
        M[-1] = np.round(Ls-fbas[-2])
        
    np.clip(M, min_win, np.inf, out=M)

    if sliced: 
###        g = [blackharr(m) for m in M]

        rfbas = np.concatenate((np.floor(fbas[:lbas+2]), np.ceil(fbas[lbas+2:])))
        corr_shift = fbas-rfbas

#        shift = N.concatenate(([(-rfbas[-1])%Ls],N.diff(rfbas)))

        g,M = np.array([blackharrcw(m*Qvar, csh) for m,csh in izip(M, corr_shift)]).T  ####
#        M = N.ceil(M/4).astype(int)*4  # bailing out for some strange reason....
        M = np.array([ceil(m/4)*4 for m in M], dtype=int)
    else:
        g = [hannwin(m) for m in M]
    
    
    
    if sliced:
        for kk in (1,lbas+2):
            if M[kk-1] > M[kk]:
                g[kk-1] = np.ones(M[kk-1],dtype=g[kk-1].dtype)
                g[kk-1][M[kk-1]//2-M[kk]//2:M[kk-1]//2+ceil(M[kk]/2.)] = hannwin(M[kk])
        
        rfbas = np.round(fbas/2.).astype(int)*2
    else:
        fbas[lbas] = (fbas[lbas-1]+fbas[lbas+1])/2
        fbas[lbas+2] = Ls-fbas[lbas]
        rfbas = np.round(fbas).astype(int)
        
#    print "rfbas",rfbas
#    print "g",g    
    
    return g,rfbas,M