Welcome, Guest. Please login or register.

Author Topic: Fourier Transform - Help Request  (Read 4271 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline countzeroTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2005
  • Posts: 1938
    • Show all replies
    • http://blog.coze.org
Fourier Transform - Help Request
« on: July 26, 2006, 12:38:54 PM »
I want to write a c function which implements fourier transform in 2d in a quick manner. I wrote my own version by looking at the formula and it seems to work ok. However I was not very pleased with the speed so I wrote a fast fourier function from the book numerical recipes in c. However, when I check the results don't match. There is a -1 coefficient difference in the imaginary set with my inverse fourier and numerical recipes in c's fast fourier version.  I was wondering how can I go about and check which one is correct ? does anyone have an idea ?

numerical recipes in c
wikipedia fourier transform(where I implemented the formula from)
another page with fft c code(probably buggy though)

my humble code for those willing to debug : (which assumes all data is real)
Code: [Select]


double m2pi = -2.0*pi;
for (u=0; u<M; u++)
for (v=0; v<N; v++) {
temp = 0, itemp = 0;
for (x=0; x<M; x++)
for (y=0; y<N; y++) {
temp += A[x][y].re*cos(m2pi*u*x/M+m2pi*v*y/N);
itemp += A[x][y].re*sin(m2pi*u*x/M+m2pi*v*y/N);
}
B[u][v].re = temp;
B[u][v].im = itemp;
}


and the inverse

Code: [Select]

double m2pi = 2.0*pi;
for (u=0; u for (v=0; v temp = 0, itemp = 0;
for (x=0; x for (y=0; y constant = m2pi*u*x/M+m2pi*v*y/N;
cos_coef = cos(constant);
sin_coef = sin(constant);
temp += B[x][y].re*cos_coef - B[x][y].im*sin_coef;
itemp += B[x][y].im*cos_coef + B[x][y].re*sin_coef;
}
C[u][v].re = temp/(M*N);
C[u][v].im = itemp/(M*N);
}

I believe in mt. Fuji
 

Offline countzeroTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2005
  • Posts: 1938
    • Show all replies
    • http://blog.coze.org
Re: Fourier Transform - Help Request
« Reply #1 on: July 26, 2006, 05:55:46 PM »
yes, it was a nuisiance but I believe I fixed it.

now, when I use the numerical recipes code, it just works both inverse and forward. I believe I'm doing something wrong in my code, cause I get the negative of the imaginary part. However I can still get the original sequence with the inverse transform (my code). my code vs num. rep. just doesn't work because of the difference of the imaginary parts.

no, it's not for image processing. It was for somekind of contest, which I already failed to submit in time.
I believe in mt. Fuji
 

Offline countzeroTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2005
  • Posts: 1938
    • Show all replies
    • http://blog.coze.org
Re: Fourier Transform - Help Request
« Reply #2 on: July 27, 2006, 08:58:56 AM »
ok, I just setup matlab to test results and it turns out that my code was correct after all and the num reps was wrong. probably I made a mistake when I was fiddling with the offsets. Now I'm trying to understand what actually num. rep. code does and debug it. I wonder if it would be easier if I set about to writing my own fft code.
I believe in mt. Fuji