2 summary:: Mel frequency cepstral coefficients
3 categories:: UGens>Analysis
4 related:: Classes/BeatTrack, Classes/Loudness, Classes/Onsets, Classes/Pitch, Classes/KeyTrack
7 Generates a set of MFCCs; these are obtained from a band-based frequency representation (using the Mel scale by default), and then a discrete cosine transform (DCT). The DCT is an efficient approximation for principal components analysis, so that it allows a compression, or reduction of dimensionality, of the data, in this case reducing 42 band readings to a smaller set of MFCCs. A small number of features (the coefficients) end up describing the spectrum. The MFCCs are commonly used as timbral descriptors.
9 Output values are somewhat normalised for the range 0.0 to 1.0, but there are no guarantees on exact conformance to this. Commonly, the first coefficient will be the highest value.
15 [fft] Audio input to track, which has been pre-analysed by the FFT UGen; see examples below for the expected FFT size.
17 [s] Number of coefficients, defaults to 13, maximum of 42; more efficient to use less of course!
19 returns:: code::#coeff1, coeff2, ...::
23 // Technical note: The 0th coefficient is not generated as it consists of multiplying all bands by 1 and summing
26 // assumes hop of half fftsize, fine
27 b = Buffer.alloc(s, 1024, 1); // for sampling rates 44100 and 48000
28 //b = Buffer.alloc(s, 2048, 1); // for sampling rates 88200 and 96000
30 d = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
37 //in = PlayBuf.ar(1, d, BufRateScale.kr(d), 1, 0, 1);
49 Out.ar(0,Pan2.ar(in));
54 c = Bus.new('control', 0, 13);
57 c.getn(13, { arg val; { val.plot; }.defer });
60 // Continuous graphical display of MFCC values; free routine before closing window
65 w = Window.new("Thirteen MFCC coefficients", Rect(200, 400, 300, 300));
67 ms = MultiSliderView.new(w, Rect(10, 10, 260, 280));
69 ms.value_(Array.fill(13, 0.0));
70 ms.valueThumbSize_(20.0);
71 ms.indexThumbSize_(20.0);
80 c.getn(13, { arg val; { ms.value_(val * 0.9) }.defer });
82 0.04.wait; // 25 frames per second
100 Research notes: Drafts of an MFCC UGen were prepared by both Dan Stowell and Nick Collins; their various ideas are combined here in a cross platform compatible UGen. Mel scale spacing with triangular crossfade overlap is used by default for the bands, approximately tracking the human critical band spacing and bandwidth. Variants such as BFCC (Bark) and EFCC (ERB) given similar results in practice; the Mel scale as used here is the standard as adapted from the speech recognition literature and now applied in music information retrieval.
103 // Calculating Mel Scale Bands; allow up to 42 coefficients, so up to 42 bands
104 // first part of this code adapted from Dan Stowell and Jamie Bullock Mel scale implementation
105 // could later add Bark and ERB options, and possibility of buffer of data to be passed to the UGen for alternative freq warpings
107 var mel_freq_max, mel_freq_min, freq_bw_mel, freq_bands, freq_max, freq_min;
108 var mel_peak, lin_peak, fft_peak;
110 var fftbinstart, fftbinend, fftbinmult, fftbincumul;
112 var sr, fftsize, halffftsize;
113 var whichbandscale, lintoscale, scaletolin;
119 halffftsize = fftsize.div(2);
122 //whichbandscale = 0; // 0 = mel; 1 = bark (CB) 2 = ERB
124 //lintoscale = {arg freq;
125 //switch(whichbandscale,0,{1127 * log(1 + (freq / 700))}, 1, {}, 2, {}).value
127 //scaletolin = {arg scalepos;
128 //switch(whichbandscale, 0, {700 * (exp(scalepos / 1127.0) -1);}, 1, {}, 2, {}).value
131 lintoscale = {arg freq;
132 1127 * log(1 + (freq / 700))
134 scaletolin = {arg scalepos;
135 700 * (exp(scalepos / 1127.0) -1);
138 mel_freq_max = lintoscale.value(freq_max); // 1127 * log(1 + (freq_max / 700));
139 mel_freq_min = lintoscale.value(freq_min); //1127 * log(1 + (freq_min / 700));
140 freq_bw_mel = (mel_freq_max - mel_freq_min) / freq_bands;
142 [mel_freq_max, mel_freq_min, freq_bw_mel].postln;
144 mel_peak = Array.fill(freq_bands + 2, {0.0});
145 lin_peak = Array.fill(freq_bands + 2, {0.0});
146 fft_peak = Array.fill(freq_bands + 2, {0.0});
148 freqperbin = sr / fftsize; // SR/N
150 mel_peak[0] = mel_freq_min;
151 lin_peak[0] = freq_min; // === 700 * (exp(mel_peak[0] / 1127) - 1);
152 fft_peak[0] = (lin_peak[0] / freqperbin).asInteger;
154 for(1, freq_bands + 1,{|n|
156 mel_peak[n] = mel_peak[n - 1] + freq_bw_mel;
157 lin_peak[n] = scaletolin.value(mel_peak[n]); // 700 * (exp(mel_peak[n] / 1127.0) -1);
158 fft_peak[n] = ((lin_peak[n] / freqperbin).asInteger).min(halffftsize); // fft size //rounds down here
162 //Post << mel_peak << nl;
163 //Post << lin_peak << nl;
164 //Post << fft_peak << nl;
166 // [2 / (lin_peak[freq_bands + 1] - lin_peak[freq_bands-1]), 1.0 / (2 / (lin_peak[2] - lin_peak[0]))].postln;
168 fftbinstart = Array.fill(freq_bands, {0});
169 fftbinend = Array.fill(freq_bands, {0});
170 fftbincumul = Array.fill(freq_bands+1, {0});
177 //var normmult=1.0; // preserve power, don't modify band power by area
178 var startbin, endbin, numbins, averager;
182 endbin = fft_peak[i + 1] - 1;
184 startbin = fft_peak[i - 1] + 1;
185 endbin = fft_peak[i + 1] - 1;
188 numbins = endbin - startbin + 1;
189 averager = 1.0 / numbins;
191 // linear crossfade (intended in power) between consecutive band centres
193 tmp = fft_peak[i] - startbin;
195 // could divide by averager but I'm not convinced by the perceptual necessity for this?
196 // ie fftbinmult = fftbinmult ++ (Array.series(tmp + 1, 1.0 / (tmp + 1), 1.0 / (tmp + 1)) * averager);
198 fftbinmult = fftbinmult ++ (Array.series(tmp + 1, 1.0 / (tmp + 1), 1.0 / (tmp + 1)));
200 tmp= endbin- (fft_peak[i]);
202 fftbinmult = fftbinmult ++ (Array.series(tmp, 1.0 + ((-1.0) / (tmp + 1)), (-1.0) / (tmp + 1)));
204 fftbinstart[i] = startbin;
205 fftbinend[i] = endbin;
206 fftbincumul[i] = pos;
208 pos = pos + (endbin - startbin + 1);
211 fftbincumul[freq_bands] = pos - 1;
213 Post << fftbinstart << nl;
214 Post << fftbinend << nl;
215 Post << fftbincumul << nl;
216 Post << fftbinmult << nl;
221 // future work: see http://www.ling.su.se/STAFF/hartmut/bark.htm
224 a = (26.81 / (1 + (1960 / ((100, 200..22000))))) - 0.53;
227 // ERBs (rough calculation, only really valid under 6000Hz, real scale goes up to 42 rather than 37 in 22000 Hz)
228 a = Array.fill(220,{|i| var f; f = i * 100; 11.17 * log((f + 312) / (f + 14675)) + 43.0});
231 // generating DCT coefficients
232 // don't generate i=0 coefficient since it
233 a = Array.fill(42, {|i| cos(pi / 42.0 * ((0..41) + 0.5) * (i + 1))});
234 Post << a.flatten << nl;