27#include "vtkCommonMathModule.h"
34#include "vtk_kissfft.h"
36#include VTK_KISSFFT_HEADER(kiss_fft.h)
37#include VTK_KISSFFT_HEADER(tools/kiss_fftr.h)
46VTK_ABI_NAMESPACE_BEGIN
76 "vtkFFT::ComplexNumber definition is not valid");
125 static std::vector<ComplexNumber>
Fft(
const std::vector<ScalarNumber>& in);
127 static std::vector<ComplexNumber>
Fft(
const std::vector<ComplexNumber>& in);
141 static std::vector<ComplexNumber>
RFft(
const std::vector<ScalarNumber>& in);
158 static std::vector<ComplexNumber>
IFft(
const std::vector<ComplexNumber>& in);
168 static std::vector<ScalarNumber>
IRFft(
const std::vector<ComplexNumber>& in);
188 static std::vector<ScalarNumber>
FftFreq(
int windowLength,
double sampleSpacing);
194 static std::vector<ScalarNumber>
RFftFreq(
int windowLength,
double sampleSpacing);
204 OctaveSubdivision octaveSubdivision = OctaveSubdivision::Full,
bool baseTwo =
true);
227 template <typename T, typename TW, typename std::enable_if<isFftType<T>::value>::type* =
nullptr>
229 const std::vector<TW>& window, std::size_t noverlap,
bool detrend,
bool onesided,
230 unsigned int* shape =
nullptr);
232 template <
typename TW>
234 const std::vector<TW>& window, std::size_t noverlap,
bool detrend,
bool onesided,
235 unsigned int* shape =
nullptr);
284 template <typename T, typename TW, typename std::enable_if<isFftType<T>::value>::type* =
nullptr>
285 static std::vector<ComplexNumber>
Spectrogram(
const std::vector<T>& signal,
286 const std::vector<TW>& window,
double sampleRate,
int noverlap,
bool detrend,
bool onesided,
288 bool transpose =
false);
290 template <
typename TW>
294 unsigned int* shape =
nullptr,
bool transpose =
false);
321 template <typename T, typename TW, typename std::enable_if<isFftType<T>::value>::type* =
nullptr>
322 static std::vector<vtkFFT::ScalarNumber>
Csd(
const std::vector<T>& signal,
323 const std::vector<TW>& window,
double sampleRate,
int noverlap,
bool detrend,
bool onesided,
326 template <
typename TW>
328 const std::vector<TW>& window,
double sampleRate,
int noverlap,
bool detrend,
bool onesided,
344 template <
typename T>
360 static inline ScalarNumber HanningGenerator(std::size_t x, std::size_t size);
361 static inline ScalarNumber BartlettGenerator(std::size_t x, std::size_t size);
362 static inline ScalarNumber SineGenerator(std::size_t x, std::size_t size);
363 static inline ScalarNumber BlackmanGenerator(std::size_t x, std::size_t size);
364 static inline ScalarNumber RectangularGenerator(std::size_t x, std::size_t size);
371 template <
typename T>
372 static void GenerateKernel1D(T* kernel, std::size_t n,
WindowGenerator generator);
378 template <
typename T>
379 static void GenerateKernel2D(T* kernel, std::size_t n, std::size_t m,
WindowGenerator generator);
392 template <
typename T>
393 constexpr static T Zero();
400 template <
typename InputIt>
402 InputIt begin, InputIt end,
Scaling scaling,
double fs);
408 template <
typename T,
typename TW>
425 template <
typename TW>
432 void operator=(
const vtkFFT&) =
delete;
449 return static_cast<T
>(0);
472 (lhs.r * rhs.i) + (lhs.i * rhs.r) };
482 const double divisor = rhs.r * rhs.r + rhs.i * rhs.i;
484 ((lhs.i * rhs.r) - (lhs.r * rhs.i)) / divisor };
495 return std::sqrt(in.r * in.r + in.i * in.i);
501 return in.r * in.r + in.i * in.i;
513 return 0.5 * (1.0 - std::cos(2.0 *
vtkMath::Pi() * x / (size - 1)));
519 return 2.0 * x / (size - 1);
531 const double cosin = std::cos((2.0 *
vtkMath::Pi() * x) / (size - 1));
532 return 0.42 - 0.5 * cosin + 0.08 * (2.0 * cosin * cosin - 1.0);
545 std::size_t half = (n / 2) + (n % 2);
546 for (std::size_t i = 0; i < half; ++i)
548 kernel[i] = kernel[n - 1 - i] = generator(i, n);
556 const std::size_t halfX = (n / 2) + (n % 2);
557 const std::size_t halfY = (m / 2) + (m % 2);
558 for (std::size_t i = 0; i < halfX; ++i)
560 for (std::size_t j = 0; j < halfY; ++j)
564 = kernel[n - 1 - i][j]
565 = kernel[i][m - 1 - j]
566 = kernel[n - 1 - i][m - 1 - j]
567 = generator(i, n) * generator(j, m);
Array-Of-Structs implementation of vtkGenericDataArray.
perform Discrete Fourier Transforms
static ScalarNumber BlackmanGenerator(std::size_t x, std::size_t size)
Window generator functions.
static ScalarNumber SineGenerator(std::size_t x, std::size_t size)
Window generator functions.
static void Fft(ComplexNumber *input, std::size_t size, ComplexNumber *result)
Compute the one-dimensional DFT for complex input.
ScalarNumber(*)(std::size_t, std::size_t) WindowGenerator
Window generator functions.
SpectralMode
Spectral modes for Spectrogram and Csd functions.
kiss_fft_scalar ScalarNumber
Useful type definitions and utilities.
OctaveSubdivision
Enum specifying which octave band we want to compute.
static std::vector< ScalarNumber > IRFft(const std::vector< ComplexNumber > &in)
Compute the inverse of RFft.
static ScalarNumber RectangularGenerator(std::size_t x, std::size_t size)
Window generator functions.
static void ScaleFft(ComplexNumber *fft, unsigned int shape[2], const std::vector< TW > &window, double sampleRate, bool onesided, vtkFFT::Scaling scaling, vtkFFT::SpectralMode mode)
Scale a fft according to its window and some mode.
static ScalarNumber HanningGenerator(std::size_t x, std::size_t size)
Window generator functions.
static void RFft(ComplexNumber *input, std::size_t size, ComplexNumber *result)
XXX(c++17): This function should NOT exist and is here just for the sake template unfolding purposes.
static vtkSmartPointer< vtkScalarNumberArray > RFft(vtkScalarNumberArray *input)
Compute the one-dimensional DFT for real input.
static ScalarNumber Abs(const ComplexNumber &in)
Return the absolute value (also known as norm, modulus, or magnitude) of complex number.
static void Transpose(T *data, unsigned int *shape)
Transpose in place an inlined 2D matrix.
static vtkSmartPointer< vtkFFT::vtkScalarNumberArray > Spectrogram(vtkFFT::vtkScalarNumberArray *signal, const std::vector< TW > &window, double sampleRate, int noverlap, bool detrend, bool onesided, vtkFFT::Scaling scaling, vtkFFT::SpectralMode mode, unsigned int *shape=nullptr, bool transpose=false)
Compute a spectrogram with consecutive Fourier transforms using Welch method.
static void GenerateKernel2D(T *kernel, std::size_t n, std::size_t m, WindowGenerator generator)
Given a window generator function, create a symmetric 2D kernel.
static ComplexNumber Conjugate(const ComplexNumber &in)
Return the conjugate of the given complex number.
static std::array< double, 2 > GetOctaveFrequencyRange(Octave octave, OctaveSubdivision octaveSubdivision=OctaveSubdivision::Full, bool baseTwo=true)
Return lower and upper frequency from a octave band number / nominal midband frequency.
static std::vector< ComplexNumber > Spectrogram(const std::vector< T > &signal, const std::vector< TW > &window, double sampleRate, int noverlap, bool detrend, bool onesided, vtkFFT::Scaling scaling, vtkFFT::SpectralMode mode, unsigned int *shape=nullptr, bool transpose=false)
Compute a spectrogram with consecutive Fourier transforms using Welch method.
static void GenerateKernel1D(T *kernel, std::size_t n, WindowGenerator generator)
Given a window generator function, create a symmetric 1D kernel.
static void RFft(ScalarNumber *input, std::size_t size, ComplexNumber *result)
Compute the one-dimensional DFT for real input.
static std::iterator_traits< InputIt >::value_type ComputeScaling(InputIt begin, InputIt end, Scaling scaling, double fs)
For a given window defined by begin and end, compute the scaling needed to apply to the resulting FFT...
static void PreprocessAndDispatchFft(const T *segment, const std::vector< TW > &window, bool detrend, bool onesided, vtkFFT::ComplexNumber *result)
Dispatch the signal to the right FFT function according to the given parameters.
static std::vector< vtkFFT::ScalarNumber > Csd(const std::vector< T > &signal, const std::vector< TW > &window, double sampleRate, int noverlap, bool detrend, bool onesided, vtkFFT::Scaling scaling)
Compute the Cross Spectral Density of a given signal.
static std::vector< ScalarNumber > FftFreq(int windowLength, double sampleSpacing)
Return the DFT sample frequencies.
static std::vector< ComplexNumber > Fft(const std::vector< ScalarNumber > &in)
Compute the one-dimensional DFT for complex input.
static std::vector< ScalarNumber > RFftFreq(int windowLength, double sampleSpacing)
Return the DFT sample frequencies for the real version of the dft (see Rfft).
static ScalarNumber BartlettGenerator(std::size_t x, std::size_t size)
Window generator functions.
static std::vector< ComplexNumber > Fft(const std::vector< ComplexNumber > &in)
Compute the one-dimensional DFT for complex input.
~vtkFFT() override=default
Octave
Enum containing octave band numbers, named upon their nominal midband frequency.
static vtkFFT::ComplexNumber * OverlappingFft(vtkFFT::vtkScalarNumberArray *signal, const std::vector< TW > &window, std::size_t noverlap, bool detrend, bool onesided, unsigned int *shape=nullptr)
Compute consecutive Fourier transforms Welch method without averaging nor scaling the result.
static vtkSmartPointer< vtkScalarNumberArray > Fft(vtkScalarNumberArray *input)
Compute the one-dimensional DFT for complex input.
static constexpr T Zero()
Templated zero value, specialized for vtkFFT::ComplexNumber.
static ScalarNumber SquaredAbs(const ComplexNumber &in)
Return the squared absolute value of the complex number.
static std::vector< ComplexNumber > RFft(const std::vector< ScalarNumber > &in)
Compute the one-dimensional DFT for real input.
kiss_fft_cpx ComplexNumber
Useful type definitions and utilities.
static std::vector< ComplexNumber > IFft(const std::vector< ComplexNumber > &in)
Compute the inverse of Fft.
Scaling
Scaling modes for Spectrogram and Csd functions.
static std::vector< ComplexNumber > OverlappingFft(const std::vector< T > &signal, const std::vector< TW > &window, std::size_t noverlap, bool detrend, bool onesided, unsigned int *shape=nullptr)
Compute consecutive Fourier transforms Welch method without averaging nor scaling the result.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static vtkSmartPointer< vtkFFT::vtkScalarNumberArray > Csd(vtkScalarNumberArray *signal, const std::vector< TW > &window, double sampleRate, int noverlap, bool detrend, bool onesided, vtkFFT::Scaling scaling)
Compute the Cross Spectral Density of a given signal.
static void Fft(ScalarNumber *input, std::size_t size, ComplexNumber *result)
Compute the one-dimensional DFT for complex input.
a simple class to control print indentation
static constexpr double Pi()
A mathematical constant.
abstract base class for most VTK objects
Hold a reference to a vtkObjectBase instance.
STL-compatible iterable ranges that provide access to vtkDataArray elements.
vtkFFT::ComplexNumber operator*(const vtkFFT::ComplexNumber &lhs, const vtkFFT::ComplexNumber &rhs)
vtkFFT::ComplexNumber operator+(const vtkFFT::ComplexNumber &lhs, const vtkFFT::ComplexNumber &rhs)
vtkFFT::ComplexNumber operator-(const vtkFFT::ComplexNumber &lhs, const vtkFFT::ComplexNumber &rhs)
vtkFFT::ComplexNumber operator/(const vtkFFT::ComplexNumber &lhs, const vtkFFT::ComplexNumber &rhs)