1// This file is part of Eigen, a lightweight C++ template library
4// Copyright (C) 2009 Mark Borgerding mark a borgerding net
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10#ifndef EIGEN_FFT_MODULE_H
11#define EIGEN_FFT_MODULE_H
16#include "../../Eigen/Core"
19 * \defgroup FFT_Module Fast Fourier Transform module
22 * #include <unsupported/Eigen/FFT>
25 * This module provides Fast Fourier transformation, with a configurable backend
28 * The default implementation is based on kissfft. It is a small, free, and
29 * reasonably efficient default.
31 * There are currently four implementation backend:
33 * - kissfft(https://github.com/mborgerding/kissfft) : Simple and not so fast, BSD-3-Clause.
34 * It is a mixed-radix Fast Fourier Transform based up on the principle, "Keep It Simple, Stupid."
35 * Notice that:kissfft fails to handle "atypically-sized" inputs(i.e., sizes with large factors),a workaround is using
37 * - fftw (http://www.fftw.org) : faster, GPL -- incompatible with Eigen in LGPL form, bigger code size.
38 * - MKL (https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-download.html) : fastest, free -- may be
39 * incompatible with Eigen in GPL form.
40 * - PocketFFT/DUCC (https://gitlab.mpcdf.mpg.de/mtr/pocketfft, https://gitlab.mpcdf.mpg.de/mtr/ducc) : faster than kissfft, BSD 3-clause.
41 * It is a heavily modified implementation of FFTPack, with the following advantages:
42 * 1.strictly C++11 compliant
43 * 2.more accurate twiddle factor computation
44 * 3.very fast plan generation
45 * 4.worst case complexity for transform sizes with large prime factors is N*log(N), because Bluestein's algorithm is
46 * According to the author, DUCC contains the "evolution" of pocketfft, though the interface is very similar.
47 * used for these cases
49 * \section FFTDesign Design
51 * The following design decisions were made concerning scaling and
52 * half-spectrum for real FFT.
54 * The intent is to facilitate generic programming and ease migrating code
56 * We think the default behavior of Eigen/FFT should favor correctness and
57 * generality over speed. Of course, the caller should be able to "opt-out" from this
58 * behavior and get the speed increase if they want it.
61 * Other libraries (FFTW,IMKL,KISSFFT) do not perform scaling, so there
62 * is a constant gain incurred after the forward&inverse transforms , so
63 * IFFT(FFT(x)) = Kx; this is done to avoid a vector-by-value multiply.
64 * The downside is that algorithms that worked correctly in Matlab/octave
65 * don't behave the same way once implemented in C++.
67 * How Eigen/FFT differs: invertible scaling is performed so IFFT( FFT(x) ) = x.
69 * 2) Real FFT half-spectrum
70 * Other libraries use only half the frequency spectrum (plus one extra
71 * sample for the Nyquist bin) for a real FFT, the other half is the
72 * conjugate-symmetric of the first half. This saves them a copy and some
73 * memory. The downside is the caller needs to have special logic for the
74 * number of bins in complex vs real.
76 * How Eigen/FFT differs: The full spectrum is returned from the forward
77 * transform. This facilitates generic template programming by obviating
78 * separate specializations for real vs complex. On the inverse
79 * transform, only half the spectrum is actually used if the output type is real.
82#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
84// IWYU pragma: begin_exports
86#ifdef EIGEN_FFTW_DEFAULT
87// FFTW: faster, GPL -- incompatible with Eigen in LGPL form, bigger code size
89#include "src/FFT/fftw_impl.h"
91// template <typename T> typedef struct internal::fftw_impl default_fft_impl; this does not work
93struct default_fft_impl : public internal::fftw_impl<T> {};
95#elif defined EIGEN_MKL_DEFAULT
96// intel Math Kernel Library: fastest, free -- may be incompatible with Eigen in GPL form
97#include "src/FFT/imklfft_impl.h"
100struct default_fft_impl : public internal::imklfft::imklfft_impl<T> {};
102#elif defined EIGEN_POCKETFFT_DEFAULT
103// internal::pocketfft_impl: a heavily modified implementation of FFTPack, with many advantages.
104#include <pocketfft_hdronly.h>
105#include "src/FFT/pocketfft_impl.h"
108struct default_fft_impl : public internal::pocketfft_impl<T> {};
110#elif defined EIGEN_DUCCFFT_DEFAULT
111#include <ducc0/fft/fft.h>
112#include <ducc0/infra/string_utils.h>
113#include <ducc0/fft/fft.h>
114#include <ducc0/fft/fftnd_impl.h>
115#include "src/FFT/duccfft_impl.h"
118struct default_fft_impl : public internal::duccfft_impl<T> {};
121// internal::kissfft_impl: small, free, reasonably efficient default, derived from kissfft
122#include "src/FFT/kissfft_impl.h"
125struct default_fft_impl : public internal::kissfft_impl<T> {};
129// IWYU pragma: end_exports
134template <typename T_SrcMat, typename T_FftIfc>
136template <typename T_SrcMat, typename T_FftIfc>
140template <typename T_SrcMat, typename T_FftIfc>
141struct traits<fft_fwd_proxy<T_SrcMat, T_FftIfc> > {
142 typedef typename T_SrcMat::PlainObject ReturnType;
144template <typename T_SrcMat, typename T_FftIfc>
145struct traits<fft_inv_proxy<T_SrcMat, T_FftIfc> > {
146 typedef typename T_SrcMat::PlainObject ReturnType;
148} // namespace internal
150template <typename T_SrcMat, typename T_FftIfc>
151struct fft_fwd_proxy : public ReturnByValue<fft_fwd_proxy<T_SrcMat, T_FftIfc> > {
152 typedef DenseIndex Index;
154 fft_fwd_proxy(const T_SrcMat& src, T_FftIfc& fft, Index nfft) : m_src(src), m_ifc(fft), m_nfft(nfft) {}
156 template <typename T_DestMat>
157 void evalTo(T_DestMat& dst) const;
159 Index rows() const { return m_src.rows(); }
160 Index cols() const { return m_src.cols(); }
163 const T_SrcMat& m_src;
168template <typename T_SrcMat, typename T_FftIfc>
169struct fft_inv_proxy : public ReturnByValue<fft_inv_proxy<T_SrcMat, T_FftIfc> > {
170 typedef DenseIndex Index;
172 fft_inv_proxy(const T_SrcMat& src, T_FftIfc& fft, Index nfft) : m_src(src), m_ifc(fft), m_nfft(nfft) {}
174 template <typename T_DestMat>
175 void evalTo(T_DestMat& dst) const;
177 Index rows() const { return m_src.rows(); }
178 Index cols() const { return m_src.cols(); }
181 const T_SrcMat& m_src;
186template <typename T_Scalar, typename T_Impl = default_fft_impl<T_Scalar> >
189 typedef T_Impl impl_type;
190 typedef DenseIndex Index;
191 typedef typename impl_type::Scalar Scalar;
192 typedef typename impl_type::Complex Complex;
195 static constexpr Flag Default = 0;
196 static constexpr Flag Unscaled = 1;
197 static constexpr Flag HalfSpectrum = 2;
198 static constexpr Flag Speedy = 32767;
200 FFT(const impl_type& impl = impl_type(), Flag flags = Default) : m_impl(impl), m_flag(flags) {
201 eigen_assert((flags == Default || flags == Unscaled || flags == HalfSpectrum || flags == Speedy) &&
202 "invalid flags argument");
205 inline bool HasFlag(Flag f) const { return (m_flag & (int)f) == f; }
207 inline void SetFlag(Flag f) { m_flag |= (int)f; }
209 inline void ClearFlag(Flag f) { m_flag &= (~(int)f); }
211 inline void fwd(Complex* dst, const Scalar* src, Index nfft) {
212 m_impl.fwd(dst, src, static_cast<int>(nfft));
213 if (HasFlag(HalfSpectrum) == false) ReflectSpectrum(dst, nfft);
216 inline void fwd(Complex* dst, const Complex* src, Index nfft) { m_impl.fwd(dst, src, static_cast<int>(nfft)); }
218#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_DUCCFFT_DEFAULT || \
219 defined EIGEN_MKL_DEFAULT
220 inline void fwd2(Complex* dst, const Complex* src, int n0, int n1) { m_impl.fwd2(dst, src, n0, n1); }
223 template <typename Input_>
224 inline void fwd(std::vector<Complex>& dst, const std::vector<Input_>& src) {
225 if (NumTraits<Input_>::IsComplex == 0 && HasFlag(HalfSpectrum))
226 dst.resize((src.size() >> 1) + 1); // half the bins + Nyquist bin
228 dst.resize(src.size());
229 fwd(&dst[0], &src[0], src.size());
232 template <typename InputDerived, typename ComplexDerived>
233 inline void fwd(MatrixBase<ComplexDerived>& dst, const MatrixBase<InputDerived>& src, Index nfft = -1) {
234 typedef typename ComplexDerived::Scalar dst_type;
235 typedef typename InputDerived::Scalar src_type;
236 EIGEN_STATIC_ASSERT_VECTOR_ONLY(InputDerived)
237 EIGEN_STATIC_ASSERT_VECTOR_ONLY(ComplexDerived)
238 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ComplexDerived, InputDerived) // size at compile-time
240 (internal::is_same<dst_type, Complex>::value),
241 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
242 EIGEN_STATIC_ASSERT(int(InputDerived::Flags) & int(ComplexDerived::Flags) & DirectAccessBit,
243 THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES)
245 if (nfft < 1) nfft = src.size();
247 Index dst_size = nfft;
248 if (NumTraits<src_type>::IsComplex == 0 && HasFlag(HalfSpectrum)) {
249 dst_size = (nfft >> 1) + 1;
251 dst.derived().resize(dst_size);
253 if (src.innerStride() != 1 || src.size() < nfft) {
254 Matrix<src_type, 1, Dynamic> tmp;
255 if (src.size() < nfft) {
257 tmp.block(0, 0, src.size(), 1) = src;
261 if (dst.innerStride() != 1) {
262 Matrix<dst_type, 1, Dynamic> out(1, dst_size);
263 fwd(&out[0], &tmp[0], nfft);
266 fwd(&dst[0], &tmp[0], nfft);
269 if (dst.innerStride() != 1) {
270 Matrix<dst_type, 1, Dynamic> out(1, dst_size);
271 fwd(&out[0], &src[0], nfft);
274 fwd(&dst[0], &src[0], nfft);
279 template <typename InputDerived>
280 inline fft_fwd_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> > fwd(const MatrixBase<InputDerived>& src,
282 return fft_fwd_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> >(src, *this, nfft);
285 template <typename InputDerived>
286 inline fft_inv_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> > inv(const MatrixBase<InputDerived>& src,
288 return fft_inv_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> >(src, *this, nfft);
291 inline void inv(Complex* dst, const Complex* src, Index nfft) {
292 m_impl.inv(dst, src, static_cast<int>(nfft));
293 if (HasFlag(Unscaled) == false) scale(dst, Scalar(1. / nfft), nfft); // scale the time series
296 inline void inv(Scalar* dst, const Complex* src, Index nfft) {
297 m_impl.inv(dst, src, static_cast<int>(nfft));
298 if (HasFlag(Unscaled) == false) scale(dst, Scalar(1. / nfft), nfft); // scale the time series
301 template <typename OutputDerived, typename ComplexDerived>
302 inline void inv(MatrixBase<OutputDerived>& dst, const MatrixBase<ComplexDerived>& src, Index nfft = -1) {
303 typedef typename ComplexDerived::Scalar src_type;
304 typedef typename ComplexDerived::RealScalar real_type;
305 typedef typename OutputDerived::Scalar dst_type;
306 const bool realfft = (NumTraits<dst_type>::IsComplex == 0);
307 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OutputDerived)
308 EIGEN_STATIC_ASSERT_VECTOR_ONLY(ComplexDerived)
309 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ComplexDerived, OutputDerived) // size at compile-time
311 (internal::is_same<src_type, Complex>::value),
312 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
313 EIGEN_STATIC_ASSERT(int(OutputDerived::Flags) & int(ComplexDerived::Flags) & DirectAccessBit,
314 THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES)
316 if (nfft < 1) { // automatic FFT size determination
317 if (realfft && HasFlag(HalfSpectrum))
318 nfft = 2 * (src.size() - 1); // assume even fft size
322 dst.derived().resize(nfft);
324 // check for nfft that does not fit the input data size
325 Index resize_input = (realfft && HasFlag(HalfSpectrum)) ? ((nfft / 2 + 1) - src.size()) : (nfft - src.size());
327 if (src.innerStride() != 1 || resize_input) {
328 // if the vector is strided, then we need to copy it to a packed temporary
329 Matrix<src_type, 1, Dynamic> tmp;
331 size_t ncopy = (std::min)(src.size(), src.size() + resize_input);
332 tmp.setZero(src.size() + resize_input);
333 if (realfft && HasFlag(HalfSpectrum)) {
334 // pad at the Nyquist bin
335 tmp.head(ncopy) = src.head(ncopy);
336 tmp(ncopy - 1) = real(tmp(ncopy - 1)); // enforce real-only Nyquist bin
339 nhead = 1 + ncopy / 2 - 1; // range [0:pi)
340 ntail = ncopy / 2 - 1; // range (-pi:0)
341 tmp.head(nhead) = src.head(nhead);
342 tmp.tail(ntail) = src.tail(ntail);
344 0) { // shrinking -- create the Nyquist bin as the average of the two bins that fold into it
345 tmp(nhead) = (src(nfft / 2) + src(src.size() - nfft / 2)) * real_type(.5);
346 } else { // expanding -- split the old Nyquist bin into two halves
347 tmp(nhead) = src(nhead) * real_type(.5);
348 tmp(tmp.size() - nhead) = tmp(nhead);
355 if (dst.innerStride() != 1) {
356 Matrix<dst_type, 1, Dynamic> out(1, nfft);
357 inv(&out[0], &tmp[0], nfft);
360 inv(&dst[0], &tmp[0], nfft);
363 if (dst.innerStride() != 1) {
364 Matrix<dst_type, 1, Dynamic> out(1, nfft);
365 inv(&out[0], &src[0], nfft);
368 inv(&dst[0], &src[0], nfft);
373 template <typename Output_>
374 inline void inv(std::vector<Output_>& dst, const std::vector<Complex>& src, Index nfft = -1) {
376 nfft = (NumTraits<Output_>::IsComplex == 0 && HasFlag(HalfSpectrum)) ? 2 * (src.size() - 1) : src.size();
378 inv(&dst[0], &src[0], nfft);
381#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_DUCCFFT_DEFAULT || \
382 defined EIGEN_MKL_DEFAULT
383 inline void inv2(Complex* dst, const Complex* src, int n0, int n1) {
384 m_impl.inv2(dst, src, n0, n1);
385 if (HasFlag(Unscaled) == false) scale(dst, 1. / (n0 * n1), n0 * n1);
389 inline impl_type& impl() { return m_impl; }
392 template <typename T_Data>
393 inline void scale(T_Data* x, Scalar s, Index nx) {
395 for (int k = 0; k < nx; ++k) *x++ *= s;
397 if (((ptrdiff_t)x) & 15)
398 Matrix<T_Data, Dynamic, 1>::Map(x, nx) *= s;
400 Matrix<T_Data, Dynamic, 1>::MapAligned(x, nx) *= s;
404 inline void ReflectSpectrum(Complex* freq, Index nfft) {
405 // create the implicit right-half spectrum (conjugate-mirror of the left-half)
406 Index nhbins = (nfft >> 1) + 1;
407 for (Index k = nhbins; k < nfft; ++k) freq[k] = conj(freq[nfft - k]);
414template <typename T_SrcMat, typename T_FftIfc>
415template <typename T_DestMat>
416inline void fft_fwd_proxy<T_SrcMat, T_FftIfc>::evalTo(T_DestMat& dst) const {
417 m_ifc.fwd(dst, m_src, m_nfft);
420template <typename T_SrcMat, typename T_FftIfc>
421template <typename T_DestMat>
422inline void fft_inv_proxy<T_SrcMat, T_FftIfc>::evalTo(T_DestMat& dst) const {
423 m_ifc.inv(dst, m_src, m_nfft);
428#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"