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