op_fft_meat.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup op_fft
  16. //! @{
  17. //
  18. // op_fft_real
  19. template<typename T1>
  20. inline
  21. void
  22. op_fft_real::apply( Mat< std::complex<typename T1::pod_type> >& out, const mtOp<std::complex<typename T1::pod_type>,T1,op_fft_real>& in )
  23. {
  24. arma_extra_debug_sigprint();
  25. typedef typename T1::pod_type in_eT;
  26. typedef typename std::complex<in_eT> out_eT;
  27. const Proxy<T1> P(in.m);
  28. const uword n_rows = P.get_n_rows();
  29. const uword n_cols = P.get_n_cols();
  30. const uword n_elem = P.get_n_elem();
  31. const bool is_vec = ( (n_rows == 1) || (n_cols == 1) );
  32. const uword N_orig = (is_vec) ? n_elem : n_rows;
  33. const uword N_user = (in.aux_uword_b == 0) ? in.aux_uword_a : N_orig;
  34. fft_engine<out_eT,false> worker(N_user);
  35. // no need to worry about aliasing, as we're going from a real object to complex complex, which by definition cannot alias
  36. if(is_vec)
  37. {
  38. (n_cols == 1) ? out.set_size(N_user, 1) : out.set_size(1, N_user);
  39. if( (out.n_elem == 0) || (N_orig == 0) )
  40. {
  41. out.zeros();
  42. return;
  43. }
  44. if( (N_user == 1) && (N_orig >= 1) )
  45. {
  46. out[0] = out_eT( P[0] );
  47. return;
  48. }
  49. podarray<out_eT> data(N_user);
  50. out_eT* data_mem = data.memptr();
  51. if(N_user > N_orig) { arrayops::fill_zeros( &data_mem[N_orig], (N_user - N_orig) ); }
  52. const uword N = (std::min)(N_user, N_orig);
  53. if(Proxy<T1>::use_at == false)
  54. {
  55. typename Proxy<T1>::ea_type X = P.get_ea();
  56. for(uword i=0; i < N; ++i) { data_mem[i] = out_eT( X[i], in_eT(0) ); }
  57. }
  58. else
  59. {
  60. if(n_cols == 1)
  61. {
  62. for(uword i=0; i < N; ++i) { data_mem[i] = out_eT( P.at(i,0), in_eT(0) ); }
  63. }
  64. else
  65. {
  66. for(uword i=0; i < N; ++i) { data_mem[i] = out_eT( P.at(0,i), in_eT(0) ); }
  67. }
  68. }
  69. worker.run( out.memptr(), data_mem );
  70. }
  71. else
  72. {
  73. // process each column seperately
  74. out.set_size(N_user, n_cols);
  75. if( (out.n_elem == 0) || (N_orig == 0) )
  76. {
  77. out.zeros();
  78. return;
  79. }
  80. if( (N_user == 1) && (N_orig >= 1) )
  81. {
  82. for(uword col=0; col < n_cols; ++col) { out.at(0,col) = out_eT( P.at(0,col) ); }
  83. return;
  84. }
  85. podarray<out_eT> data(N_user);
  86. out_eT* data_mem = data.memptr();
  87. if(N_user > N_orig) { arrayops::fill_zeros( &data_mem[N_orig], (N_user - N_orig) ); }
  88. const uword N = (std::min)(N_user, N_orig);
  89. for(uword col=0; col < n_cols; ++col)
  90. {
  91. for(uword i=0; i < N; ++i) { data_mem[i] = P.at(i, col); }
  92. worker.run( out.colptr(col), data_mem );
  93. }
  94. }
  95. }
  96. //
  97. // op_fft_cx
  98. template<typename T1>
  99. inline
  100. void
  101. op_fft_cx::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_fft_cx>& in)
  102. {
  103. arma_extra_debug_sigprint();
  104. typedef typename T1::elem_type eT;
  105. const Proxy<T1> P(in.m);
  106. if(P.is_alias(out) == false)
  107. {
  108. op_fft_cx::apply_noalias<T1,false>(out, P, in.aux_uword_a, in.aux_uword_b);
  109. }
  110. else
  111. {
  112. Mat<eT> tmp;
  113. op_fft_cx::apply_noalias<T1,false>(tmp, P, in.aux_uword_a, in.aux_uword_b);
  114. out.steal_mem(tmp);
  115. }
  116. }
  117. template<typename T1, bool inverse>
  118. inline
  119. void
  120. op_fft_cx::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P, const uword a, const uword b)
  121. {
  122. arma_extra_debug_sigprint();
  123. typedef typename T1::elem_type eT;
  124. const uword n_rows = P.get_n_rows();
  125. const uword n_cols = P.get_n_cols();
  126. const uword n_elem = P.get_n_elem();
  127. const bool is_vec = ( (n_rows == 1) || (n_cols == 1) );
  128. const uword N_orig = (is_vec) ? n_elem : n_rows;
  129. const uword N_user = (b == 0) ? a : N_orig;
  130. fft_engine<eT,inverse> worker(N_user);
  131. if(is_vec)
  132. {
  133. (n_cols == 1) ? out.set_size(N_user, 1) : out.set_size(1, N_user);
  134. if( (out.n_elem == 0) || (N_orig == 0) )
  135. {
  136. out.zeros();
  137. return;
  138. }
  139. if( (N_user == 1) && (N_orig >= 1) )
  140. {
  141. out[0] = P[0];
  142. return;
  143. }
  144. if( (N_user > N_orig) || (is_Mat<typename Proxy<T1>::stored_type>::value == false) )
  145. {
  146. podarray<eT> data(N_user);
  147. eT* data_mem = data.memptr();
  148. if(N_user > N_orig) { arrayops::fill_zeros( &data_mem[N_orig], (N_user - N_orig) ); }
  149. op_fft_cx::copy_vec( data_mem, P, (std::min)(N_user, N_orig) );
  150. worker.run( out.memptr(), data_mem );
  151. }
  152. else
  153. {
  154. const unwrap< typename Proxy<T1>::stored_type > tmp(P.Q);
  155. worker.run( out.memptr(), tmp.M.memptr() );
  156. }
  157. }
  158. else
  159. {
  160. // process each column seperately
  161. out.set_size(N_user, n_cols);
  162. if( (out.n_elem == 0) || (N_orig == 0) )
  163. {
  164. out.zeros();
  165. return;
  166. }
  167. if( (N_user == 1) && (N_orig >= 1) )
  168. {
  169. for(uword col=0; col < n_cols; ++col) { out.at(0,col) = P.at(0,col); }
  170. return;
  171. }
  172. if( (N_user > N_orig) || (is_Mat<typename Proxy<T1>::stored_type>::value == false) )
  173. {
  174. podarray<eT> data(N_user);
  175. eT* data_mem = data.memptr();
  176. if(N_user > N_orig) { arrayops::fill_zeros( &data_mem[N_orig], (N_user - N_orig) ); }
  177. const uword N = (std::min)(N_user, N_orig);
  178. for(uword col=0; col < n_cols; ++col)
  179. {
  180. for(uword i=0; i < N; ++i) { data_mem[i] = P.at(i, col); }
  181. worker.run( out.colptr(col), data_mem );
  182. }
  183. }
  184. else
  185. {
  186. const unwrap< typename Proxy<T1>::stored_type > tmp(P.Q);
  187. for(uword col=0; col < n_cols; ++col)
  188. {
  189. worker.run( out.colptr(col), tmp.M.colptr(col) );
  190. }
  191. }
  192. }
  193. // correct the scaling for the inverse transform
  194. if(inverse == true)
  195. {
  196. typedef typename get_pod_type<eT>::result T;
  197. const T k = T(1) / T(N_user);
  198. eT* out_mem = out.memptr();
  199. const uword out_n_elem = out.n_elem;
  200. for(uword i=0; i < out_n_elem; ++i) { out_mem[i] *= k; }
  201. }
  202. }
  203. template<typename T1>
  204. arma_hot
  205. inline
  206. void
  207. op_fft_cx::copy_vec(typename Proxy<T1>::elem_type* dest, const Proxy<T1>& P, const uword N)
  208. {
  209. arma_extra_debug_sigprint();
  210. if(is_Mat< typename Proxy<T1>::stored_type >::value == true)
  211. {
  212. op_fft_cx::copy_vec_unwrap(dest, P, N);
  213. }
  214. else
  215. {
  216. op_fft_cx::copy_vec_proxy(dest, P, N);
  217. }
  218. }
  219. template<typename T1>
  220. arma_hot
  221. inline
  222. void
  223. op_fft_cx::copy_vec_unwrap(typename Proxy<T1>::elem_type* dest, const Proxy<T1>& P, const uword N)
  224. {
  225. arma_extra_debug_sigprint();
  226. const unwrap< typename Proxy<T1>::stored_type > tmp(P.Q);
  227. arrayops::copy(dest, tmp.M.memptr(), N);
  228. }
  229. template<typename T1>
  230. arma_hot
  231. inline
  232. void
  233. op_fft_cx::copy_vec_proxy(typename Proxy<T1>::elem_type* dest, const Proxy<T1>& P, const uword N)
  234. {
  235. arma_extra_debug_sigprint();
  236. if(Proxy<T1>::use_at == false)
  237. {
  238. typename Proxy<T1>::ea_type X = P.get_ea();
  239. for(uword i=0; i < N; ++i) { dest[i] = X[i]; }
  240. }
  241. else
  242. {
  243. if(P.get_n_cols() == 1)
  244. {
  245. for(uword i=0; i < N; ++i) { dest[i] = P.at(i,0); }
  246. }
  247. else
  248. {
  249. for(uword i=0; i < N; ++i) { dest[i] = P.at(0,i); }
  250. }
  251. }
  252. }
  253. //
  254. // op_ifft_cx
  255. template<typename T1>
  256. inline
  257. void
  258. op_ifft_cx::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_ifft_cx>& in)
  259. {
  260. arma_extra_debug_sigprint();
  261. typedef typename T1::elem_type eT;
  262. const Proxy<T1> P(in.m);
  263. if(P.is_alias(out) == false)
  264. {
  265. op_fft_cx::apply_noalias<T1,true>(out, P, in.aux_uword_a, in.aux_uword_b);
  266. }
  267. else
  268. {
  269. Mat<eT> tmp;
  270. op_fft_cx::apply_noalias<T1,true>(tmp, P, in.aux_uword_a, in.aux_uword_b);
  271. out.steal_mem(tmp);
  272. }
  273. }
  274. //! @}