Fast Fourrier Transform fft

edited August 2016 in Library Questions

I want to do a fast fourier transform (fft) with 2^15 = 32768 bands. Even if it basically works I get often crashes while starting the sektch : Java(TM) Platform SE binary does not function So I need to start the same sketch 2 - 3 times before it runs. I am worrying because of stability issues?

A further question is: how can I determine when a new value for a frequency (fft - bin) is computed and available? this sketch gives every 1500 ms a new value because with 2^15 bands one fft frame computation needs 2*bands/sampleRate = 2 * 2^15 /44100 = 1.486 sec = 1486 ms. with frameRate 30 this is achieved within 45 frames so 1500 ms. Is there any event Detection for a new FFT frame or need I to count frames?

import processing.sound.*;

FFT fft;
AudioIn in;    

int bands = 32768;
float[] spectrum = new float[bands];

void setup()
{ 
  size(500, 300);     

   // Create an Input stream which is routed into the Amplitude analyzer
  fft = new FFT(this, bands);
  in = new AudioIn(this, 0);

   // start the Audio Input
  in.start();

   // patch the AudioIn
  fft.input(in);    
}

void draw() {       
  fft.analyze(spectrum);  
  println("spectrum["+149+"]: "+spectrum[149]+" time: "+millis());
}

Answers

  • From the code below I can see the update of every processed frame is about every 16ms. In my system, processing 500 frames takes about 8300 msecs. Here I am loading an array of 500 entries and plotting them after the full array is loaded before it reloads a new set of 500 frames.

    Notice the manipulation in my map function is arbitrary and chosen based on observed values. You should adjusted to your suitability.

    I am not sure how the fft is managing the audio input. Looking at the reference, there are not many methods available for these classes. Maybe you can check the source code if it is available in gitHub?

    Kf

    import processing.sound.*;
    
    FFT fft;
    AudioIn in;    
    
    int bands = 32768;
    float[] spectrum = new float[bands];
    
    float v149[];
    int t0;
    
    void setup()
    { 
      size(500, 300);     
    
       // Create an Input stream which is routed into the Amplitude analyzer
      fft = new FFT(this, bands);
      in = new AudioIn(this, 0);
    
       // start the Audio Input
      in.start();
    
       // patch the AudioIn
      fft.input(in);  
    
      v149 = new float[width];
      fill(200,25,25);
      t0=millis();
    }
    
    int bin=0;
    void draw() {       
      fft.analyze(spectrum);  
      //println("spectrum["+149+"]: "+spectrum[149]+" time: "+millis());
    
    
      if(bin==width){
        background(0);
    
        for(bin=0;bin<width;bin++)
          ellipse(bin,map(v149[bin]*10000,0,1,1,height),10,10);
    
          bin=0;
          println("Bands updated after " + (millis()-t0)+" msecs.");
          t0=millis();
      }
      else{
    
        v149[bin]=spectrum[149];
        bin++;
      }
    
    }
    
  • Ok, so I was looking more into this and you are right, the analyzed frame is only updated every 1.3 or so seconds. Maybe you could try creating a buffer directly from the audio input and then you update the buffer with new data while removing data at the same rate. You call analyze on that buffer. In this way you could access analyzed data faster, only if the FFT function can handle faster rates.

    The algorithm would look like this

    *)Before draw Create buffer Initilize buffer with a full data set of 1.3 secs

    *)In draw Remove 100ms worth of data Add 100 ms worth of data (HERE 100 ms assumes you are running at lower cps) Analyze(Buffer) Extract bin 149

    In this way you are not analyzing every 1.3 secs but maybe every 100 msecs. Here a circular link list will come handy.

    I did not implement the algorithm. The code below just plots the algorithm in dB scale and it was just a test case for your original code.

    Kf

    import processing.sound.*;

    FFT fft;
    AudioIn in;    
    
    final float log10=log(10);
    
    int bands = 32768;
    float[] spectrum = new float[bands];
    
    
    void setup()
    { 
      size(500, 300);   
      frameRate(30);
      background(0);
    
      // Create an Input stream which is routed into the Amplitude analyzer
      fft = new FFT(this, bands);
      in = new AudioIn(this, 0);
    
      stroke(180);
      strokeWeight(2);
      line(0,height>>1,width,height>>1);
      stroke(250);
      strokeWeight(1);  
      line(0,mapLog10(2),width,mapLog10(2));
      line(0,mapLog10(0.100),width,mapLog10(0.100));
      line(0,mapLog10(0.010),width,mapLog10(0.010));
      line(0,mapLog10(0.001),width,mapLog10(0.001));
    
      // start the Audio Input
      in.start();
    
      // patch the AudioIn
      fft.input(in);  
    
      fill(200, 25, 25);
      noStroke();
    }
    
    int bin=0;
    int ctr=0;
    void draw() {     
    
      ctr++;
      if (ctr<45)
        return;
    
      fft.analyze(spectrum);  
      println("spectrum["+149+"]: "+spectrum[149]+" time: "+millis());
    
      if (bin<width) {
        ellipse(bin, mapLog10(spectrum[149]), 5, 5);
        ellipse(bin, mapLog10(1.0/bin), 5, 5);
      } 
      else
        bin=0;
    
      bin++;
      ctr=0;
    }
    
    float calcLog10(float x){
      if(x>0)
      return -10*log(x)/log10;
      else return 0;
    }
    
    float mapLog10(float v){
      return map(calcLog10(v), 0, 60, 0, height);
    }
    
  • thanks kfrajer for your answer. the hard drive of my working pc is broken and I have to do some recover issue. So I hope tomorrow I can try and follow your hints :)

  • thanks kfrajer for your suggestions!

    My pc is working again but I am still concerned with a familiary bereavement happened in the last days. I try to continue this case.

    I think my problem at this time is a bit more basic: I get nearly always "could not run the sektch (Target VM failed to initialize)" messages if I try to run any of the above code. Other sketches without processing sound library work fine. So it seems to have to do with the processing sound library?

    I am using a Behringer FCA1616 audio intefrace with Asio drivers on Win7 64 bit. Do not know if the driver of Beghringer cause this problem.

Sign In or Register to comment.