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)
{
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 = (maxon::Float)i * resolution;
const maxon::Float amplitude = spectrum[i];
const maxon::Float normalizedAmplitude = (amplitude / maxon::Float(sampleCount));
DiagnosticOutput("@ Hz: @", frequency, normalizedAmplitude);
}
Py_ssize_t i
Definition: abstract.h:645
ArrayImpl< COLLECTION & > ToArray()
Definition: array.h:2046
Definition: basearray.h:412
ResultMem EnsureCapacity(Int requestedCapacity, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY)
Definition: basearray.h:1480
MAXON_ATTRIBUTE_FORCE_INLINE ResultRef< T > Append(ARG &&x)
Definition: basearray.h:677
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: basearray.h:573
void Py_ssize_t * pos
Definition: dictobject.h:50
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
Float64 Float
Definition: apibase.h:197
static constexpr Float64 PI
floating point constant: PI
Definition: apibasemath.h:139
MAXON_ATTRIBUTE_FORCE_INLINE Float32 Sin(Float32 val)
Calculates the sine of a value.
Definition: apibasemath.h:198
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
FFT_SUPPORT
Flags for GetSupportOptions()
Definition: fft.h:25
@ TRANSFORM_1D
Component supports 1D transformation.
#define iferr_return
Definition: resultbase.h:1519
PyObject PyObject * step
Definition: sliceobject.h:34

Further Reading