# 2.1.22.2.1.8 FFT

## Description

These two Functions will do the 1 dimension Fast Fourier Transform. Data matrix should be of type double. If user have the data matrix in integer form, user should first transform it to double using the member function of matrixbase "CastToDouble".

This function performs 1-dimensional Fast Fourier Transform on each row of data in a matrix. Data matrix should be of type complex. If user have the data matrix in integer form, user should first transform it to complex.

## Syntax

int FFT( matrix & matSource, matrix<complex> & matFFTResult, int nColProcess = -1 )

int FFT( matrix<complex> & matSource, matrix<complex> & matFFTResult, int nColProcess = -1 )

int FFT( vector<complex> & vecSource, vector<complex> & vecFFTResult )

## Parameters

matSource
[input] The data source matrix
matFFTResult
[output] The matrix containing the 1 dimension FFT results
nColProcess
[input] The number of columns user provided. If it is -1, function will not do any padding or truncating. But if it is larger than the size of the matrix, it will pad zero to meet the size if less than, it will truncate the data matrix.

matSource
[input] The data source matrix
matFFTResult
[output] The matrix containing the 1 dimension FFT results
nColProcess
[input] The number of columns user provided. If it is -1, function will not do any padding or truncating. But if it is larger than the size of the matrix, it will pad zero to meet the size if less than, it will truncate the data matrix.

vecSource
[input] The data source vector
vecFFTResult
[output] The vector containing the 1-dimensional FFT result

## Return

If succeed, it will return 0; Otherwise it returns error indications.

Error:

-1: Provided column number or row number. Must be -1 or positive integer

-2: The data source matrix is empty

-3: When calling member function, it fails.

-4: When calling GetReal or GetImage member function of matrix class, it failed.

If succeed, it will return 0; Otherwise it returns error indications.

Error:

-1: Provided column number or row number. Must be -1 or positive integer

-2: The data source matrix is empty

-3: When calling member function, it fails.

-4: When calling GetReal or GetImage member function of matrix class, it failed.

If succeed, it will return 0; Otherwise it returns error indications.

Error:

-5: source vector is of zero length

n: other error codes returned by matrix-based FFT function

## Examples

Prior to compilation, load fft_utils.c to the workspace by executing the following LabTalk command:

Run.LoadOC("Originlab\fft_utils.c", 16);

To retain fft_utils.c in the workspace for successive sessions, drag and drop the file from the Temporary folder to the System folder.

EX1

//This example computes the 1 dimension fast fourier transform on each row of the matrix mRe.
#include <fft_utils.h>
void FFT_ex1()
{
matrix <double> mRe = {{1,2.5,3.5,2,1.0,2.3},{3.2,4.1,2.2,1.0,4,2}};
int ii , jj;

matrix <complex> mResult;

//test 1d fft function for arbitrary source matrix.
int Err = FFT(mRe, mResult, -1);

if ( 0 != Err )
{
ASSERT(false);
printf("one-dimension FFT fails.\n");
}
else
{
for(ii = 0; ii < mResult.GetNumRows();  ii++)
{
for(jj = 0; jj < mResult.GetNumCols(); jj++)
printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
printf("\n");
}
}
}

/**
The output is as following:
(5.021454, 0.000000i)  (-0.347011, -0.954594i)  (-0.673610, 0.813173i)  (-0.530723, 0.000000i)  (-0.673610, -0.813173i)  (-0.347011, 0.954594i)
(6.736097, 0.000000i)  (0.877734, -0.106066i)  (-0.796084, -1.378858i)  (0.938971, 0.000000i)  (-0.796084, 1.378858i)  (0.877734, 0.106066i)
**/

EX2

//This example computes the 1 dimension fast fourier transform on each row of the matrix mSource.
#include <fft_utils.h>
void FFT_ex2()
{
matrix <double> mRe = {{1,2.5,3.5,2,1.0,2.3},{3.2,4.1,2.2,1.0,4,2}};
matrix <double> mIm = {{1,0.5,1.0,0.8,3.1,0.3},{2.2,3.2,3.0,1.5,2,2.1}};
int ii , jj;

matrix <complex> mSource, mResult;
mSource.MakeComplex(mRe,mIm);

//test 1d fft function for complex source matrix.

int Err = FFT(mSource, mResult);

if ( 0 != Err )
{
ASSERT(false);
printf("one-dimension FFT fails\n");
}
else
{
for(ii = 0; ii < mResult.GetNumRows();  ii++)
{
for(jj = 0; jj < mResult.GetNumCols(); jj++)
printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
printf("\n");

}
}
}

/**
The output is as following:
(5.021454, 2.735264i)  (-1.018762, -1.546554i)  (0.139563, 0.547811i)  (-0.530723, 1.428869i)  (-1.486782, -1.078534i)  (0.324740, 0.362634i)
(6.736097, 5.715476i)  (1.620196, 0.240945i)  (-0.760729, -1.970818i)  (0.938971, 0.163299i)  (-0.831440, 0.786898i)  (0.135272, 0.453077i)

**/

EX3

//This example computes the 1-dimensional Fast Fourier Transform on a complex vector.
#include <fft_utils.h>
void FFT_ex3()
{
vector vR = { 1,2.5,3.5,2,1.0,2.3 };
vector vI = { 1,0.5,1.0,0.8,3.1,0.3 };
vector <complex> vC(vR, vI), vResult(vC.GetSize());

//tedt 1d fft function on data in a complex vector
int Err = FFT(vC, vResult);

if ( 0 != Err )
{
ASSERT(false);
printf("one-dimension FFT fails\n");
}
else
{
for(int ii = 0; ii < vResult.GetSize();  ii++)
{
printf("(%f, %fi)  ",vResult[ii].m_re,vResult[ii].m_im);
printf("\n");
}
}
}

/**
The output is as following:
(5.021454,  2.735264i)
(-1.018762, -1.546554i)
(0.139563,  0.547811i)
(-0.530723, 1.428869i)
(-1.486782, -1.078534i)
(0.324740,  0.362634i)

**/

## Remark

This function performs 1-dimensional Fast Fourier Transform on data in a complex vector.

Data vector should be of type complex. If user have the data vector in integer or double form, user should first transform it or copy it to a complex vector.

This function in turn calls the matrix-based FFT function.