From: Thomas Fillon Date: Wed, 19 Mar 2014 14:02:28 +0000 (+0100) Subject: Analyzer/irit_speech_4hz : fix NaN issue X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=20cc2ca42fccbdf77b1c288b47e793af2b046f10;p=timeside-diadems.git Analyzer/irit_speech_4hz : fix NaN issue --- diff --git a/timeside/analyzer/irit_speech_4hz.py b/timeside/analyzer/irit_speech_4hz.py index a8d12c3..c04e438 100644 --- a/timeside/analyzer/irit_speech_4hz.py +++ b/timeside/analyzer/irit_speech_4hz.py @@ -24,7 +24,7 @@ from timeside.analyzer.core import Analyzer from timeside.analyzer.utils import melFilterBank, computeModulation from timeside.analyzer.utils import segmentFromValues from timeside.api import IAnalyzer -from numpy import array, hamming, dot, mean, float +from numpy import array, hamming, dot, mean, float, isnan from numpy.fft import rfft from scipy.signal import firwin, lfilter @@ -118,7 +118,7 @@ class IRITSpeech4Hz(Analyzer): energy = sum(energy) # Normalization - if self.normalizeEnergy: + if self.normalizeEnergy and energy.any(): energy = energy / mean(energy) # Energy Modulation diff --git a/timeside/analyzer/utils.py b/timeside/analyzer/utils.py index 27a1c35..6299a9c 100644 --- a/timeside/analyzer/utils.py +++ b/timeside/analyzer/utils.py @@ -19,7 +19,7 @@ # Author: Paul Brossier -import numpy +import numpy as np def downsample_blocking(frames, hop_s, dtype='float32'): # downmixing to one channel @@ -30,13 +30,15 @@ def downsample_blocking(frames, hop_s, dtype='float32'): # zero padding to have a multiple of hop_s if downsampled.shape[0] % hop_s != 0: pad_length = hop_s + downsampled.shape[0] / hop_s * hop_s - downsampled.shape[0] - downsampled = numpy.hstack([downsampled, numpy.zeros(pad_length, dtype = dtype)]) + downsampled = np.hstack([downsampled, np.zeros(pad_length, dtype = dtype)]) # blocking return downsampled.reshape(downsampled.shape[0] / hop_s, hop_s) -def computeModulation(serie,wLen,withLog=True): + +def computeModulation(serie, wLen, withLog=True): ''' - Compute the modulation of a parameter centered. Extremums are set to zero. + Compute the modulation of a parameter centered. + Extremums are set to zero. Args : - serie : list or numpy array containing the serie. @@ -47,24 +49,30 @@ def computeModulation(serie,wLen,withLog=True): - modul : Modulation of the serie. ''' - - modul = numpy.zeros((1,len(serie)))[0]; + sLen = len(serie) + modul = np.zeros((sLen,)) w = int(wLen/2) - for i in range(w,len(serie)-w): + if withLog: + machine_epsilon = np.finfo(np.float32).eps + + for i in range(w, sLen-w): d = serie[i-w:i+w] if withLog: - d = numpy.log(d) - modul[i] = numpy.var(d) + if not (d > 0).all(): + d[d <= 0] = machine_epsilon # prevent log(0)=inf + d = np.log(d) - modul[:w] = modul[w] + modul[i] = np.var(d) + modul[:w] = modul[w] modul[-w:] = modul[-w-1] - return modul; + return modul + -def segmentFromValues(values,offset=0): +def segmentFromValues(values, offset=0): ''' ''' @@ -105,15 +113,15 @@ def melFilterBank(nbFilters,fftLen,sr) : ''' fh = float(sr)/2.0 - mh = 2595*numpy.log10(1+fh/700) + mh = 2595*np.log10(1+fh/700) step = mh/nbFilters; - mcenter = numpy.arange(step,mh,step) + mcenter = np.arange(step,mh,step) fcenter = 700*(10**(mcenter/2595)-1) - filterbank = numpy.zeros((fftLen,nbFilters)); + filterbank = np.zeros((fftLen,nbFilters)); for i,_ in enumerate(fcenter) : @@ -127,8 +135,8 @@ def melFilterBank(nbFilters,fftLen,sr) : else : fmax = fcenter[i+1] - imin = numpy.ceil(fmin/fh*fftLen) - imax = numpy.ceil(fmax/fh*fftLen) + imin = np.ceil(fmin/fh*fftLen) + imax = np.ceil(fmax/fh*fftLen) filterbank[imin:imax,i] = triangle(imax-imin) @@ -145,15 +153,15 @@ def triangle(length): - triangle : triangle filter. ''' - triangle = numpy.zeros((1,length))[0] - climax= numpy.ceil(length/2) + triangle = np.zeros((1,length))[0] + climax= np.ceil(length/2) - triangle[0:climax] = numpy.linspace(0,1,climax) - triangle[climax:length] = numpy.linspace(1,0,length-climax) + triangle[0:climax] = np.linspace(0,1,climax) + triangle[climax:length] = np.linspace(1,0,length-climax) return triangle -def entropy(serie,nbins=10,base=numpy.exp(1),approach='unbiased'): +def entropy(serie,nbins=10,base=np.exp(1),approach='unbiased'): ''' Compute entropy of a serie using the histogram method. @@ -177,23 +185,23 @@ def entropy(serie,nbins=10,base=numpy.exp(1),approach='unbiased'): estimate = 0 sigma = 0 - bins,edges = numpy.histogram(serie,nbins); + bins,edges = np.histogram(serie,nbins); ncell = len(bins) - norm = (numpy.max(edges)-numpy.min(edges))/len(bins) + norm = (np.max(edges)-np.min(edges))/len(bins) for b in bins : if b == 0 : logf = 0 else : - logf = numpy.log(b) + logf = np.log(b) estimate = estimate - b*logf sigma = sigma + b * logf**2 - count = numpy.sum(bins) + count = np.sum(bins) estimate=estimate/count; - sigma=numpy.sqrt( (sigma/count-estimate**2)/float(count-1) ); - estimate=estimate+numpy.log(count)+numpy.log(norm); + sigma=np.sqrt( (sigma/count-estimate**2)/float(count-1) ); + estimate=estimate+np.log(count)+np.log(norm); nbias=-(ncell-1)/(2*count); if approach =='unbiased' : @@ -210,8 +218,8 @@ def entropy(serie,nbins=10,base=numpy.exp(1),approach='unbiased'): else : return 0 - estimate=estimate/numpy.log(base); - nbias =nbias /numpy.log(base); - sigma =sigma /numpy.log(base); + estimate=estimate/np.log(base); + nbias =nbias /np.log(base); + sigma =sigma /np.log(base); return estimate