Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB code for calculating MFCC

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.

like image 420
Celdor Avatar asked Nov 19 '13 13:11

Celdor


Video Answer


1 Answers

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.

like image 86
ben Avatar answered Oct 02 '22 14:10

ben