@@ -39,14 +39,43 @@ namespace nc::fft
3939 {
4040 // ===========================================================================
4141 // Method Description:
42- // / Fast Fourier Transform
42+ // / Inverse Fast Fourier Transform
4343 // /
4444 // / @param x the data
4545 // / @param shape Shape (length of each transformed axis) of the output
4646 // /
4747 inline NdArray<std::complex <double >> ifft2_internal (const NdArray<std::complex <double >>& x, const Shape& shape)
4848 {
49- return {};
49+ if (shape.rows == 0 || shape.cols == 0 )
50+ {
51+ return {};
52+ }
53+
54+ auto result = NdArray<std::complex <double >>(shape.rows , shape.cols );
55+
56+ stl_algorithms::for_each (result.begin (),
57+ result.end (),
58+ [&](auto & resultElement)
59+ {
60+ const auto i = &resultElement - result.data ();
61+ const auto m = static_cast <double >(i / shape.cols );
62+ const auto n = static_cast <double >(i % shape.cols );
63+ resultElement = std::complex <double >{ 0 ., 0 . };
64+ for (auto k = 0u ; k < std::min (shape.rows , x.numRows ()); ++k)
65+ {
66+ for (auto l = 0u ; l < std::min (shape.cols , x.numCols ()); ++l)
67+ {
68+ const auto angle =
69+ constants::twoPi *
70+ (((static_cast <double >(k) * m) / static_cast <double >(shape.rows )) +
71+ ((static_cast <double >(l) * n) / static_cast <double >(shape.cols )));
72+ resultElement += (x (k, l) * std::polar (1 ., angle));
73+ }
74+ }
75+ resultElement /= shape.size ();
76+ });
77+
78+ return result;
5079 }
5180 } // namespace detail
5281
@@ -62,11 +91,12 @@ namespace nc::fft
6291 // / @return NdArray
6392 // /
6493 template <typename dtype>
65- NdArray<double > ifft2 (const NdArray<dtype>& inArray, const Shape& inShape)
94+ NdArray<std:: complex < double > > ifft2 (const NdArray<dtype>& inArray, const Shape& inShape)
6695 {
6796 STATIC_ASSERT_ARITHMETIC (dtype);
6897
69- return {};
98+ const auto data = nc::complex <dtype, double >(inArray);
99+ return detail::ifft2_internal (data, inShape);
70100 }
71101
72102 // ===========================================================================
@@ -80,7 +110,7 @@ namespace nc::fft
80110 // / @return NdArray
81111 // /
82112 template <typename dtype>
83- NdArray<double > ifft2 (const NdArray<dtype>& inArray)
113+ NdArray<std:: complex < double > > ifft2 (const NdArray<dtype>& inArray)
84114 {
85115 STATIC_ASSERT_ARITHMETIC (dtype);
86116
@@ -103,7 +133,8 @@ namespace nc::fft
103133 {
104134 STATIC_ASSERT_ARITHMETIC (dtype);
105135
106- return {};
136+ const auto data = nc::complex <dtype, double >(inArray);
137+ return detail::ifft2_internal (data, inShape);
107138 }
108139
109140 // ============================================================================
0 commit comments