Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::FFT< Real >::Plan Class Reference

Precomputed FFT plan for repeated transforms of the same size. More...

#include <fft.H>

Collaboration diagram for Aleph::FFT< Real >::Plan:
[legend]

Public Member Functions

 Plan ()=default
 Constructs an empty plan (size 0).
 
 Plan (const size_t n)
 Constructs a plan for transforms of size n.
 
 Plan (const Plan &other)=default
 Copy the tables of another plan.
 
 Plan (Plan &&other) noexcept
 Take the tables of another plan, which is left empty (size 0).
 
Plan & operator= (const Plan &other)=default
 Copy the tables of another plan.
 
Plan & operator= (Plan &&other) noexcept
 Exchange this plan with another, which is left with the previous tables of this one.
 
void swap (Plan &other) noexcept
 Exchange in O(1) the tables of this plan and another.
 
size_t size () const noexcept
 Returns the transform size this plan was built for.
 
void transform (Array< Complex > &a, const bool invert) const
 Computes the FFT or IFFT in-place using precomputed tables.
 
Array< Complex > transformed (const Array< Complex > &input, const bool invert=false) const
 Computes the FFT or IFFT returning a new array.
 
Array< Complex > inverse_transform (const Array< Complex > &input) const
 Returns the inverse transform as a new array.
 
Array< Real > inverse_transform_real (const Array< Complex > &input) const
 Returns the IFFT projected back to real values.
 
Array< Real > pinverse_transform_real (ThreadPool &pool, const Array< Complex > &input, const size_t chunk_size=0) const
 Parallel IFFT projected back to real values.
 
void ptransform (ThreadPool &pool, Array< Complex > &a, const bool invert, const size_t chunk_size=0) const
 Parallel in-place transform using a thread pool.
 
Array< Complex > ptransformed (ThreadPool &pool, const Array< Complex > &input, const bool invert=false, const size_t chunk_size=0) const
 Parallel transform returning a new array.
 
Array< Complex > pinverse_transform (ThreadPool &pool, const Array< Complex > &input, const size_t chunk_size=0) const
 Parallel inverse transform returning a new array.
 
void transform_batch (Array< Array< Complex > > &batch, const bool invert, const bool prefer_simd=true) const
 In-place batch transform for equal-length complex inputs.
 
void ptransform_batch (ThreadPool &pool, Array< Array< Complex > > &batch, const bool invert, const size_t chunk_size=0, const bool prefer_simd=true) const
 Parallel batch transform for equal-length complex inputs.
 
Array< Array< Complex > > transformed_batch (const Array< Array< Complex > > &input, const bool invert=false, const bool prefer_simd=true) const
 Returns the batch FFT/IFFT as a new array of arrays.
 
Array< Array< Complex > > ptransformed_batch (ThreadPool &pool, const Array< Array< Complex > > &input, const bool invert=false, const size_t chunk_size=0, const bool prefer_simd=true) const
 Returns the parallel batch FFT/IFFT as a new array of arrays.
 
Array< Array< Complex > > inverse_transform_batch (const Array< Array< Complex > > &input) const
 Returns the inverse batch transform as a new array of arrays.
 
Array< Array< Complex > > pinverse_transform_batch (ThreadPool &pool, const Array< Array< Complex > > &input, const size_t chunk_size=0, const bool prefer_simd=true) const
 Returns the parallel inverse batch transform.
 
Array< Array< Real > > inverse_transform_real_batch (const Array< Array< Complex > > &input) const
 Returns the inverse batch transform projected back to real values.
 
Array< Array< Real > > pinverse_transform_real_batch (ThreadPool &pool, const Array< Array< Complex > > &input, const size_t chunk_size=0, const bool prefer_simd=true) const
 Returns the parallel inverse batch transform projected back to real values.
 
Array< Complex > rfft (const Array< Real > &input) const
 Computes the Real-to-Complex FFT (RFFT).
 
Array< Complex > prfft (ThreadPool &pool, const Array< Real > &input, const size_t chunk_size=0) const
 Parallel Real-to-Complex FFT (RFFT).
 
Array< Real > irfft (const Array< Complex > &spectrum) const
 Computes the Complex-to-Real Inverse FFT (IRFFT).
 
Array< Real > pirfft (ThreadPool &pool, const Array< Complex > &spectrum, const size_t chunk_size=0) const
 Parallel Complex-to-Real Inverse FFT (IRFFT).
 
Array< Array< Complex > > rfft_batch (const Array< Array< Real > > &input) const
 Computes a batch of Real-to-Complex FFTs.
 
Array< Array< Complex > > prfft_batch (ThreadPool &pool, const Array< Array< Real > > &input, const size_t chunk_size=0) const
 Computes a parallel batch of Real-to-Complex FFTs.
 
Array< Array< Real > > irfft_batch (const Array< Array< Complex > > &spectra) const
 Computes a batch of Complex-to-Real Inverse FFTs.
 
Array< Array< Real > > pirfft_batch (ThreadPool &pool, const Array< Array< Complex > > &spectra, const size_t chunk_size=0) const
 Computes a parallel batch of Complex-to-Real Inverse FFTs.
 

Private Types

enum class  Strategy { empty , power_of_two , mixed_radix , bluestein }
 

Private Member Functions

void initialize_power_of_two_plan ()
 
void initialize_mixed_radix_plan ()
 
void initialize_bluestein_plan ()
 
void apply_bit_reversal (Array< Complex > &a) const noexcept
 
bool automatic_batch_simd_candidate (const size_t batch_size) const noexcept
 
bool should_use_avx2 (ThreadPool *pool, const bool prefer_simd) const noexcept
 
bool should_use_neon (ThreadPool *pool, const bool prefer_simd) const noexcept
 
void apply_butterflies (Array< Complex > &a, const bool invert, ThreadPool *pool, const size_t chunk_size, const bool prefer_simd) const
 
void apply_transform (Array< Complex > &a, const bool invert, ThreadPool *pool, const size_t chunk_size, const bool prefer_simd) const
 
Complex root_for_length (const size_t length, const size_t exponent, const bool invert) const
 
Array< Complex > mixed_radix_transform_recursive (const Array< Complex > &input, const size_t factor_index, const size_t current_size, const bool invert) const
 
void apply_mixed_radix_transform (Array< Complex > &a, const bool invert) const
 
void apply_bluestein_transform (Array< Complex > &a, const bool invert, ThreadPool *pool, const size_t chunk_size) const
 
bool supports_power_of_two_real_optimization () const noexcept
 

Private Attributes

size_t n_ = 0
 
Strategy strategy_ = Strategy::empty
 
size_t log_n_ = 0
 
Array< size_t > bit_rev_
 
Array< Complex > twiddles_
 
std::shared_ptr< const Plan > half_plan_
 
Array< size_t > factors_
 
Array< Complex > roots_
 
size_t bluestein_size_ = 0
 
Array< Complex > bluestein_chirp_
 
Array< Complex > bluestein_kernel_forward_
 
Array< Complex > bluestein_kernel_inverse_
 
std::shared_ptr< const Plan > bluestein_plan_
 

Detailed Description

template<std::floating_point Real = double>
class Aleph::FFT< Real >::Plan

Precomputed FFT plan for repeated transforms of the same size.

A Plan precomputes the state needed for a given transform size N. Power-of-two sizes cache the bit-reversal permutation and radix-2/radix-4 twiddle tables. Other sizes cache either a mixed-radix factorization or the Bluestein convolution kernel. Subsequent calls to transform reuse these tables instead of rebuilding them.

This is beneficial when multiple transforms of the same size are performed (e.g., streaming audio, sliding-window spectral analysis).

Note
Construction cost is O(N). Each transform execution is O(N log N).
FFT<double>::Plan plan(1024);
plan.transform(signal_a, false); // forward, in-place
plan.transform(signal_b, false); // reuses precomputed tables
auto spectrum = plan.transformed(signal_c); // functional variant
Precomputed FFT plan for repeated transforms of the same size.
Definition fft.H:6318
static Array< Complex > spectrum(const Array< Complex > &input)
DSP alias for the forward FFT.
Definition fft.H:8687

Definition at line 6317 of file fft.H.

Member Enumeration Documentation

◆ Strategy

template<std::floating_point Real = double>
enum class Aleph::FFT::Plan::Strategy
strongprivate
Enumerator
empty 
power_of_two 
mixed_radix 
bluestein 

Definition at line 6319 of file fft.H.

Constructor & Destructor Documentation

◆ Plan() [1/4]

template<std::floating_point Real = double>
Aleph::FFT< Real >::Plan::Plan ( )
default

Constructs an empty plan (size 0).

◆ Plan() [2/4]

template<std::floating_point Real = double>
Aleph::FFT< Real >::Plan::Plan ( const size_t  n)
inlineexplicit

Constructs a plan for transforms of size n.

Precomputes the bit-reversal permutation and all twiddle factors. Construction cost is O(N).

Parameters
nTransform size (must be positive).
Exceptions
std::invalid_argumentif n is zero.

Definition at line 7280 of file fft.H.

References ah_invalid_argument_if, Aleph::FFT< Real >::Plan::empty, Aleph::FFT< Real >::factor_small_radices(), Aleph::FFT< Real >::Plan::factors_, Aleph::FFT< Real >::Plan::initialize_bluestein_plan(), Aleph::FFT< Real >::Plan::initialize_mixed_radix_plan(), Aleph::FFT< Real >::Plan::initialize_power_of_two_plan(), Aleph::Array< T >::is_empty(), Aleph::FFT< Real >::is_power_of_two(), Aleph::FFT< Real >::Plan::n_, and Aleph::FFT< Real >::Plan::strategy_.

◆ Plan() [3/4]

template<std::floating_point Real = double>
Aleph::FFT< Real >::Plan::Plan ( const Plan &  other)
default

Copy the tables of another plan.

Parameters
[in]otherplan to copy.
Exceptions
std::bad_allocif the tables cannot be allocated.

◆ Plan() [4/4]

template<std::floating_point Real = double>
Aleph::FFT< Real >::Plan::Plan ( Plan &&  other)
inlinenoexcept

Take the tables of another plan, which is left empty (size 0).

The implicit move took the tables but copied the size and the strategy: the moved-from plan then read a bit-reversal table that it no longer had.

Parameters
[in,out]otherplan to move from.
Note
Complexity: O(1).

Definition at line 7322 of file fft.H.

Member Function Documentation

◆ apply_bit_reversal()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::Plan::apply_bit_reversal ( Array< Complex > &  a) const
inlineprivatenoexcept

◆ apply_bluestein_transform()

◆ apply_butterflies()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::Plan::apply_butterflies ( Array< Complex > &  a,
const bool  invert,
ThreadPool *  pool,
const size_t  chunk_size,
const bool  prefer_simd 
) const
inlineprivate

◆ apply_mixed_radix_transform()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::Plan::apply_mixed_radix_transform ( Array< Complex > &  a,
const bool  invert 
) const
inlineprivate

◆ apply_transform()

◆ automatic_batch_simd_candidate()

template<std::floating_point Real = double>
bool Aleph::FFT< Real >::Plan::automatic_batch_simd_candidate ( const size_t  batch_size) const
inlineprivatenoexcept

◆ initialize_bluestein_plan()

◆ initialize_mixed_radix_plan()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::Plan::initialize_mixed_radix_plan ( )
inlineprivate

◆ initialize_power_of_two_plan()

◆ inverse_transform()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::inverse_transform ( const Array< Complex > &  input) const
inline

Returns the inverse transform as a new array.

Definition at line 7408 of file fft.H.

References Aleph::FFT< Real >::Plan::transformed().

◆ inverse_transform_batch()

template<std::floating_point Real = double>
Array< Array< Complex > > Aleph::FFT< Real >::Plan::inverse_transform_batch ( const Array< Array< Complex > > &  input) const
inline

Returns the inverse batch transform as a new array of arrays.

Definition at line 7593 of file fft.H.

References Aleph::FFT< Real >::Plan::transformed_batch().

◆ inverse_transform_real()

◆ inverse_transform_real_batch()

template<std::floating_point Real = double>
Array< Array< Real > > Aleph::FFT< Real >::Plan::inverse_transform_real_batch ( const Array< Array< Complex > > &  input) const
inline

◆ irfft()

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::Plan::irfft ( const Array< Complex > &  spectrum) const
inline

Computes the Complex-to-Real Inverse FFT (IRFFT).

Parameters
spectrumCompact complex spectrum (size N/2 + 1).
Returns
Real signal of size N.

Definition at line 7696 of file fft.H.

References Aleph::FFT< Real >::expand_real_spectrum(), Aleph::FFT< Real >::Plan::inverse_transform_real(), Aleph::FFT< Real >::Plan::n_, and Aleph::FFT< Real >::spectrum().

◆ irfft_batch()

template<std::floating_point Real = double>
Array< Array< Real > > Aleph::FFT< Real >::Plan::irfft_batch ( const Array< Array< Complex > > &  spectra) const
inline

Computes a batch of Complex-to-Real Inverse FFTs.

Definition at line 7735 of file fft.H.

References Aleph::FFT< Real >::expand_real_batch_spectra(), Aleph::FFT< Real >::Plan::inverse_transform_real_batch(), and Aleph::FFT< Real >::Plan::n_.

Referenced by Aleph::FFT< Real >::irfft_batch().

◆ mixed_radix_transform_recursive()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::mixed_radix_transform_recursive ( const Array< Complex > &  input,
const size_t  factor_index,
const size_t  current_size,
const bool  invert 
) const
inlineprivate

◆ operator=() [1/2]

template<std::floating_point Real = double>
Plan & Aleph::FFT< Real >::Plan::operator= ( const Plan &  other)
default

Copy the tables of another plan.

Parameters
[in]otherplan to copy.
Returns
this plan.
Exceptions
std::bad_allocif the tables cannot be allocated.

◆ operator=() [2/2]

template<std::floating_point Real = double>
Plan & Aleph::FFT< Real >::Plan::operator= ( Plan &&  other)
inlinenoexcept

Exchange this plan with another, which is left with the previous tables of this one.

Parameters
[in,out]otherplan to move from.
Returns
this plan.
Note
Complexity: O(1).

Definition at line 7351 of file fft.H.

References Aleph::FFT< Real >::Plan::swap().

◆ pinverse_transform()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::pinverse_transform ( ThreadPool &  pool,
const Array< Complex > &  input,
const size_t  chunk_size = 0 
) const
inline

Parallel inverse transform returning a new array.

Definition at line 7501 of file fft.H.

References Aleph::FFT< Real >::Plan::ptransformed().

◆ pinverse_transform_batch()

template<std::floating_point Real = double>
Array< Array< Complex > > Aleph::FFT< Real >::Plan::pinverse_transform_batch ( ThreadPool &  pool,
const Array< Array< Complex > > &  input,
const size_t  chunk_size = 0,
const bool  prefer_simd = true 
) const
inline

Returns the parallel inverse batch transform.

Definition at line 7600 of file fft.H.

References Aleph::FFT< Real >::Plan::ptransformed_batch().

◆ pinverse_transform_real()

◆ pinverse_transform_real_batch()

template<std::floating_point Real = double>
Array< Array< Real > > Aleph::FFT< Real >::Plan::pinverse_transform_real_batch ( ThreadPool &  pool,
const Array< Array< Complex > > &  input,
const size_t  chunk_size = 0,
const bool  prefer_simd = true 
) const
inline

◆ pirfft()

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::Plan::pirfft ( ThreadPool &  pool,
const Array< Complex > &  spectrum,
const size_t  chunk_size = 0 
) const
inline

◆ pirfft_batch()

template<std::floating_point Real = double>
Array< Array< Real > > Aleph::FFT< Real >::Plan::pirfft_batch ( ThreadPool &  pool,
const Array< Array< Complex > > &  spectra,
const size_t  chunk_size = 0 
) const
inline

Computes a parallel batch of Complex-to-Real Inverse FFTs.

Definition at line 7743 of file fft.H.

References Aleph::FFT< Real >::expand_real_batch_spectra(), Aleph::FFT< Real >::Plan::n_, and Aleph::FFT< Real >::Plan::pinverse_transform_real_batch().

Referenced by Aleph::FFT< Real >::pirfft_batch().

◆ prfft()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::prfft ( ThreadPool &  pool,
const Array< Real > &  input,
const size_t  chunk_size = 0 
) const
inline

◆ prfft_batch()

template<std::floating_point Real = double>
Array< Array< Complex > > Aleph::FFT< Real >::Plan::prfft_batch ( ThreadPool &  pool,
const Array< Array< Real > > &  input,
const size_t  chunk_size = 0 
) const
inline

Computes a parallel batch of Real-to-Complex FFTs.

Definition at line 7725 of file fft.H.

References Aleph::FFT< Real >::compact_real_batch_spectra(), and Aleph::FFT< Real >::project_to_plan_batch_spectra().

Referenced by Aleph::FFT< Real >::prfft_batch().

◆ ptransform()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::Plan::ptransform ( ThreadPool &  pool,
Array< Complex > &  a,
const bool  invert,
const size_t  chunk_size = 0 
) const
inline

Parallel in-place transform using a thread pool.

Parameters
poolThread pool for parallel butterfly stages.
aArray of complex values (size must equal plan size).
invertIf true, compute the inverse transform.
chunk_sizeMinimum iterations per parallel chunk (0 = auto).

Definition at line 7481 of file fft.H.

References Aleph::FFT< Real >::Plan::apply_transform().

Referenced by Aleph::FFT< Real >::OverlapAdd::convolve_impl(), Aleph::FFT< Real >::Plan::pinverse_transform_real(), Aleph::FFT< Real >::OverlapAdd::process_chunk_impl(), Aleph::FFT< Real >::project_to_plan_spectrum(), Aleph::FFT< Real >::Plan::ptransformed(), Aleph::FFT< Real >::transform_any_size_impl(), and Aleph::FFT< Real >::transform_axis_impl().

◆ ptransform_batch()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::Plan::ptransform_batch ( ThreadPool &  pool,
Array< Array< Complex > > &  batch,
const bool  invert,
const size_t  chunk_size = 0,
const bool  prefer_simd = true 
) const
inline

◆ ptransformed()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::ptransformed ( ThreadPool &  pool,
const Array< Complex > &  input,
const bool  invert = false,
const size_t  chunk_size = 0 
) const
inline

Parallel transform returning a new array.

Definition at line 7490 of file fft.H.

References output, and Aleph::FFT< Real >::Plan::ptransform().

Referenced by Aleph::FFT< Real >::Plan::pinverse_transform().

◆ ptransformed_batch()

template<std::floating_point Real = double>
Array< Array< Complex > > Aleph::FFT< Real >::Plan::ptransformed_batch ( ThreadPool &  pool,
const Array< Array< Complex > > &  input,
const bool  invert = false,
const size_t  chunk_size = 0,
const bool  prefer_simd = true 
) const
inline

Returns the parallel batch FFT/IFFT as a new array of arrays.

Definition at line 7580 of file fft.H.

References output, and Aleph::FFT< Real >::Plan::ptransform_batch().

Referenced by Aleph::FFT< Real >::Plan::pinverse_transform_batch().

◆ rfft()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::rfft ( const Array< Real > &  input) const
inline

Computes the Real-to-Complex FFT (RFFT).

Returns the compact spectrum (size N/2 + 1) for a real input.

Parameters
inputReal input array (size must equal plan size).
Returns
Compact complex spectrum.

Definition at line 7668 of file fft.H.

References ah_invalid_argument_if, Aleph::FFT< Real >::compact_real_spectrum(), Aleph::FFT< Real >::Plan::n_, Aleph::FFT< Real >::project_to_plan_spectrum(), and Aleph::Array< T >::size().

◆ rfft_batch()

template<std::floating_point Real = double>
Array< Array< Complex > > Aleph::FFT< Real >::Plan::rfft_batch ( const Array< Array< Real > > &  input) const
inline

Computes a batch of Real-to-Complex FFTs.

Definition at line 7717 of file fft.H.

References Aleph::FFT< Real >::compact_real_batch_spectra(), and Aleph::FFT< Real >::project_to_plan_batch_spectra().

Referenced by Aleph::FFT< Real >::rfft_batch().

◆ root_for_length()

template<std::floating_point Real = double>
Complex Aleph::FFT< Real >::Plan::root_for_length ( const size_t  length,
const size_t  exponent,
const bool  invert 
) const
inlineprivate

◆ should_use_avx2()

◆ should_use_neon()

◆ size()

template<std::floating_point Real = double>
size_t Aleph::FFT< Real >::Plan::size ( ) const
inlinenoexcept

◆ supports_power_of_two_real_optimization()

template<std::floating_point Real = double>
bool Aleph::FFT< Real >::Plan::supports_power_of_two_real_optimization ( ) const
inlineprivatenoexcept

◆ swap()

◆ transform()

◆ transform_batch()

◆ transformed()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::Plan::transformed ( const Array< Complex > &  input,
const bool  invert = false 
) const
inline

Computes the FFT or IFFT returning a new array.

Parameters
inputInput array (size must equal plan size).
invertIf true, compute the inverse transform.
Returns
A new array containing the transformed values.

Definition at line 7398 of file fft.H.

References output, and Aleph::FFT< Real >::Plan::transform().

Referenced by Aleph::FFT< Real >::Plan::inverse_transform().

◆ transformed_batch()

template<std::floating_point Real = double>
Array< Array< Complex > > Aleph::FFT< Real >::Plan::transformed_batch ( const Array< Array< Complex > > &  input,
const bool  invert = false,
const bool  prefer_simd = true 
) const
inline

Returns the batch FFT/IFFT as a new array of arrays.

Definition at line 7569 of file fft.H.

References output, and Aleph::FFT< Real >::Plan::transform_batch().

Referenced by Aleph::FFT< Real >::Plan::inverse_transform_batch().

Member Data Documentation

◆ bit_rev_

template<std::floating_point Real = double>
Array<size_t> Aleph::FFT< Real >::Plan::bit_rev_
private

◆ bluestein_chirp_

template<std::floating_point Real = double>
Array<Complex> Aleph::FFT< Real >::Plan::bluestein_chirp_
private

◆ bluestein_kernel_forward_

template<std::floating_point Real = double>
Array<Complex> Aleph::FFT< Real >::Plan::bluestein_kernel_forward_
private

◆ bluestein_kernel_inverse_

template<std::floating_point Real = double>
Array<Complex> Aleph::FFT< Real >::Plan::bluestein_kernel_inverse_
private

◆ bluestein_plan_

template<std::floating_point Real = double>
std::shared_ptr<const Plan> Aleph::FFT< Real >::Plan::bluestein_plan_
private

◆ bluestein_size_

template<std::floating_point Real = double>
size_t Aleph::FFT< Real >::Plan::bluestein_size_ = 0
private

◆ factors_

template<std::floating_point Real = double>
Array<size_t> Aleph::FFT< Real >::Plan::factors_
private

◆ half_plan_

◆ log_n_

◆ n_

◆ roots_

template<std::floating_point Real = double>
Array<Complex> Aleph::FFT< Real >::Plan::roots_
private

◆ strategy_

◆ twiddles_

template<std::floating_point Real = double>
Array<Complex> Aleph::FFT< Real >::Plan::twiddles_
private

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