Eigen-unsupported  3.4.1 (git rev 28ded8800c26864e537852658428ab44c8399e87)
 
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_H
11#define EIGEN_FFT_H
12
13#include <complex>
14#include <vector>
15#include <map>
16#include "../../Eigen/Core"
17
18
69
70
71#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
72
73#ifdef EIGEN_FFTW_DEFAULT
74// FFTW: faster, GPL -- incompatible with Eigen in LGPL form, bigger code size
75# include <fftw3.h>
76# include "src/FFT/ei_fftw_impl.h"
77 namespace Eigen {
78 //template <typename T> typedef struct internal::fftw_impl default_fft_impl; this does not work
79 template <typename T> struct default_fft_impl : public internal::fftw_impl<T> {};
80 }
81#elif defined EIGEN_MKL_DEFAULT
82// TODO
83// intel Math Kernel Library: fastest, commercial -- may be incompatible with Eigen in GPL form
84# include "src/FFT/ei_imklfft_impl.h"
85 namespace Eigen {
86 template <typename T> struct default_fft_impl : public internal::imklfft_impl {};
87 }
88#else
89// internal::kissfft_impl: small, free, reasonably efficient default, derived from kissfft
90//
91# include "src/FFT/ei_kissfft_impl.h"
92 namespace Eigen {
93 template <typename T>
94 struct default_fft_impl : public internal::kissfft_impl<T> {};
95 }
96#endif
97
98namespace Eigen {
99
100
101//
102template<typename T_SrcMat,typename T_FftIfc> struct fft_fwd_proxy;
103template<typename T_SrcMat,typename T_FftIfc> struct fft_inv_proxy;
104
105namespace internal {
106template<typename T_SrcMat,typename T_FftIfc>
107struct traits< fft_fwd_proxy<T_SrcMat,T_FftIfc> >
108{
109 typedef typename T_SrcMat::PlainObject ReturnType;
110};
111template<typename T_SrcMat,typename T_FftIfc>
112struct traits< fft_inv_proxy<T_SrcMat,T_FftIfc> >
113{
114 typedef typename T_SrcMat::PlainObject ReturnType;
115};
116}
117
118template<typename T_SrcMat,typename T_FftIfc>
119struct fft_fwd_proxy
120 : public ReturnByValue<fft_fwd_proxy<T_SrcMat,T_FftIfc> >
121{
122 typedef DenseIndex Index;
123
124 fft_fwd_proxy(const T_SrcMat& src,T_FftIfc & fft, Index nfft) : m_src(src),m_ifc(fft), m_nfft(nfft) {}
125
126 template<typename T_DestMat> void evalTo(T_DestMat& dst) const;
127
128 Index rows() const { return m_src.rows(); }
129 Index cols() const { return m_src.cols(); }
130protected:
131 const T_SrcMat & m_src;
132 T_FftIfc & m_ifc;
133 Index m_nfft;
134};
135
136template<typename T_SrcMat,typename T_FftIfc>
137struct fft_inv_proxy
138 : public ReturnByValue<fft_inv_proxy<T_SrcMat,T_FftIfc> >
139{
140 typedef DenseIndex Index;
141
142 fft_inv_proxy(const T_SrcMat& src,T_FftIfc & fft, Index nfft) : m_src(src),m_ifc(fft), m_nfft(nfft) {}
143
144 template<typename T_DestMat> void evalTo(T_DestMat& dst) const;
145
146 Index rows() const { return m_src.rows(); }
147 Index cols() const { return m_src.cols(); }
148protected:
149 const T_SrcMat & m_src;
150 T_FftIfc & m_ifc;
151 Index m_nfft;
152};
153
154
155template <typename T_Scalar,
156 typename T_Impl=default_fft_impl<T_Scalar> >
157class FFT
158{
159 public:
160 typedef T_Impl impl_type;
161 typedef DenseIndex Index;
162 typedef typename impl_type::Scalar Scalar;
163 typedef typename impl_type::Complex Complex;
164
165 typedef int Flag;
166 static const Flag Default = 0;
167 static const Flag Unscaled = 1;
168 static const Flag HalfSpectrum = 2;
169 static const Flag Speedy = 32767;
170
171 FFT(const impl_type& impl = impl_type(), Flag flags = Default) : m_impl(impl), m_flag(flags)
172 {
173 eigen_assert((flags == Default || flags == Unscaled || flags == HalfSpectrum || flags == Speedy) && "invalid flags argument");
174 }
175
176 inline
177 bool HasFlag(Flag f) const { return (m_flag & (int)f) == f;}
178
179 inline
180 void SetFlag(Flag f) { m_flag |= (int)f;}
181
182 inline
183 void ClearFlag(Flag f) { m_flag &= (~(int)f);}
184
185 inline
186 void fwd( Complex * dst, const Scalar * src, Index nfft)
187 {
188 m_impl.fwd(dst,src,static_cast<int>(nfft));
189 if ( HasFlag(HalfSpectrum) == false)
190 ReflectSpectrum(dst,nfft);
191 }
192
193 inline
194 void fwd( Complex * dst, const Complex * src, Index nfft)
195 {
196 m_impl.fwd(dst,src,static_cast<int>(nfft));
197 }
198
199 /*
200 inline
201 void fwd2(Complex * dst, const Complex * src, int n0,int n1)
202 {
203 m_impl.fwd2(dst,src,n0,n1);
204 }
205 */
206
207 template <typename _Input>
208 inline
209 void fwd( std::vector<Complex> & dst, const std::vector<_Input> & src)
210 {
211 if ( NumTraits<_Input>::IsComplex == 0 && HasFlag(HalfSpectrum) )
212 dst.resize( (src.size()>>1)+1); // half the bins + Nyquist bin
213 else
214 dst.resize(src.size());
215 fwd(&dst[0],&src[0],src.size());
216 }
217
218 template<typename InputDerived, typename ComplexDerived>
219 inline
220 void fwd( MatrixBase<ComplexDerived> & dst, const MatrixBase<InputDerived> & src, Index nfft=-1)
221 {
222 typedef typename ComplexDerived::Scalar dst_type;
223 typedef typename InputDerived::Scalar src_type;
224 EIGEN_STATIC_ASSERT_VECTOR_ONLY(InputDerived)
225 EIGEN_STATIC_ASSERT_VECTOR_ONLY(ComplexDerived)
226 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ComplexDerived,InputDerived) // size at compile-time
227 EIGEN_STATIC_ASSERT((internal::is_same<dst_type, Complex>::value),
228 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
229 EIGEN_STATIC_ASSERT(int(InputDerived::Flags)&int(ComplexDerived::Flags)&DirectAccessBit,
230 THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES)
231
232 if (nfft<1)
233 nfft = src.size();
234
235 if ( NumTraits< src_type >::IsComplex == 0 && HasFlag(HalfSpectrum) )
236 dst.derived().resize( (nfft>>1)+1);
237 else
238 dst.derived().resize(nfft);
239
240 if ( src.innerStride() != 1 || src.size() < nfft ) {
241 Matrix<src_type,1,Dynamic> tmp;
242 if (src.size()<nfft) {
243 tmp.setZero(nfft);
244 tmp.block(0,0,src.size(),1 ) = src;
245 }else{
246 tmp = src;
247 }
248 fwd( &dst[0],&tmp[0],nfft );
249 }else{
250 fwd( &dst[0],&src[0],nfft );
251 }
252 }
253
254 template<typename InputDerived>
255 inline
256 fft_fwd_proxy< MatrixBase<InputDerived>, FFT<T_Scalar,T_Impl> >
257 fwd( const MatrixBase<InputDerived> & src, Index nfft=-1)
258 {
259 return fft_fwd_proxy< MatrixBase<InputDerived> ,FFT<T_Scalar,T_Impl> >( src, *this,nfft );
260 }
261
262 template<typename InputDerived>
263 inline
264 fft_inv_proxy< MatrixBase<InputDerived>, FFT<T_Scalar,T_Impl> >
265 inv( const MatrixBase<InputDerived> & src, Index nfft=-1)
266 {
267 return fft_inv_proxy< MatrixBase<InputDerived> ,FFT<T_Scalar,T_Impl> >( src, *this,nfft );
268 }
269
270 inline
271 void inv( Complex * dst, const Complex * src, Index nfft)
272 {
273 m_impl.inv( dst,src,static_cast<int>(nfft) );
274 if ( HasFlag( Unscaled ) == false)
275 scale(dst,Scalar(1./nfft),nfft); // scale the time series
276 }
277
278 inline
279 void inv( Scalar * dst, const Complex * src, Index nfft)
280 {
281 m_impl.inv( dst,src,static_cast<int>(nfft) );
282 if ( HasFlag( Unscaled ) == false)
283 scale(dst,Scalar(1./nfft),nfft); // scale the time series
284 }
285
286 template<typename OutputDerived, typename ComplexDerived>
287 inline
288 void inv( MatrixBase<OutputDerived> & dst, const MatrixBase<ComplexDerived> & src, Index nfft=-1)
289 {
290 typedef typename ComplexDerived::Scalar src_type;
291 typedef typename ComplexDerived::RealScalar real_type;
292 typedef typename OutputDerived::Scalar dst_type;
293 const bool realfft= (NumTraits<dst_type>::IsComplex == 0);
294 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OutputDerived)
295 EIGEN_STATIC_ASSERT_VECTOR_ONLY(ComplexDerived)
296 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ComplexDerived,OutputDerived) // size at compile-time
297 EIGEN_STATIC_ASSERT((internal::is_same<src_type, Complex>::value),
298 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
299 EIGEN_STATIC_ASSERT(int(OutputDerived::Flags)&int(ComplexDerived::Flags)&DirectAccessBit,
300 THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES)
301
302 if (nfft<1) { //automatic FFT size determination
303 if ( realfft && HasFlag(HalfSpectrum) )
304 nfft = 2*(src.size()-1); //assume even fft size
305 else
306 nfft = src.size();
307 }
308 dst.derived().resize( nfft );
309
310 // check for nfft that does not fit the input data size
311 Index resize_input= ( realfft && HasFlag(HalfSpectrum) )
312 ? ( (nfft/2+1) - src.size() )
313 : ( nfft - src.size() );
314
315 if ( src.innerStride() != 1 || resize_input ) {
316 // if the vector is strided, then we need to copy it to a packed temporary
317 Matrix<src_type,1,Dynamic> tmp;
318 if ( resize_input ) {
319 size_t ncopy = (std::min)(src.size(),src.size() + resize_input);
320 tmp.setZero(src.size() + resize_input);
321 if ( realfft && HasFlag(HalfSpectrum) ) {
322 // pad at the Nyquist bin
323 tmp.head(ncopy) = src.head(ncopy);
324 tmp(ncopy-1) = real(tmp(ncopy-1)); // enforce real-only Nyquist bin
325 }else{
326 size_t nhead,ntail;
327 nhead = 1+ncopy/2-1; // range [0:pi)
328 ntail = ncopy/2-1; // range (-pi:0)
329 tmp.head(nhead) = src.head(nhead);
330 tmp.tail(ntail) = src.tail(ntail);
331 if (resize_input<0) { //shrinking -- create the Nyquist bin as the average of the two bins that fold into it
332 tmp(nhead) = ( src(nfft/2) + src( src.size() - nfft/2 ) )*real_type(.5);
333 }else{ // expanding -- split the old Nyquist bin into two halves
334 tmp(nhead) = src(nhead) * real_type(.5);
335 tmp(tmp.size()-nhead) = tmp(nhead);
336 }
337 }
338 }else{
339 tmp = src;
340 }
341 inv( &dst[0],&tmp[0], nfft);
342 }else{
343 inv( &dst[0],&src[0], nfft);
344 }
345 }
346
347 template <typename _Output>
348 inline
349 void inv( std::vector<_Output> & dst, const std::vector<Complex> & src,Index nfft=-1)
350 {
351 if (nfft<1)
352 nfft = ( NumTraits<_Output>::IsComplex == 0 && HasFlag(HalfSpectrum) ) ? 2*(src.size()-1) : src.size();
353 dst.resize( nfft );
354 inv( &dst[0],&src[0],nfft);
355 }
356
357
358 /*
359 // TODO: multi-dimensional FFTs
360 inline
361 void inv2(Complex * dst, const Complex * src, int n0,int n1)
362 {
363 m_impl.inv2(dst,src,n0,n1);
364 if ( HasFlag( Unscaled ) == false)
365 scale(dst,1./(n0*n1),n0*n1);
366 }
367 */
368
369 inline
370 impl_type & impl() {return m_impl;}
371 private:
372
373 template <typename T_Data>
374 inline
375 void scale(T_Data * x,Scalar s,Index nx)
376 {
377#if 1
378 for (int k=0;k<nx;++k)
379 *x++ *= s;
380#else
381 if ( ((ptrdiff_t)x) & 15 )
383 else
385 //Matrix<T_Data, Dynamic, Dynamic>::Map(x,nx) * s;
386#endif
387 }
388
389 inline
390 void ReflectSpectrum(Complex * freq, Index nfft)
391 {
392 // create the implicit right-half spectrum (conjugate-mirror of the left-half)
393 Index nhbins=(nfft>>1)+1;
394 for (Index k=nhbins;k < nfft; ++k )
395 freq[k] = conj(freq[nfft-k]);
396 }
397
398 impl_type m_impl;
399 int m_flag;
400};
401
402template<typename T_SrcMat,typename T_FftIfc>
403template<typename T_DestMat> inline
404void fft_fwd_proxy<T_SrcMat,T_FftIfc>::evalTo(T_DestMat& dst) const
405{
406 m_ifc.fwd( dst, m_src, m_nfft);
407}
408
409template<typename T_SrcMat,typename T_FftIfc>
410template<typename T_DestMat> inline
411void fft_inv_proxy<T_SrcMat,T_FftIfc>::evalTo(T_DestMat& dst) const
412{
413 m_ifc.inv( dst, m_src, m_nfft);
414}
415
416}
417
418#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
419
420#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)