MFCC calculation

I've got a project and need to calculate MFCCs. I tried to read some tutorials and then make a MATLAB function but I seem to have wrong answers. I'd like to feed MFCCs to one of the classification model--my choice would probably be NN or SVM. I am going to classify sound samples that either belong to one of many categories or not.

To calculate MFCC, the process currently looks like below:

1. Process signal by using pre-emphasis filter: x = x - 0.95*[0;x(1:N-1)];
2. Take windows of 430 samples that overlap by 215 samples (equvalence of ~ 50ms window)
3. Apply Hamming window to a segment
4. Calculate FFT: X = fft(x);
5. Calculate energy excluding the negative frequency part (the second half), therefore I take only frequencies between 0-4kHz

N2 = max([floor(N+1)/2 floor(N/2)+1]);
P = 1/N*abs(X(1:N2)).^2;
6. Get a bank of 40 triangularly shaped filters with centres spread over mel frequency between 20Hz and 4kHz

7. Apply filters on the vector of energy, sum the vectors and take natural logarithm:

L = log(mfccShapes'*P);

8. Apply DCT and take13 coefficients:

All_MFCC = cos( (pi/N*((0:N-1) + 0.5)).'*(0:N-1) )'*L;
MFCC = All_MFCC(1:13);

I have two problems in calculation of this:

• First, in calculation in the tutorial I have read, the energy is divided by length of the frequency vector. Is that the correct step?
• The second problem is the DCT. I understand that by taking DCT, we calculate cepstrum of the frequency. But I take the DCT on summed and filters energy vectors. I have applied DCT-II, in a vectorized from, and I think it is calculated correctly. The problem is I have always very low negative value of the first coefficient.

Also, the shape of and MFCC of low energy seem to be very similar. I realised that it is similar after I take DCT.

Where is the my mistake in calculation?

Cheers! Celdor

EDIT:

I understand now why the first MFCC coeficient is very low. If I look at DCT II, its first component is just a straight line:

This is equivalent of just summing all energy log coefficients. As all of them are negative, the overall sum becomes very low.

• I met a similar problem when calculating MFCCs, the c0 turns to be a quite big negative value while when I use HCOPY to generate a .mfc file its co is positive. Did you sole the problem? – user7775 Feb 3 '14 at 11:34
• Yes. I did sort it out. This routine is correct. Still confused by DCT but I compared my matrix version with MATLAB code and it gives the same results. The point to consider is to use proper coefficient when calculating DCT to make coefficients orthogonal. Then you can "come" back to frequency by calculating inverse DCT. – Celdor Feb 7 '14 at 12:10
• @user7775 Trying to answer your question again. Probably you have already found the answer, then ignore it. Otherwise, I hope it can help you or the others. The reason of all negative values is that the very first MFCC is simply the result of a sum of all filter bank energies. If all of log energies are negative, the sum will result with a very low negative number. It is consequence of applying DCT for k = 0 that is constant and equal 1 (if not normalized). As DCT is a sum of products of log energies and cos(), in case k = 0, cos(0) = 1, and then the result is just a sum of log energies. – Celdor Mar 10 '15 at 13:47
• That's a pity I missed that question - could've answered it. Would you mind posting the answer, so that question won't come back to the main page as unanswered? – jojek Mar 24 '16 at 8:15
• @jojek I could but I am not really convinced my edit qualifies to a proper full answer. The topic is still too advanced to me and I am not 100% I could provide full explanation. Feel free to give an answer. I'll accept it. – Celdor May 26 '16 at 8:15