NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
fft2.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
33#include "NumCpp/NdArray.hpp"
34
35namespace nc::fft
36{
37 namespace detail
38 {
39 //===========================================================================
40 // Method Description:
46 inline NdArray<std::complex<double>> fft2_internal(const NdArray<std::complex<double>>& x, const Shape& shape)
47 {
48 if (shape.rows == 0 || shape.cols == 0)
49 {
50 return {};
51 }
52
53 auto result = NdArray<std::complex<double>>(shape.rows, shape.cols);
54
55 stl_algorithms::for_each(result.begin(),
56 result.end(),
57 [&](auto& resultElement)
58 {
59 const auto i = &resultElement - result.data();
60 const auto k = static_cast<double>(i / shape.cols);
61 const auto l = static_cast<double>(i % shape.cols);
62 resultElement = std::complex<double>{ 0., 0. };
63 for (auto m = 0u; m < std::min(shape.rows, x.numRows()); ++m)
64 {
65 for (auto n = 0u; n < std::min(shape.cols, x.numCols()); ++n)
66 {
67 const auto angle =
69 (((static_cast<double>(m) * k) / static_cast<double>(shape.rows)) +
70 ((static_cast<double>(n) * l) / static_cast<double>(shape.cols)));
71 resultElement += (x(m, n) * std::polar(1., angle));
72 }
73 }
74 });
75
76 return result;
77 }
78 } // namespace detail
79
80 //===========================================================================
81 // Method Description:
91 template<typename dtype>
92 NdArray<std::complex<double>> fft2(const NdArray<dtype>& inArray, const Shape& inShape)
93 {
95
96 const auto data = nc::complex<dtype, double>(inArray);
97 return detail::fft2_internal(data, inShape);
98 }
99
100 //===========================================================================
101 // Method Description:
110 template<typename dtype>
112 {
114
115 return fft2(inArray, inArray.shape());
116 }
117
118 //============================================================================
119 // Method Description:
129 template<typename dtype>
130 NdArray<std::complex<double>> fft2(const NdArray<std::complex<dtype>>& inArray, const Shape& inShape)
131 {
133
134 const auto data = nc::complex<dtype, double>(inArray);
135 return detail::fft2_internal(data, inShape);
136 }
137
138 //============================================================================
139 // Method Description:
148 template<typename dtype>
149 NdArray<std::complex<double>> fft2(const NdArray<std::complex<dtype>>& inArray)
150 {
152
153 return fft2(inArray, inArray.shape());
154 }
155} // namespace nc::fft
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
constexpr double twoPi
2Pi
Definition Core/Constants.hpp:40
Definition FFT/FFT.hpp:42
NdArray< std::complex< double > > fft2_internal(const NdArray< std::complex< double > > &x, const Shape &shape)
Definition fft2.hpp:46
Definition FFT/FFT.hpp:40
NdArray< std::complex< double > > fft2(const NdArray< dtype > &inArray, const Shape &inShape)
Definition fft2.hpp:92
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition StlAlgorithms.hpp:226
auto angle(const std::complex< dtype > &inValue)
Definition angle.hpp:48
auto complex(dtype inReal)
Definition complex.hpp:47
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition Functions/shape.hpp:42