EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
echomap::FFTWBuffers Class Reference

Simple RAII wrapper for commonly used C types from FFTW(f). More...

#include <FFTWBuffers.hpp>

Public Member Functions

 FFTWBuffers (std::size_t size, const std::optional< std::span< const float > > &inplace_input)
 Setup contextual buffers for FFTW.
 ~FFTWBuffers () noexcept
 Deallocate the buffers.

Public Attributes

fftwf_complex_fwd *const coefficients
 Complex coefficients (output of real-to-complex FFT).
float * input
 Real-valued inputs, either managed by us or aliased by the user.
std::size_t input_size
 Size of the input vector.

Private Types

using fftwf_complex_fwd = float[2]

Private Attributes

bool is_input_managed
 Are we responsible for the input buffer?

Detailed Description

Simple RAII wrapper for commonly used C types from FFTW(f).

This interface is intentionally "raw" (e.g. direct access to member variables) since it's often used in hot loops. We want to make it as easy as possible for the optimiser.

Definition at line 26 of file FFTWBuffers.hpp.

Member Typedef Documentation

◆ fftwf_complex_fwd

using echomap::FFTWBuffers::fftwf_complex_fwd = float[2]
private

Definition at line 28 of file FFTWBuffers.hpp.

Constructor & Destructor Documentation

◆ FFTWBuffers()

echomap::FFTWBuffers::FFTWBuffers ( std::size_t size,
const std::optional< std::span< const float > > & inplace_input )
explicit

Setup contextual buffers for FFTW.

Parameters
sizeThe number of samples in the input data.
inplace_inputThe array to which the input should be aliased; if not provided, FFTW allocates a real-valued array in the size of the given sample count.
Exceptions
std::runtime_errorOne or more buffers could not be allocated.
Postcondition
The coefficients buffer is non-null.
The input buffer is non-null.

Definition at line 21 of file FFTWBuffers.cpp.

24 :
25 coefficients(fftwf_alloc_complex(size / 2 + 1)),
26 input(inplace_input.has_value() ? const_cast<float*>(inplace_input->data()) : fftwf_alloc_real(size)),
27 input_size(size),
28 is_input_managed(!inplace_input.has_value())
29{
30 /*
31 * It's not very nice, but we cast away the const on the input buffer as required by the FFTW C API. We know that
32 * in-place transforms are not going to take place, so it doesn't make sense to take optional<float*> (mutable
33 * pointer) as a constructor parameter.
34 */
35
36 if (coefficients == nullptr)
37 throw std::runtime_error("Failed to create FFTW coefficients buffer.");
38
39 if (!inplace_input.has_value() && input == nullptr) {
40 fftwf_free(coefficients);
41 throw std::runtime_error("Failed to create FFTW input buffer.");
42 }
43
44 assert(input != nullptr);
45 assert(coefficients != nullptr);
46}
bool is_input_managed
Are we responsible for the input buffer?
std::size_t input_size
Size of the input vector.
fftwf_complex_fwd *const coefficients
Complex coefficients (output of real-to-complex FFT).
float * input
Real-valued inputs, either managed by us or aliased by the user.
T has_value(T... args)

◆ ~FFTWBuffers()

echomap::FFTWBuffers::~FFTWBuffers ( )
noexcept

Deallocate the buffers.

Definition at line 48 of file FFTWBuffers.cpp.

49{
50 if (is_input_managed && input != nullptr)
51 fftwf_free(input);
52
53 if (coefficients != nullptr)
54 fftwf_free(coefficients);
55}

Member Data Documentation

◆ coefficients

fftwf_complex_fwd* const echomap::FFTWBuffers::coefficients

Complex coefficients (output of real-to-complex FFT).

Definition at line 31 of file FFTWBuffers.hpp.

◆ input

float* echomap::FFTWBuffers::input

Real-valued inputs, either managed by us or aliased by the user.

Definition at line 32 of file FFTWBuffers.hpp.

◆ input_size

std::size_t echomap::FFTWBuffers::input_size

Size of the input vector.

Definition at line 33 of file FFTWBuffers.hpp.

◆ is_input_managed

bool echomap::FFTWBuffers::is_input_managed
private

Are we responsible for the input buffer?

Definition at line 58 of file FFTWBuffers.hpp.


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