File Exchange

image thumbnail


version 1.2 (307 KB) by

Mel frequency cepstral coefficient feature extraction that closely matches that of HTK's HCopy.



View License

Computes mel frequency cepstral coefficient (MFCC) features from a given speech signal. The speech signal is first preemphasised using a first order FIR filter with preemphasis coefficient. The preemphasised speech signal is subjected to the short-time Fourier transform analysis with a specified frame duration, frame shift and analysis window function. This is followed by magnitude spectrum computation, followed by filterbank design with M triangular filters uniformly spaced on the mel scale between lower and upper frequency limits. The filterbank is applied to the magnitude spectrum values to produce filterbank energies (FBEs). Log-compressed FBEs are then decorrelated using the discrete cosine transform to produce cepstral coefficients. Final step applies sinusoidal lifter to produce liftered MFCCs that closely match those produced by HTK. Demo scripts are included.

Comments and Ratings (43)

safa zighem

thanks, you have been really helpful!

Kamil Wojcicki

Hi Salvatore,

If the audio file has multiple channels, then yes, you'll get a matrix after loading the file into MATLAB. For example:

>> [x,fs]=audioread('speech.wav');
>> size(x)
ans =
       46417 2

This means that there are two channels of audio, each with 46417 samples. It really depends on your task what you do with it. For example, if you:

>> x=x(:);
ans =
       92834 1

then you essentially concatenating the samples for the two channels, i.e., samples from the second channel are appended after the last sample of the first channel. You can verify this by plotting the signal waveform and/or spectrogram. Now, if you pass this concatenated x vector through the mfcc() function it will extract the features as expected.

One alternative would be loop over each channel and pass one channel at the time to the mfcc() function to get only the features for that channel at a time.


Thanks for the answer! I have an another problem. I read that "speech" has to be a vector not a matrix, I compute it and I get a matrix,so I changed it in a vector with function speech1 = speech(:). Do you think that it is a correct transformation to obtain the mfcc conefficients?

Kamil Wojcicki

MATLAB has removed the wavread and wavwrite functions some releases ago (and I haven't got around to revising the FileExchange submissions). They replaced these with audioread and audiowrite functions, so you'll have to use those. Note that API is somewhat different (e.g., those do not return nbits as the third output).

So in the case you are pointing out, you would replace the wavread line with:
[ speech, fs ] = audioread( 'trail.wav' );


[speech, fs, nbits ] = wavread( 'trial.wav' );
Undefined function or variable 'wavread'.

why?I install toolbox

Jovan Galic

Jovan Galic

Once again, thank you very much!

Kamil Wojcicki

Hi Jovan,

Unfortunately there isn't.

One possibility would be to cite HTK documentation, as well as to provide the link to the implementation in a footnote, e.g.:

"The MFCC features were extracted according to [1] using MATLAB.^#"

# The MATLAB-based MFCC routines can be found at:

[1] Young, S., Evermann, G., Gales, M., Hain, T., Kershaw, D., Liu, X., Moore, G., Odell, J., Ollason, D., Povey, D., Valtchev, V., Woodland, P., 2006. The HTK Book (HTK Version 3.4.1). Engineering Department, Cambridge University.

Alternatively, you could cite the original paper by Davis and Mermelstein (1980).

Unless you are doing something like assessing various implementations (e.g., Ganchev et al., 2005), I would say that the footnote part is optional given how standard this task is. I would just ensure to state the relevant settings used in your task in the methods section.


Jovan Galic

Is there some conference or article paper for citing this code in MATLAB ?

Jovan Galic

Kamil, thank you for clear explanation.


Lucas R

Kamil Wojcicki

Hi Jovan,

>> And how the code would be if warping function is between mel and linear for example warp = 0.5 * hz?

Well, 0.5*hz is linear. You'll need a pair of nonlinear forward/backward warping functions instead if you want the filters to be non-uniformly spaced on the Hz scale.

>> Is it enough to change only mel2hz and hz2mel?

Yes. Simply assign function handles to these backward and forward warping functions, respectively.

See the documentation for the trifbank function. Use the example provided there to visualize the triangular filterbanks for the different warping functions you may want to try.


Jovan Galic

And how the code would be if warping function is between mel and linear for example warp = 0.5 * hz?

Is it enough to change only mel2hz and hz2mel?


Jovan Galic

Dear Kamil,

thank you very much for very helpful and quick reply!

Best regards.

Kamil Wojcicki

Hi Jovan,

Modify mfcc.m as follows:

lin = @(x)(x);

hz2mel = @( hz )( 1127*log(1+hz/700) ); % Hertz to mel warping function
    mel2hz = @( mel )( 700*exp(mel/1127)-700 ); % mel to Hertz warping function

hz2mel = lin;
mel2hz = lin;


Jovan Galic


How and where modify the code to get LFCC feature vectors, where triangular filters are uniformaly distributed over linear (not mel) frequency scale?
In some applications lfcc show greater robustness.


Kamil Wojcicki

Hi Donal.

Yes, the zeroth coefficient is included in the output from mfcc.m. This function emulates HTK's MFCC_0 feature computation, which includes the zeroth coefficient (i.e., the _0 modifier).

Note, however, that for plotting purposes in the included example, the zeroth coefficient was discarded. See example.m, and specifically the following lines:
subplot( 313 );
    imagesc( time_frames, [1:C], MFCCs(2:end,:) ); % HTK's TARGETKIND: MFCC
    %imagesc( time_frames, [1:C+1], MFCCs ); % HTK's TARGETKIND: MFCC_0


Hi Does this MFCC calculation include the c(0) (first MFCC) in the output?

Kamil Wojcicki

Hi Mehmet, the triangular filterbank function implementation is based on information for a speech processing book, a reference to which is included in trifbank.m. HTH, Kamil

hi, where is your article , this is stated in trifbank script.
I am trying to built mel filter bank


Kamil Wojcicki

Hi Ankur,

Could you elaborate on what you mean?

If you are wondering how to load audio from a file and extract features using the mfcc function, take a look at example.m.

Note that for newer MATLAB releases you may want to replace wavread with audio read, i.e.,

[ x, fs ] = audioread( wav_file );

Hope that helps.

Ankur Kalita

Is there any specification for the input audio file?

Kamil Wojcicki

Hi Yi,

In general you don't really have to do that. It is just that here we are trying to match the output of HTK feature extractor when it reads in audio data as 16-bit signed shorts. With this, you can compare directly the output features generated using this MATLAB routine with the corresponding features extracted with HTK (as demonstrated in compare.m).

Beyond that, i.e., if you are not comparing w/ HTK and just are looking to extract features for some task, you can drop this scaling.


yi wu

yi wu (view profile)

i am confused about the sentence
% Explode samples to the range of 16 bit shorts
    if( max(abs(speech))<=1 ), speech = speech * 2^15; end;
why speech need to multiply 2^15 before calculate STFT? Can someone kindly help to answer it? Thank you!

Kamil Wojcicki

Yibo, the overlap used is as defined in:

Huang, X., Acero, A., Hon, H., 2001. Spoken Language Processing: A guide to theory, algorithm, and system development. Prentice Hall, Upper Saddle River, NJ, USA (pp. 314-315).

Kamil Wojcicki

Olessya, what is the dimensionality of your input vector? i.e., what is size(speech)? It must be a vector and not a matrix.

hi, i am trying to use your code but it gives me the usage error:
[ MFCCs, FBEs, frames ] = ...
                    mfcc( speech, fs, Tw, Ts, alpha, @hamming, [LF HF], M, C+1, L );
Error using vec2frames (line 83)
usage: [ frames, indexes ] = vec2frames( vector, frame_length, frame_shift, direction, window, padding );

Error in mfcc (line 151)
    frames = vec2frames( speech, Nw, Ns, 'cols', window, false );
Do you have any idea what the problem might be? Thank you

Yibo Yang

Quick question Kamil: how can I tweak the trifbank code so that I can generate triangular filters with, say, 50% overlaps in the mel scale?
Thanks for your work!

Kamil Wojcicki

Brittany, are you using the provided example.m with sp10.wav, or your own audio files? If the audio file you are using happens to have long sections of zero only samples, that could explain NaN MFCC values. If that is the case, you could add some very low level noise to your audio samples, e.g.,

speech = speech + randn(size(speech))*1E-10;

Hope this helps.

I get some NaN values in the MFCC variable. Why is that so?

clarissa yong

Does anyone know which file should I run to achieve the final outcome? please help,thanks!!

Adnan Farooq

In case of sequence of images.. how can we use MFCC? 1-> we convert each frame to 2D/3D to 1D vector. but i am confuse how can i use these parameters ?
"fs, Tw, Ts, alpha, window, R, M, N, L"

Agus Reza

Telkom University Indonesia - was here :D


yingxue wang



Lehigh (view profile)

very good!


FJK (view profile)



Title change

MATLAB Release
MATLAB 7.10 (R2010a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video