annotate toolboxes/MIRtoolbox1.3.2/AuditoryToolbox/mfcc.m @ 0:cc4b1211e677 tip

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