Eigen-unsupported  5.0.1-dev+284dcc12
 
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
10#ifndef EIGEN_FFT_MODULE_H
11#define EIGEN_FFT_MODULE_H
12
13#include <complex>
14#include <vector>
15#include <map>
16#include "../../Eigen/Core"
17
81
82#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
83
84// IWYU pragma: begin_exports
85
86#ifdef EIGEN_FFTW_DEFAULT
87// FFTW: faster, GPL -- incompatible with Eigen in LGPL form, bigger code size
88#include <fftw3.h>
89#include "src/FFT/fftw_impl.h"
90namespace Eigen {
91// template <typename T> typedef struct internal::fftw_impl default_fft_impl; this does not work
92template <typename T>
93struct default_fft_impl : public internal::fftw_impl<T> {};
94} // namespace Eigen
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"
98namespace Eigen {
99template <typename T>
100struct default_fft_impl : public internal::imklfft::imklfft_impl<T> {};
101} // namespace Eigen
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"
106namespace Eigen {
107template <typename T>
108struct default_fft_impl : public internal::pocketfft_impl<T> {};
109} // namespace Eigen
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"
116namespace Eigen {
117template <typename T>
118struct default_fft_impl : public internal::duccfft_impl<T> {};
119} // namespace Eigen
120#else
121// internal::kissfft_impl: small, free, reasonably efficient default, derived from kissfft
122#include "src/FFT/kissfft_impl.h"
123namespace Eigen {
124template <typename T>
125struct default_fft_impl : public internal::kissfft_impl<T> {};
126} // namespace Eigen
127#endif
128
129// IWYU pragma: end_exports
130
131namespace Eigen {
132
133//
134template <typename T_SrcMat, typename T_FftIfc>
135struct fft_fwd_proxy;
136template <typename T_SrcMat, typename T_FftIfc>
137struct fft_inv_proxy;
138
139namespace internal {
140template <typename T_SrcMat, typename T_FftIfc>
141struct traits<fft_fwd_proxy<T_SrcMat, T_FftIfc> > {
142 typedef typename T_SrcMat::PlainObject ReturnType;
143};
144template <typename T_SrcMat, typename T_FftIfc>
145struct traits<fft_inv_proxy<T_SrcMat, T_FftIfc> > {
146 typedef typename T_SrcMat::PlainObject ReturnType;
147};
148} // namespace internal
149
150template <typename T_SrcMat, typename T_FftIfc>
151struct fft_fwd_proxy : public ReturnByValue<fft_fwd_proxy<T_SrcMat, T_FftIfc> > {
152 typedef DenseIndex Index;
153
154 fft_fwd_proxy(const T_SrcMat& src, T_FftIfc& fft, Index nfft) : m_src(src), m_ifc(fft), m_nfft(nfft) {}
155
156 template <typename T_DestMat>
157 void evalTo(T_DestMat& dst) const;
158
159 Index rows() const { return m_src.rows(); }
160 Index cols() const { return m_src.cols(); }
161
162 protected:
163 const T_SrcMat& m_src;
164 T_FftIfc& m_ifc;
165 Index m_nfft;
166};
167
168template <typename T_SrcMat, typename T_FftIfc>
169struct fft_inv_proxy : public ReturnByValue<fft_inv_proxy<T_SrcMat, T_FftIfc> > {
170 typedef DenseIndex Index;
171
172 fft_inv_proxy(const T_SrcMat& src, T_FftIfc& fft, Index nfft) : m_src(src), m_ifc(fft), m_nfft(nfft) {}
173
174 template <typename T_DestMat>
175 void evalTo(T_DestMat& dst) const;
176
177 Index rows() const { return m_src.rows(); }
178 Index cols() const { return m_src.cols(); }
179
180 protected:
181 const T_SrcMat& m_src;
182 T_FftIfc& m_ifc;
183 Index m_nfft;
184};
185
186template <typename T_Scalar, typename T_Impl = default_fft_impl<T_Scalar> >
187class FFT {
188 public:
189 typedef T_Impl impl_type;
190 typedef DenseIndex Index;
191 typedef typename impl_type::Scalar Scalar;
192 typedef typename impl_type::Complex Complex;
193
194 using Flag = int;
195 static constexpr Flag Default = 0;
196 static constexpr Flag Unscaled = 1;
197 static constexpr Flag HalfSpectrum = 2;
198 static constexpr Flag Speedy = 32767;
199
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");
203 }
204
205 inline bool HasFlag(Flag f) const { return (m_flag & (int)f) == f; }
206
207 inline void SetFlag(Flag f) { m_flag |= (int)f; }
208
209 inline void ClearFlag(Flag f) { m_flag &= (~(int)f); }
210
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);
214 }
215
216 inline void fwd(Complex* dst, const Complex* src, Index nfft) { m_impl.fwd(dst, src, static_cast<int>(nfft)); }
217
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); }
221#endif
222
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
227 else
228 dst.resize(src.size());
229 fwd(&dst[0], &src[0], src.size());
230 }
231
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
239 EIGEN_STATIC_ASSERT(
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)
244
245 if (nfft < 1) nfft = src.size();
246
247 Index dst_size = nfft;
248 if (NumTraits<src_type>::IsComplex == 0 && HasFlag(HalfSpectrum)) {
249 dst_size = (nfft >> 1) + 1;
250 }
251 dst.derived().resize(dst_size);
252
253 if (src.innerStride() != 1 || src.size() < nfft) {
254 Matrix<src_type, 1, Dynamic> tmp;
255 if (src.size() < nfft) {
256 tmp.setZero(nfft);
257 tmp.block(0, 0, src.size(), 1) = src;
258 } else {
259 tmp = src;
260 }
261 if (dst.innerStride() != 1) {
262 Matrix<dst_type, 1, Dynamic> out(1, dst_size);
263 fwd(&out[0], &tmp[0], nfft);
264 dst.derived() = out;
265 } else {
266 fwd(&dst[0], &tmp[0], nfft);
267 }
268 } else {
269 if (dst.innerStride() != 1) {
270 Matrix<dst_type, 1, Dynamic> out(1, dst_size);
271 fwd(&out[0], &src[0], nfft);
272 dst.derived() = out;
273 } else {
274 fwd(&dst[0], &src[0], nfft);
275 }
276 }
277 }
278
279 template <typename InputDerived>
280 inline fft_fwd_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> > fwd(const MatrixBase<InputDerived>& src,
281 Index nfft = -1) {
282 return fft_fwd_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> >(src, *this, nfft);
283 }
284
285 template <typename InputDerived>
286 inline fft_inv_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> > inv(const MatrixBase<InputDerived>& src,
287 Index nfft = -1) {
288 return fft_inv_proxy<MatrixBase<InputDerived>, FFT<T_Scalar, T_Impl> >(src, *this, nfft);
289 }
290
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
294 }
295
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
299 }
300
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
310 EIGEN_STATIC_ASSERT(
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)
315
316 if (nfft < 1) { // automatic FFT size determination
317 if (realfft && HasFlag(HalfSpectrum))
318 nfft = 2 * (src.size() - 1); // assume even fft size
319 else
320 nfft = src.size();
321 }
322 dst.derived().resize(nfft);
323
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());
326
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;
330 if (resize_input) {
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
337 } else {
338 size_t nhead, ntail;
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);
343 if (resize_input <
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);
349 }
350 }
351 } else {
352 tmp = src;
353 }
354
355 if (dst.innerStride() != 1) {
356 Matrix<dst_type, 1, Dynamic> out(1, nfft);
357 inv(&out[0], &tmp[0], nfft);
358 dst.derived() = out;
359 } else {
360 inv(&dst[0], &tmp[0], nfft);
361 }
362 } else {
363 if (dst.innerStride() != 1) {
364 Matrix<dst_type, 1, Dynamic> out(1, nfft);
365 inv(&out[0], &src[0], nfft);
366 dst.derived() = out;
367 } else {
368 inv(&dst[0], &src[0], nfft);
369 }
370 }
371 }
372
373 template <typename Output_>
374 inline void inv(std::vector<Output_>& dst, const std::vector<Complex>& src, Index nfft = -1) {
375 if (nfft < 1)
376 nfft = (NumTraits<Output_>::IsComplex == 0 && HasFlag(HalfSpectrum)) ? 2 * (src.size() - 1) : src.size();
377 dst.resize(nfft);
378 inv(&dst[0], &src[0], nfft);
379 }
380
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);
386 }
387#endif
388
389 inline impl_type& impl() { return m_impl; }
390
391 private:
392 template <typename T_Data>
393 inline void scale(T_Data* x, Scalar s, Index nx) {
394#if 1
395 for (int k = 0; k < nx; ++k) *x++ *= s;
396#else
397 if (((ptrdiff_t)x) & 15)
398 Matrix<T_Data, Dynamic, 1>::Map(x, nx) *= s;
399 else
400 Matrix<T_Data, Dynamic, 1>::MapAligned(x, nx) *= s;
401#endif
402 }
403
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]);
408 }
409
410 impl_type m_impl;
411 int m_flag;
412};
413
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);
418}
419
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);
424}
425
426} // namespace Eigen
427
428#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
429
430#endif
const unsigned int DirectAccessBit
Namespace containing all symbols from the Eigen library.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)