Skip to content

Commit 2035b6b

Browse files
committed
Merge branch 'newTone' of https://github.com/processing/p5.js-sound into newTone
2 parents ca7e10e + 62cf23c commit 2035b6b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/fft.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,97 @@ define(function (require) {
371371
var x = this.getEnergy(freq1, freq2);
372372
return x;
373373
}
374+
375+
376+
377+
/**
378+
* Returns the
379+
* <a href="en.wikipedia.org/wiki/Spectral_centroid" target="_blank">
380+
* spectral centroid</a> of the input signal.
381+
* <em>NOTE: analyze() must be called prior to getCentroid(). Analyze()
382+
* tells the FFT to analyze frequency data, and getCentroid() uses
383+
* the results determine the spectral centroid.</em></p>
384+
*
385+
* @method getCentroid
386+
* @return {Number} Spectral Centroid Frequency Frequency of the spectral centroid in Hz.
387+
*
388+
*
389+
* @example
390+
* <div><code>
391+
*
392+
*
393+
*function setup(){
394+
* cnv = createCanvas(800,400);
395+
* sound = new p5.AudioIn();
396+
* sound.start();
397+
* fft = new p5.FFT();
398+
* sound.connect(fft);
399+
*}
400+
*
401+
*
402+
*function draw(){
403+
*
404+
* var centroidplot = 0.0;
405+
* var spectralCentroid = 0;
406+
*
407+
*
408+
* background(0);
409+
* stroke(0,255,0);
410+
* var spectrum = fft.analyze();
411+
* fill(0,255,0); // spectrum is green
412+
*
413+
* //draw the spectrum
414+
*
415+
* for (var i = 0; i< spectrum.length; i++){
416+
* var x = map(log(i), 0, log(spectrum.length), 0, width);
417+
* var h = map(spectrum[i], 0, 255, 0, height);
418+
* var rectangle_width = (log(i+1)-log(i))*(width/log(spectrum.length));
419+
* rect(x, height, rectangle_width, -h )
420+
* }
421+
422+
* var nyquist = 22050;
423+
*
424+
* // get the centroid
425+
* spectralCentroid = fft.getCentroid();
426+
*
427+
* // the mean_freq_index calculation is for the display.
428+
* var mean_freq_index = spectralCentroid/(nyquist/spectrum.length);
429+
*
430+
* centroidplot = map(log(mean_freq_index), 0, log(spectrum.length), 0, width);
431+
*
432+
*
433+
* stroke(255,0,0); // the line showing where the centroid is will be red
434+
*
435+
* rect(centroidplot, 0, width / spectrum.length, height)
436+
* noStroke();
437+
* fill(255,255,255); // text is white
438+
* textSize(40);
439+
* text("centroid: "+round(spectralCentroid)+" Hz", 10, 40);
440+
*}
441+
* </code></div>
442+
*/
443+
p5.FFT.prototype.getCentroid = function() {
444+
var nyquist = p5sound.audiocontext.sampleRate/2;
445+
var cumulative_sum = 0;
446+
var centroid_normalization = 0;
447+
448+
for (var i = 0; i < this.freqDomain.length; i++)
449+
{
450+
cumulative_sum += i * this.freqDomain[i];
451+
centroid_normalization += this.freqDomain[i];
452+
}
453+
454+
var mean_freq_index = 0;
455+
456+
if (centroid_normalization != 0)
457+
{
458+
mean_freq_index = (cumulative_sum / centroid_normalization);
459+
}
460+
461+
var spec_centroid_freq = (mean_freq_index * (nyquist / this.freqDomain.length));
462+
return spec_centroid_freq;
463+
}
464+
374465
/**
375466
* Smooth FFT analysis by averaging with the last analysis frame.
376467
*

0 commit comments

Comments
 (0)