# VB FFT1 D Example

← All NMath Code Examples

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

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Examples.VisualBasic;

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

Sub Main()

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

Console.WriteLine()

Create some random signal data.
Dim Rand As RandomNumberGenerator = New RandGenMTwist(4230987)
Dim Data As New DoubleVector(1024, Rand)

Create the 1D real FFT instance
Dim FFT1024 As New DoubleForward1DFFT(1024)

Compute the FFT
This will create a complex conjugate symmetric packed result.
FFT1024.FFTInPlace(Data)

Ask the FFT instance for the correct reader, to unpacked the result.

Dim ThirdElement As DoubleComplex = Reader(2)

Console.WriteLine()
Console.WriteLine("1D real 1024 point FFT computed.")
Console.WriteLine("-----------------------------------" + Environment.NewLine)

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

Create some new random signal data.
Dim CData As New DoubleComplexVector(1000, Rand)

Create the 1D backward complex FFT instance
Dim FFT1000 As New DoubleComplexBackward1DFFT(1000)

Compute the FFT
Complex FFTs generated unpacked results.
FFT1000.FFTInPlace(CData)

Console.WriteLine("1D complex 1000 point FFT computed.")
Console.WriteLine("-----------------------------------" + Environment.NewLine)

Compute a small 1D real FFT out-of-place using arrays.

Create the input signal data and the fft
Dim Data5() As Double = {1, 2, 3, 4, 5}
Dim FFT5Result(5) As Double
Dim FFT5 As New DoubleForward1DFFT(5)

Compute the FFT
FFT5.FFT(Data5, FFT5Result)

Look at packed complex-conjugate symmetric result
Console.WriteLine("1D real FFT result.")
Console.WriteLine()
Console.WriteLine("Input data =")
Console.WriteLine("[1, 2, 3, 4, 5]" + Environment.NewLine)
Console.WriteLine("fft symmetric half packed = ")
Console.Write("[ ")
Dim I As Integer
For I = 0 To 4
Console.Write("{0,4:0.00} ", FFT5Result(I))
Next
Console.WriteLine(" ]" + Environment.NewLine)

Ask the FFT instance for the correct reader, to unpacked the result.
Console.WriteLine("fft fully unpacked (requires twice the memory)= ")
Console.Write("[ ")
For I = 0 To 4
Next
Console.WriteLine(" ]")
Console.WriteLine("-----------------------------------" + Environment.NewLine)

Compute the same 1D FFT using the complex instance

Create the input signal data and the fft
Dim CData5 As New DoubleComplexVector("[ (1,0) (2,0) (3,0) (4,0) (5,0) ]")
Dim CFFT5 As New DoubleComplexForward1DFFT(5)

Show input data
Console.WriteLine("1D complex FFT result.")
Console.WriteLine()
Console.WriteLine("Input data = ")
Console.Write("[ ")
For I = 0 To CData5.Length - 1
Console.Write("{0,4:0.00} ", CData5(I).Real)
Next
Console.WriteLine("]" + Environment.NewLine)

Compute the FFT in-place.
CFFT5.FFTInPlace(CData5)

Look at the (unpacked) complex fft result
Note that complex FFTs never create packed results.
Console.WriteLine("complex fft = ")
Console.Write("[ ")
For I = 0 To CData5.Length - 1
Console.Write("({0,4:0.00},{1,4:0.00}) ", CData5(I).Real, CData5(I).Imag)
Next
Console.WriteLine(" ]")
Console.WriteLine("-----------------------------------" + Environment.NewLine)

Compute a forward real 1D fft and then reverse the fft

Create the input signal data and the fft
Dim Data4 As New DoubleVector("[ 1 2 2 1 ]")
Dim FFT4Result As New DoubleVector(4)
Dim FFT4Reverse As New DoubleVector(4)

Build the two FFT instances
fft4 is for forward real 1D FFTs.
Dim FFT4 As New DoubleForward1DFFT(4)

rfft4 is for reversing the complex-conjugate symmetric results of DoubleForward1DFFTs.
DoubleSymmetricBackward1DFFT inputs are always assumed have this complex-conjugate symmetry.
Dim RFFT4 As New DoubleSymmetricBackward1DFFT(4)

Compute forward real the FFT.
FFT4.FFT(Data4, FFT4Result)

Now reverse the FFT, and for fft4reverse should match the original input data.
RFFT4.SetScaleFactorByLength() This scaling is necessary to match data4.
RFFT4.FFT(FFT4Result, FFT4Reverse)

Look at the packed complex-conjugate symmetric fft result
Note that complex FFTs never create packed results.
Console.WriteLine("1D real forward & backward FFT results.")

Console.WriteLine("Input data = ")
Console.Write("[ ")
For I = 0 To RFFT4.Length - 1
Console.Write("{0,4:0.00} ", Data4(I))
Next
Console.WriteLine("]" + Environment.NewLine)

Console.WriteLine("Complex-conjugate symmetric packed result of real fft = ")
Console.Write("[ ")
For I = 0 To FFT4.Length - 1
Console.Write("{0,4:0.00} ", FFT4Result(I))
Next
Console.WriteLine("]" + Environment.NewLine)

Console.WriteLine("Backward of real fft = ")
Console.Write("[ ")
For I = 0 To RFFT4.Length - 1
Console.Write("{0,4:0.00} ", FFT4Reverse(I))
Next
Console.WriteLine("]")
Console.WriteLine("-----------------------------------" + Environment.NewLine)

Example computing a 1D FFT of an offset and strided signal.
When working with strided signals, the FFT must be configured
separately, and then used to create an advanced general FFT instance.

Build our FFT configuration: direction: Forward, precision: Double, forward domain: REAL, Dimension: 1, Length: 4
Dim ComplexConfiguration As New FFTConfiguration(FFTDirection.FORWARD, FFTPrecision.DOUBLE, FFTDomain.REAL, 1, 4)

ComplexConfiguration.DataOffset = 3 Skip three data values before starting
ComplexConfiguration.DataStride = 2 Skip every other data value.
ComplexConfiguration.InPlace = True This will be configured for an in-place computation.

Not create the data set, with an offset of 3, and stride of 2.
Dim Signal() As Double = {94423, -341, 42343, 1, -1, 2, -1, 2, -1, 1, -85, 22}
Dim GFFTResult(4) As Double

Now build the general FFT object using this configuration.
Dim GFFT As New DoubleGeneral1DFFT(ComplexConfiguration)

Now compute the FFT, it should match the previous example.
GFFT.FFT(Signal, GFFTResult)

Console.WriteLine("Input data = ")
Console.Write("[ ")
For I = 0 To Signal.Length - 1
Console.Write("{0,4:0.00} ", Signal(I))
Next
Console.WriteLine("]" + Environment.NewLine)

Show the results
Console.WriteLine("Complex-conjugate symmetric packed result of real fft = ")
Console.Write("[ ")
For I = 0 To FFT4.Length - 1
Console.Write("{0,4:0.00} ", GFFTResult(I))
Next
Console.WriteLine("]" + Environment.NewLine)
Console.WriteLine("-----------------------------------")

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