Exemple #1
0
def fbank(signal,samplerate=16000,winlen=0.025,winstep=0.01,
          nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97):
    """Compute Mel-filterbank energy features from an audio signal.

    :param signal: the audio signal from which to compute features. Should be an N*1 array
    :param samplerate: the samplerate of the signal we are working with.
    :param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)    
    :param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)    
    :param nfilt: the number of filters in the filterbank, default 26.
    :param nfft: the FFT size. Default is 512.
    :param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
    :param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
    :param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97. 
    :returns: 2 values. The first is a numpy array of size (NUMFRAMES by nfilt) containing features. Each row holds 1 feature vector. The
        second return value is the energy in each frame (total energy, unwindowed)
    """          
    highfreq= highfreq or samplerate/2
    print "preemph %s"%(preemph)
    signal = sigproc.preemphasis(signal,preemph)
    frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate)
    matchframes(frames[0], frames[1])
    pspec = sigproc.powspec(frames,nfft)
    energy = pylab.sum(pspec,1) # this stores the total energy in each frame
    energy = pylab.where(energy == 0, pylab.finfo(float).eps, energy) # if energy is zero, we get problems with log
    fb = get_filterbanks(nfilt, nfft, samplerate, lowfreq, highfreq)
    print "len(fb) %s"%(len(fb))
    colour = "k-"
    for i in range(len(fb)):
        if colour == "k-":
            colour = "r-"
        else:
            colour = "k-"
        startedplot = False
        midpoint = 0
        for j in range(len(fb[i])):
            if fb[i][j] > 0:
                if startedplot == False:
                    startedplot = j
                if j > 0:
                    pylab.plot([j-1, j], [fb[i][j-1], fb[i][j]], colour)
                    if fb[i][j] == 1.0:
                        midpoint = j
            else:
                if not startedplot == False:
                    pylab.plot([j-1, j], [fb[i][j-1], 0], colour)
                    try:
                        print "slope to midpoint %.3f, slope from midpoint %.3f"%(1.0/float(midpoint-startedplot), 1.0/float(midpoint-j+1))
                    except:
                        pass
                    break
    pylab.show()
    feat = pylab.dot(pspec, fb.T) # compute the filterbank energies
    feat = pylab.where(feat == 0, pylab.finfo(float).eps, feat) # if feat is zero, we get problems with log
    return feat, energy
Exemple #2
0
def ssc(signal,samplerate=16000,winlen=0.025,winstep=0.01,
          nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97):
    """Compute Spectral Subband Centroid features from an audio signal.

    :param signal: the audio signal from which to compute features. Should be an N*1 array
    :param samplerate: the samplerate of the signal we are working with.
    :param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)    
    :param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)    
    :param nfilt: the number of filters in the filterbank, default 26.
    :param nfft: the FFT size. Default is 512.
    :param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
    :param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
    :param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97. 
    :returns: A numpy array of size (NUMFRAMES by nfilt) containing features. Each row holds 1 feature vector. 
    """          
    highfreq= highfreq or samplerate/2
    signal = sigproc.preemphasis(signal,preemph)
    frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate)
    pspec = sigproc.powspec(frames,nfft)
    pspec = pylab.where(pspec == 0,pylab.finfo(float).eps,pspec) # if things are all zeros we get problems
    
    fb = get_filterbanks(nfilt,nfft,samplerate,lowfreq,highfreq)
    feat = pylab.dot(pspec,fb.T) # compute the filterbank energies
    R = pylab.tile(pylab.linspace(1,samplerate/2,pylab.size(pspec,1)),(pylab.size(pspec,0),1))
    
    return pylab.dot(pspec*R,fb.T) / feat
def TVD(phiOld, c, nt, limiterFunc):
    "advection for nt time steps with a Courant number of c using LW"
    "and upwind combined with van-Leer limiter"

    # the number of independent points
    nx = len(phiOld) - 1
    # add two wrap-around points for cyclic boundaries
    phiOld = pl.append(phiOld, [phiOld[1:3]])

    # new time-step arrays for phi
    phi = phiOld.copy()

    # all the time steps
    for it in xrange(nt):

        #low and high order fluxes
        phiL = phi[1:-1]
        phiH = 0.5 * ((1 + c) * phi[1:-1] + (1 - c) * phi[2:])

        denom = pl.where(\
            (abs(phi[2:] - phi[1:-1])<pl.finfo(pl.double).resolution),\
            pl.finfo(pl.double).resolution, phi[2:] - phi[1:-1])

        r = (phi[1:-1] - phi[0:-2]) / denom
        limiter = limiterFunc(r)

        # limited flux at the mid-points
        phiMid = limiter * phiH + (1 - limiter) * phiL

        phi[2:-1] = phiOld[2:-1] - c * (phiMid[1:] - phiMid[:-1])

        # cyclic BCs
        phi[0] = phi[nx]
        phi[1] = phi[nx + 1]
        phi[nx + 2] = phi[2]

        # update arrays
        phiOld = phi.copy()

    # return phiNew (without the cyclic wrap-around points)
    return phi[0:nx + 1]