FFT Function Error: ArrayOutoftheBoundException 6

edited March 2014 in Questions about Code

Hi, I have followd a tutorial for implementing custom FFT function but it is not working properly. Tutorial

Actually I am trying to implement the same as shown in this video but some how the FFT graph is not coming up as expected.

Here is my attempt

/*FFT: http://blog.datasingularity.com/?s=fft */
ArrayList<Value> poop = new ArrayList();
int t=0;
float avg=0;
float[] data;
void setup() {
  size(800, 300);
}

//void mouseDragged() {
//  Value P = new Value(50, mouseX);
//  poop.add(P);
//}

void draw() {
  background(-1);
  line(width/2, 10, mouseX, 10);
  Value P = new Value(50, mouseX);
  poop.add(P);
  //--------------------------------
  data =  new float[poop.size()];
  //--------------------------------
  for (int i=0;i<poop.size();i++) {
    Value val = (Value) poop.get(i);
    data[i] = val.y;
  }
  //-----------------------------------------------
  float[] dat = four1(data, data.length/2, 1);
  beginShape();
  for (int j=0;j<dat.length;j++) {
    float yval = map(dat[j], 0, max(dat), height/2, 10);
    vertex(j, yval);
  }
  endShape();
}

// CLASS FOR STORING VALUES
class Value {
  float x, y;
  Value(float x, float y) {
    this.x = x;
    this.y = y;
  }
}
// num : number of omplex samples
// data[] : array containing the data  samples real and imaginary 
//          part in alternating order ( lengt  2*num)
// isign: is 1 to calculate FFT and -1 to calculate unverse FFT
// FFT FUNCTION
public float[] four1(float data[], int num, int isign) {

  float wt, theta, wr, wi, wpr, wpi, tempi, tempr;
  int i, j, n, m;
  //n = nn << 1;
  n = 2*num;
  j = 0;
  for (i =1; i < n; i += 2) {
    if (j > i) {
      float temp;
      temp = data[j];
      data[j] = data[i];
      data[i] = temp;
      temp = data[j+1];
      data[j+1] = data[i+1];
      data[i+1] = temp;
    }
    //m = n >> 1;
    m = num;
    while (m >= 2 && j > m) {
      j -= m;
      m >>= 1;
    }
    j += m;
  }

  // Danielson -Lanzcos algorithm
  int mmax, istep;
  mmax = 2;
  while (n > mmax) {  // each Danielson -Lanzcos step
    istep = (mmax << 1);
    theta = isign*(2.0*PI/mmax);
    wt = sin(0.5*theta);
    // wpr = -2.0*wt*wt;
    wpr = cos(theta);
    wpi = sin(theta);
    wr = 1.0;
    wi = 0.0;
    for (m = 1; m < mmax; m += 2) {
      for (i = m; i <= n; i += istep) {
        j = i+mmax;
        tempr = wr*data[j-1]-wi*data[j];
        tempi = wr*data[j]+wi*data[j-1];
        data[j-1] = data[i-1] - tempr;
        data[j] = data[i] - tempi;
        data[i-1] += tempr;
        data[i] += tempi;
      }
      wt = wr;// new line
      wr = wt*wpr-wi*wpi;
      wi = wi*wpr+wt*wpi;
      //      wr = wt*wpr-wi*wpi+wr;
      //      wi = wi*wpr+wt*wpi+wi;
    }
    mmax = istep;
  }
  return data;
}

Answers

  • edited March 2014

    I have several doubt

    1. Am I correctly creating the data sample for FFT ? If not then how can I create data smaple?
    2. When I change data.length/2 --> data.length/4 in line #28 it works but I am not sure about the graph wheather it showns correctly or not. Why is this happening? Since in the tutorial it has been mentioned that buffer size would be the half of the length of the data array.
Sign In or Register to comment.