I've done some reading, and learned the best way to convert recursion to iteration is to use stacks. It's been hard to implement though, because of how return values are used. Any assistance would be fantastic. Here is the recursive function:
public Complex[] fft(Complex[] x, int L) {
int ii;
int kk = 0;
int N = x.Length;
Complex[] Y = new Complex[N];
if (N == 1) {
Y[0] = x[0];
} else {
Complex[] E = new Complex[N / 2];
Complex[] O = new Complex[N / 2];
Complex[] even = new Complex[N / 2];
Complex[] odd = new Complex[N / 2];
for (ii = 0; ii < N; ii++) {
if (ii % 2 == 0) {
even[ii / 2] = x[ii];
}
if (ii % 2 == 1) {
odd[(ii - 1) / 2] = x[ii];
}
}
E = fft(even, L); // RECURSION HERE
O = fft(odd, L); // RECURSION HERE
// RECURSION RESULTS USED HERE
for (kk = 0; kk < N; kk++) {
Y[kk] = E[(kk % (N / 2))] + O[(kk % (N / 2))] * twiddles[kk * (L / N)];
}
}
return Y;
}
The code below doesn't work but it shows my attempt so far
public Complex[] fft2(Complex[] x, int L) {
Stack<Complex[]> stack = new Stack<Complex[]>();
stack.Push(x);
int ii;
int kk;
int N;
while (stack.Count > 0) {
x = stack.Pop();
kk = 0;
N = x.Length;
Complex[] Y = new Complex[N];
if (N == 1) {
Y[0] = x[0];
} else {
Complex[] E = new Complex[N / 2];
Complex[] O = new Complex[N / 2];
Complex[] even = new Complex[N / 2];
Complex[] odd = new Complex[N / 2];
for (ii = 0; ii < N; ii++) {
if (ii % 2 == 0) {
even[ii / 2] = x[ii];
}
if (ii % 2 == 1) {
odd[(ii - 1) / 2] = x[ii];
}
}
stack.Push(even);
stack.Push(odd);
// E = fft2(even, L);
// O = fft2(odd, L);
for (kk = 0; kk < N; kk++) {
Y[kk] = E[(kk % (N / 2))] + O[(kk % (N / 2))] * twiddles[kk * (L / N)];
}
}
}
return Y;
}