• Welcome to Religious Forums, a friendly forum to discuss all religions in a friendly surrounding.

    Your voice is missing! You will need to register to get access to the following site features:
    • Reply to discussions and create your own threads.
    • Our modern chat room. No add-ons or extensions required, just login and start chatting!
    • Access to private conversations with other members.

    We hope to see you as a part of our community soon!

Fast Fourier Transform

Onyx

Active Member
Premium Member
One of the more interesting routines in my code arsenal is the FFT, which I have included below. The accompanying image shows it being used to process a picture of delicious cake, but it can be used for a variety of other purposes.

Code:
void forwardFFT(float *real, float *imag, int size)
{
  int j = size / 2;

  for(int i = 1; i <= size - 2; i++)
  {
    if(i < j)
    {
      const float tr = real[j];
      const float ti = imag[j];
      real[j] = real[i];
      imag[j] = imag[i];
      real[i] = tr;
      imag[i] = ti;
    }

    int k = size / 2;

    while(k <= j)
    {
      j -= k;
      k /= 2;
    }

    j += k;
  }

  for(int k = 1; k <= (int)(logf(size) / logf(2)); k++)
  {
    const int le = (int)(powf(2, k));
    const int le2 = le / 2;

    float ur = 1;
    float ui = 0;
    float sr = cosf(M_PI / le2);
    float si = -sinf(M_PI / le2);

    for(int j = 1; j <= le2; j++)
    {
      for(int i = j - 1; i < size; i += le)
      {
        const int ip = i + le2;
        const float tr = real[ip] * ur - imag[ip] * ui;
        const float ti = real[ip] * ui + imag[ip] * ur;
        real[ip] = real[i] - tr;
        imag[ip] = imag[i] - ti;
        real[i] += tr;
        imag[i] += ti;
      }

      const float tr = ur;
      ur = tr * sr - ui * si;
      ui = tr * si + ui * sr;
    }
  }
}

void inverseFFT(float *real, float *imag, int size)
{
  for(int k = 0; k < size; k++)
    imag[k] = -imag[k];

  forwardFFT(real, imag, size);

  for(int i = 0; i < size; i++)
  {
    real[i] = real[i] / size;
    imag[i] = -imag[i] / size;
  }
}

cake_fft.jpg
 

Polymath257

Think & Care
Staff member
Premium Member
If you like the results of using a FFT, you might also like to see what using wavelets allows. Matlab has a nice wavelet package, I believe.
 

icehorse

......unaffiliated...... anti-dogmatist
Premium Member
fun to see some code! and a fun app too! but i prefer not to devote an entire line to opening curly braces! (we gotta argue about something, right? ;) )
 

suncowiam

Well-Known Member
fun to see some code! and a fun app too! but i prefer not to devote an entire line to opening curly braces! (we gotta argue about something, right? ;) )

Also, I don't like to devote to an entire routine or set of routines so I use APIs. ;)
 

sun rise

The world is on fire
Premium Member
Finally we have something worth fighting about - indentation styles, editors, OS including choice of desktop. And, of course, the one true programming language.

And the unwashed will of course not understand the essential importance of these issues.
 

Polymath257

Think & Care
Staff member
Premium Member
Finally we have something worth fighting about - indentation styles, editors, OS including choice of desktop. And, of course, the one true programming language.

And the unwashed will of course not understand the essential importance of these issues.

Well, it *is* a religious forum after all......
 

Polymath257

Think & Care
Staff member
Premium Member
More seriously, Fourier Transforms calculate the frequencies used in producing a function. They can be done in any dimension (your picture uses 2 dimensions).

Edges are high frequency effects, for example, which is why your high-pass filter picks them up so well.

FFT allows the computation of FT faster by taking advantage of some scaling properties (which is why powers of 2 are used in your code).

There are tomes written solely about FFT, let alone the broader topics in Harmonic Analysis.

\E: And I was serious about looking into wavelets. They tend to compactify information better than FTs.
 

Onyx

Active Member
Premium Member
\E: And I was serious about looking into wavelets. They tend to compactify information better than FTs.
I will look into that and try to make something of it. (I like to write or adapt my own code to avoid dependencies on libraries, learn stuff etc.) Thanks!

P.S. I think you should start thread(s) about wavelets and other topics of interest to you. Maybe boil things down a little for us non-math people, though. :D
 

Polymath257

Think & Care
Staff member
Premium Member
I find OS X to be a nice solid implementation of Unix. No windoze for me. I'm cool with Linux.


I run the local Linux network at work. I tell everyone 'I don't do Windows!'. The old system ran Solaris 9.

(why did I feel an urge to escape that single quote?)
 

icehorse

......unaffiliated...... anti-dogmatist
Premium Member
And, of course, the one true programming language.

Yikes! That's a tough one! If I could only have one language for every conceivable thing I might ever want to do, I'd have to go with C. But it seems to me a dev. ought to have a suite of appropriate languages to to suit the situation. One example is that for it's job, SQL is pretty awesome. I earn my living from Java (primarily), but if I could step back and learn and choose, I think I'd really like to learn Clojure.
 
Top