Printing the band with the highest amplitude (fft)


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");
  fft = new FFT(this, bands);

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

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; 


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,



The description will explain this:

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

This is what you were doing:

Try this:

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