Printing the band with the highest amplitude (fft)

Hello,

I am currently trying to get into the sound library and especially fft analysis. I want to print the number of the band with the highest amplitude in the console for each draw loop. I used the following code but it is only returning 0.0 and 1.0:

import processing.sound.*;

FFT fft;
SoundFile sound;

int bands = 512;
float[] spectrum = new float[bands];
float max = 0;

void setup(){
  sound = new SoundFile(this, "demo_II.mp3");
  sound.loop();
  
  fft = new FFT(this, bands);
  fft.input(sound);
}

void draw(){
  fft.analyze(spectrum);
  max = 0;
  
  for(int i = 0; i < bands; i++){
    //println(i); // <- returning the correct value of i
    if(spectrum[i] > max){
      max = float(i);
    }
  }
  
  println(max);
}

I think I know what’s happening.

You want to println the number of the highest band (not how high the amp of this band is)

Now, max stores just i, the number of the band

You need an addtional var maxValue to compare to

    if(spectrum[i] > maxValue ){
       maxValue = spectrum[i]; 
       max = i; 

Remark

I think maybe spectrum[i] is only between 0 and 1 because in the example they use spectrum[i]*height*5 :

see FFT \ Language (API) \ Processing 3+

Warm regards,

Chrisir

Hello,

The description will explain this:
https://www.processing.org/reference/libraries/sound/FFT.html

Normalization usually means to scale a variable to have a values between 0 and 1.

This is what you were doing:
https://processing.org/reference/float.html

Try this:

    if(spectrum[i] > max)
      {
      //max = float(i);
      max = spectrum[i];
      index = i;
      }
  }
  println(index, max);

:)