Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
FFT
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Mark Borgerding mark a borgerding net
5//
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/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_FFT_MODULE_H
12#define EIGEN_FFT_MODULE_H
13
14#include <complex>
15#include <vector>
16#include <map>
17#include "../../Eigen/Core"
18
103
104#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
105
106// IWYU pragma: begin_exports
107
108#ifdef EIGEN_FFTW_DEFAULT
109// FFTW: faster, GPL -- incompatible with Eigen in LGPL form, bigger code size
110#include <fftw3.h>
111#include "src/FFT/fftw_impl.h"
112namespace Eigen {
113template <typename T>
114struct default_fft_impl : public internal::fftw_impl<T> {};
115} // namespace Eigen
116#elif defined EIGEN_MKL_DEFAULT
117// intel Math Kernel Library: fastest, free -- may be incompatible with Eigen in GPL form
118#include "src/FFT/imklfft_impl.h"
119namespace Eigen {
120template <typename T>
121struct default_fft_impl : public internal::imklfft::imklfft_impl<T> {};
122} // namespace Eigen
123#elif defined EIGEN_POCKETFFT_DEFAULT
124// internal::pocketfft_impl: a heavily modified implementation of FFTPack, with many advantages.
125#include <pocketfft_hdronly.h>
126#include "src/FFT/pocketfft_impl.h"
127namespace Eigen {
128template <typename T>
129struct default_fft_impl : public internal::pocketfft_impl<T> {};
130} // namespace Eigen
131#elif defined EIGEN_DUCCFFT_DEFAULT
132#include <ducc0/fft/fft.h>
133#include <ducc0/infra/string_utils.h>
134#include <ducc0/fft/fftnd_impl.h>
135#include "src/FFT/duccfft_impl.h"
136namespace Eigen {
137template <typename T>
138struct default_fft_impl : public internal::duccfft_impl<T> {};
139} // namespace Eigen
140#else
141// internal::kissfft_impl: small, free, reasonably efficient default, derived from kissfft
142#include "src/FFT/kissfft_impl.h"
143namespace Eigen {
144template <typename T>
145struct default_fft_impl : public internal::kissfft_impl<T> {};
146} // namespace Eigen
147#endif
148
149// IWYU pragma: end_exports
150
151namespace Eigen {
152
153//
154template <typename T_SrcMat, typename T_FftIfc>
155struct fft_fwd_proxy;
156template <typename T_SrcMat, typename T_FftIfc>
157struct fft_inv_proxy;
158
159namespace internal {
160template <typename T_SrcMat, typename T_FftIfc>
161struct traits<fft_fwd_proxy<T_SrcMat, T_FftIfc> > {
162 typedef typename T_SrcMat::PlainObject ReturnType;
163};
164template <typename T_SrcMat, typename T_FftIfc>
165struct traits<fft_inv_proxy<T_SrcMat, T_FftIfc> > {
166 typedef typename T_SrcMat::PlainObject ReturnType;
167};
168} // namespace internal
169
170template <typename T_SrcMat, typename T_FftIfc>
171struct fft_fwd_proxy : public ReturnByValue<fft_fwd_proxy<T_SrcMat, T_FftIfc> > {
172 typedef Eigen::Index Index;
173
174 fft_fwd_proxy(const T_SrcMat& src, T_FftIfc& fft, Index nfft) : m_src(src), m_ifc(fft), m_nfft(nfft) {}
175
176 template <typename T_DestMat>
177 void evalTo(T_DestMat& dst) const;
178
179 Index rows() const { return m_src.rows(); }
180 Index cols() const { return m_src.cols(); }
181
182 protected:
183 const T_SrcMat& m_src;
184 T_FftIfc& m_ifc;
185 Index m_nfft;
186};
187
188template <typename T_SrcMat, typename T_FftIfc>
189struct fft_inv_proxy : public ReturnByValue<fft_inv_proxy<T_SrcMat, T_FftIfc> > {
190 typedef Eigen::Index Index;
191
192 fft_inv_proxy(const T_SrcMat& src, T_FftIfc& fft, Index nfft) : m_src(src), m_ifc(fft), m_nfft(nfft) {}
193
194 template <typename T_DestMat>
195 void evalTo(T_DestMat& dst) const;
196
197 Index rows() const { return m_src.rows(); }
198 Index cols() const { return m_src.cols(); }
199
200 protected:
201 const T_SrcMat& m_src;
202 T_FftIfc& m_ifc;
203 Index m_nfft;
204};
205
206namespace internal {
207
208// The transforms are one-dimensional, but a matrix with a dynamic dimension only knows at run
209// time whether that dimension is one, so it cannot be rejected at compile time the way a matrix
210// of known two-dimensional shape can. Only the latter is excluded here.
211template <typename Derived>
212struct fft_accepts_as_vector {
213 static constexpr bool value = bool(Derived::IsVectorAtCompileTime) || int(Derived::RowsAtCompileTime) == Dynamic ||
214 int(Derived::ColsAtCompileTime) == Dynamic;
215};
216
217// Distance between consecutive elements of a one-dimensional operand. The inner stride does not
218// answer this once matrices are accepted: a matrix holding a single row steps along its outer
219// stride, and only a unit distance lets the transform run over the operand's own memory.
220template <typename Derived>
221EIGEN_STRONG_INLINE Index fft_linear_stride(const DenseBase<Derived>& x) {
222 return x.rows() == 1 ? x.derived().colStride() : x.derived().rowStride();
223}
224
225// Resize a one-dimensional destination. Every orientation a compile-time dimension leaves open
226// is settled here: a fixed dimension other than one can only be the long one, and a destination
227// with two dynamic dimensions takes the orientation of the source.
228template <typename Derived>
229EIGEN_STRONG_INLINE void fft_resize_as_vector(DenseBase<Derived>& dst, Index size, bool source_is_row) {
230 // A destination already holding the transform's length in one dimension keeps that shape, which
231 // is the only one a view such as a Map can be given at all.
232 if ((dst.rows() == 1 && dst.cols() == size) || (dst.cols() == 1 && dst.rows() == size)) return;
233
234 constexpr int Rows = int(Derived::RowsAtCompileTime);
235 constexpr int Cols = int(Derived::ColsAtCompileTime);
236 const bool row = Rows == 1 ? true
237 : Cols == 1 ? false
238 : Cols != Dynamic ? true
239 : Rows != Dynamic ? false
240 : source_is_row;
241 dst.derived().resize(row ? 1 : size, row ? size : 1);
242}
243
244} // namespace internal
245
246#define EIGEN_STATIC_ASSERT_FFT_VECTOR(TYPE) \
247 EIGEN_STATIC_ASSERT(internal::fft_accepts_as_vector<TYPE>::value, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
248
249template <typename T_Scalar, typename T_Impl = default_fft_impl<T_Scalar> >
250class FFT {
251 public:
252 typedef T_Impl impl_type;
253 typedef Eigen::Index Index;
254 typedef typename impl_type::Scalar Scalar;
255 typedef typename impl_type::Complex Complex;
256
257 using Flag = int;
258 static constexpr Flag Default = 0;
259 static constexpr Flag Unscaled = 1;
260 static constexpr Flag HalfSpectrum = 2;
261 static constexpr Flag Speedy = 32767;
262
263 FFT(const impl_type& impl = impl_type(), Flag flags = Default) : m_impl(impl), m_flag(flags) {
264 eigen_assert((flags == Default || flags == Unscaled || flags == HalfSpectrum || flags == Speedy) &&
265 "invalid flags argument");
266 }
267
268 inline bool HasFlag(Flag f) const { return (m_flag & (int)f) == f; }
269
270 inline void SetFlag(Flag f) { m_flag |= (int)f; }
271
272 inline void ClearFlag(Flag f) { m_flag &= (~(int)f); }
273
274 inline void fwd(Complex* dst, const Scalar* src, Index nfft) {
275 m_impl.fwd(dst, src, static_cast<int>(nfft));
276 if (HasFlag(HalfSpectrum) == false) ReflectSpectrum(dst, nfft);
277 }
278
279 inline void fwd(Complex* dst, const Complex* src, Index nfft) { m_impl.fwd(dst, src, static_cast<int>(nfft)); }
280
281#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_DUCCFFT_DEFAULT || \
282 defined EIGEN_MKL_DEFAULT
283 inline void fwd2(Complex* dst, const Complex* src, int n0, int n1) { m_impl.fwd2(dst, src, n0, n1); }
284#endif
285
286 template <typename Input_>
287 inline void fwd(std::vector<Complex>& dst, const std::vector<Input_>& src) {
288 EIGEN_IF_CONSTEXPR (NumTraits<Input_>::IsComplex == 0) {
289 if (HasFlag(HalfSpectrum))
290 dst.resize((src.size() >> 1) + 1); // half the bins + Nyquist bin
291 else
292 dst.resize(src.size());
293 } else {
294 dst.resize(src.size());
295 }
296 fwd(&dst[0], &src[0], src.size());
297 }
298
299 template <typename InputDerived, typename ComplexDerived>
300 inline void fwd(DenseBase<ComplexDerived>& dst, const DenseBase<InputDerived>& src, Index nfft = -1) {
301 typedef typename ComplexDerived::Scalar dst_type;
302 typedef typename InputDerived::Scalar src_type;
303 EIGEN_STATIC_ASSERT_FFT_VECTOR(InputDerived)
304 EIGEN_STATIC_ASSERT_FFT_VECTOR(ComplexDerived)
305 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ComplexDerived, InputDerived) // size at compile-time
306 EIGEN_STATIC_ASSERT(
307 (std::is_same<dst_type, Complex>::value),
308 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
309 EIGEN_STATIC_ASSERT(int(InputDerived::Flags) & int(ComplexDerived::Flags) & DirectAccessBit,
310 THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES)
311 eigen_assert((src.rows() == 1 || src.cols() == 1) && "the input of an FFT must be one-dimensional");
312
313 if (nfft < 1) nfft = src.size();
314
315 Index dst_size = nfft;
316 EIGEN_IF_CONSTEXPR (NumTraits<src_type>::IsComplex == 0) {
317 if (HasFlag(HalfSpectrum)) {
318 dst_size = (nfft >> 1) + 1;
319 }
320 }
321 internal::fft_resize_as_vector(dst, dst_size, src.rows() == 1);
322
323 if (internal::fft_linear_stride(src) != 1 || src.size() < nfft) {
324 Matrix<src_type, 1, Dynamic> tmp(1, nfft);
325 const Index copy_size = (std::min)(src.size(), nfft);
326 if (copy_size > 0) {
327 tmp.head(copy_size) = src.reshaped().head(copy_size);
328 }
329 if (copy_size < nfft) tmp.tail(nfft - copy_size).setZero();
330 if (internal::fft_linear_stride(dst) != 1) {
331 Matrix<dst_type, 1, Dynamic> out(1, dst_size);
332 fwd(out.data(), tmp.data(), nfft);
333 dst.derived() = out.reshaped(dst.rows(), dst.cols());
334 } else {
335 fwd(dst.derived().data(), tmp.data(), nfft);
336 }
337 } else {
338 if (internal::fft_linear_stride(dst) != 1) {
339 Matrix<dst_type, 1, Dynamic> out(1, dst_size);
340 fwd(out.data(), src.derived().data(), nfft);
341 dst.derived() = out.reshaped(dst.rows(), dst.cols());
342 } else {
343 fwd(dst.derived().data(), src.derived().data(), nfft);
344 }
345 }
346 }
347
348 template <typename InputDerived>
349 inline fft_fwd_proxy<DenseBase<InputDerived>, FFT<T_Scalar, T_Impl> > fwd(const DenseBase<InputDerived>& src,
350 Index nfft = -1) {
351 return fft_fwd_proxy<DenseBase<InputDerived>, FFT<T_Scalar, T_Impl> >(src, *this, nfft);
352 }
353
354 template <typename InputDerived>
355 inline fft_inv_proxy<DenseBase<InputDerived>, FFT<T_Scalar, T_Impl> > inv(const DenseBase<InputDerived>& src,
356 Index nfft = -1) {
357 return fft_inv_proxy<DenseBase<InputDerived>, FFT<T_Scalar, T_Impl> >(src, *this, nfft);
358 }
359
360 inline void inv(Complex* dst, const Complex* src, Index nfft) {
361 m_impl.inv(dst, src, static_cast<int>(nfft));
362 if (HasFlag(Unscaled) == false) scale(dst, Scalar(1. / nfft), nfft); // scale the time series
363 }
364
365 inline void inv(Scalar* dst, const Complex* src, Index nfft) {
366 m_impl.inv(dst, src, static_cast<int>(nfft));
367 if (HasFlag(Unscaled) == false) scale(dst, Scalar(1. / nfft), nfft); // scale the time series
368 }
369
370 template <typename OutputDerived, typename ComplexDerived>
371 inline void inv(DenseBase<OutputDerived>& dst, const DenseBase<ComplexDerived>& src, Index nfft = -1) {
372 typedef typename ComplexDerived::Scalar src_type;
373 typedef typename ComplexDerived::RealScalar real_type;
374 typedef typename OutputDerived::Scalar dst_type;
375 const bool realfft = (NumTraits<dst_type>::IsComplex == 0);
376 EIGEN_STATIC_ASSERT_FFT_VECTOR(OutputDerived)
377 EIGEN_STATIC_ASSERT_FFT_VECTOR(ComplexDerived)
378 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ComplexDerived, OutputDerived) // size at compile-time
379 EIGEN_STATIC_ASSERT(
380 (std::is_same<src_type, Complex>::value),
381 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
382 EIGEN_STATIC_ASSERT(int(OutputDerived::Flags) & int(ComplexDerived::Flags) & DirectAccessBit,
383 THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES)
384 eigen_assert((src.rows() == 1 || src.cols() == 1) && "the input of an FFT must be one-dimensional");
385
386 if (nfft < 1) { // automatic FFT size determination
387 if (realfft && HasFlag(HalfSpectrum))
388 nfft = 2 * (src.size() - 1); // assume even fft size
389 else
390 nfft = src.size();
391 }
392 internal::fft_resize_as_vector(dst, nfft, src.rows() == 1);
393
394 // check for nfft that does not fit the input data size
395 Index resize_input = (realfft && HasFlag(HalfSpectrum)) ? ((nfft / 2 + 1) - src.size()) : (nfft - src.size());
396
397 if (internal::fft_linear_stride(src) != 1 || resize_input) {
398 // if the vector is strided, then we need to copy it to a packed temporary
399 const auto src_lin = src.reshaped();
400 Matrix<src_type, 1, Dynamic> tmp;
401 if (resize_input) {
402 size_t ncopy = (std::min)(src.size(), src.size() + resize_input);
403 tmp.setZero(src.size() + resize_input);
404 if (realfft && HasFlag(HalfSpectrum)) {
405 // pad at the Nyquist bin
406 tmp.head(ncopy) = src_lin.head(ncopy);
407 tmp(ncopy - 1) = real(tmp(ncopy - 1)); // enforce real-only Nyquist bin
408 } else {
409 size_t nhead, ntail;
410 nhead = 1 + ncopy / 2 - 1; // range [0:pi)
411 ntail = ncopy / 2 - 1; // range (-pi:0)
412 tmp.head(nhead) = src_lin.head(nhead);
413 tmp.tail(ntail) = src_lin.tail(ntail);
414 if (resize_input <
415 0) { // shrinking -- create the Nyquist bin as the average of the two bins that fold into it
416 tmp(nhead) = (src_lin(nfft / 2) + src_lin(src.size() - nfft / 2)) * real_type(.5);
417 } else { // expanding -- split the old Nyquist bin into two halves
418 tmp(nhead) = src_lin(nhead) * real_type(.5);
419 tmp(tmp.size() - nhead) = tmp(nhead);
420 }
421 }
422 } else {
423 tmp = src_lin;
424 }
425
426 if (internal::fft_linear_stride(dst) != 1) {
427 Matrix<dst_type, 1, Dynamic> out(1, nfft);
428 inv(out.data(), tmp.data(), nfft);
429 dst.derived() = out.reshaped(dst.rows(), dst.cols());
430 } else {
431 inv(dst.derived().data(), tmp.data(), nfft);
432 }
433 } else {
434 if (internal::fft_linear_stride(dst) != 1) {
435 Matrix<dst_type, 1, Dynamic> out(1, nfft);
436 inv(out.data(), src.derived().data(), nfft);
437 dst.derived() = out.reshaped(dst.rows(), dst.cols());
438 } else {
439 inv(dst.derived().data(), src.derived().data(), nfft);
440 }
441 }
442 }
443
444 template <typename Output_>
445 inline void inv(std::vector<Output_>& dst, const std::vector<Complex>& src, Index nfft = -1) {
446 if (nfft < 1)
447 nfft = (NumTraits<Output_>::IsComplex == 0 && HasFlag(HalfSpectrum)) ? 2 * (src.size() - 1) : src.size();
448 dst.resize(nfft);
449 inv(&dst[0], &src[0], nfft);
450 }
451
452#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT || defined EIGEN_DUCCFFT_DEFAULT || \
453 defined EIGEN_MKL_DEFAULT
454 inline void inv2(Complex* dst, const Complex* src, int n0, int n1) {
455 m_impl.inv2(dst, src, n0, n1);
456 if (HasFlag(Unscaled) == false) scale(dst, Scalar(1) / (n0 * n1), n0 * n1);
457 }
458#endif
459
460 inline impl_type& impl() { return m_impl; }
461
462 private:
463 template <typename T_Data>
464 inline void scale(T_Data* x, Scalar s, Index nx) {
465 for (int k = 0; k < nx; ++k) *x++ *= s;
466 }
467
468 inline void ReflectSpectrum(Complex* freq, Index nfft) {
469 // create the implicit right-half spectrum (conjugate-mirror of the left-half)
470 Index nhbins = (nfft >> 1) + 1;
471 for (Index k = nhbins; k < nfft; ++k) freq[k] = conj(freq[nfft - k]);
472 }
473
474 impl_type m_impl;
475 int m_flag;
476};
477
478template <typename T_SrcMat, typename T_FftIfc>
479template <typename T_DestMat>
480inline void fft_fwd_proxy<T_SrcMat, T_FftIfc>::evalTo(T_DestMat& dst) const {
481 m_ifc.fwd(dst, m_src, m_nfft);
482}
483
484template <typename T_SrcMat, typename T_FftIfc>
485template <typename T_DestMat>
486inline void fft_inv_proxy<T_SrcMat, T_FftIfc>::evalTo(T_DestMat& dst) const {
487 m_ifc.inv(dst, m_src, m_nfft);
488}
489
490} // namespace Eigen
491
492#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
493
494#endif // EIGEN_FFT_MODULE_H
constexpr unsigned int DirectAccessBit
Namespace containing all symbols from the Eigen library.