NumCpp  2.16.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
rfft.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
35#include "NumCpp/Core/Types.hpp"
37#include "NumCpp/NdArray.hpp"
38
39namespace nc::fft
40{
41 namespace detail
42 {
43 //===========================================================================
44 // Method Description:
50 inline NdArray<std::complex<double>> rfft_internal(const NdArray<std::complex<double>>& x, uint32 n)
51 {
52 if (n == 0)
53 {
54 return {};
55 }
56
57 const auto realN = n / 2 + 1;
58 auto result = NdArray<std::complex<double>>(1, realN);
59
60 stl_algorithms::for_each(result.begin(),
61 result.end(),
62 [&](auto& resultElement)
63 {
64 const auto k = static_cast<double>(&resultElement - result.data());
65 const auto minusTwoPiKOverN = -constants::twoPi * k / static_cast<double>(n);
66 resultElement = std::complex<double>{ 0., 0. };
67 for (auto m = 0u; m < std::min(n, x.size()); ++m)
68 {
69 const auto angle = minusTwoPiKOverN * static_cast<double>(m);
70 resultElement += (x[m] * std::polar(1., angle));
71 }
72 });
73
74 return result;
75 }
76 } // namespace detail
77
78 //===========================================================================
79 // Method Description:
90 template<typename dtype>
91 NdArray<std::complex<double>> rfft(const NdArray<dtype>& inArray, uint32 inN, Axis inAxis = Axis::NONE)
92 {
94
95 switch (inAxis)
96 {
97 case Axis::NONE:
98 {
99 const auto data = nc::complex<dtype, double>(inArray);
100 return detail::rfft_internal(data, inN);
101 }
102 case Axis::COL:
103 {
104 auto data = nc::complex<dtype, double>(inArray);
105 const auto& shape = inArray.shape();
106 const auto realN = inN / 2 + 1;
107 auto result = NdArray<std::complex<double>>(shape.rows, realN);
108 const auto dataColSlice = data.cSlice();
109 const auto resultColSlice = result.cSlice();
110
111 for (uint32 row = 0; row < data.numRows(); ++row)
112 {
113 const auto rowData = data(row, dataColSlice);
114 const auto rowResult = detail::rfft_internal(rowData, inN);
115 result.put(row, resultColSlice, rowResult);
116 }
117
118 return result;
119 }
120 case Axis::ROW:
121 {
122 return rfft(inArray.transpose(), inN, Axis::COL).transpose();
123 }
124 default:
125 {
126 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
127 return {};
128 }
129 }
130 }
131
132 //===========================================================================
133 // Method Description:
143 template<typename dtype>
144 NdArray<std::complex<double>> rfft(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
145 {
147
148 switch (inAxis)
149 {
150 case Axis::NONE:
151 {
152 return rfft(inArray, inArray.size(), inAxis);
153 }
154 case Axis::COL:
155 {
156 return rfft(inArray, inArray.numCols(), inAxis);
157 }
158 case Axis::ROW:
159 {
160 return rfft(inArray, inArray.numRows(), inAxis);
161 }
162 default:
163 {
164 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
165 return {};
166 }
167 }
168 }
169} // namespace nc::fft
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#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:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4604
self_type transpose() const
Definition: NdArrayCore.hpp:4963
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3545
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4591
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3557
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition: NdArrayCore.hpp:1008
uint32 rows
Definition: Core/shape.hpp:44
NdArray< std::complex< double > > rfft_internal(const NdArray< std::complex< double > > &x, uint32 n)
Definition: rfft.hpp:50
Definition: FFT/fft.hpp:40
NdArray< std::complex< double > > rfft(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rfft.hpp:144
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:226
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:44
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
auto angle(const std::complex< dtype > &inValue)
Definition: angle.hpp:48
auto polar(dtype magnitude, dtype phaseAngle)
Definition: polar.hpp:49
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/shape.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40