Skip to content

Commit d378839

Browse files
author
Watcharapol Watcharawisetkul
committed
Add parameter winfunc to mfcc, fbank, and ssc
1 parent 0bba68a commit d378839

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

‎features/base.py‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,53 @@
1111
xrange=range
1212

1313
def mfcc(signal,samplerate=16000,winlen=0.025,winstep=0.01,numcep=13,
14-
nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97,ceplifter=22,appendEnergy=True):
14+
nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97,ceplifter=22,appendEnergy=True,
15+
winfunc=lambda x:numpy.ones((x,))):
1516
"""Compute MFCC features from an audio signal.
1617
1718
:param signal: the audio signal from which to compute features. Should be an N*1 array
1819
:param samplerate: the samplerate of the signal we are working with.
19-
:param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)
20-
:param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)
21-
:param numcep: the number of cepstrum to return, default 13
20+
:param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)
21+
:param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)
22+
:param numcep: the number of cepstrum to return, default 13
2223
:param nfilt: the number of filters in the filterbank, default 26.
2324
:param nfft: the FFT size. Default is 512.
2425
:param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
2526
:param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
2627
:param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97.
2728
:param ceplifter: apply a lifter to final cepstral coefficients. 0 is no lifter. Default is 22.
2829
:param appendEnergy: if this is true, the zeroth cepstral coefficient is replaced with the log of the total frame energy.
30+
:param winfunc: the analysis window to apply to each frame. By default no window is applied.
2931
:returns: A numpy array of size (NUMFRAMES by numcep) containing features. Each row holds 1 feature vector.
3032
"""
31-
feat,energy = fbank(signal,samplerate,winlen,winstep,nfilt,nfft,lowfreq,highfreq,preemph)
33+
feat,energy = fbank(signal,samplerate,winlen,winstep,nfilt,nfft,lowfreq,highfreq,preemph,winfunc)
3234
feat = numpy.log(feat)
3335
feat = dct(feat, type=2, axis=1, norm='ortho')[:,:numcep]
3436
feat = lifter(feat,ceplifter)
3537
if appendEnergy: feat[:,0] = numpy.log(energy) # replace first cepstral coefficient with log of frame energy
3638
return feat
3739

3840
def fbank(signal,samplerate=16000,winlen=0.025,winstep=0.01,
39-
nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97):
41+
nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97,
42+
winfunc=lambda x:numpy.ones((x,))):
4043
"""Compute Mel-filterbank energy features from an audio signal.
4144
4245
:param signal: the audio signal from which to compute features. Should be an N*1 array
4346
:param samplerate: the samplerate of the signal we are working with.
4447
:param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)
45-
:param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)
48+
:param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)
4649
:param nfilt: the number of filters in the filterbank, default 26.
4750
:param nfft: the FFT size. Default is 512.
4851
:param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
4952
:param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
50-
:param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97.
53+
:param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97.
54+
:param winfunc: the analysis window to apply to each frame. By default no window is applied.
5155
:returns: 2 values. The first is a numpy array of size (NUMFRAMES by nfilt) containing features. Each row holds 1 feature vector. The
5256
second return value is the energy in each frame (total energy, unwindowed)
5357
"""
5458
highfreq= highfreq or samplerate/2
5559
signal = sigproc.preemphasis(signal,preemph)
56-
frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate)
60+
frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate, winfunc)
5761
pspec = sigproc.powspec(frames,nfft)
5862
energy = numpy.sum(pspec,1) # this stores the total energy in each frame
5963
energy = numpy.where(energy == 0,numpy.finfo(float).eps,energy) # if energy is zero, we get problems with log
@@ -83,7 +87,8 @@ def logfbank(signal,samplerate=16000,winlen=0.025,winstep=0.01,
8387
return numpy.log(feat)
8488

8589
def ssc(signal,samplerate=16000,winlen=0.025,winstep=0.01,
86-
nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97):
90+
nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97,
91+
winfunc=lambda x:numpy.ones((x,))):
8792
"""Compute Spectral Subband Centroid features from an audio signal.
8893
8994
:param signal: the audio signal from which to compute features. Should be an N*1 array
@@ -94,12 +99,13 @@ def ssc(signal,samplerate=16000,winlen=0.025,winstep=0.01,
9499
:param nfft: the FFT size. Default is 512.
95100
:param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
96101
:param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
97-
:param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97.
102+
:param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97.
103+
:param winfunc: the analysis window to apply to each frame. By default no window is applied.
98104
:returns: A numpy array of size (NUMFRAMES by nfilt) containing features. Each row holds 1 feature vector.
99105
"""
100106
highfreq= highfreq or samplerate/2
101107
signal = sigproc.preemphasis(signal,preemph)
102-
frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate)
108+
frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate, winfunc)
103109
pspec = sigproc.powspec(frames,nfft)
104110
pspec = numpy.where(pspec == 0,numpy.finfo(float).eps,pspec) # if things are all zeros we get problems
105111

0 commit comments

Comments
 (0)