Click here to Skip to main content
Email Password   helpLost your password?

Introduction

A Fast Fourier transform (FFT) is an efficient algorithm to compute the discrete Fourier transform (DFT) and its inverse. There are many distinct FFT algorithms involving a wide range of mathematics, from simple complex-number arithmetic to group theory and number theory; this article gives an overview of the available techniques and some of their general properties, while the specific algorithms are described in subsidiary articles linked below.

A DFT decomposes a sequence of values into components of different frequencies. This operation is useful in many fields (see discrete Fourier transform for properties and applications of the transform), but computing it directly from the definition is often too slow to be practical. An FFT is a way to compute the same result more quickly: computing a DFT of N points in the obvious way, using the definition, takes O(N 2) arithmetical operations, while an FFT can compute the same result in only O(N log N) operations. The difference in speed can be substantial, especially for long data sets where N may be in the thousands or millions—in practice, the computation time can be reduced by several orders of magnitude in such cases, and the improvement is roughly proportional to N/log(N). This huge improvement made many DFT-based algorithms practical; FFTs are of great importance to a wide variety of applications, from digital signal processing and solving partial differential equations to algorithms for quick multiplication of large integers.

Cooley–Tukey algorithm

By far the most common FFT is the Cooley–Tukey algorithm. This is a divide and conquer algorithm that recursively breaks down a DFT of any composite size N = N1N2 into many smaller DFTs of sizes N1 and N2, along with O(N) multiplications by complex roots of unity traditionally called twiddle factors.

This method (and the general idea of an FFT) was popularized by a publication of J. W. Cooley and J. W. Tukey in 1965, but it was later discovered (Heideman & Burrus, 1984) that those two authors had independently re-invented an algorithm known to Carl Friedrich Gauss around 1805 (and subsequently rediscovered several times in limited forms).

The most well-known use of the Cooley–Tukey algorithm is to divide the transform into two pieces of size N / 2 at each step, and is therefore limited to power-of-two sizes, but any factorization can be used in general (as was known to both Gauss and Cooley/Tukey). These are called the radix-2 and mixed-radix cases, respectively (and other variants such as the split-radix FFT have their own names as well). Although the basic idea is recursive, most traditional implementations rearrange the algorithm to avoid explicit recursion. Also, because the Cooley–Tukey algorithm breaks the DFT into smaller DFTs, it can be combined arbitrarily with any other algorithm for the DFT, such as those described below.

Using the code

First, we read the selected image into a 2D array of integers; we are using a pointer based image reading for the same. The code is as follows:

private void ReadImage()
{
    int i, j;
    GreyImage = new int[Width, Height];  //[Row,Column]
    Bitmap image = Obj;
    BitmapData bitmapData1 = 
       image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
       ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    unsafe
    {
        byte* imagePointer1 = (byte*)bitmapData1.Scan0;

        for (i = 0; i < bitmapData1.Height; i++)
        {
            for (j = 0; j < bitmapData1.Width; j++)
            {
                GreyImage[j, i] = (int)((imagePointer1[0] + 
                   imagePointer1[1] + imagePointer1[2]) / 3.0);
                //4 bytes per pixel
                imagePointer1 += 4;
            }//end for j
            //4 bytes per pixel
            imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
        }//end for i
    }//end unsafe
    image.UnlockBits(bitmapData1);
    return;
}

FFT of an image will be a complex array; we need to store this thing, so we define a complex structure for the same.

struct COMPLEX
{
    public double real, imag;
    public COMPLEX(double x, double y)
    {
        real = x;
        imag = y;
    }
    public float Magnitude()
    {
        return ((float)Math.Sqrt(real * real + imag * imag));
    }
    public float Phase()
    {            
         return ((float)Math.Atan(imag / real));
    }
}

The FFT is implemented using the separability property of a Fourier transform. We find the FFT of the rows of an image and then the columns.

Points of interest

The Fourier plot is generated, frequency shifting is performed on the complex Fourier coefficients array, and using dynamic range compression, we generate the Fourier plot.

History

This is my first article on FFT, any suggestions and modifications are welcome.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralInverse FFT issue
Collin Jasnoch
9:29 23 Dec '09  
Applying the inverse FFT results in different results if using it a second time. This should not be the case since there is no change to the Raw Fourier array. Why does this occur?

ASCII stupid question, get a stupid ANSI

GeneralRe: Inverse FFT issue
Vinayak Bharadi
18:01 23 Dec '09  
I have used temporary array to store the coefficients, which may be common. You can tray the sequence

Image - do FFT - do Inv FFT- do FFT - do Inverse FFT;
this gives correct result.
Instead you might be doing
Image - do FFT- do Inverse FFT - do Inverse FFT;

this gives wrong results, as the common array takes inverse of the Image values (grey Values due to Inv FFT ) instead of FFT Coefficients. Please try this.

Regards,
Vinayak
General[My vote of 2] Simple but lacking
Collin Jasnoch
11:13 15 Dec '09  
Thanks for posting this. I do not think you need to go into detail of the FFT as if a person does not know what it is then they should research that. However, with that said you have not coded anything for FFT tweaks like filtering. If you added that the project would be complete.

The article however lacks any explanation of your code. It is too simple for such a complex topic. Since computing the FFT is intesive you should walkthrough the calculations and show why what you have coded is fast and efficient.

Still thank you for the submission. I am able to make use of it Smile

ASCII stupid question, get a stupid ANSI

GeneralRe: [My vote of 2] Simple but lacking
Collin Jasnoch
12:16 15 Dec '09  
Can you explain what the FFTShift method is doing?

ASCII stupid question, get a stupid ANSI

GeneralRe: [My vote of 2] Simple but lacking
Collin Jasnoch
12:21 15 Dec '09  
Nm.. I figured it out. You are shifting 0w to the center.

ASCII stupid question, get a stupid ANSI

GeneralRe: [My vote of 2] Simple but lacking
Vinayak Bharadi
21:36 18 Dec '09  
The FFTShift is used to display the fourier plot with the center of plot with frequency 0,0. If we don't do this the four corners will have the frequency of 0,0. In FFT analysis if we shift the DC (freq 0,0) at the center it is easy to operate and analyse. we can easily apply filters etc on that FFT plot. You can read the book Digital Image Processing by R Gonzalez.
Generalonly on gray images?
cifftsifir
4:27 2 Dec '09  
How can we work the algorithm at colorful images? I want to get a colorful image after ifft. Hmmm

Thank You for Your useful article
GeneralRe: only on gray images?
Vinayak Bharadi
17:49 2 Dec '09  
Yes you can work on colour images; but you have to separate R G B Plane and operate on each plane separately; combine the results back to get original image.
Hope this helps you!!
GeneralRe: only on gray images?
cifftsifir
20:49 2 Dec '09  
Thank You for Your reply. I'm gonna search it.
GeneralRe: only on gray images?
cifftsifir
3:59 4 Dec '09  
Hi;


I want to ask one more thing if i have a matrix has values between 0 and 65535. Could I calculate with this algorithm the true fft values? Or there is a limit for the array values like 255? Sigh
GeneralRe: only on gray images?
Vinayak Bharadi
17:35 4 Dec '09  
If it is an image the grey levels are generally between (0 to 255); just check the reason for high values (o to 65535) this might be combined word for RGB Value( Possibly) . If it is not such case and you just have ordinary 2D signal matrix then you can directly take FFT.
Just remember that the posted article is meant for images with grey levels of 0 to 255. If you have colour image you have to separate RGB Planes and operate on each plane separately. In your case you may give me some more detail about the matrix that you have so that i can give more precise answer.
Good Luck!!
Vinayak
GeneralRe: only on gray images?
Amarnath S
4:27 11 Dec '09  
Yes - there are 16-bit grayscale images. I work with digital X-ray detectors and we get these images all the time. Here, the pixel value can take any value from 0 (dark) to 65535 (bright). Well, using the Libtiff library, you can generate a 16-bit TIFF file. You may find some 16-bit TIFF files on the Internet. I have a Codeproject article on displaying 16-bit raw files.
GeneralComplex angle
syutzy
4:19 20 Nov '09  
Good start, looking forward to seeing the rest.

One quick comment:
You might want to to use Math.Atan2(imag, real) rather than Math.Atan. Atan2 gives the phase angle in all 4 quadrants rather than just 2.
GeneralRe: Complex angle
Vinayak Bharadi
17:52 20 Nov '09  
Sure , i will look for this. I was unaware of the suggested function.
GeneralNeeds More
John Simmons / outlaw programmer
0:36 20 Nov '09  
What is a "fourier transform"? Why is one better than the other? What's are fourier transforms used/useful for?

.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

GeneralRe: Needs More
f r i s c h
5:44 20 Nov '09  
I second that. I've never heard of FFT and i am curious about how and when it is used.

Ofcourse i could do a google but time is money. Smile
GeneralRe: Needs More
Andre Luiz V Sanches
7:26 20 Nov '09  
"Fast Fourier Transform" is stuff for mathematicians and/or scientists/researchers who work with digital signal processing (i.e. computer vision or audio processing)..

Believe me, to explain what a FFT is would take a whole series of articles.. Instead the author should of just specify in the header section who the target audience was for the article, that way lowly corporate software developers such as myself wouldn't waste time reading it through.

---------
Andre Sanches

"UNIX is friendly, it's just picky about its friends"

GeneralRe: Needs More
Vinayak Bharadi
17:50 20 Nov '09  
FFT is an algorithm used for spectral analysis of signals, 2D FFT is used for FFT (Fast Fourier Transform)of an Image. we can use the fourier coeff array for frequency domain filtering and spectral analysis of an image.
DFT is used for image (Discrete fourier transform) but it is very slow, we use cooley tuckey algorithm to improve the speed, this is called FFT it is 10 times faster.
details :
http://en.wikipedia.org/wiki/Fast_Fourier_transform[^]
GeneralRe: Needs More
Vinayak Bharadi
17:50 20 Nov '09  
FFT is an algorithm used for spectral analysis of signals, 2D FFT is used for FFT (Fast Fourier Transform)of an Image. we can use the fourier coeff array for frequency domain filtering and spectral analysis of an image.
DFT is used for image (Discrete fourier transform) but it is very slow, we use cooley tuckey algorithm to improve the speed, this is called FFT it is 10 times faster.
details :
<a href="http://en.wikipedia.org/wiki/Fast_Fourier_transform">http://en.wikipedia.org/wiki/Fast_Fourier_transform</a>[<a href="http://en.wikipedia.org/wiki/Fast_Fourier_transform" target="_blank" title="New Window">^</a>]
GeneralRe: Needs More
John Simmons / outlaw programmer
2:21 21 Nov '09  
No, you obviously don't understand - put it in the article, and do NOT include a link to "more info". Explain it all in the article. That's what an article is for.

.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

GeneralRe: Needs More
Jonathan C Dickinson
15:38 23 Nov '09  
A common example is the spectrum analyzer in Winamp. The most common usage case (as far as I can tell) is for audio effects (DSP - digital signal processing) like an equalizer and so forth.

He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb]

Jonathan C Dickinson (C# Software Engineer)

GeneralRe: Needs More
Vinayak Bharadi
17:46 23 Nov '09  
you are right. for audio processing we use 1 Dimensional Fast Fourier transform, Images are 2 D Signals, for frequency domain image analysis we have to use 2D FFT. This is done by operating 1D FFT on rows and then on columns of the image array.
GeneralArticle Template Text
Abhijit Jana
23:37 19 Nov '09  
In Background Section Article Template Text is Preset. Please Fix that !

Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.


GeneralRe: Article Template Text
Vinayak Bharadi
1:56 20 Nov '09  
Fixed
Thanks


Last Updated 20 Nov 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010