annotate Code/Descriptors/Matlab/Speech/mfcc.m @ 4:92ca03a8fa99 tip

Update to ICASSP 2013 benchmark
author Dawn Black
date Wed, 13 Feb 2013 11:02:39 +0000
parents
children
rev   line source
Dawn@4 1 % mfcc - Mel frequency cepstrum coefficient analysis.
Dawn@4 2 % [ceps,freqresp,fb,fbrecon,freqrecon] = ...
Dawn@4 3 % mfcc(input, samplingRate, [frameRate])
Dawn@4 4 % Find the cepstral coefficients (ceps) corresponding to the
Dawn@4 5 % input. Four other quantities are optionally returned that
Dawn@4 6 % represent:
Dawn@4 7 % the detailed fft magnitude (freqresp) used in MFCC calculation,
Dawn@4 8 % the mel-scale filter bank output (fb)
Dawn@4 9 % the filter bank output by inverting the cepstrals with a cosine
Dawn@4 10 % transform (fbrecon),
Dawn@4 11 % the smooth frequency response by interpolating the fb reconstruction
Dawn@4 12 % (freqrecon)
Dawn@4 13 % -- Malcolm Slaney, August 1993
Dawn@4 14 % Modified a bit to make testing an algorithm easier... 4/15/94
Dawn@4 15 % Fixed Cosine Transform (indices of cos() were swapped) - 5/26/95
Dawn@4 16 % Added optional frameRate argument - 6/8/95
Dawn@4 17 % Added proper filterbank reconstruction using inverse DCT - 10/27/95
Dawn@4 18 % Added filterbank inversion to reconstruct spectrum - 11/1/95
Dawn@4 19
Dawn@4 20 % (c) 1998 Interval Research Corporation
Dawn@4 21
Dawn@4 22 function [ceps,freqresp,fb,fbrecon,freqrecon] = ...
Dawn@4 23 mfcc(input, samplingRate, frameRate)
Dawn@4 24 global mfccDCTMatrix mfccFilterWeights
Dawn@4 25
Dawn@4 26 [r c] = size(input);
Dawn@4 27 if (r > c)
Dawn@4 28 input=input';
Dawn@4 29 end
Dawn@4 30
Dawn@4 31 % Filter bank parameters
Dawn@4 32 lowestFrequency = 133.3333;
Dawn@4 33 linearFilters = 13;
Dawn@4 34 linearSpacing = 66.66666666;
Dawn@4 35 logFilters = 27;
Dawn@4 36 logSpacing = 1.0711703;
Dawn@4 37 fftSize = 512;
Dawn@4 38 cepstralCoefficients = 13;
Dawn@4 39 windowSize = 400;
Dawn@4 40 windowSize = 256; % Standard says 400, but 256 makes more sense
Dawn@4 41 % Really should be a function of the sample
Dawn@4 42 % rate (and the lowestFrequency) and the
Dawn@4 43 % frame rate.
Dawn@4 44 if (nargin < 2) samplingRate = 16000; end;
Dawn@4 45 if (nargin < 3) frameRate = 100; end;
Dawn@4 46
Dawn@4 47 % Keep this around for later....
Dawn@4 48 totalFilters = linearFilters + logFilters;
Dawn@4 49
Dawn@4 50 % Now figure the band edges. Interesting frequencies are spaced
Dawn@4 51 % by linearSpacing for a while, then go logarithmic. First figure
Dawn@4 52 % all the interesting frequencies. Lower, center, and upper band
Dawn@4 53 % edges are all consequtive interesting frequencies.
Dawn@4 54
Dawn@4 55 freqs = lowestFrequency + (0:linearFilters-1)*linearSpacing;
Dawn@4 56 freqs(linearFilters+1:totalFilters+2) = ...
Dawn@4 57 freqs(linearFilters) * logSpacing.^(1:logFilters+2);
Dawn@4 58
Dawn@4 59 lower = freqs(1:totalFilters);
Dawn@4 60 center = freqs(2:totalFilters+1);
Dawn@4 61 upper = freqs(3:totalFilters+2);
Dawn@4 62
Dawn@4 63 % We now want to combine FFT bins so that each filter has unit
Dawn@4 64 % weight, assuming a triangular weighting function. First figure
Dawn@4 65 % out the height of the triangle, then we can figure out each
Dawn@4 66 % frequencies contribution
Dawn@4 67 mfccFilterWeights = zeros(totalFilters,fftSize);
Dawn@4 68 triangleHeight = 2./(upper-lower);
Dawn@4 69 fftFreqs = (0:fftSize-1)/fftSize*samplingRate;
Dawn@4 70
Dawn@4 71 for chan=1:totalFilters
Dawn@4 72 mfccFilterWeights(chan,:) = ...
Dawn@4 73 (fftFreqs > lower(chan) & fftFreqs <= center(chan)).* ...
Dawn@4 74 triangleHeight(chan).*(fftFreqs-lower(chan))/(center(chan)-lower(chan)) + ...
Dawn@4 75 (fftFreqs > center(chan) & fftFreqs < upper(chan)).* ...
Dawn@4 76 triangleHeight(chan).*(upper(chan)-fftFreqs)/(upper(chan)-center(chan));
Dawn@4 77 end
Dawn@4 78 %semilogx(fftFreqs,mfccFilterWeights')
Dawn@4 79 %axis([lower(1) upper(totalFilters) 0 max(max(mfccFilterWeights))])
Dawn@4 80
Dawn@4 81 hamWindow = 0.54 - 0.46*cos(2*pi*(0:windowSize-1)/windowSize);
Dawn@4 82
Dawn@4 83 if 0 % Window it like ComplexSpectrum
Dawn@4 84 windowStep = samplingRate/frameRate;
Dawn@4 85 a = .54;
Dawn@4 86 b = -.46;
Dawn@4 87 wr = sqrt(windowStep/windowSize);
Dawn@4 88 phi = pi/windowSize;
Dawn@4 89 hamWindow = 2*wr/sqrt(4*a*a+2*b*b)* ...
Dawn@4 90 (a + b*cos(2*pi*(0:windowSize-1)/windowSize + phi));
Dawn@4 91 end
Dawn@4 92
Dawn@4 93 % Figure out Discrete Cosine Transform. We want a matrix
Dawn@4 94 % dct(i,j) which is totalFilters x cepstralCoefficients in size.
Dawn@4 95 % The i,j component is given by
Dawn@4 96 % cos( i * (j+0.5)/totalFilters pi )
Dawn@4 97 % where we have assumed that i and j start at 0.
Dawn@4 98 mfccDCTMatrix = 1/sqrt(totalFilters/2)*cos((0:(cepstralCoefficients-1))' * ...
Dawn@4 99 (2*(0:(totalFilters-1))+1) * pi/2/totalFilters);
Dawn@4 100 mfccDCTMatrix(1,:) = mfccDCTMatrix(1,:) * sqrt(2)/2;
Dawn@4 101
Dawn@4 102 %imagesc(mfccDCTMatrix);
Dawn@4 103
Dawn@4 104 % Filter the input with the preemphasis filter. Also figure how
Dawn@4 105 % many columns of data we will end up with.
Dawn@4 106 if 1
Dawn@4 107 preEmphasized = filter([1 -.97], 1, input);
Dawn@4 108 else
Dawn@4 109 preEmphasized = input;
Dawn@4 110 end
Dawn@4 111 windowStep = samplingRate/frameRate;
Dawn@4 112 cols = fix((length(input)-windowSize)/windowStep);
Dawn@4 113
Dawn@4 114 % Allocate all the space we need for the output arrays.
Dawn@4 115 ceps = zeros(cepstralCoefficients, cols);
Dawn@4 116 if (nargout > 1) freqresp = zeros(fftSize/2, cols); end;
Dawn@4 117 if (nargout > 2) fb = zeros(totalFilters, cols); end;
Dawn@4 118
Dawn@4 119 % Invert the filter bank center frequencies. For each FFT bin
Dawn@4 120 % we want to know the exact position in the filter bank to find
Dawn@4 121 % the original frequency response. The next block of code finds the
Dawn@4 122 % integer and fractional sampling positions.
Dawn@4 123 if (nargout > 4)
Dawn@4 124 fr = (0:(fftSize/2-1))'/(fftSize/2)*samplingRate/2;
Dawn@4 125 j = 1;
Dawn@4 126 for i=1:(fftSize/2)
Dawn@4 127 if fr(i) > center(j+1)
Dawn@4 128 j = j + 1;
Dawn@4 129 end
Dawn@4 130 if j > totalFilters-1
Dawn@4 131 j = totalFilters-1;
Dawn@4 132 end
Dawn@4 133 fr(i) = min(totalFilters-.0001, ...
Dawn@4 134 max(1,j + (fr(i)-center(j))/(center(j+1)-center(j))));
Dawn@4 135 end
Dawn@4 136 fri = fix(fr);
Dawn@4 137 frac = fr - fri;
Dawn@4 138
Dawn@4 139 freqrecon = zeros(fftSize/2, cols);
Dawn@4 140 end
Dawn@4 141
Dawn@4 142 % Ok, now let's do the processing. For each chunk of data:
Dawn@4 143 % * Window the data with a hamming window,
Dawn@4 144 % * Shift it into FFT order,
Dawn@4 145 % * Find the magnitude of the fft,
Dawn@4 146 % * Convert the fft data into filter bank outputs,
Dawn@4 147 % * Find the log base 10,
Dawn@4 148 % * Find the cosine transform to reduce dimensionality.
Dawn@4 149 for start=0:cols-1
Dawn@4 150 first = start*windowStep + 1;
Dawn@4 151 last = first + windowSize-1;
Dawn@4 152 fftData = zeros(1,fftSize);
Dawn@4 153 fftData(1:windowSize) = preEmphasized(first:last).*hamWindow;
Dawn@4 154 fftMag = abs(fft(fftData));
Dawn@4 155 earMag = log10(mfccFilterWeights * fftMag');
Dawn@4 156
Dawn@4 157 ceps(:,start+1) = mfccDCTMatrix * earMag;
Dawn@4 158 if (nargout > 1) freqresp(:,start+1) = fftMag(1:fftSize/2)'; end;
Dawn@4 159 if (nargout > 2) fb(:,start+1) = earMag; end
Dawn@4 160 if (nargout > 3)
Dawn@4 161 fbrecon(:,start+1) = ...
Dawn@4 162 mfccDCTMatrix(1:cepstralCoefficients,:)' * ...
Dawn@4 163 ceps(:,start+1);
Dawn@4 164 end
Dawn@4 165 if (nargout > 4)
Dawn@4 166 f10 = 10.^fbrecon(:,start+1);
Dawn@4 167 freqrecon(:,start+1) = samplingRate/fftSize * ...
Dawn@4 168 (f10(fri).*(1-frac) + f10(fri+1).*frac);
Dawn@4 169 end
Dawn@4 170 end
Dawn@4 171
Dawn@4 172 % OK, just to check things, let's also reconstruct the original FB
Dawn@4 173 % output. We do this by multiplying the cepstral data by the transpose
Dawn@4 174 % of the original DCT matrix. This all works because we were careful to
Dawn@4 175 % scale the DCT matrix so it was orthonormal.
Dawn@4 176 if 1 & (nargout > 3)
Dawn@4 177 fbrecon = mfccDCTMatrix(1:cepstralCoefficients,:)' * ceps;
Dawn@4 178 % imagesc(mt(:,1:cepstralCoefficients)*mfccDCTMatrix);
Dawn@4 179 end;
Dawn@4 180
Dawn@4 181