Minim's FFT "normalise"

edited April 2014 in Questions about Code

Hi all,

two questions about FFT:

  1. I don't know how to normalise the output so the values can only ever vary between 0 to 1. When I shout in the mic, values exceed 1 by a bit (or more than a bit), showing 1.13, 1.6473, or whatever. I need this to be normalised; is it possible?

  2. I've come up with my own awkward solution to target certain ranges of bands (i.e. only read the low end of the bands, for instance) with a function which takes the upper and lower limit as arguments. Any more professional way to do this in your opinion?

Code:

// Implementing the minim library for audio analysis
import ddf.minim.analysis.*;
import ddf.minim.*;

// Create the minim object
Minim minim;
AudioInput in;
FFT fft;

// Needed later to make fft manipulations
int bands = 0;
float bandsfloat = 0;
float avg_bands = 0;

void setup() {
  size(100, 100);

  // Setting up minim objects
  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 512);
  fft = new FFT(in.bufferSize(), in.sampleRate());
  fft.logAverages(60, 7);

  // Getting the number of bands from fft
  bandsfloat = float(fft.avgSize());
}

void draw() {
// Minim stuff
  fft.forward(in.mix);
  avgBands(0, 5);
}

// This function returns average volume of a selection of bands
// The two arguments are lo and hi percentages of all bands
void avgBands(float lo_t, float hi_t) {
  float bandadd = 0;
  float adder = bandsfloat/100;

  lo_t = lo_t*adder;
  hi_t = hi_t*adder;

  int lo_t_int = int(lo_t);
  int hi_t_int = int(hi_t);

  for (int i = lo_t_int; i < hi_t_int; i = i+1) {
    bandadd += (fft.getAvg(i)/32);
  }
  avg_bands = bandadd/(hi_t_int-lo_t_int);
  println("Band average: " + avg_bands);
  //println(avg_bands);
}

void stop()
{
  minim.stop();
  super.stop();
}
Tagged:
Sign In or Register to comment.