PCL
FFT1D.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/FFT1D.h - Released 2024-12-28T16:53:48Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_FFT1D_h
53 #define __PCL_FFT1D_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Complex.h>
60 #include <pcl/Vector.h>
61 
62 namespace pcl
63 {
64 
65 // ----------------------------------------------------------------------------
66 
67 class FFT1DBase
68 {
69 protected:
70 
71  static int OptimizedLength( int, fcomplex* );
72  static int OptimizedLength( int, dcomplex* );
73  static int OptimizedLength( int, float* );
74  static int OptimizedLength( int, double* );
75 
76  static void* Create( int, fcomplex* );
77  static void* Create( int, dcomplex* );
78  static void* Create( int, float* );
79  static void* Create( int, double* );
80 
81  static void* CreateInv( int, fcomplex* );
82  static void* CreateInv( int, dcomplex* );
83  static void* CreateInv( int, float* );
84  static void* CreateInv( int, double* );
85 
86  static void Destroy( void* );
87 
88  static void Transform( void*, fcomplex*, const fcomplex* );
89  static void Transform( void*, dcomplex*, const dcomplex* );
90  static void InverseTransform( void*, fcomplex*, const fcomplex* );
91  static void InverseTransform( void*, dcomplex*, const dcomplex* );
92  static void Transform( void*, fcomplex*, const float* );
93  static void Transform( void*, dcomplex*, const double* );
94  static void InverseTransform( void*, float*, const fcomplex* );
95  static void InverseTransform( void*, double*, const dcomplex* );
96 };
97 
98 // ----------------------------------------------------------------------------
99 
109 #define PCL_FFT_FORWARD -1
110 
116 #define PCL_FFT_BACKWARD +1
117 
118 // ----------------------------------------------------------------------------
119 
129 template <typename T>
130 class PCL_CLASS AbstractFFT : public FFT1DBase
131 {
132 public:
133 
137  using scalar = T;
138 
143 
148 
153 
159 
163  AbstractFFT( int length )
164  : m_length( length )
165  {
166  }
167 
172  virtual ~AbstractFFT()
173  {
174  Release();
175  }
176 
182  int Length() const
183  {
184  return m_length;
185  }
186 
197  transform DFT() const
198  {
199  return m_dft;
200  }
201 
213  {
214  return m_dft;
215  }
216 
228  const complex* operator *() const
229  {
230  return *m_dft;
231  }
232 
245  {
246  return *m_dft;
247  }
248 
253  virtual void Release()
254  {
255  if ( m_handle != nullptr )
256  this->Destroy( m_handle ), m_handle = nullptr;
257  if ( m_handleInv != nullptr )
258  this->Destroy( m_handleInv ), m_handleInv = nullptr;
259  m_dft = transform();
260  }
261 
262 private:
263 
264  int m_length;
265 
266 protected:
267 
268  mutable void* m_handle = nullptr; // Opaque pointers to internal control structures
269  mutable void* m_handleInv = nullptr;
270  transform m_dft; // DFT of complex or real data
271 };
272 
273 // ----------------------------------------------------------------------------
274 
275 #define m_handle this->m_handle
276 #define m_handleInv this->m_handleInv
277 #define m_dft this->m_dft
278 #define m_length this->Length()
279 
280 // ----------------------------------------------------------------------------
281 
294 template <typename T>
295 class PCL_CLASS GenericFFT : public AbstractFFT<T>
296 {
297 public:
298 
303 
307  using scalar = typename base::scalar;
308 
312  using complex = typename base::complex;
313 
317  using vector = typename base::vector;
318 
323 
328  using transform = typename base::transform;
329 
344  GenericFFT( int n )
345  : base( n )
346  {
347  }
348 
352  ~GenericFFT() override
353  {
354  }
355 
366  {
367  if ( m_dft.IsEmpty() )
368  m_dft = transform( m_length );
369  if ( m_handle == nullptr )
370  m_handle = this->Create( m_length, static_cast<complex*>( nullptr ) );
371  this->Transform( m_handle, *m_dft, x );
372  return *this;
373  }
374 
388  {
389  if ( m_dft.IsEmpty() )
390  throw Error( "Invalid out-of-place inverse FFT: No FFT has been performed." );
391  if ( m_handleInv == nullptr )
392  m_handleInv = this->CreateInv( m_length, static_cast<complex*>( nullptr ) );
393  this->InverseTransform( m_handleInv, y, *m_dft );
394  return *this;
395  }
396 
406  {
407  PCL_PRECONDITION( x.Length() >= m_length )
408  if ( x.Length() < m_length )
409  throw Error( "Invalid FFT input vector length." );
410  return operator <<( *x );
411  }
412 
426  {
427  if ( y.Length() < m_length )
428  throw Error( "Invalid FFT output vector length." );
429  return operator >>( *y );
430  }
431 
462  GenericFFT& operator()( complex* y, const complex* x, int dir = PCL_FFT_FORWARD ) const
463  {
464  if ( dir == PCL_FFT_BACKWARD )
465  {
466  if ( m_handleInv == nullptr )
467  m_handleInv = this->CreateInv( m_length, static_cast<complex*>( nullptr ) );
468  this->InverseTransform( m_handleInv, y, x );
469  }
470  else // PCL_FFT_FORWARD
471  {
472  if ( m_handle == nullptr )
473  m_handle = this->Create( m_length, static_cast<complex*>( nullptr ) );
474  this->Transform( m_handle, y, x );
475  }
476  return const_cast<GenericFFT&>( *this );
477  }
478 
486  static int OptimizedLength( int n )
487  {
488  return FFT1DBase::OptimizedLength( n, static_cast<complex*>( nullptr ) );
489  }
490 };
491 
492 // ----------------------------------------------------------------------------
493 
508 template <typename T>
509 class PCL_CLASS GenericRealFFT : public AbstractFFT<T>
510 {
511 public:
512 
517 
521  using scalar = typename base::scalar;
522 
526  using complex = typename base::complex;
527 
531  using vector = typename base::vector;
532 
537 
542  using transform = typename base::transform;
543 
562  GenericRealFFT( int length )
563  : base( length )
564  {
565  PCL_PRECONDITION( (length & 1) == 0 )
566  }
567 
571  ~GenericRealFFT() override
572  {
573  }
574 
585  {
586  if ( m_dft.IsEmpty() )
587  m_dft = transform( m_length/2 + 1 );
588  if ( m_handle == nullptr )
589  m_handle = this->Create( m_length, static_cast<scalar*>( nullptr ) );
590  this->Transform( m_handle, *m_dft, x );
591  return *this;
592  }
593 
607  {
608  if ( m_dft.IsEmpty() )
609  throw Error( "Invalid out-of-place inverse FFT: No FFT has been performed." );
610  if ( m_handleInv == nullptr )
611  m_handleInv = this->CreateInv( m_length, static_cast<scalar*>( nullptr ) );
612  this->InverseTransform( m_handleInv, y, *m_dft );
613  return *this;
614  }
615 
625  {
626  if ( x.Length() < m_length )
627  throw Error( "Invalid FFT input vector length." );
628  return operator <<( *x );
629  }
630 
644  {
645  if ( y.Length() < m_length )
646  throw Error( "Invalid FFT output vector length." );
647  return operator >>( *y );
648  }
649 
670  GenericRealFFT& operator()( complex* y, const scalar* x ) const
671  {
672  if ( m_handle == nullptr )
673  m_handle = this->Create( m_length, static_cast<scalar*>( nullptr ) );
674  this->Transform( m_handle, y, x );
675  return const_cast<GenericRealFFT&>( *this );
676  }
677 
701  GenericRealFFT& operator()( scalar* y, const complex* x ) const
702  {
703  if ( m_handleInv == nullptr )
704  m_handleInv = this->CreateInv( m_length, static_cast<scalar*>( nullptr ) );
705  this->InverseTransform( m_handleInv, y, x );
706  return const_cast<GenericRealFFT&>( *this );
707  }
708 
716  static int OptimizedLength( int n )
717  {
718  return FFT1DBase::OptimizedLength( n, static_cast<scalar*>( nullptr ) );
719  }
720 };
721 
722 // ----------------------------------------------------------------------------
723 
724 #undef m_handle
725 #undef m_handleInv
726 #undef m_dft
727 #undef m_length
728 
729 // ----------------------------------------------------------------------------
730 
731 #ifndef __PCL_NO_FFT1D_INSTANTIATE
732 
744 using FFFT = GenericFFT<float>;
745 
753 using DFFT = GenericFFT<double>;
754 
762 using FRealFFT = GenericRealFFT<float>;
763 
771 using DRealFFT = GenericRealFFT<double>;
772 
781 using FFT = FFFT;
782 
791 using RealFFT = FRealFFT;
792 
793 #endif // __PCL_NO_FFT1D_INSTANTIATE
794 
795 // ----------------------------------------------------------------------------
796 
797 } // pcl
798 
799 #endif // __PCL_FFT1D_h
800 
801 // ----------------------------------------------------------------------------
802 // EOF pcl/FFT1D.h - Released 2024-12-28T16:53:48Z
Abstract base class of all fast Fourier transform classes.
Definition: FFT1D.h:131
int Length() const
Definition: FFT1D.h:182
transform & DFT()
Definition: FFT1D.h:212
virtual void Release()
Definition: FFT1D.h:253
AbstractFFT(int length)
Definition: FFT1D.h:163
transform DFT() const
Definition: FFT1D.h:197
virtual ~AbstractFFT()
Definition: FFT1D.h:172
Generic complex number.
Definition: Complex.h:84
Fast Fourier transform of 64-bit floating point complex data.
Fast Fourier transform of 64-bit floating point real data.
A simple exception with an associated error message.
Definition: Exception.h:239
Fast Fourier transform of 32-bit floating point complex data.
Fast Fourier transform of 32-bit floating point complex data.
Fast Fourier transform of 32-bit floating point real data.
Generic fast Fourier transform of complex data.
Definition: FFT1D.h:296
GenericFFT & operator()(complex *y, const complex *x, int dir=PCL_FFT_FORWARD) const
Definition: FFT1D.h:462
static int OptimizedLength(int n)
Definition: FFT1D.h:486
GenericFFT(int n)
Definition: FFT1D.h:344
~GenericFFT() override
Definition: FFT1D.h:352
Generic fast Fourier transform of real data.
Definition: FFT1D.h:510
static int OptimizedLength(int n)
Definition: FFT1D.h:716
GenericRealFFT(int length)
Definition: FFT1D.h:562
GenericRealFFT & operator()(complex *y, const scalar *x) const
Definition: FFT1D.h:670
GenericRealFFT & operator()(scalar *y, const complex *x) const
Definition: FFT1D.h:701
~GenericRealFFT() override
Definition: FFT1D.h:571
int Length() const noexcept
Definition: Vector.h:1802
Fast Fourier transform of 32-bit floating point real data.
A complex number whose components are 64-bit floating point real numbers.
A complex number whose components are 32-bit floating point real numbers.
Array< T, A > & operator<<(Array< T, A > &x, const V &v)
Definition: Array.h:2313
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:562
Console & operator>>(Console &o, char &c)
Definition: Console.h:807
#define PCL_FFT_BACKWARD
Indicates an inverse Fourier transform.
Definition: FFT1D.h:116
#define PCL_FFT_FORWARD
Indicates a Fourier transform.
Definition: FFT1D.h:109
void Destroy(T *p)
Definition: Allocator.h:277
PCL root namespace.
Definition: AbstractImage.h:77