C# Class SharpMath.FFT.FFT1D

Performs the Fast Fourier Transform (FFT) or Inverse-FFT of a 1-Dimensional discrete complex signal This is a slow CPU version purely designed to test the Fourier transform and for debugging purpose The idea behind the "Fast" Discrete Fourier Transform is that we can recursively subdivide the analyze of a domain into sub-domains that are easier to compute, and re-use the results each stage. It turns a O(N²) computation into log2(N) stages of O(N) computations. Basically, a DFT is: N-1 X_k = Sum[ x_n * e^(-2PI * n/N * k) ] n=0 For k€[0,N[ Assuming a power of two length N, we can separate the even and odd powers and write: N/2-1 X_k = Sum[ x_(2n) * e^(-i * 2PI * (2n)/N * k) ] n=0 N/2-1 + Sum[ x_(2n+1) * e^(-i * 2PI * (2n+1)/N * k) ] n=0 That we rewrite: X_k = E_k + e^(-i * 2PI * k / N) * O_k With: N/2-1 E_k = Sum[ x_(2n) * e^(-i * 2PI * (2n)/N * k) ] n=0 N/2-1 O_k = Sum[ x_(2n+1) * e^(-i * 2PI * (2n)/N * k) ] n=0 Noticing that: e^(-i * 2PI * (2n)/N * (k+N/2)) = e^(-i * 2PI * (2n)/N * k) * e^(-i * 2PI * (2n/N)*(N/2) ) = e^(-i * 2PI * (2n)/N * k) * e^(-i * 2PI * n) = e^(-i * 2PI * (2n)/N * k) E_(k+N/2) = E_k and O_(k+N/2) = O_k Therefore we can rewrite: X_k = E_k + e^(-i * 2PI * k / N) * O_k if 0 <= k < N/2 X_k = E_(k-N/2) + e^(-i * 2PI * k / N) * O_(k-N/2) if N/2 <= k < N (that is the part where we reuse existing results) Noticing the "twiddle factor" also simplifies: e^(-i * 2PI * (k+N/2) / N) = e^(-i * 2PI * k / N) * e^(-i * PI) = -e^(-i * 2PI * k / N) Thus we get the final result for 0 <= k < N/2 : X_k = E_k + e^(-i * 2PI * k / N) * O_k X_(k+N/2) = E_k - e^(-i * 2PI * k / N) * O_k Expressing the DFT of length N recursively in terms of two DFTs of length N/2 allows to reach N*log(N) speeds instead of the traditional N² form.
Show file Open project: Patapom/GodComplex

Public Methods

Method Description
FFT_Forward ( System.Complex _inputSignal ) : System.Complex[]

Applies the Discrete Fourier Transform to an input signal

Throws an exception if signal size is not a power of two!

FFT_Forward ( System.Complex _inputSignal, System.Complex _outputSpectrum ) : void

Applies the Discrete Fourier Transform to an input signal

Throws an exception if signal and spectrum sizes mismatch and if their size are not a power of two!

FFT_Inverse ( System.Complex _inputSpectrum ) : System.Complex[]

Applies the Inverse Discrete Fourier Transform to an input frequency spectrum

Throws an exception if spectrum size is not a power of two!

FFT_Inverse ( System.Complex _inputSpectrum, System.Complex _outputSignal ) : void

Applies the Inverse Discrete Fourier Transform to an input frequency spectrum

Throws an exception if signal and spectrum sizes mismatch and if their size are not a power of two!

Private Methods

Method Description
BROUTEFORCE_FFT ( System.Complex _inputSignal, int _inputIndex, int _inputStride, int _length, double _baseFrequency ) : System.Complex[]
FFT_BreadthFirst ( System.Complex _input, System.Complex _output, int _size, int _POT, double _baseFrequency ) : void
GenerateIndexList ( int _stageIndex, int _indices ) : void
GenerateIndexList ( int _stageIndex, int _indices, int _targetIndex, int _sourceIndex, int _length, int _stride ) : void

Method Details

FFT_Forward() public static method

Applies the Discrete Fourier Transform to an input signal
Throws an exception if signal size is not a power of two!
public static FFT_Forward ( System.Complex _inputSignal ) : System.Complex[]
_inputSignal System.Complex The input signal of complex-valued amplitudes for each time sample
return System.Complex[]

FFT_Forward() public static method

Applies the Discrete Fourier Transform to an input signal
Throws an exception if signal and spectrum sizes mismatch and if their size are not a power of two!
public static FFT_Forward ( System.Complex _inputSignal, System.Complex _outputSpectrum ) : void
_inputSignal System.Complex The input signal of complex-valued amplitudes for each time sample
_outputSpectrum System.Complex The output complex-valued spectrum of amplitudes for each frequency. /// The spectrum of frequencies goes from [-N/2,N/2[ hertz, where N is the length of the input signal
return void

FFT_Inverse() public static method

Applies the Inverse Discrete Fourier Transform to an input frequency spectrum
Throws an exception if spectrum size is not a power of two!
public static FFT_Inverse ( System.Complex _inputSpectrum ) : System.Complex[]
_inputSpectrum System.Complex The input complex-valued spectrum of amplitudes for each frequency
return System.Complex[]

FFT_Inverse() public static method

Applies the Inverse Discrete Fourier Transform to an input frequency spectrum
Throws an exception if signal and spectrum sizes mismatch and if their size are not a power of two!
public static FFT_Inverse ( System.Complex _inputSpectrum, System.Complex _outputSignal ) : void
_inputSpectrum System.Complex The input complex-valued spectrum of amplitudes for each frequency
_outputSignal System.Complex The output signal of complex-valued amplitudes for each time sample
return void