17#ifdef CORRELATION_USE_FFTW3
20#include <unordered_map>
21#elifdef CORRELATION_USE_MKL
23#include <unordered_map>
54#if defined(CORRELATION_USE_FFTW3) || defined(CORRELATION_USE_MKL)
62 while (len < target) {
69#ifdef CORRELATION_USE_FFTW3
76 std::unordered_map<size_t, fftw_plan> forward_plans;
77 std::unordered_map<size_t, fftw_plan> backward_plans;
80 FFTWPlanCache() =
default;
81 FFTWPlanCache(
const FFTWPlanCache &) =
delete;
82 FFTWPlanCache &operator=(
const FFTWPlanCache &) =
delete;
83 FFTWPlanCache(FFTWPlanCache &&) =
delete;
84 FFTWPlanCache &operator=(FFTWPlanCache &&) =
delete;
87 for (
auto &[key, plan] : forward_plans) {
88 fftw_destroy_plan(plan);
90 for (
auto &[key, plan] : backward_plans) {
91 fftw_destroy_plan(plan);
96inline void computeFFT(std::vector<std::complex<double>> &data,
bool invert) {
97 const size_t size = data.size();
102 thread_local FFTWPlanCache cache;
103 auto &plans =
invert ? cache.backward_plans : cache.forward_plans;
104 auto iterator = plans.find(size);
105 fftw_plan plan =
nullptr;
106 auto *fftw_data =
reinterpret_cast<fftw_complex *
>(data.data());
108 if (iterator != plans.end()) {
109 plan = iterator->second;
111 static std::mutex planner_mutex;
112 const std::scoped_lock lock(planner_mutex);
114 plan = fftw_plan_dft_1d(
static_cast<int>(size), fftw_data, fftw_data,
115 invert ? FFTW_BACKWARD : FFTW_FORWARD, FFTW_ESTIMATE);
116 if (plan ==
nullptr) {
117 throw std::runtime_error(
"Failed to create FFTW plan for size " + std::to_string(size));
122 fftw_execute_dft(plan, fftw_data, fftw_data);
125 const double scale = 1.0 /
static_cast<double>(size);
126 for (
auto &val : data) {
132#elifdef CORRELATION_USE_MKL
138struct MKLDescriptorCache {
139 std::unordered_map<size_t, DFTI_DESCRIPTOR_HANDLE>
141 std::unordered_map<size_t, DFTI_DESCRIPTOR_HANDLE>
144 ~MKLDescriptorCache() {
145 for (
auto &p : forward_handles) {
146 DftiFreeDescriptor(&p.second);
148 for (
auto &p : backward_handles) {
149 DftiFreeDescriptor(&p.second);
154inline void computeFFT(std::vector<std::complex<double>> &data,
bool invert) {
155 size_t size = data.size();
160 thread_local MKLDescriptorCache cache;
161 auto &handles =
invert ? cache.backward_handles : cache.forward_handles;
162 auto it = handles.find(size);
163 DFTI_DESCRIPTOR_HANDLE handle =
nullptr;
165 if (it != handles.end()) {
169 DftiCreateDescriptor(&handle, DFTI_DOUBLE, DFTI_COMPLEX, 1,
static_cast<MKL_LONG
>(size));
170 if (status != DFTI_NO_ERROR) {
171 throw std::runtime_error(
"MKL DftiCreateDescriptor failed");
174 double scale = 1.0 /
static_cast<double>(size);
175 DftiSetValue(handle, DFTI_BACKWARD_SCALE, scale);
177 status = DftiCommitDescriptor(handle);
178 if (status != DFTI_NO_ERROR) {
179 DftiFreeDescriptor(&handle);
180 throw std::runtime_error(
"MKL DftiCommitDescriptor failed");
182 handles[size] = handle;
187 status = DftiComputeBackward(handle, data.data());
189 status = DftiComputeForward(handle, data.data());
192 if (status != DFTI_NO_ERROR) {
193 throw std::runtime_error(
"MKL DFTI computation failed");
206template <
typename T =
double>
208 size_t size = data.size();
213 if ((size & (size - 1)) != 0) {
214 throw std::invalid_argument(
"FFT length must be a power of 2");
218 for (
size_t i = 1, j = 0; i < size; i++) {
219 size_t bit = size >> 1;
220 for (; (j & bit) != 0; bit >>= 1) {
225 std::swap(data[i], data[j]);
229 constexpr double double_two_pi = 6.283185307179586476925286766559005768;
230 for (
size_t len = 2; len <= size; len <<= 1) {
231 T angle =
static_cast<T
>(double_two_pi /
static_cast<double>(len) * (
invert ? -1 : 1));
232 std::complex<T> wlen(std::cos(angle), std::sin(angle));
233 for (
size_t i = 0; i < size; i += len) {
234 std::complex<T> twiddle(
static_cast<T
>(1.0),
static_cast<T
>(0.0));
235 for (
size_t j = 0; j < len / 2; j++) {
236 std::complex<T> even_val = data[i + j];
237 std::complex<T> odd_val = data[i + j + len / 2] * twiddle;
238 data[i + j] = even_val + odd_val;
239 data[i + j + len / 2] = even_val - odd_val;
246 for (
auto &val : data) {
247 val /=
static_cast<T
>(size);
262template <
typename T = real_t>
263[[nodiscard]]
inline std::vector<T>
autocorrelate(
const std::vector<T> &signal,
264 std::vector<std::complex<T>> &workspace) {
265 const size_t size = signal.size();
273 workspace.assign(len, {
static_cast<T
>(0.0),
static_cast<T
>(0.0)});
274 for (
size_t i = 0; i < size; ++i) {
275 workspace[i] = {signal[i],
static_cast<T
>(0.0)};
279 std::vector<std::complex<double>> double_ws(len);
280 for (
size_t i = 0; i < len; ++i) {
281 double_ws[i] = {
static_cast<double>(workspace[i].real()),
282 static_cast<double>(workspace[i].imag())};
288 for (
size_t i = 0; i < len; ++i) {
289 const double real_part = double_ws[i].real();
290 const double imag_part = double_ws[i].imag();
291 double_ws[i] = {real_part * real_part + imag_part * imag_part, 0.0};
296 std::vector<T> result(size);
297 for (
size_t i = 0; i < size; ++i) {
298 result[i] =
static_cast<T
>(double_ws[i].real());
299 workspace[i] = {
static_cast<T
>(double_ws[i].real()),
static_cast<T
>(double_ws[i].imag())};
312template <
typename T = real_t>
313[[nodiscard]]
inline std::vector<T>
autocorrelate(
const std::vector<T> &signal) {
314 std::vector<std::complex<T>> workspace;
Mathematical and physical constants for the Correlation tool.
Floating point precision type definitions for Correlation.
Defines universal mathematical and physical constants.
size_t findNextGoodFFTSize(size_t target)
Finds the next "good" FFT size >= target.
Definition FFTUtils.hpp:53
std::vector< T > autocorrelate(const std::vector< T > &signal, std::vector< std::complex< T > > &workspace)
Computes the autocorrelation of a 1D real sequence using FFT.
Definition FFTUtils.hpp:263
void computeFFT(std::vector< std::complex< T > > &data, bool invert)
Simple Radix-2 Cooley-Tukey FFT implementation with bit-reversal optimization.
Definition FFTUtils.hpp:207
constexpr Matrix3< T > invert(const Matrix3< T > &matrix)
Computes the inverse of a 3x3 matrix.
Definition LinearAlgebra.hpp:571
bool isGoodFFTSize(size_t n)
Helper to check if a size is a product of small prime factors (2, 3, 5). Highly optimized for FFTW3 a...
Definition FFTUtils.hpp:34