NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
rfft2.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
34#include "NumCpp/NdArray.hpp"
35
36namespace nc::fft
37{
38 namespace detail
39 {
40 //===========================================================================
41 // Method Description:
47 inline NdArray<std::complex<double>> rfft2_internal(const NdArray<std::complex<double>>& x, const Shape& shape)
48 {
49 if (shape.rows == 0 || shape.cols == 0)
50 {
51 return {};
52 }
53
54 const auto realN = shape.cols / 2 + 1;
55 auto result = NdArray<std::complex<double>>(shape.rows, realN);
56
57 stl_algorithms::for_each(result.begin(),
58 result.end(),
59 [&](auto& resultElement)
60 {
61 const auto i = &resultElement - result.data();
62 const auto k = static_cast<double>(i / realN);
63 const auto l = static_cast<double>(i % realN);
64 resultElement = std::complex<double>{ 0., 0. };
65 for (auto m = 0u; m < std::min(shape.rows, x.numRows()); ++m)
66 {
67 for (auto n = 0u; n < std::min(shape.cols, x.numCols()); ++n)
68 {
69 const auto angle =
71 (((static_cast<double>(m) * k) / static_cast<double>(shape.rows)) +
72 ((static_cast<double>(n) * l) / static_cast<double>(shape.cols)));
73 resultElement += (x(m, n) * std::polar(1., angle));
74 }
75 }
76 });
77
78 return result;
79 }
80 } // namespace detail
81
82 //============================================================================
83 // Method Description:
93 template<typename dtype>
94 NdArray<std::complex<double>> rfft2(const NdArray<dtype>& inArray, const Shape& inShape)
95 {
97
98 const auto data = nc::complex<dtype, double>(inArray);
99 return detail::rfft2_internal(data, inShape);
100 }
101
102 //============================================================================
103 // Method Description:
112 template<typename dtype>
114 {
116
117 return rfft2(inArray, inArray.shape());
118 }
119} // 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 > > rfft2_internal(const NdArray< std::complex< double > > &x, const Shape &shape)
Definition rfft2.hpp:47
Definition FFT/FFT.hpp:40
NdArray< std::complex< double > > rfft2(const NdArray< dtype > &inArray, const Shape &inShape)
Definition rfft2.hpp:94
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