VTK  9.1.0
vtkFFT.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkFFT.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
29#ifndef vtkFFT_h
30#define vtkFFT_h
31
32#include "vtkCommonMathModule.h" // For export macro
33#include "vtkMath.h" // For vtkMath::Pi
34#include "vtkObject.h"
35
36#include "vtk_kissfft.h" // For kiss_fft_scalar, kiss_fft_cpx
37// clang-format off
38#include VTK_KISSFFT_HEADER(kiss_fft.h)
39#include VTK_KISSFFT_HEADER(tools/kiss_fftr.h)
40// clang-format on
41
42#include <vector> // For std::vector
43#include <cmath> // for std::sin, std::cos, std::sqrt
44
45class VTKCOMMONMATH_EXPORT vtkFFT : public vtkObject
46{
47public:
48 using ScalarNumber = kiss_fft_scalar;
49 using ComplexNumber = kiss_fft_cpx;
50
51 static vtkFFT* New();
52 vtkTypeMacro(vtkFFT, vtkObject);
53 void PrintSelf(ostream& os, vtkIndent indent) override;
54
56
63 static std::vector<ComplexNumber> Fft(const std::vector<ComplexNumber>& in);
64 static std::vector<ComplexNumber> Fft(const std::vector<ScalarNumber>& in);
66
73 static std::vector<ComplexNumber> RFft(const std::vector<ScalarNumber>& in);
74
85 static std::vector<ComplexNumber> IFft(const std::vector<ComplexNumber>& in);
86
95 static std::vector<ScalarNumber> IRFft(const std::vector<ComplexNumber>& in);
96
100 static inline double Abs(const ComplexNumber& in);
101
105 static inline double SquaredAbs(const ComplexNumber& in);
106
110 static std::vector<double> FftFreq(int windowLength, double sampleSpacing);
111
116 static std::vector<double> RFftFreq(int windowLength, double sampleSpacing);
117
119
128 using WindowGenerator = double (*)(const std::size_t, const std::size_t);
129
130 static inline double HanningGenerator(const std::size_t x, const std::size_t size);
131 static inline double BartlettGenerator(const std::size_t x, const std::size_t size);
132 static inline double SineGenerator(const std::size_t x, const std::size_t size);
133 static inline double BlackmanGenerator(const std::size_t x, const std::size_t size);
134 static inline double RectangularGenerator(const std::size_t x, const std::size_t size);
136
141 template <typename Array1D>
142 static void GenerateKernel1D(Array1D* kernel, const std::size_t n, WindowGenerator generator);
143
148 template <typename Array2D>
149 static void GenerateKernel2D(
150 Array2D* kernel, const std::size_t n, const std::size_t m, WindowGenerator generator);
151
152protected:
153 vtkFFT() = default;
154 ~vtkFFT() override = default;
155
156private:
157 vtkFFT(const vtkFFT&) = delete;
158 void operator=(const vtkFFT&) = delete;
159};
160
161//------------------------------------------------------------------------------
162double vtkFFT::Abs(const ComplexNumber& in)
163{
164 return std::sqrt(in.r * in.r + in.i * in.i);
165}
166
167//------------------------------------------------------------------------------
169{
170 return in.r * in.r + in.i * in.i;
171}
172
173//------------------------------------------------------------------------------
174double vtkFFT::HanningGenerator(const std::size_t x, const std::size_t size)
175{
176 return 0.5 * (1.0 - std::cos(2.0 * vtkMath::Pi() * x / (size - 1)));
177}
178
179//------------------------------------------------------------------------------
180double vtkFFT::BartlettGenerator(const std::size_t x, const std::size_t size)
181{
182 return 2.0 * x / (size - 1);
183}
184
185//------------------------------------------------------------------------------
186double vtkFFT::SineGenerator(const std::size_t x, const std::size_t size)
187{
188 return std::sin(vtkMath::Pi() * x / size);
189}
190
191//------------------------------------------------------------------------------
192double vtkFFT::BlackmanGenerator(const std::size_t x, const std::size_t size)
193{
194 return 0.42 - 0.5 * std::cos((2.0 * vtkMath::Pi() * x) / size) +
195 0.08 * std::cos((4.0 * vtkMath::Pi() * x) / size);
196}
197
198//------------------------------------------------------------------------------
199double vtkFFT::RectangularGenerator(const std::size_t, const std::size_t)
200{
201 return 1.0;
202}
203
204//------------------------------------------------------------------------------
205template <typename Array1D>
206void vtkFFT::GenerateKernel1D(Array1D* kernel, const std::size_t n, WindowGenerator generator)
207{
208 const std::size_t half = (n / 2) + (n % 2);
209 for (std::size_t i = 0; i < half; ++i)
210 {
211 kernel[i] = kernel[n - 1 - i] = generator(i, n);
212 }
213}
214
215//------------------------------------------------------------------------------
216template <typename Array2D>
218 Array2D* kernel, const std::size_t n, const std::size_t m, WindowGenerator generator)
219{
220 const std::size_t halfX = (n / 2) + (n % 2);
221 const std::size_t halfY = (m / 2) + (m % 2);
222 for (std::size_t i = 0; i < halfX; ++i)
223 {
224 for (std::size_t j = 0; j < halfY; ++j)
225 {
226 // clang-format off
227 kernel[i][j]
228 = kernel[n - 1 - i][j]
229 = kernel[i][m - 1 - j]
230 = kernel[n - 1 - i][m - 1 - j]
231 = generator(i, n) * generator(j, m);
232 // clang-format on
233 }
234 }
235}
236
237#endif
perform Discrete Fourier Transforms
Definition: vtkFFT.h:46
static double BlackmanGenerator(const std::size_t x, const std::size_t size)
Definition: vtkFFT.h:192
static std::vector< double > RFftFreq(int windowLength, double sampleSpacing)
Return the DFT sample frequencies for the real version of the dft (see Rfft).
static double RectangularGenerator(const std::size_t x, const std::size_t size)
Definition: vtkFFT.h:199
kiss_fft_scalar ScalarNumber
Definition: vtkFFT.h:48
static void GenerateKernel1D(Array1D *kernel, const std::size_t n, WindowGenerator generator)
Given a window generator function, create a symmetric 1D kernel.
Definition: vtkFFT.h:206
static std::vector< ScalarNumber > IRFft(const std::vector< ComplexNumber > &in)
Compute the inverse of RFft.
vtkFFT()=default
static double SquaredAbs(const ComplexNumber &in)
Return the squared absolute value of the complex number.
Definition: vtkFFT.h:168
static std::vector< ComplexNumber > Fft(const std::vector< ScalarNumber > &in)
double(*)(const std::size_t, const std::size_t) WindowGenerator
Window generator functions.
Definition: vtkFFT.h:128
static double HanningGenerator(const std::size_t x, const std::size_t size)
Definition: vtkFFT.h:174
static std::vector< ComplexNumber > Fft(const std::vector< ComplexNumber > &in)
Compute the one-dimensional DFT for complex input.
~vtkFFT() override=default
static double BartlettGenerator(const std::size_t x, const std::size_t size)
Definition: vtkFFT.h:180
static void GenerateKernel2D(Array2D *kernel, const std::size_t n, const std::size_t m, WindowGenerator generator)
Given a window generator function, create a symmetric 2D kernel.
Definition: vtkFFT.h:217
static double Abs(const ComplexNumber &in)
Return the absolute value (also known as norm, modulus, or magnitude) of complex number.
Definition: vtkFFT.h:162
static std::vector< ComplexNumber > RFft(const std::vector< ScalarNumber > &in)
Compute the one-dimensional DFT for real input.
kiss_fft_cpx ComplexNumber
Definition: vtkFFT.h:49
static std::vector< double > FftFreq(int windowLength, double sampleSpacing)
Return the DFT sample frequencies.
static std::vector< ComplexNumber > IFft(const std::vector< ComplexNumber > &in)
Compute the inverse of Fft.
static double SineGenerator(const std::size_t x, const std::size_t size)
Definition: vtkFFT.h:186
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static vtkFFT * New()
a simple class to control print indentation
Definition: vtkIndent.h:113
static constexpr double Pi()
A mathematical constant.
Definition: vtkMath.h:206
abstract base class for most VTK objects
Definition: vtkObject.h:73
@ size
Definition: vtkX3D.h:259