#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# FFT Function Error: ArrayOutoftheBoundException 6

edited March 2014

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);
//}

void draw() {
background(-1);
line(width/2, 10, mouseX, 10);
Value P = new Value(50, mouseX);
//--------------------------------
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;
}
``````
Tagged: