DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::FFTReal< T > Class Template Reference

FFT optimised for real-valued input signals (the common audio case). More...

#include <FFT.h>

Public Member Functions

 FFTReal (size_t size)
 Constructs a real FFT processor.
 
size_t getSize () const noexcept
 Returns the number of real input samples (N).
 
size_t getFrequencyDomainSize () const noexcept
 Returns the frequency-domain buffer size in elements (N + 2).
 
size_t getNumBins () const noexcept
 Returns the number of frequency bins (N/2 + 1) including DC and Nyquist.
 
void forward (const T *timeData, T *freqData) noexcept
 Forward transform: real time-domain -> complex frequency-domain.
 
void inverse (const T *freqData, T *timeData) noexcept
 Inverse transform: complex frequency-domain -> real time-domain.
 
int getInverseStepCount () const noexcept
 Returns the number of steps inverseStep() splits inverse() into (the same for the life of the object).
 
void inverseStep (int step, const T *freqData, T *timeData) noexcept
 Runs one step of inverse(), for callers that spread a large transform over several calls (a real-time thread that must not spend the whole transform in one callback).
 
void computeMagnitudes (const T *freqData, T *magnitudes) const noexcept
 Computes the magnitude of each frequency bin.
 
void computePhases (const T *freqData, T *phases) const noexcept
 Computes the phase angle of each frequency bin.
 
void computePowerSpectrum (const T *freqData, T *power) const noexcept
 Computes the power spectrum (magnitude squared) of each bin.
 

Static Public Member Functions

static size_t allocationBytes (size_t size) noexcept
 Upper bound of the heap bytes a constructor of this size requests, excluding the object itself, with up to 64 bytes per allocation for standard-library bookkeeping. Allocates nothing; use it to budget memory before constructing. The count depends on the build's kernel sets.
 
static T binToFrequency (size_t binIndex, double sampleRate, size_t fftSize) noexcept
 Returns the frequency in Hz corresponding to a given bin index.
 
static size_t frequencyToBin (double frequency, double sampleRate, size_t fftSize) noexcept
 Returns the bin index closest to a given frequency.
 

Detailed Description

template<typename T>
class dspark::FFTReal< T >

FFT optimised for real-valued input signals (the common audio case).

Transforms the samples as W interleaved sequences, one in each lane of a vector, with real radix-4 passes, and joins their spectra in one last step (see the file comment): about half the work of a complex transform of the same length. The shortest sizes run a half-size complex FFT on the (even, odd) sample pairs and split its spectrum with a post-pass.

Frequency domain layout: N+2 elements (interleaved complex).

  • Bins 0 to N/2 inclusive -> (N/2 + 1) complex values -> (N + 2) floats.
  • data[2*k] = real part of bin k.
  • data[2*k+1] = imaginary part of bin k.
  • Bin 0 = DC, bin N/2 = Nyquist.

In-place use is supported: timeData and freqData may point to the same buffer as long as it holds getFrequencyDomainSize() = N+2 elements.

Template Parameters
TSample type (float or double).

Definition at line 1196 of file FFT.h.

Constructor & Destructor Documentation

◆ FFTReal()

template<typename T >
dspark::FFTReal< T >::FFTReal ( size_t  size)
inlineexplicit

Constructs a real FFT processor.

Parameters
sizeNumber of real samples. Must be a power of two and >= 4.
Exceptions
std::invalid_argumentif size is invalid (with DSPARK_NO_EXCEPTIONS: asserts and degrades to the minimal valid size instead).

Definition at line 1205 of file FFT.h.

Member Function Documentation

◆ allocationBytes()

template<typename T >
static size_t dspark::FFTReal< T >::allocationBytes ( size_t  size)
inlinestaticnoexcept

Upper bound of the heap bytes a constructor of this size requests, excluding the object itself, with up to 64 bytes per allocation for standard-library bookkeeping. Allocates nothing; use it to budget memory before constructing. The count depends on the build's kernel sets.

Parameters
sizeNumber of real samples: a power of two >= 4 (other sizes return 0).
Returns
At least the sum of the constructor's allocation requests.

Definition at line 1243 of file FFT.h.

◆ binToFrequency()

template<typename T >
static T dspark::FFTReal< T >::binToFrequency ( size_t  binIndex,
double  sampleRate,
size_t  fftSize 
)
inlinestaticnoexcept

Returns the frequency in Hz corresponding to a given bin index.

Parameters
binIndexBin index (0 to N/2).
sampleRateSample rate in Hz.
fftSizeFFT size (N).
Returns
Center frequency of the requested bin.

Definition at line 1431 of file FFT.h.

◆ computeMagnitudes()

template<typename T >
void dspark::FFTReal< T >::computeMagnitudes ( const T *  freqData,
T *  magnitudes 
) const
inlinenoexcept

Computes the magnitude of each frequency bin.

Parameters
freqDataInput: frequency-domain data (N+2 elements).
magnitudesOutput: N/2+1 magnitude values. Must be pre-allocated.

Definition at line 1384 of file FFT.h.

◆ computePhases()

template<typename T >
void dspark::FFTReal< T >::computePhases ( const T *  freqData,
T *  phases 
) const
inlinenoexcept

Computes the phase angle of each frequency bin.

Parameters
freqDataInput: frequency-domain data (N+2 elements).
phasesOutput: N/2+1 phase values in radians (-pi to +pi).

Definition at line 1399 of file FFT.h.

◆ computePowerSpectrum()

template<typename T >
void dspark::FFTReal< T >::computePowerSpectrum ( const T *  freqData,
T *  power 
) const
inlinenoexcept

Computes the power spectrum (magnitude squared) of each bin.

Parameters
freqDataInput: frequency-domain data (N+2 elements).
powerOutput: N/2+1 power values.

Definition at line 1414 of file FFT.h.

◆ forward()

template<typename T >
void dspark::FFTReal< T >::forward ( const T *  timeData,
T *  freqData 
)
inlinenoexcept

Forward transform: real time-domain -> complex frequency-domain.

Parameters
timeDataInput: N real samples.
freqDataOutput: N+2 elements (interleaved complex, N/2+1 bins).

Definition at line 1269 of file FFT.h.

◆ frequencyToBin()

template<typename T >
static size_t dspark::FFTReal< T >::frequencyToBin ( double  frequency,
double  sampleRate,
size_t  fftSize 
)
inlinestaticnoexcept

Returns the bin index closest to a given frequency.

Parameters
frequencyFrequency in Hz.
sampleRateSample rate in Hz.
fftSizeFFT size (N).
Returns
Integer index of the nearest bin.

Definition at line 1443 of file FFT.h.

◆ getFrequencyDomainSize()

template<typename T >
size_t dspark::FFTReal< T >::getFrequencyDomainSize ( ) const
inlinenoexcept

Returns the frequency-domain buffer size in elements (N + 2).

Definition at line 1228 of file FFT.h.

◆ getInverseStepCount()

template<typename T >
int dspark::FFTReal< T >::getInverseStepCount ( ) const
inlinenoexcept

Returns the number of steps inverseStep() splits inverse() into (the same for the life of the object).

Definition at line 1322 of file FFT.h.

◆ getNumBins()

template<typename T >
size_t dspark::FFTReal< T >::getNumBins ( ) const
inlinenoexcept

Returns the number of frequency bins (N/2 + 1) including DC and Nyquist.

Definition at line 1231 of file FFT.h.

◆ getSize()

template<typename T >
size_t dspark::FFTReal< T >::getSize ( ) const
inlinenoexcept

Returns the number of real input samples (N).

Definition at line 1225 of file FFT.h.

◆ inverse()

template<typename T >
void dspark::FFTReal< T >::inverse ( const T *  freqData,
T *  timeData 
)
inlinenoexcept

Inverse transform: complex frequency-domain -> real time-domain.

Parameters
freqDataInput: N+2 elements (interleaved complex, N/2+1 bins).
timeDataOutput: N real samples.

Definition at line 1297 of file FFT.h.

◆ inverseStep()

template<typename T >
void dspark::FFTReal< T >::inverseStep ( int  step,
const T *  freqData,
T *  timeData 
)
inlinenoexcept

Runs one step of inverse(), for callers that spread a large transform over several calls (a real-time thread that must not spend the whole transform in one callback).

Step 0 reads the spectrum and each later step is at most one pass over the data: the count is the same for every kernel set of the build, and a set whose transform has fewer passes does nothing in the steps right after the first. Running steps 0 .. getInverseStepCount() - 1 in order, with no other transform on this instance in between, writes exactly what inverse() writes, bit for bit.

Parameters
stepStep index, 0 .. getInverseStepCount() - 1.
freqDataInput spectrum, N+2 elements (read by step 0 only).
timeDataOutput: N real samples (written by the last step only).

Definition at line 1345 of file FFT.h.


The documentation for this class was generated from the following file: