fft_wrapper.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef _SCTL_FFT_WRAPPER_
  2. #define _SCTL_FFT_WRAPPER_
  3. #include <cmath>
  4. #include <cassert>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include SCTL_INCLUDE(common.hpp)
  8. #include SCTL_INCLUDE(mem_mgr.hpp)
  9. #include SCTL_INCLUDE(matrix.hpp)
  10. namespace SCTL_NAMESPACE {
  11. template <class ValueType> class Complex {
  12. public:
  13. Complex<ValueType> operator*(const Complex<ValueType>& x){
  14. Complex<ValueType> z;
  15. z.real = real * x.real - imag * x.imag;
  16. z.imag = imag * x.real - real * x.imag;
  17. return z;
  18. }
  19. Complex<ValueType> operator*(const ValueType& x){
  20. Complex<ValueType> z;
  21. z.real = real * x;
  22. z.imag = imag * x;
  23. return z;
  24. }
  25. Complex<ValueType> operator+(const Complex<ValueType>& x){
  26. Complex<ValueType> z;
  27. z.real = real + x.real;
  28. z.imag = imag + x.imag;
  29. return z;
  30. }
  31. Complex<ValueType> operator+(const ValueType& x){
  32. Complex<ValueType> z;
  33. z.real = real + x;
  34. z.imag = imag;
  35. return z;
  36. }
  37. Complex<ValueType> operator-(const Complex<ValueType>& x){
  38. Complex<ValueType> z;
  39. z.real = real - x.real;
  40. z.imag = imag - x.imag;
  41. return z;
  42. }
  43. Complex<ValueType> operator-(const ValueType& x){
  44. Complex<ValueType> z;
  45. z.real = real - x;
  46. z.imag = imag;
  47. return z;
  48. }
  49. ValueType real;
  50. ValueType imag;
  51. };
  52. template <class ValueType> Complex<ValueType> operator*(const ValueType& x, const Complex<ValueType>& y){
  53. Complex<ValueType> z;
  54. z.real = y.real * x;
  55. z.imag = y.imag * x;
  56. return z;
  57. }
  58. template <class ValueType> Complex<ValueType> operator+(const ValueType& x, const Complex<ValueType>& y){
  59. Complex<ValueType> z;
  60. z.real = y.real + x;
  61. z.imag = y.imag;
  62. return z;
  63. }
  64. template <class ValueType> Complex<ValueType> operator-(const ValueType& x, const Complex<ValueType>& y){
  65. Complex<ValueType> z;
  66. z.real = y.real - x;
  67. z.imag = y.imag;
  68. return z;
  69. }
  70. enum class FFT_Type {R2C, C2C, C2C_INV, C2R};
  71. template <class ValueType> class FFT {
  72. typedef Complex<ValueType> ComplexType;
  73. struct FFTPlan {
  74. std::vector<Matrix<ValueType>> M;
  75. FFT_Type fft_type;
  76. Long howmany;
  77. };
  78. public:
  79. void Setup(FFT_Type fft_type, Long howmany, const Vector<Long>& dim_vec) {
  80. Long rank = dim_vec.Dim();
  81. plan.fft_type = fft_type;
  82. plan.howmany = howmany;
  83. plan.M.resize(0);
  84. if (fft_type == FFT_Type::R2C) {
  85. plan.M.push_back(fft_r2c(dim_vec[rank - 1]));
  86. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  87. } else if (fft_type == FFT_Type::C2C) {
  88. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  89. } else if (fft_type == FFT_Type::C2C_INV) {
  90. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  91. } else if (fft_type == FFT_Type::C2R) {
  92. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  93. plan.M.push_back(fft_c2r(dim_vec[rank - 1]));
  94. }
  95. Long N0 = howmany * 2;
  96. Long N1 = howmany * 2;
  97. for (const auto M : plan.M) {
  98. N0 = N0 * M.Dim(0) / 2;
  99. N1 = N1 * M.Dim(1) / 2;
  100. }
  101. }
  102. Long Dim(Integer i) const {
  103. Long N = plan.howmany * 2;
  104. for (const auto M : plan.M) N = N * M.Dim(i) / 2;
  105. return N;
  106. }
  107. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  108. Long howmany = plan.howmany;
  109. Long N0 = Dim(0);
  110. Long N1 = Dim(1);
  111. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  112. if (out.Dim() != N1) out.ReInit(N1);
  113. Vector<ValueType> buff0(N0 + N1);
  114. Vector<ValueType> buff1(N0 + N1);
  115. Long rank = plan.M.size();
  116. if (rank <= 0) return;
  117. Long N = N0;
  118. if (plan.fft_type == FFT_Type::C2R) {
  119. const Matrix<ValueType>& M = plan.M[rank - 1];
  120. transpose<ComplexType>(buff0.begin(), in.begin(), N / M.Dim(0), M.Dim(0) / 2);
  121. for (Long i = 0; i < rank - 1; i++) {
  122. const Matrix<ValueType>& M = plan.M[i];
  123. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  124. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  125. Matrix<ValueType>::GEMM(vo, vi, M);
  126. N = N * M.Dim(1) / M.Dim(0);
  127. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  128. }
  129. transpose<ComplexType>(buff1.begin(), buff0.begin(), N / howmany / 2, howmany);
  130. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff1.begin(), false);
  131. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), out.begin(), false);
  132. Matrix<ValueType>::GEMM(vo, vi, M);
  133. } else {
  134. memcopy(buff0.begin(), in.begin(), in.Dim());
  135. for (Long i = 0; i < rank; i++) {
  136. const Matrix<ValueType>& M = plan.M[i];
  137. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  138. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  139. Matrix<ValueType>::GEMM(vo, vi, M);
  140. N = N * M.Dim(1) / M.Dim(0);
  141. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  142. }
  143. transpose<ComplexType>(out.begin(), buff0.begin(), N / howmany / 2, howmany);
  144. }
  145. }
  146. static void test() {
  147. Vector<Long> fft_dim;
  148. fft_dim.PushBack(2);
  149. fft_dim.PushBack(5);
  150. fft_dim.PushBack(3);
  151. if (1){ // R2C, C2R
  152. FFT<ValueType> myfft0, myfft1;
  153. myfft0.Setup(FFT_Type::R2C, 1, fft_dim);
  154. myfft1.Setup(FFT_Type::C2R, 1, fft_dim);
  155. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  156. for (int i = 0; i < v0.Dim(); i++) v0[i] = 1 + i;
  157. myfft0.Execute(v0, v1);
  158. myfft1.Execute(v1, v2);
  159. { // Print error
  160. ValueType err = 0;
  161. SCTL_ASSERT(v0.Dim() == v2.Dim());
  162. for (Long i=0;i<v0.Dim();i++) err = std::max(err, fabs(v0[i] - v2[i]));
  163. std::cout<<"Error : "<<err<<'\n';
  164. }
  165. }
  166. std::cout<<'\n';
  167. { // C2C, C2C_INV
  168. FFT<ValueType> myfft0, myfft1;
  169. myfft0.Setup(FFT_Type::C2C, 1, fft_dim);
  170. myfft1.Setup(FFT_Type::C2C_INV, 1, fft_dim);
  171. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  172. for (int i = 0; i < v0.Dim(); i++) v0[i] = 1 + i;
  173. myfft0.Execute(v0, v1);
  174. myfft1.Execute(v1, v2);
  175. { // Print error
  176. ValueType err = 0;
  177. SCTL_ASSERT(v0.Dim() == v2.Dim());
  178. for (Long i=0;i<v0.Dim();i++) err = std::max(err, fabs(v0[i] - v2[i]));
  179. std::cout<<"Error : "<<err<<'\n';
  180. }
  181. }
  182. }
  183. private:
  184. static Matrix<ValueType> fft_r2c(Long N0) {
  185. ValueType s = 1 / sqrt<ValueType>(N0);
  186. Long N1 = (N0 / 2 + 1);
  187. Matrix<ValueType> M(N0, 2 * N1);
  188. for (Long j = 0; j < N0; j++)
  189. for (Long i = 0; i < N1; i++) {
  190. M[j][2 * i + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  191. M[j][2 * i + 1] = sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  192. }
  193. return M;
  194. }
  195. static Matrix<ValueType> fft_c2c(Long N0) {
  196. ValueType s = 1 / sqrt<ValueType>(N0);
  197. Matrix<ValueType> M(2 * N0, 2 * N0);
  198. for (Long i = 0; i < N0; i++)
  199. for (Long j = 0; j < N0; j++) {
  200. M[2 * i + 0][2 * j + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  201. M[2 * i + 1][2 * j + 0] = sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  202. M[2 * i + 0][2 * j + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  203. M[2 * i + 1][2 * j + 1] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  204. }
  205. return M;
  206. }
  207. static Matrix<ValueType> fft_c2r(Long N0) {
  208. ValueType s = 1 / sqrt<ValueType>(N0);
  209. Long N1 = (N0 / 2 + 1);
  210. Matrix<ValueType> M(2 * N1, N0);
  211. for (Long i = 0; i < N1; i++) {
  212. for (Long j = 0; j < N0; j++) {
  213. M[2 * i + 0][j] = 2 * cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  214. M[2 * i + 1][j] = 2 * sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  215. }
  216. }
  217. if (N1 > 0) {
  218. for (Long j = 0; j < N0; j++) {
  219. M[0][j] = M[0][j] * (ValueType)0.5;
  220. M[1][j] = M[1][j] * (ValueType)0.5;
  221. }
  222. }
  223. if (N0 % 2 == 0) {
  224. for (Long j = 0; j < N0; j++) {
  225. M[2 * N1 - 2][j] = M[2 * N1 - 2][j] * (ValueType)0.5;
  226. M[2 * N1 - 1][j] = M[2 * N1 - 1][j] * (ValueType)0.5;
  227. }
  228. }
  229. return M;
  230. }
  231. template <class T> static void transpose(Iterator<ValueType> out, ConstIterator<ValueType> in, Long N0, Long N1) {
  232. Matrix<T> M0(N0, N1, (Iterator<T>)in, false);
  233. Matrix<T> M1(N1, N0, (Iterator<T>)out, false);
  234. M1 = M0.Transpose();
  235. }
  236. FFTPlan plan;
  237. };
  238. } // end namespace
  239. #endif //_SCTL_FFT_WRAPPER_