DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
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.
 
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 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).

Uses a half-size complex FFT internally, saving ~50% computation.

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 418 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 427 of file FFT.h.

Member Function Documentation

◆ 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 527 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 480 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 495 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 510 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 450 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 539 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 440 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 443 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 437 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 466 of file FFT.h.


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