5

I have a question if that's ok. I was recently looking for algorithm to calculate MFCCs. I found a good tutorial rather than code so I tried to code it by myself. I still feel like I am missing one thing. In the code below I took FFT of a signal, calculated normalized power, filter a signal using triangular shapes and eventually sum energies corresponding to each bank to obtain MFCCs.

function output = mfcc(x,M,fbegin,fs)
    MF = @(f) 2595.*log10(1 + f./700);
    invMF = @(m) 700.*(10.^(m/2595)-1);

    M = M+2; % number of triangular filers
    mm = linspace(MF(fbegin),MF(fs/2),M); % equal space in mel-frequency
    ff = invMF(mm); % convert mel-frequencies into frequency

    X = fft(x);
    N = length(X); % length of a short time window
    N2 = max([floor(N+1)/2 floor(N/2)+1]); %
    P = abs(X(1:N2,:)).^2./N; % NoFr no. of periodograms
    mfccShapes = triangularFilterShape(ff,N,fs); %

    output = log(mfccShapes'*P);
end

function [out,k] = triangularFilterShape(f,N,fs)
    N2 = max([floor(N+1)/2 floor(N/2)+1]);
    M = length(f);
    k = linspace(0,fs/2,N2);
    out = zeros(N2,M-2);
    for m=2:M-1
        I = k >= f(m-1) & k <= f(m);
        J = k >= f(m) & k <= f(m+1);
        out(I,m-1) = (k(I) - f(m-1))./(f(m) - f(m-1));
        out(J,m-1) = (f(m+1) - k(J))./(f(m+1) - f(m));
    end
end

Could someone please confirm that this is all right or direct me if I made mistake> I tested it on a simple pure tone and it gives me, in my opinion, reasonable answers.

Any help greatly appreciated :)

PS. I am working on how to apply vectorized Cosinus Transform. It looks like I would need a matrix of MxM of transform coefficients but I did not find any source that would explain how to do it.

Celdor
  • 2,437
  • 2
  • 23
  • 44
  • If someone is interested in this thread, I found a fairly good website [Nokia DEVELOPER](https://projects.developer.nokia.com/DSP/wiki/Mel_frequency_cepstral_coefficients). There's information about a signal preconditioning etc. – Celdor Nov 19 '13 at 17:00
  • Also asked at http://dsp.stackexchange.com/questions/12812/mfcc-calculation – Nikolay Shmyrev Nov 22 '13 at 00:56
  • Yes that's because I did not get any answer here. – Celdor Nov 22 '13 at 07:38

1 Answers1

6

You can test it yourself by comparing your results against other implementations like this one here you will find a fully configurable matlab toolbox incl. MFCCs and even a function to reverse MFCC back to a time signal, which is quite handy for testing purposes:

melfcc.m - main function for calculating PLP and MFCCs from sound waveforms, supports many options.

invmelfcc.m - main function for inverting back from cepstral coefficients to spectrograms and (noise-excited) waveforms, options exactly match melfcc (to invert that processing).

the page itself has a lot of information on the usage of the package.

ben
  • 1,380
  • 9
  • 14
  • Thanks for the link [ben](http://stackoverflow.com/users/3336192/ben). I am going to try that library. – Celdor Sep 26 '14 at 09:44