NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
ifft2.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>> ifft2_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 m = static_cast<double>(i / shape.cols);
61 const auto n = static_cast<double>(i % shape.cols);
62 resultElement = std::complex<double>{ 0., 0. };
63 for (auto k = 0u; k < std::min(shape.rows, x.numRows()); ++k)
64 {
65 for (auto l = 0u; l < std::min(shape.cols, x.numCols()); ++l)
66 {
67 const auto angle =
69 (((static_cast<double>(k) * m) / static_cast<double>(shape.rows)) +
70 ((static_cast<double>(l) * n) / static_cast<double>(shape.cols)));
71 resultElement += (x(k, l) * std::polar(1., angle));
72 }
73 }
74 resultElement /= shape.size();
75 });
76
77 return result;
78 }
79 } // namespace detail
80
81 //===========================================================================
82 // Method Description:
92 template<typename dtype>
93 NdArray<std::complex<double>> ifft2(const NdArray<dtype>& inArray, const Shape& inShape)
94 {
96
97 const auto data = nc::complex<dtype, double>(inArray);
98 return detail::ifft2_internal(data, inShape);
99 }
100
101 //===========================================================================
102 // Method Description:
111 template<typename dtype>
113 {
115
116 return ifft2(inArray, inArray.shape());
117 }
118
119 //============================================================================
120 // Method Description:
130 template<typename dtype>
131 NdArray<std::complex<double>> ifft2(const NdArray<std::complex<dtype>>& inArray, const Shape& inShape)
132 {
134
135 const auto data = nc::complex<dtype, double>(inArray);
136 return detail::ifft2_internal(data, inShape);
137 }
138
139 //============================================================================
140 // Method Description:
149 template<typename dtype>
150 NdArray<std::complex<double>> ifft2(const NdArray<std::complex<dtype>>& inArray)
151 {
153
154 return ifft2(inArray, inArray.shape());
155 }
156} // 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 > > ifft2_internal(const NdArray< std::complex< double > > &x, const Shape &shape)
Definition ifft2.hpp:46
Definition FFT/FFT.hpp:40
NdArray< std::complex< double > > ifft2(const NdArray< dtype > &inArray, const Shape &inShape)
Definition ifft2.hpp:93
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