NumCpp  2.16.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
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/FFT/ifft.hpp"
39#include "NumCpp/NdArray.hpp"
40
41namespace nc::fft
42{
43 namespace detail
44 {
45 //===========================================================================
46 // Method Description:
52 inline NdArray<double> irfft_internal(const NdArray<std::complex<double>>& x, uint32 n)
53 {
54 if (x.size() == 0 || n == 0)
55 {
56 return {};
57 }
58
59 const auto necessaryInputPoints = n / 2 + 1;
61 if (x.size() > necessaryInputPoints)
62 {
64 }
65 else if (x.size() < necessaryInputPoints)
66 {
68 stl_algorithms::copy(x.begin(), x.end(), input.begin());
69 }
70 else
71 {
72 input = x;
73 }
74
75 auto realN = 2 * (input.size() - 1);
76 realN += n % 2 == 1 ? 1 : 0;
78 stl_algorithms::copy(input.begin(), input.end(), fullOutput.begin());
80 fullOutput.begin() + input.size(),
81 fullOutput.rbegin(),
82 [](const auto& value) { return std::conj(value); });
84 }
85 } // namespace detail
86
87 //============================================================================
88 // Method Description:
99 template<typename dtype>
101 {
103
104 switch (inAxis)
105 {
106 case Axis::NONE:
107 {
108 const auto data = nc::complex<dtype, double>(inArray);
109 return detail::irfft_internal(data, inN);
110 }
111 case Axis::COL:
112 {
113 const auto data = nc::complex<dtype, double>(inArray);
114 const auto& shape = inArray.shape();
116 const auto dataColSlice = data.cSlice();
117 const auto resultColSlice = result.cSlice();
118
119 for (uint32 row = 0; row < data.numRows(); ++row)
120 {
121 const auto rowData = data(row, dataColSlice);
124 }
125
126 return result;
127 }
128 case Axis::ROW:
129 {
130 return irfft(inArray.transpose(), inN, Axis::COL).transpose();
131 }
132 default:
133 {
134 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
135 return {};
136 }
137 }
138 }
139
140 //============================================================================
141 // Method Description:
151 template<typename dtype>
152 NdArray<double> irfft(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
153 {
155
156 switch (inAxis)
157 {
158 case Axis::NONE:
159 {
160 return irfft(inArray, 2 * (inArray.size() - 1), inAxis);
161 }
162 case Axis::COL:
163 {
164 return irfft(inArray, 2 * (inArray.numCols() - 1), inAxis);
165 }
166 case Axis::ROW:
167 {
168 return irfft(inArray, 2 * (inArray.numRows() - 1), inAxis);
169 }
170 default:
171 {
172 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
173 return {};
174 }
175 }
176 }
177} // 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
self_type transpose() const
Definition NdArrayCore.hpp:4959
self_type flatten() const
Definition NdArrayCore.hpp:2923
uint32 rows
Definition Core/shape.hpp:44
A Class for slicing into NdArrays.
Definition Slice.hpp:45
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:52
Definition FFT/FFT.hpp:40
NdArray< double > irfft(const NdArray< std::complex< dtype > > &inArray, uint32 inN, Axis inAxis=Axis::NONE)
Definition irfft.hpp:100
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:775
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition StlAlgorithms.hpp:97
Axis
Enum To describe an axis.
Definition Enums.hpp:36
auto real(const std::complex< dtype > &inValue)
Definition real.hpp:48
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
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