NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
irfft.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
34#include "NumCpp/Core/Types.hpp"
35#include "NumCpp/FFT/ifft.hpp"
38#include "NumCpp/NdArray.hpp"
39
40namespace nc::fft
41{
42 namespace detail
43 {
44 //===========================================================================
45 // Method Description:
51 inline NdArray<double> irfft_internal(const NdArray<std::complex<double>>& x, uint32 n)
52 {
53 if (x.size() == 0 || n == 0)
54 {
55 return {};
56 }
57
58 const auto necessaryInputPoints = n / 2 + 1;
59 auto input = NdArray<std::complex<double>>{};
60 if (x.size() > necessaryInputPoints)
61 {
62 input = x.flatten()(0, Slice(necessaryInputPoints + 1));
63 }
64 else if (x.size() < necessaryInputPoints)
65 {
66 input = NdArray<std::complex<double>>(1, necessaryInputPoints).zeros();
67 stl_algorithms::copy(x.begin(), x.end(), input.begin());
68 }
69 else
70 {
71 input = x;
72 }
73
74 auto realN = 2 * (input.size() - 1);
75 realN += n % 2 == 1 ? 1 : 0;
76 auto fullOutput = NdArray<std::complex<double>>(1, realN);
77 stl_algorithms::copy(input.begin(), input.end(), fullOutput.begin());
78 stl_algorithms::transform(fullOutput.begin() + 1,
79 fullOutput.begin() + input.size(),
80 fullOutput.rbegin(),
81 [](const auto& value) { return std::conj(value); });
82 return real(ifft_internal(fullOutput, n));
83 }
84 } // namespace detail
85
86 //============================================================================
87 // Method Description:
98 template<typename dtype>
99 NdArray<double> irfft(const NdArray<std::complex<dtype>>& inArray, uint32 inN, Axis inAxis = Axis::NONE)
100 {
102
103 switch (inAxis)
104 {
105 case Axis::NONE:
106 {
107 const auto data = nc::complex<dtype, double>(inArray);
108 return detail::irfft_internal(data, inN);
109 }
110 case Axis::COL:
111 {
112 const auto data = nc::complex<dtype, double>(inArray);
113 const auto& shape = inArray.shape();
114 auto result = NdArray<double>(shape.rows, inN);
115 const auto dataColSlice = data.cSlice();
116 const auto resultColSlice = result.cSlice();
117
118 for (uint32 row = 0; row < data.numRows(); ++row)
119 {
120 const auto rowData = data(row, dataColSlice);
121 const auto rowResult = detail::irfft_internal(rowData, inN);
122 result.put(row, resultColSlice, rowResult);
123 }
124
125 return result;
126 }
127 case Axis::ROW:
128 {
129 return irfft(inArray.transpose(), inN, Axis::COL).transpose();
130 }
131 default:
132 {
133 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
134 return {};
135 }
136 }
137 }
138
139 //============================================================================
140 // Method Description:
150 template<typename dtype>
151 NdArray<double> irfft(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
152 {
154
155 switch (inAxis)
156 {
157 case Axis::NONE:
158 {
159 return irfft(inArray, 2 * (inArray.size() - 1), inAxis);
160 }
161 case Axis::COL:
162 {
163 return irfft(inArray, 2 * (inArray.numCols() - 1), inAxis);
164 }
165 case Axis::ROW:
166 {
167 return irfft(inArray, 2 * (inArray.numRows() - 1), inAxis);
168 }
169 default:
170 {
171 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
172 return {};
173 }
174 }
175 }
176} // 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:135
self_type transpose() const
Definition NdArrayCore.hpp:4959
A Class for slicing into NdArrays.
Definition Slice.hpp:45
Definition FFT/FFT.hpp:42
NdArray< std::complex< double > > ifft_internal(const NdArray< std::complex< double > > &x, uint32 n)
Definition ifft.hpp:50
NdArray< double > irfft_internal(const NdArray< std::complex< double > > &x, uint32 n)
Definition irfft.hpp:51
Definition FFT/FFT.hpp:40
NdArray< double > irfft(const NdArray< std::complex< dtype > > &inArray, uint32 inN, Axis inAxis=Axis::NONE)
Definition irfft.hpp:99
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:776
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition StlAlgorithms.hpp:98
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
auto real(const std::complex< dtype > &inValue)
Definition real.hpp:48
auto complex(dtype inReal)
Definition complex.hpp:47
NdArray< dtype > zeros(uint32 inSquareSize)
Definition zeros.hpp:48
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition Functions/shape.hpp:42
std::uint32_t uint32
Definition Types.hpp:40