# FFT

### Description

The FFT class computes the Fourier transform of an *N* dimensional signal using a Fast Fourier Transform algorithm. The FFT class computes *N* independent FFTs, one for each dimension in the input signal. The FFT is computed using the previous *M* samples input to the FFT instance, where *M* is equal to the **FFT Window Size** / 2. The FFT Window Size parameter must be a power of two (16,32,64,128,256,512,1024,2048,4096,etc.). Note that, because of the symmetries within the FFT computation, the results of an FFT will be half the FFT Window Size. This means that if you create an FFT with a FFT Window Size of 256, the resulting vectors that you will get (such as the magnitude and phase) will each have 128 elements in them.

For each *N* dimensional input signal, the FFT will output:

- An
*M*dimensional vector containing the magnitude of each frequency component - An
*M*dimensional vector containing the phase of each frequency component

where *M* is equal to the **FFT Window Size** / 2. These values will be concatenated into one vector, in the order of **{ [magnitude dimension 1] [phase dimension 1] [magnitude dimension 2] [phase dimension 2] ... [magnitude dimension N] [phase dimension N] }**. The size of this concatenated output vector will therefore be equal to **N * M * 2** - where *N* is the number of input dimensions to the FFT, *M* is the **FFT Window Size** / 2, and 2 represents the magnitude and phase vectors. If you only really need the magnitude or phase of a signal, as opposed to both, then you can turn off the computation and concatenation of the element you do not need to save unnecessary computations and memory copies, this can either be done in the FFT's constructor or by using the `setComputeMagnitude(bool computeMagnitude)`

and `setComputePhase(bool computePhase)`

functions. Check the FFT documentation for more information.

The FFT class is part of the Feature Extraction Modules.

An example showing the output (magnitude data only) of the FFT Feature Extraction module. The input to the FFT consisted of a 100Hz sine wave signal, this is why you can see a large peak in the FFT magnitude data at a frequency value of 100. Note that the FFT Window size for this example was set to 256, this is why you can see the frequencies 'spill' into the surrounding frequency bins around 100Hz, setting the FFT Window size value to a larger window size (for instance 4096, 8192, etc.) would result in a much more accurate FFT analysis, although this would be more computationally expensive. FFTMagnitudeDataExampleImage1.jpg

### Applications

The FFT module is good at converting a signal from the time domain to the frequency domain. This is useful as a number of powerful features can then be extracted from the frequency domain signals, such as the fundamental frequency of the signal, the centroid, the fundamental frequency/signal ratio, etc.. These additional features are not computed by the FFT module itself, the FFT module should therefore be used with the FFTFeatures module, or you could easily write your own custom FFT feature extraction module if you need specific features extracted from the FFT signal.

### Example Code

#include "GRT.h"

using namespace GRT;

int main (int argc, const char * argv[])

{

//Create a new instance of an FFT with a window size of 256 and a hop size of 1

FFT fft(256,1);

//Create some varaibles to help generate the signal data

const UINT numSeconds = 10; //The number of seconds of data we want to generate

double t = 0; //This keeps track of the time

double tStep = 1.0/1000.0; //This is how much the time will be updated at each iteration in the for loop

double freq = 100; //Stores the frequency

//Generate the signal and filter the data

for(UINT i=0; i<numSeconds*1000; i++){

//Generate the signal

double signal = sin( t * TWO_PI*freq );

//Compute the FFT of the input signal (and the previous buffer data)

fft.update( signal );

//Update the t

t += tStep;

}

//Create and open a file to save the data

fstream file;

file.open("FFTData.txt", iostream::out);

//Take the output of the last FFT and save the values to a file

vector<FastFourierTransform> fftResults = fft.getFFTResults();

//The input signal is a 1 dimensional signal, so get the magnitude data for dimension 1 (which is at element 0)

vector< double > magnitudeData = fftResults[0].getMagnitudeData();

//Write the magnitude data to a file

for(UINT i=0; i<magnitudeData.size(); i++){

file << magnitudeData[i] << endl;

}

//Close the file

file.close();

return EXIT_SUCCESS;

}

### Code & Resources

### Documentation

You can find the documentation for this class at FFT documentation.