# Not sure how to work with Minim's FFT feature

**URL:** <https://discourse.processing.org/t/not-sure-how-to-work-with-minims-fft-feature/6638>\
**Category:** Libraries\
**Created:** [December 15, 2018, 9:47pm UTC](https://discourse.processing.org/t/not-sure-how-to-work-with-minims-fft-feature/6638 "2018-12-15T21:47:41Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 16, 2018, 2:21am UTC](https://discourse.processing.org/t/not-sure-how-to-work-with-minims-fft-feature/6638/2 "2018-12-16T02:21:59Z")

</div>

pls check also

> [@FFT frequencies](https://discourse.processing.org/t/fft-frequencies/6626/2):
>
> hi, i can not answer about the mentioned “stock library” as i never used it, is it good? where you get it from ? but about FFT let us just assume you would have imported / use the [http://code.compartmental.net/minim/](http://code.compartmental.net/minim/) sound library, then there are examples to find like processing IDE 3.4 / File /Examples / Contributes Libraries / Minim / Basics / AnalyzeSound using that example you could make following changes ( to understand ) in setup () { //.. add println("jingle.bufferSize()…

i think at first you need to know more about the actual numbers…  
so try the code i show later

also i not know about exact differences  
between Processing 3.4 Minim  
and the P5.js version,

> **[GitHub - processing/p5.js-sound: p5.sound brings the Processing approach to...](https://github.com/processing/p5.js-sound)**
>
> p5.sound brings the Processing approach to Web Audio and p5.js. Demos: - GitHub - processing/p5.js-sound: p5.sound brings the Processing approach to Web Audio and p5.js. Demos:

but your comment sounds like  
the p5.js version is normalizing the amplitude???  
as far as i know,  
a FFT gets a series of numbers and spits out  
the 2\* average and the amplitude of  
a sinus what fits in that numbersequences  
( and all harmonics. 2,3,4,5… fitting sinus. )  
again,  
if you give 1024 samples the result is a array of 512 +1  
so, why Fourier only counts the first 512 frequencies  
he could calculate more, ha? kind of lazy?

the basic idea is, if you do one measurement there is no way to  
know if there are frequencies in that signal.  
but if you make 2 measurements you can try to fit that to a sinus.  
( even it still can be anything else, no way to know )  
that is the first problem of any digitizing, sampling of analog signals.  
the more samples the more you know  
also that says you need min 2 samples to fit a frequency,  
that is called the

> **[Signals Sampling Theorem](https://www.tutorialspoint.com/signals_and_systems/signals_sampling_theorem.htm)**
>
> Signals Sampling Theorem - Statement: A continuous time signal can be represented in its samples and can be recovered back when sampling frequency fs is greater than or equal to the twice the highest frequency component of message signal. i. e.

turn that around and you know why FFT only do 512 sinus in 1024 samples.

and for the correlation to the sound frequencies?  
again the FFT does not know frequencies, only samples  
the base frequency ( one sinus in the 1024 samples ) you have to  
calculate what it really means  
by find out how many msecs that 1024 samples mean  
that base frequency ( amplitude in fft[1] ) would be 1 / ( sec of 1024 samples )

now to make from that fft information any of that  
“today” spectrum show

- green to red color on amplitude
- show max of it for some second then fading down…

is sure a challenging coding,  
but sounds more like you looking for ready examples or “better” libraries  
i would think its out there already but i don’t know

> **[Libraries](https://processing.org/reference/libraries/#sound)**
>
> Extend Processing beyond graphics and images into audio, video, and communication with other devices.

also check google tutorial videos…  
[Processing Tutorial #5 on Vimeo](https://vimeo.com/7596987) …

// update:  
i play little bit and got the idea that we need a common ground  
so we can compare data:  
i started my raspberry pi, and used the default audio program i install there

> audacity

and with this i generated a 440 Hz tone “a”  
and export to mp3 file  
that i used in the little bit modified

> **code**
>
> ```auto
> import ddf.minim.*;
> import ddf.minim.analysis.*;
> 
> //String soundfile = "untitled.mp3";
> String soundfile = "the_a.mp3";
> 
> float amp =1;
> float max = -1000;
> 
> Minim minim;
> AudioPlayer sound;
> FFT spectrum;
> 
> void setup() {
> size(600, 600);
> noStroke();
> fill(255);
> minim = new Minim(this);
> sound = minim.loadFile(soundfile,1024);
> sound.loop();
> spectrum = new FFT(sound.bufferSize(), sound.sampleRate());
> println("bufferSize() "+sound.bufferSize());
> println("sampleRate() "+sound.sampleRate());
> println("fft.specSize()"+spectrum.specSize());
> }
> 
> void draw() {
> background(0);
> spectrum.forward(sound.mix);
> if ( keyPressed ) max = -1000;
> for (int i = 0; i < spectrum.specSize(); i++) {
> float x = map(i, 0, spectrum.specSize(), 0, width);
> if ( keyPressed ) if ( spectrum.getBand(i) > max ) max = spectrum.getBand(i);
> //float y = map(spectrum.getBand(i), 0, ???, 0, height);
> rect(x, height-10, width/spectrum.specSize(), -spectrum.getBand(i)*amp);
> if ( keyPressed ) if ( i < 20) print(" i: "+i+" "+nf(spectrum.getBand(i),1,1));
> }
> if ( keyPressed ) println("\nmax "+nf(max,1,1));
> if ( keyPressed ) println("\nnext");
> 
> }
> 
> ```

and you can get the same audio file from:

```auto
http://kll.engineering-news.org/kllfusion01/downloads/the_a.mp3

```

now using this shows

 ![snap-064](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/501bb241c27be3dc6a4bf8aa4c9f3015a2e803b8.png)  
and press any key  
 ![snap-065](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a9abe4f489cf2499b97b0e03278c84dbce68ae77.png)  
the max amplitude at i=10  
ha? can that be related to

> sampleRate() 44100.0  
> and the selected frequency 440Hz

and buffersize 1024

and with buffersize 512  
i find the max at i=5

---

_[View the full topic](https://discourse.processing.org/t/not-sure-how-to-work-with-minims-fft-feature/6638)._
