# Implementation of DCT for MFCC

I am in the process of implementing MFCC and I'm unsure about the DCT part.

This is how I implemented it (using DCT II).

//some coefficients
let k = Math.PI/numFilters;
let w1 = 1.0/Math.sqrt(2);
let w2 = Math.sqrt(2.0/numFilters);
let numCoeffs = numFilters;
let dctMatrix = new Float32Array(numCoeffs*numFilters);

// compute the dct matrix
for(let i = 0; i < numCoeffs; i++){
for (let j = 0; j < numFilters; j++) {
let idx = i + (j*numCoeffs);
if(i === 0){
dctMatrix[idx] = w1 * Math.cos(k * (i+1) * (j+0.5));
}
else{
dctMatrix[idx] = w2 * Math.cos(k * (i+1) * (j+0.5));
}
}
}

// apply it on the logged energies.
let mfccs = new Float32Array(numCoeffs);
for (let k = 0; k < numCoeffs; k++) {
let v = 0;
for (let n = 0; n < numFilters; n++) {
let idx = k + (n*numCoeffs);
v += (dctMatrix[idx] * loggedMelBands[n]);
}
mfccs[k] = v;
}


I'm getting 26 values (which is the number of filters I defined), both negative and positive values, where the first value is always has highest magnitude and the rest are in the range of -1 and 1.

Does this implementation seem correct to you?

• why are you implementing a very common algorithm yourself? Jan 19 '16 at 10:16
• Why not ? :D I want to learn. Jan 20 '16 at 15:41
• fair point, really! I think the best way to verify it would be checking it against a reference implementation. Get your octave ready :) Jan 20 '16 at 16:15