Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline countzeroTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2005
  • Posts: 1938
    • Show only replies by countzero
    • 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
 

  • Guest
Re: Fourier Transform - Help Request
« Reply #1 on: July 26, 2006, 12:49:29 PM »
One for Piru & Karlos I reckon.
 

Offline Oliver

  • Hero Member
  • *****
  • Join Date: Sep 2005
  • Posts: 803
    • Show only replies by Oliver
Re: Fourier Transform - Help Request
« Reply #2 on: July 26, 2006, 03:10:12 PM »
I don't have time to debug your code right now, but I suggest you check all your output with a known maths package like MATLAB.  I believe MATLAB has 2D FFT transforms available.  It's also very easy to use.

P.S. Is this for image analysis?

P.P.S. I'm pretty sure there's a lot of literature available on this topic.  You could have a look at some journals if general web pages are not sufficient.
Good good study, day day up!
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Fourier Transform - Help Request
« Reply #3 on: July 26, 2006, 05:15:15 PM »
Numerical Recipes doesn't use zero-offset arrays, but against ALL GOOD PROGRAMMING PRACTICE has them start at 1. This has been an endless source of confusion and screws up efficient coding because you cannot use reverse loops (for (i=MAX; i--;)) easily.

What I'm trying to say is: did you take this into account when testing?
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline countzeroTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2005
  • Posts: 1938
    • Show only replies by countzero
    • http://blog.coze.org
Re: Fourier Transform - Help Request
« Reply #4 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 Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: Fourier Transform - Help Request
« Reply #5 on: July 26, 2006, 06:58:36 PM »
Last time I looked, jpeglib had a utility file full of fft functions...
int p; // A
 

Offline Cyberus

  • Hero Member
  • *****
  • Join Date: Feb 2003
  • Posts: 5696
    • Show only replies by Cyberus
Re: Fourier Transform - Help Request
« Reply #6 on: July 27, 2006, 08:06:17 AM »
Quote

Oliver wrote:
I don't have time to debug your code right now, but I suggest you check all your output with a known maths package like MATLAB.  I believe MATLAB has 2D FFT transforms available.  It's also very easy to use.

P.S. Is this for image analysis?

P.P.S. I'm pretty sure there's a lot of literature available on this topic.  You could have a look at some journals if general web pages are not sufficient.


When I was doing my Imaging course, we used Matlab to do FFTs. The great thing being you can easily get decent graphical output as well.

I'm guessing you could you also use Fortran?
I like Amigas
 

Offline countzeroTopic starter

  • Hero Member
  • *****
  • Join Date: Mar 2005
  • Posts: 1938
    • Show only replies by countzero
    • http://blog.coze.org
Re: Fourier Transform - Help Request
« Reply #7 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
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Fourier Transform - Help Request
« Reply #8 on: July 27, 2006, 01:11:28 PM »
Because I couldn't sleep from the heat in these parts (I think it was 23 or 24 degrees C outside, I worked a bit on your code, and concluded it contained no errors. But you already saw that yourself too.

I looked up the code for the 2D-FFT, and it looks like there are quite a lot of assumptions being made which could all too easily be broken if you don't follow NR's coding standard exactly. You need to grab the PDF for subchapter 12.4 to get an idea. But I trust you already figured that out too. Good luck with the code study, in any case.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Hyperspeed

  • Hero Member
  • *****
  • Join Date: Jun 2004
  • Posts: 1749
    • Show only replies by Hyperspeed
Re: Fourier Transform - Help Request
« Reply #9 on: October 19, 2006, 03:58:57 AM »
 

Offline Speelgoedmannetje

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 9656
    • Show only replies by Speelgoedmannetje
Re: Fourier Transform - Help Request
« Reply #10 on: October 19, 2006, 11:38:03 AM »
Quote

Cymric wrote:
Numerical Recipes doesn't use zero-offset arrays, but against ALL GOOD PROGRAMMING PRACTICE has them start at 1. This has been an endless source of confusion and screws up efficient coding because you cannot use reverse loops (for (i=MAX; i--;)) easily.
How can I agree more. Zero is an offset, not a number.


o.t.
I'll check your code later.
And the canary said: \'chirp\'