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

Reusable overlap-add convolver for long real sequences. More...

#include <fft.H>

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

Public Member Functions

 OverlapAdd (const Array< Real > &kernel, const size_t block_size=0)
 
 OverlapAdd (const OverlapAdd &other)=default
 Copy the kernel, the plan and the pending tail of another convolver.
 
 OverlapAdd (OverlapAdd &&other) noexcept
 Take the state of another convolver, which is left empty.
 
OverlapAdd & operator= (const OverlapAdd &other)=default
 Copy the kernel, the plan and the pending tail of another convolver.
 
OverlapAdd & operator= (OverlapAdd &&other) noexcept
 Exchange this convolver with another, which is left with the previous state of this one.
 
void swap (OverlapAdd &other) noexcept
 Exchange in O(1) the state of this convolver and another.
 
size_t block_size () const noexcept
 
size_t fft_size () const noexcept
 
const Array< Real > & kernel () const noexcept
 
size_t overlap_size () const noexcept
 
void reset ()
 
Array< Real > convolve (const Array< Real > &signal) const
 Convolve a whole signal with the kernel.
 
Array< Real > pconvolve (ThreadPool &pool, const Array< Real > &signal, const size_t chunk_size=0) const
 Convolve a whole signal with the kernel, with parallel transforms.
 
Array< Real > process_block (const Array< Real > &block)
 Convolve the next samples of a stream; the tail of each block is added to the next ones, and flush() returns the last one.
 
Array< Real > pprocess_block (ThreadPool &pool, const Array< Real > &block, const size_t chunk_size=0)
 process_block() with parallel transforms.
 
Array< Real > flush ()
 
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > convolve (const Container &signal) const
 convolve() of the samples of a real container.
 
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > pconvolve (ThreadPool &pool, const Container &signal, const size_t chunk_size=0) const
 pconvolve() of the samples of a real container.
 
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > process_block (const Container &block)
 process_block() of the samples of a real container.
 
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > pprocess_block (ThreadPool &pool, const Container &block, const size_t chunk_size=0)
 pprocess_block() of the samples of a real container.
 

Private Member Functions

Array< Complex > build_signal_block (const Array< Real > &signal, const size_t offset, const size_t length) const
 Rationale: Prepares a chunk of signal for FFT by zero-padding it to the internal fft_size().
 
void pointwise_multiply (Array< Complex > &spectrum, ThreadPool *pool, const size_t chunk_size) const
 Rationale: Applies the precomputed FIR kernel in the frequency domain via element-wise multiplication.
 
void clear_overlap ()
 Rationale: Resets the overlap buffer between independent convolution runs.
 
Array< Real > process_chunk_impl (const Array< Real > &block, ThreadPool *pool, const size_t chunk_size)
 Rationale: Implements the core Overlap-Add logic for a single block:
 
Array< Real > convolve_impl (const Array< Real > &signal, ThreadPool *pool, const size_t chunk_size) const
 implementation of one-shot linear convolution.
 

Static Private Member Functions

static size_t default_block_size (const size_t kernel_size)
 

Private Attributes

Array< Real > kernel_
 
size_t block_size_ = 0
 
size_t fft_size_ = 0
 
Plan plan_
 
Array< Complex > kernel_spectrum_
 
Array< Real > overlap_
 
bool has_pending_tail_ = false
 

Detailed Description

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

Reusable overlap-add convolver for long real sequences.

The kernel spectrum is precomputed once. Input signals are processed in blocks of size block_size(), each block transformed with an FFT of size fft_size() and accumulated in the output via overlap-add.

Definition at line 14104 of file fft.H.

Constructor & Destructor Documentation

◆ OverlapAdd() [1/3]

◆ OverlapAdd() [2/3]

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

Copy the kernel, the plan and the pending tail of another convolver.

Parameters
[in]otherconvolver to copy.
Exceptions
std::bad_allocif the buffers cannot be allocated.

◆ OverlapAdd() [3/3]

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

Take the state of another convolver, which is left empty.

The implicit move took the kernel, the plan and the buffers but copied the sizes: the moved-from convolver then transformed blocks with a plan that it no longer had. Now its sizes are 0 and it has no kernel: it rejects every block and signal, while reset(), flush() and an assignment to it still work.

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

Definition at line 14318 of file fft.H.

Member Function Documentation

◆ block_size()

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

◆ build_signal_block()

template<std::floating_point Real = double>
Array< Complex > Aleph::FFT< Real >::OverlapAdd::build_signal_block ( const Array< Real > &  signal,
const size_t  offset,
const size_t  length 
) const
inlineprivate

Rationale: Prepares a chunk of signal for FFT by zero-padding it to the internal fft_size().

Definition at line 14127 of file fft.H.

References Aleph::Array< T >::create(), Aleph::FFT< Real >::OverlapAdd::fft_size_, and offset.

Referenced by Aleph::FFT< Real >::OverlapAdd::convolve_impl(), and Aleph::FFT< Real >::OverlapAdd::process_chunk_impl().

◆ clear_overlap()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::OverlapAdd::clear_overlap ( )
inlineprivate

◆ convolve() [1/2]

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::OverlapAdd::convolve ( const Array< Real > &  signal) const
inline

Convolve a whole signal with the kernel.

Parameters
[in]signalsignal to convolve.
Returns
the linear convolution, with signal.size() + kernel().size() - 1 samples; empty for an empty signal.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).
std::invalid_argumentif the size of the result overflows size_t.

Definition at line 14383 of file fft.H.

References ah_runtime_error_if, Aleph::FFT< Real >::OverlapAdd::convolve_impl(), Aleph::Array< T >::is_empty(), and Aleph::FFT< Real >::OverlapAdd::kernel_.

Referenced by Aleph::FFT< Real >::OverlapAdd::convolve(), Aleph::FFT< Real >::filtfilt(), and Aleph::FFT< Real >::overlap_add_convolution().

◆ convolve() [2/2]

template<std::floating_point Real = double>
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > Aleph::FFT< Real >::OverlapAdd::convolve ( const Container &  signal) const
inline

convolve() of the samples of a real container.

Template Parameters
Containertype of the container.
Parameters
[in]signalsignal to convolve.
Returns
the linear convolution.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).

Definition at line 14508 of file fft.H.

References Aleph::FFT< Real >::build_real_input(), and Aleph::FFT< Real >::OverlapAdd::convolve().

◆ convolve_impl()

◆ default_block_size()

template<std::floating_point Real = double>
static size_t Aleph::FFT< Real >::OverlapAdd::default_block_size ( const size_t  kernel_size)
inlinestaticprivate

◆ fft_size()

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

Definition at line 14363 of file fft.H.

References Aleph::FFT< Real >::OverlapAdd::fft_size_.

◆ flush()

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::OverlapAdd::flush ( )
inline

◆ kernel()

template<std::floating_point Real = double>
const Array< Real > & Aleph::FFT< Real >::OverlapAdd::kernel ( ) const
inlinenoexcept

Definition at line 14364 of file fft.H.

References Aleph::FFT< Real >::OverlapAdd::kernel_.

◆ operator=() [1/2]

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

Copy the kernel, the plan and the pending tail of another convolver.

Parameters
[in]otherconvolver to copy.
Returns
this convolver.
Exceptions
std::bad_allocif the buffers cannot be allocated.

◆ operator=() [2/2]

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

Exchange this convolver with another, which is left with the previous state of this one.

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

Definition at line 14342 of file fft.H.

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

◆ overlap_size()

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

Definition at line 14365 of file fft.H.

References Aleph::FFT< Real >::OverlapAdd::overlap_, and Aleph::Array< T >::size().

◆ pconvolve() [1/2]

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::OverlapAdd::pconvolve ( ThreadPool &  pool,
const Array< Real > &  signal,
const size_t  chunk_size = 0 
) const
inline

Convolve a whole signal with the kernel, with parallel transforms.

Parameters
[in]poolthread pool for the transforms.
[in]signalsignal to convolve.
[in]chunk_sizeminimum iterations per parallel chunk (0 = auto).
Returns
the linear convolution, as convolve() gives it.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).
std::invalid_argumentif the size of the result overflows size_t.

Definition at line 14404 of file fft.H.

References ah_runtime_error_if, Aleph::FFT< Real >::OverlapAdd::convolve_impl(), Aleph::Array< T >::is_empty(), and Aleph::FFT< Real >::OverlapAdd::kernel_.

Referenced by Aleph::FFT< Real >::OverlapAdd::pconvolve(), Aleph::FFT< Real >::pfiltfilt(), and Aleph::FFT< Real >::poverlap_add_convolution().

◆ pconvolve() [2/2]

template<std::floating_point Real = double>
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > Aleph::FFT< Real >::OverlapAdd::pconvolve ( ThreadPool &  pool,
const Container &  signal,
const size_t  chunk_size = 0 
) const
inline

pconvolve() of the samples of a real container.

Template Parameters
Containertype of the container.
Parameters
[in]poolthread pool for the transforms.
[in]signalsignal to convolve.
[in]chunk_sizeminimum iterations per parallel chunk (0 = auto).
Returns
the linear convolution.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).

Definition at line 14526 of file fft.H.

References Aleph::FFT< Real >::build_real_input(), and Aleph::FFT< Real >::OverlapAdd::pconvolve().

◆ pointwise_multiply()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::OverlapAdd::pointwise_multiply ( Array< Complex > &  spectrum,
ThreadPool *  pool,
const size_t  chunk_size 
) const
inlineprivate

◆ pprocess_block() [1/2]

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::OverlapAdd::pprocess_block ( ThreadPool &  pool,
const Array< Real > &  block,
const size_t  chunk_size = 0 
)
inline

process_block() with parallel transforms.

Parameters
[in]poolthread pool for the transforms.
[in]blocknext samples, of any number.
[in]chunk_sizeminimum iterations per parallel chunk (0 = auto).
Returns
one output sample for each sample of block.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).

Definition at line 14458 of file fft.H.

References ah_runtime_error_if, Aleph::Array< T >::append(), Aleph::FFT< Real >::OverlapAdd::block_size_, Aleph::Array< T >::is_empty(), Aleph::FFT< Real >::OverlapAdd::kernel_, offset, Aleph::FFT< Real >::OverlapAdd::process_chunk_impl(), Aleph::Array< T >::reserve(), and Aleph::Array< T >::size().

Referenced by Aleph::FFT< Real >::OverlapAdd::pprocess_block().

◆ pprocess_block() [2/2]

template<std::floating_point Real = double>
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > Aleph::FFT< Real >::OverlapAdd::pprocess_block ( ThreadPool &  pool,
const Container &  block,
const size_t  chunk_size = 0 
)
inline

pprocess_block() of the samples of a real container.

Template Parameters
Containertype of the container.
Parameters
[in]poolthread pool for the transforms.
[in]blocknext samples.
[in]chunk_sizeminimum iterations per parallel chunk (0 = auto).
Returns
one output sample for each sample of block.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).

Definition at line 14560 of file fft.H.

References Aleph::FFT< Real >::build_real_input(), and Aleph::FFT< Real >::OverlapAdd::pprocess_block().

◆ process_block() [1/2]

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::OverlapAdd::process_block ( const Array< Real > &  block)
inline

Convolve the next samples of a stream; the tail of each block is added to the next ones, and flush() returns the last one.

Parameters
[in]blocknext samples, of any number: they are taken block_size() at a time.
Returns
one output sample for each sample of block.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).

Definition at line 14422 of file fft.H.

References ah_runtime_error_if, Aleph::Array< T >::append(), Aleph::FFT< Real >::OverlapAdd::block_size_, Aleph::Array< T >::is_empty(), Aleph::FFT< Real >::OverlapAdd::kernel_, offset, Aleph::FFT< Real >::OverlapAdd::process_chunk_impl(), Aleph::Array< T >::reserve(), and Aleph::Array< T >::size().

Referenced by Aleph::FFT< Real >::OverlapAdd::process_block().

◆ process_block() [2/2]

template<std::floating_point Real = double>
template<typename Container >
requires Is_Real_Container<Container>
Array< Real > Aleph::FFT< Real >::OverlapAdd::process_block ( const Container &  block)
inline

process_block() of the samples of a real container.

Template Parameters
Containertype of the container.
Parameters
[in]blocknext samples.
Returns
one output sample for each sample of block.
Exceptions
std::runtime_errorif the convolver is empty (it was moved from).

Definition at line 14542 of file fft.H.

References Aleph::FFT< Real >::build_real_input(), and Aleph::FFT< Real >::OverlapAdd::process_block().

◆ process_chunk_impl()

template<std::floating_point Real = double>
Array< Real > Aleph::FFT< Real >::OverlapAdd::process_chunk_impl ( const Array< Real > &  block,
ThreadPool *  pool,
const size_t  chunk_size 
)
inlineprivate

◆ reset()

template<std::floating_point Real = double>
void Aleph::FFT< Real >::OverlapAdd::reset ( )
inline

Definition at line 14367 of file fft.H.

References Aleph::FFT< Real >::OverlapAdd::clear_overlap().

◆ swap()

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

Member Data Documentation

◆ block_size_

◆ fft_size_

◆ has_pending_tail_

template<std::floating_point Real = double>
bool Aleph::FFT< Real >::OverlapAdd::has_pending_tail_ = false
private

◆ kernel_

◆ kernel_spectrum_

template<std::floating_point Real = double>
Array<Complex> Aleph::FFT< Real >::OverlapAdd::kernel_spectrum_
private

◆ overlap_

◆ plan_

template<std::floating_point Real = double>
Plan Aleph::FFT< Real >::OverlapAdd::plan_
private

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