Skip to content

Instantly share code, notes, and snippets.

@ukyo
Created May 26, 2012 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukyo/2795450 to your computer and use it in GitHub Desktop.
Save ukyo/2795450 to your computer and use it in GitHub Desktop.
create mel filter band
// http://d.hatena.ne.jp/aidiary/20120225/1330179868
// port python to JavaScript.
function melFilterBand(sampleRate, nfft, numChannels) {
var fmax = sampleRate >>> 1,
melmax = hz2mel(fmax),
nmax = nfft / 2,
df = sampleRate / nfft,
dmel = melmax / (numChannels + 1),
melcenters = [],
fcenters = [],
indexcenter = [],
indexstart = [],
indexstop = [],
filterbank = [],
i, j, n, m, increment, decrement, triWindow;
function hz2mel(f) {
return 1127.01048 * Math.log(f / 700.0 + 1.0);
}
function mel2hz(m) {
return 700.0 * (Math.exp(m / 1127.01048) - 1.0);
}
for(i = 0, n = numChannels; i < n; ++i) melcenters[i] = (i + 1) * dmel;
for(i = 0; i < n; ++i) fcenters[i] = mel2hz(melcenters[i]);
for(i = 0; i < n; ++i) indexcenter[i] = Math.round(fcenters[i] / df);
indexstart[0] = 0;
for(i = 0; i < n - 1; ++i) indexstart.push(indexcenter[i]);
for(i = 1; i < n; ++i) indexstop.push(indexcenter[i]);
indexstop.push(nmax);
for(i = 0; i < n; ++i) {
increment = 1 / (indexcenter[i] - indexstart[i]);
triWindow = [];
for(j = indexstart[i], m = indexcenter[i]; j < m; ++j) {
triWindow.push((j - indexstart[i]) * increment);
}
decrement = 1 / (indexstop[i] - indexcenter[i]);
for(j = indexcenter[i], m = indexstop[i]; j < m; ++j) {
triWindow.push(1 - (j - indexcenter[i]) * decrement);
}
filterbank[i] = triWindow;
}
return {
filterband: filterband,
indexcenter: indexcenter
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment