# VB FFT2 D Example

← All NMath Code Examples

```Imports System
Imports System.Globalization
Imports System.Text

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Examples.VisualBasic

A .NET example in Visual Basic showing how to use the 2D Fast Fourier Transform (FFT) modules.
Module FFT2DExample

Sub Main()

Compute a small out-of-place forward complex 2D FFT matrices.
Console.WriteLine()

Create some random signal data.
Dim Rand As RandomNumberGenerator = New RandGenMTwist(4230987)
Dim data2Dcomplex As New DoubleComplexMatrix(New DoubleMatrix(6, 4, Rand), New DoubleMatrix(6, 4, Rand))

Create the 2D forward complex FFT instance & the fft resultant matrix.
Dim cfft6x4 As New DoubleComplexForward2DFFT(6, 4)
Dim cfftdata As New DoubleComplexMatrix(cfft6x4.Rows, cfft6x4.Columns)

Compute the 2D FFT.
cfft6x4.FFT(data2Dcomplex, cfftdata)

Display results.
Dim SB As New StringBuilder
Console.WriteLine("2D 6x4 complex FFT result: " + Environment.NewLine)

Dim C, R As Integer
For R = 0 To cfft6x4.Rows - 1
For C = 0 To cfft6x4.Columns - 1
Console.Write(String.Format("({0,5:0.00},{1,5:0.00}) ", cfftdata(R, C).Real, cfftdata(R, C).Imag))
Next C
Console.WriteLine()
Next R

Console.WriteLine("-----------------------------------" + Environment.NewLine)

Compute a small out-of-place forward 2D FFT in the real plane using matrices.

Create a signal matrix using a random number generator.
Dim Rand2 As RandomNumberGenerator = New RandGenMTwist(4230987)
Dim data2D As New DoubleMatrix(5, 5, Rand2)

Create the 2D forward real FFT instance & the fft resultant matrix.
Dim fft5x5 As New DoubleForward2DFFT(5, 5)
Dim FFTData As New DoubleMatrix(fft5x5.Rows, fft5x5.Columns)

Compute the 2D FFT.
fft5x5.FFT(data2D, 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.)
Dim Result As DoubleComplexMatrix = Reader2D.UnpackFullToMatrix()

Display results.
Console.WriteLine("2D 5x5 real FFT result: " & Environment.NewLine)

For R = 0 To fft5x5.Rows - 1
For C = 0 To fft5x5.Columns - 1
Console.Write(String.Format("({0,5:0.00},{1,5:0.00}) ", Result(R, C).Real, Result(R, C).Imag))
Next
Console.WriteLine()
Next

Console.WriteLine("-----------------------------------" + Environment.NewLine)

Console.WriteLine()
Console.WriteLine("Finished. Press Enter Key.")