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