NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
irfft2.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
34#include "NumCpp/FFT/ifft2.hpp"
37#include "NumCpp/NdArray.hpp"
38
39namespace nc::fft
40{
41 namespace detail
42 {
43 //===========================================================================
44 // Method Description:
50 inline NdArray<double> irfft2_internal(const NdArray<std::complex<double>>& x, const Shape& shape)
51 {
52 if (x.size() == 0 || shape.rows == 0 || shape.cols == 0)
53 {
54 return {};
55 }
56
57 const auto necessaryInputPoints = shape.cols / 2 + 1;
58 auto input = NdArray<std::complex<double>>{};
59 if (x.numCols() > necessaryInputPoints)
60 {
61 input = x(x.rSlice(), Slice(necessaryInputPoints + 1));
62 }
63 else if (x.numCols() < necessaryInputPoints)
64 {
65 input = NdArray<std::complex<double>>(shape.rows, necessaryInputPoints).zeros();
66 input.put(x.rSlice(), x.cSlice(), x);
67 }
68 else
69 {
70 input = x;
71 }
72
73 auto realN = 2 * (input.numCols() - 1);
74 realN += shape.cols % 2 == 1 ? 1 : 0;
75 auto fullOutput = NdArray<std::complex<double>>(shape.rows, realN).zeros();
76 for (auto row = 0u; row < input.numRows(); ++row)
77 {
78 stl_algorithms::copy(input.begin(row), input.end(row), fullOutput.begin(row));
79 }
80 stl_algorithms::transform(fullOutput.begin(0) + 1,
81 fullOutput.begin(0) + input.numCols(),
82 fullOutput.rbegin(0),
83 [](const auto& value) { return std::conj(value); });
84 for (auto col = 1u; col < input.numCols(); ++col)
85 {
86 stl_algorithms::transform(input.colbegin(col) + 1,
87 input.colend(col),
88 fullOutput.rcolbegin(fullOutput.numCols() - col),
89 [](const auto& value) { return std::conj(value); });
90 }
91
92 return real(ifft2_internal(fullOutput, shape));
93 }
94 } // namespace detail
95
96 //============================================================================
97 // Method Description:
107 template<typename dtype>
108 NdArray<double> irfft2(const NdArray<std::complex<dtype>>& inArray, const Shape& inShape)
109 {
111
112 const auto data = nc::complex<dtype, double>(inArray);
113 return detail::irfft2_internal(data, inShape);
114 }
115
116 //============================================================================
117 // Method Description:
126 template<typename dtype>
127 NdArray<double> irfft2(const NdArray<std::complex<dtype>>& inArray)
128 {
130
131 const auto& shape = inArray.shape();
132 const auto newCols = 2 * (shape.cols - 1);
133 return irfft2(inArray, { shape.rows, newCols });
134 }
135} // 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
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
A Class for slicing into NdArrays.
Definition Slice.hpp:45
Definition FFT/FFT.hpp:42
NdArray< std::complex< double > > ifft2_internal(const NdArray< std::complex< double > > &x, const Shape &shape)
Definition ifft2.hpp:46
NdArray< double > irfft2_internal(const NdArray< std::complex< double > > &x, const Shape &shape)
Definition irfft2.hpp:50
Definition FFT/FFT.hpp:40
NdArray< double > irfft2(const NdArray< std::complex< dtype > > &inArray, const Shape &inShape)
Definition irfft2.hpp:108
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
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