FFTInterface Manual

About

The fast Fourier transform (FFT) is used to analyze a given signal in order to transform it into the frequency domain. The maxon::FFTInterface gives access to implementations of various algorithms.

Classes

Various implementations of the FFT are registered at maxon::FFTClasses:

  • maxon::FFTClasses::Generic: Generic Cinema 4D implementation.
  • maxon::FFTClasses::Kiss: Fast FFT algorithm using the KISS library.
  • maxon::FFTClasses::Cooley: Cooley Tukey algorithm.

FFTInterface

The maxon::FFTInterface provides the functions to perform the FFT:

// This example shows how to use FFT to get the spectrum of a signal.
// synthesize a signal of one second
// signal parameters
const maxon::Int sampleCount = 500;
const maxon::Float sampleTime = 1.0; // seconds
const maxon::Float samplingFrequency = maxon::Float(sampleCount) / sampleTime;
const maxon::Float step = (2 * maxon::PI) / maxon::Float(sampleCount);
// create signal
signal.EnsureCapacity(sampleCount) iferr_return;
for (maxon::Int i = 0; i < sampleCount; ++i)
{
const maxon::Float pos = maxon::Float(i) * step;
maxon::Float sample = 0.0;
// 1Hz component
sample += maxon::Sin(pos);
// 2Hz component
sample += maxon::Sin(pos * 2);
// 4Hz component
sample += maxon::Sin(pos * 4) * 0.5;
// 8Hz component
sample += maxon::Sin(pos * 8) * 0.5;
signal.Append(sample) iferr_return;
}
// perform FFT
// create FFTRef
const maxon::FFTRef genericFFT = maxon::FFTClasses::Generic().Create() iferr_return;
// check if this FFT implementation supports Transform1D()
const maxon::FFT_SUPPORT options = genericFFT.GetSupportOptions();
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
// transform signal
genericFFT.Transform1D(signal.ToArray(), complexDFT.ToArray()) iferr_return;
// convert to amplitude spectrum
spectrum.EnsureCapacity(complexDFT.GetCount()) iferr_return;
for (const auto DFT : complexDFT)
{
const maxon::Float amplitude = DFT.GetLength();
spectrum.Append(amplitude) iferr_return;
}
// display spectrum
// spectrum resolution
const maxon::Float resolution = samplingFrequency / sampleCount;
const maxon::Float spectrumRange = maxon::Float(spectrum.GetCount());
const maxon::Int maxFrequencyIndex = maxon::Int(spectrumRange * 0.5);
for (maxon::Int i = 0; i < maxFrequencyIndex; ++i)
{
const maxon::Float frequency = i * resolution;
const maxon::Float amplitude = spectrum[i];
const maxon::Float normalizedAmplitude = (amplitude / maxon::Float(sampleCount));
DiagnosticOutput("@ Hz: @", frequency, normalizedAmplitude);
}

Further Reading

maxon::FFT_SUPPORT
FFT_SUPPORT
Flags for GetSupportOptions()
Definition: fft.h:24
maxon::ArrayBase< BaseArray< T, BASEARRAY_DEFAULT_CHUNK_SIZE, BASEARRAYFLAGS::NONE, DefaultAllocator >, T, BaseArrayData< T, DefaultAllocator, STD_IS_REPLACEMENT(empty, DefaultAllocator)>, DefaultCompare >::ToArray
ArrayImpl< BaseArray< T, BASEARRAY_DEFAULT_CHUNK_SIZE, BASEARRAYFLAGS::NONE, DefaultAllocator > & > ToArray()
Definition: array.h:1386
iferr_return
#define iferr_return
Definition: resultbase.h:1434
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:66
maxon::Float
Float64 Float
Definition: apibase.h:193
maxon::BaseArray
Definition: basearray.h:366
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:168
maxon::PI
static const Float64 PI
floating point constant: PI
Definition: apibasemath.h:156
maxon::BaseArray::GetCount
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: basearray.h:527
maxon::BaseArray::Append
MAXON_ATTRIBUTE_FORCE_INLINE ResultRef< T > Append()
Definition: basearray.h:569
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:184
maxon::Sin
MAXON_ATTRIBUTE_FORCE_INLINE Float32 Sin(Float32 val)
Calculates the sine of a value.
Definition: apibasemath.h:212
maxon::FFT_SUPPORT::TRANSFORM_1D
@ TRANSFORM_1D
Component supports 1D transformation.
maxon::BaseArray::EnsureCapacity
ResultMem EnsureCapacity(Int requestedCapacity, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
Definition: basearray.h:1188