An implementation of the fast Fourier transform (FFT) in C#
AForge.net is a free (open-source) library with Fast Fourier Transform support. (See Sources/Imaging/ComplexImage.cs for usage, Sources/Math/FourierTransform.cs for implemenation)
Fast Fourier Transform in C#
AForge.NET is an open-source library with Fast Fourier Transform support.
ExocortexDSP is also another option.
ExocortexDSP example would look something like this:
Exocortex.DSP.ComplexF[] complexData = new Exocortex.DSP.ComplexF[512];
for (int i = 0; i < 512; ++i)
{
// Fill the complex data
complexData[i].Re = 1; // Add your real part here
complexData[i].Im = 2; // Add your imaginary part here
}
// FFT the time domain data to get frequency domain data
Exocortex.DSP.Fourier.FFT(complexData, Exocortex.DSP.FourierDirection.Forward);
float[] mag_dat_buffer = new float[complexData.Length];
// Loop through FFT'ed data and do something with it
for (int i = 0; i < complexData.Length; ++i)
{
// Calculate magnitude or do something with the new complex data
mag_data_buffer[i] = ImaginaryNumberMagnitude(complexData[i].Im, complexData[i].Re);
}
1D Fast Fourier Transform
The implementation of the FFT you have posted is limited to inputs of size 2m
. Here m
thus indirectly specify the size of the FFT block size. So, for your example with x = {1,1,1,1,0,0,0,0}
and y={1,1,1,1,0,0,0,0}
being arrays of size 8=23, m
would be equal to 3.
Note that there are no additional checks for the size of the arrays x
and y
so make sure they are at least that size.
C# library to do fft and ifft?
I believe this is what you're looking for.
C# FFT Library Need to know
You can have a look at this library:
http://code.google.com/p/aforge/
I use this library a fair bit, though I've never used its FFT functions:
http://www.mathdotnet.com/
There is also this library which seems to get alot of mention, but I've never used it so I can't comment on it's quality.
http://www.exocortex.org/dsp/
What is wrong with this fourier transform implementation
My days of doing complex mathematics are a ways behind me right now so I may be missing something myself. However, it appears to me that you are doing the following line:
transformed += data[i]*Complex.FromPolarCoordinates(1, argument*i);
when it should probably be more like:
transformed += data[i]*Math.Pow(Math.E, Complex.FromPolarCoordinates(1, argument*i));
Unless you have this wrapped up into the method FromPolarCoordinates()
UPDATE:
I found the following bit of code in the AForge.NET Framework library and it shows additional Cos/Sin operations being done that are not being handled in your code. This code can be found in full context in the Sources\Math\FourierTransform.cs: DFT method.
for ( int i = 0; i < n; i++ )
{
dst[i] = Complex.Zero;
arg = - (int) direction * 2.0 * System.Math.PI * (double) i / (double) n;
// sum source elements
for ( int j = 0; j < n; j++ )
{
cos = System.Math.Cos( j * arg );
sin = System.Math.Sin( j * arg );
dst[i].Re += ( data[j].Re * cos - data[j].Im * sin );
dst[i].Im += ( data[j].Re * sin + data[j].Im * cos );
}
}
It is using a custom Complex class (as it was pre-4.0). Most of the math is similar to what you have implemented but the inner iteration is doing additional mathematical operations on the Real and Imaginary portions.
FURTHER UPDATE:
After some implementation and testing I found that the code above and the code provided in the question produce the same results. I also found, based on the comments what the difference is between what is generated from this code and what is produced by WolframAlpha. The difference in the results is that it would appear that Wolfram is applying a normalization of 1/sqrt(N) to the results. In the Wolfram Link provided if each value is multiplied by Sqrt(2) then the values are the same as those generated by the above code (rounding errors aside). I tested this by passing 3, 4, and 5 values into Wolfram and found that my results were different by Sqrt(3), Sqrt(4) and Sqrt(5) respectfully. Based on the Discrete Fourier Transform information provided by wikipedia it does mention a normalization to make the transforms for DFT and IDFT unitary. This might be the avenue that you need to look down to either modify your code or understand what Wolfram may be doing.
Fast Fourier Transformation in WinForms
I strongly recommend FFTW over anything else. It is fast, reliable, widely used, portable. Yes, you will have to call from managed code into unmanaged code.
Related Topics
C# SQL Server - Passing a List to a Stored Procedure
How to Start Winform App Minimized to Tray
Observablecollection That Also Monitors Changes on the Elements in Collection
Deserializing JSON When Sometimes Array and Sometimes Object
How to Get Difference Between Two Dates in Year/Month/Week/Day
Rendering Partial View on Button Click in ASP.NET MVC
How Create a New Deep Copy (Clone) of a List<T>
How to Both Read and Write a File in C#
How to Update UI from Another Thread Running in Another Class
Is There a Generic Constructor with Parameter Constraint in C#
Testing If Object Is of Generic Type in C#
Is Using a 'Goto' Statement Bad
Solution for Overloaded Operator Constraint in .Net Generics
When to Use Blockingcollection and When Concurrentbag Instead of List<T>
What's the Point of the Var Keyword