Daniel@0: % mfcc - Mel frequency cepstrum coefficient analysis. Daniel@0: % [ceps,freqresp,fb,fbrecon,freqrecon] = ... Daniel@0: % mfcc(input, samplingRate, [frameRate]) Daniel@0: % Find the cepstral coefficients (ceps) corresponding to the Daniel@0: % input. Four other quantities are optionally returned that Daniel@0: % represent: Daniel@0: % the detailed fft magnitude (freqresp) used in MFCC calculation, Daniel@0: % the mel-scale filter bank output (fb) Daniel@0: % the filter bank output by inverting the cepstrals with a cosine Daniel@0: % transform (fbrecon), Daniel@0: % the smooth frequency response by interpolating the fb reconstruction Daniel@0: % (freqrecon) Daniel@0: % -- Malcolm Slaney, August 1993 Daniel@0: % Modified a bit to make testing an algorithm easier... 4/15/94 Daniel@0: % Fixed Cosine Transform (indices of cos() were swapped) - 5/26/95 Daniel@0: % Added optional frameRate argument - 6/8/95 Daniel@0: % Added proper filterbank reconstruction using inverse DCT - 10/27/95 Daniel@0: % Added filterbank inversion to reconstruct spectrum - 11/1/95 Daniel@0: Daniel@0: % (c) 1998 Interval Research Corporation Daniel@0: Daniel@0: function [ceps,freqresp,fb,fbrecon,freqrecon] = ... Daniel@0: mfcc(input, samplingRate, frameRate) Daniel@0: global mfccDCTMatrix mfccFilterWeights Daniel@0: Daniel@0: [r c] = size(input); Daniel@0: if (r > c) Daniel@0: input=input'; Daniel@0: end Daniel@0: Daniel@0: % Filter bank parameters Daniel@0: lowestFrequency = 133.3333; Daniel@0: linearFilters = 13; Daniel@0: linearSpacing = 66.66666666; Daniel@0: logFilters = 27; Daniel@0: logSpacing = 1.0711703; Daniel@0: fftSize = 512; Daniel@0: cepstralCoefficients = 13; Daniel@0: windowSize = 400; Daniel@0: windowSize = 256; % Standard says 400, but 256 makes more sense Daniel@0: % Really should be a function of the sample Daniel@0: % rate (and the lowestFrequency) and the Daniel@0: % frame rate. Daniel@0: if (nargin < 2) samplingRate = 16000; end; Daniel@0: if (nargin < 3) frameRate = 100; end; Daniel@0: Daniel@0: % Keep this around for later.... Daniel@0: totalFilters = linearFilters + logFilters; Daniel@0: Daniel@0: % Now figure the band edges. Interesting frequencies are spaced Daniel@0: % by linearSpacing for a while, then go logarithmic. First figure Daniel@0: % all the interesting frequencies. Lower, center, and upper band Daniel@0: % edges are all consequtive interesting frequencies. Daniel@0: Daniel@0: freqs = lowestFrequency + (0:linearFilters-1)*linearSpacing; Daniel@0: freqs(linearFilters+1:totalFilters+2) = ... Daniel@0: freqs(linearFilters) * logSpacing.^(1:logFilters+2); Daniel@0: Daniel@0: lower = freqs(1:totalFilters); Daniel@0: center = freqs(2:totalFilters+1); Daniel@0: upper = freqs(3:totalFilters+2); Daniel@0: Daniel@0: % We now want to combine FFT bins so that each filter has unit Daniel@0: % weight, assuming a triangular weighting function. First figure Daniel@0: % out the height of the triangle, then we can figure out each Daniel@0: % frequencies contribution Daniel@0: mfccFilterWeights = zeros(totalFilters,fftSize); Daniel@0: triangleHeight = 2./(upper-lower); Daniel@0: fftFreqs = (0:fftSize-1)/fftSize*samplingRate; Daniel@0: Daniel@0: for chan=1:totalFilters Daniel@0: mfccFilterWeights(chan,:) = ... Daniel@0: (fftFreqs > lower(chan) & fftFreqs <= center(chan)).* ... Daniel@0: triangleHeight(chan).*(fftFreqs-lower(chan))/(center(chan)-lower(chan)) + ... Daniel@0: (fftFreqs > center(chan) & fftFreqs < upper(chan)).* ... Daniel@0: triangleHeight(chan).*(upper(chan)-fftFreqs)/(upper(chan)-center(chan)); Daniel@0: end Daniel@0: %semilogx(fftFreqs,mfccFilterWeights') Daniel@0: %axis([lower(1) upper(totalFilters) 0 max(max(mfccFilterWeights))]) Daniel@0: Daniel@0: hamWindow = 0.54 - 0.46*cos(2*pi*(0:windowSize-1)/windowSize); Daniel@0: Daniel@0: if 0 % Window it like ComplexSpectrum Daniel@0: windowStep = samplingRate/frameRate; Daniel@0: a = .54; Daniel@0: b = -.46; Daniel@0: wr = sqrt(windowStep/windowSize); Daniel@0: phi = pi/windowSize; Daniel@0: hamWindow = 2*wr/sqrt(4*a*a+2*b*b)* ... Daniel@0: (a + b*cos(2*pi*(0:windowSize-1)/windowSize + phi)); Daniel@0: end Daniel@0: Daniel@0: % Figure out Discrete Cosine Transform. We want a matrix Daniel@0: % dct(i,j) which is totalFilters x cepstralCoefficients in size. Daniel@0: % The i,j component is given by Daniel@0: % cos( i * (j+0.5)/totalFilters pi ) Daniel@0: % where we have assumed that i and j start at 0. Daniel@0: mfccDCTMatrix = 1/sqrt(totalFilters/2)*cos((0:(cepstralCoefficients-1))' * ... Daniel@0: (2*(0:(totalFilters-1))+1) * pi/2/totalFilters); Daniel@0: mfccDCTMatrix(1,:) = mfccDCTMatrix(1,:) * sqrt(2)/2; Daniel@0: Daniel@0: %imagesc(mfccDCTMatrix); Daniel@0: Daniel@0: % Filter the input with the preemphasis filter. Also figure how Daniel@0: % many columns of data we will end up with. Daniel@0: if 1 Daniel@0: preEmphasized = filter([1 -.97], 1, input); Daniel@0: else Daniel@0: preEmphasized = input; Daniel@0: end Daniel@0: windowStep = samplingRate/frameRate; Daniel@0: cols = fix((length(input)-windowSize)/windowStep); Daniel@0: Daniel@0: % Allocate all the space we need for the output arrays. Daniel@0: ceps = zeros(cepstralCoefficients, cols); Daniel@0: if (nargout > 1) freqresp = zeros(fftSize/2, cols); end; Daniel@0: if (nargout > 2) fb = zeros(totalFilters, cols); end; Daniel@0: Daniel@0: % Invert the filter bank center frequencies. For each FFT bin Daniel@0: % we want to know the exact position in the filter bank to find Daniel@0: % the original frequency response. The next block of code finds the Daniel@0: % integer and fractional sampling positions. Daniel@0: if (nargout > 4) Daniel@0: fr = (0:(fftSize/2-1))'/(fftSize/2)*samplingRate/2; Daniel@0: j = 1; Daniel@0: for i=1:(fftSize/2) Daniel@0: if fr(i) > center(j+1) Daniel@0: j = j + 1; Daniel@0: end Daniel@0: if j > totalFilters-1 Daniel@0: j = totalFilters-1; Daniel@0: end Daniel@0: fr(i) = min(totalFilters-.0001, ... Daniel@0: max(1,j + (fr(i)-center(j))/(center(j+1)-center(j)))); Daniel@0: end Daniel@0: fri = fix(fr); Daniel@0: frac = fr - fri; Daniel@0: Daniel@0: freqrecon = zeros(fftSize/2, cols); Daniel@0: end Daniel@0: Daniel@0: % Ok, now let's do the processing. For each chunk of data: Daniel@0: % * Window the data with a hamming window, Daniel@0: % * Shift it into FFT order, Daniel@0: % * Find the magnitude of the fft, Daniel@0: % * Convert the fft data into filter bank outputs, Daniel@0: % * Find the log base 10, Daniel@0: % * Find the cosine transform to reduce dimensionality. Daniel@0: for start=0:cols-1 Daniel@0: first = start*windowStep + 1; Daniel@0: last = first + windowSize-1; Daniel@0: fftData = zeros(1,fftSize); Daniel@0: fftData(1:windowSize) = preEmphasized(first:last).*hamWindow; Daniel@0: fftMag = abs(fft(fftData)); Daniel@0: earMag = log10(mfccFilterWeights * fftMag'); Daniel@0: Daniel@0: ceps(:,start+1) = mfccDCTMatrix * earMag; Daniel@0: if (nargout > 1) freqresp(:,start+1) = fftMag(1:fftSize/2)'; end; Daniel@0: if (nargout > 2) fb(:,start+1) = earMag; end Daniel@0: if (nargout > 3) Daniel@0: fbrecon(:,start+1) = ... Daniel@0: mfccDCTMatrix(1:cepstralCoefficients,:)' * ... Daniel@0: ceps(:,start+1); Daniel@0: end Daniel@0: if (nargout > 4) Daniel@0: f10 = 10.^fbrecon(:,start+1); Daniel@0: freqrecon(:,start+1) = samplingRate/fftSize * ... Daniel@0: (f10(fri).*(1-frac) + f10(fri+1).*frac); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % OK, just to check things, let's also reconstruct the original FB Daniel@0: % output. We do this by multiplying the cepstral data by the transpose Daniel@0: % of the original DCT matrix. This all works because we were careful to Daniel@0: % scale the DCT matrix so it was orthonormal. Daniel@0: if 1 & (nargout > 3) Daniel@0: fbrecon = mfccDCTMatrix(1:cepstralCoefficients,:)' * ceps; Daniel@0: % imagesc(mt(:,1:cepstralCoefficients)*mfccDCTMatrix); Daniel@0: end; Daniel@0: Daniel@0: