title: MATLAB code for calculating MFCC
body: <p>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.</p>
  
  <pre><code>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 &gt;= f(m-1) &amp; k &lt;= f(m);
          J = k &gt;= f(m) &amp; k &lt;= 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
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help greatly appreciated :)</p>
  
  <p>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. </p>