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