C# FFT Example

← 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>
  /// A .NET example in C# showing how to use the basic Fast Fourier Transform (FFT) classes.
  /// </summary>
  class FFTExample
  {

    static void Main( string[] args )
    {
      Console.WriteLine();

      # region forward 1D real 1024 point FFT

      //
      // Simple example to compute a forward 1D real 1024 point FFT
      //

      // Create some random signal data.
      RandomNumberGenerator rand = new RandGenMTwist( 4230987 );
      var data = new DoubleVector( 1024, rand );

      // Compute the FFT
      // This will create a complex conjugate symmetric packed result.
      var fft1024 = new DoubleForward1DFFT( 1024 );
      DoubleVector fftresult = fft1024.FFT( data );

      // Ask the FFT instance for the correct reader, to unpacked the result.
      DoubleSymmetricSignalReader reader = fft1024.GetSignalReader( data );

      // The reader provides random access to any element in the pack fft result.
      DoubleComplex thirdelement = reader[2];

      Console.WriteLine();
      Console.WriteLine( "1D real forward 1024 point FFT computed." );
      Console.WriteLine( "-----------------------------------\n" );

      #endregion

      #region backward 1D complex 1000 point FFT
      //
      // Simple example to compute a backward 1D complex 1000 point FFT
      //

      // Create some new random signal data.
      var cdata = new DoubleComplexVector( 1000, rand );

      // Create the 1D backward complex FFT instance
      var fft1000 = new DoubleComplexBackward1DFFT( 1000 );

      // Compute the FFT
      // Complex FFTs generated unpacked results.
      fft1000.FFTInPlace( cdata );

      Console.WriteLine( "1D complex backward 1000 point FFT computed." );
      Console.WriteLine( "-----------------------------------\n" );

      #endregion

      Console.WriteLine();
      Console.WriteLine( "Finished. Press Enter Key." );
      Console.Read();
    }
  }
}

← All NMath Code Examples
Top