← All NMath Code Examples
using System;
using System.Globalization;
using System.Threading;
using System.Text;
using CenterSpace.NMath.Core;
namespace CenterSpace.NMath.Examples.CSharp
{
/// <summary>
/// .NET example in C# showing how to use the 2D Fast Fourier Transform (FFT) classes.
/// </summary>
class FFT2DExample
{
static void Main( string[] args )
{
Console.WriteLine();
#region forward 2D 6x4 complex FFT using matrices
//
// Compute a small out-of-place forward complex 2D FFT matrices.
//
// Create some random signal data.
RandomNumberGenerator rand = new RandGenMTwist( 4230987 );
var data2Dcomplex = new DoubleComplexMatrix( new DoubleMatrix( 6, 4, rand ), new DoubleMatrix( 6, 4, rand ) );
// Create the 2D forward complex FFT instance & the fft resultant matrix.
var cfft6x4 = new DoubleComplexForward2DFFT( 6, 4 );
var cfftdata = new DoubleComplexMatrix( cfft6x4.Rows, cfft6x4.Columns );
// Compute the 2D FFT.
cfft6x4.FFT( data2Dcomplex, ref cfftdata );
// Display results.
Console.WriteLine( "2D 6x4 complex FFT result: \n" );
for ( int r = 0; r < cfft6x4.Rows; r++ )
{
for ( int c = 0; c < cfft6x4.Columns; c++ )
{
Console.Write( String.Format( "({0,5:0.00},{1,5:0.00}) ", cfftdata[r, c].Real, cfftdata[r, c].Imag ) );
}
Console.WriteLine();
}
Console.WriteLine( "-----------------------------------\n" );
#endregion
#region forward 5x5 2D real FFT using matrices
//
// Compute a small out-of-place forward 2D FFT in the real plane using matrices.
//
// Create a signal matrix using a random number generator.
RandomNumberGenerator rand2 = new RandGenMTwist( 4230987 );
var data2D = new DoubleMatrix( 5, 5, rand2 );
// Create the 2D forward real FFT instance & the fft resultant matrix.
var fft5x5 = new DoubleForward2DFFT( 5, 5 );
var fftdata = new DoubleMatrix( fft5x5.Rows, fft5x5.Columns );
// Compute the 2D FFT.
fft5x5.FFT( data2D, ref fftdata );
// Now retrieve the unpacked fft result.
// (The results are packed because the input data is in the real domain, but the output
// is in the complex domain. Using the complex-conjugate symmetry of a real fft, we can pack
// the fft back into an array of the same size as the input.)
// First get a data reader to read the packed fft result.
DoubleSymmetric2DSignalReader reader2D = fft5x5.GetSignalReader( ref fftdata );
DoubleComplexMatrix result = reader2D.UnpackFullToMatrix();
// Display results.
Console.WriteLine( "2D 5x5 real FFT result: \n " );
for ( int r = 0; r < fft5x5.Rows; r++ )
{
for ( int c = 0; c < fft5x5.Columns; c++ )
{
Console.Write( String.Format( "({0,5:0.00},{1,5:0.00}) ", result[r, c].Real, result[r, c].Imag ) );
}
Console.WriteLine();
}
Console.WriteLine( "-----------------------------------\n" );
#endregion
Console.WriteLine();
Console.WriteLine( "Finished. Press Enter Key." );
Console.Read();
}
}
}
← All NMath Code Examples