00001 00031 #ifndef TRANSFORMS_H 00032 #define TRANSFORMS_H 00033 00034 #ifndef _MSC_VER 00035 # include <itpp/config.h> 00036 #else 00037 # include <itpp/config_msvc.h> 00038 #endif 00039 00040 #include <itpp/base/vec.h> 00041 #include <itpp/base/mat.h> 00042 #include <itpp/base/matfunc.h> 00043 00044 00045 namespace itpp 00046 { 00047 00088 00089 00090 00092 void fft(const cvec &in, cvec &out); 00094 cvec fft(const cvec &in); 00096 cvec fft(const cvec &in, const int N); 00098 void ifft(const cvec &in, cvec &out); 00100 cvec ifft(const cvec &in); 00102 cvec ifft(const cvec &in, const int N); 00103 00105 void fft_real(const vec& in, cvec &out); 00107 cvec fft_real(const vec& in); 00109 cvec fft_real(const vec &in, const int N); 00111 void ifft_real(const cvec &in, vec &out); 00113 vec ifft_real(const cvec &in); 00115 vec ifft_real(const cvec &in, const int N); 00117 00118 00160 00161 00162 00164 void dct(const vec &in, vec &out); 00166 vec dct(const vec &in); 00168 void idct(const vec &in, vec &out); 00170 vec idct(const vec &in); 00172 00173 00176 00178 template <class T> Vec<T> dht(const Vec<T> &v); 00180 template <class T> void dht(const Vec<T> &vin, Vec<T> &vout); 00182 template <class T> void self_dht(Vec<T> &v); 00183 00185 template <class T> Vec<T> dwht(const Vec<T> &v); 00187 template <class T> void dwht(const Vec<T> &vin, Vec<T> &vout); 00189 template <class T> void self_dwht(Vec<T> &v); 00190 00192 template <class T> Mat<T> dht2(const Mat<T> &m); 00194 template <class T> Mat<T> dwht2(const Mat<T> &m); 00196 00197 template <class T> 00198 Vec<T> dht(const Vec<T> &v) 00199 { 00200 Vec<T> ret(v.size()); 00201 dht(v, ret); 00202 return ret; 00203 } 00204 00206 template <class T> 00207 void bitrv(Vec<T> &out) 00208 { 00209 int N = out.size(); 00210 int j = 0; 00211 int N1 = N - 1; 00212 for (int i = 0; i < N1; ++i) { 00213 if (i < j) { 00214 T temp = out[j]; 00215 out[j] = out[i]; 00216 out[i] = temp; 00217 } 00218 int K = N / 2; 00219 while (K <= j) { 00220 j -= K; 00221 K /= 2; 00222 } 00223 j += K; 00224 } 00225 } 00226 00227 template <class T> 00228 void dht(const Vec<T> &vin, Vec<T> &vout) 00229 { 00230 int N = vin.size(); 00231 int m = levels2bits(N); 00232 it_assert_debug((1 << m) == N, "dht(): The vector size must be a power of two"); 00233 00234 vout.set_size(N); 00235 00236 // This step is separated because it copies vin to vout 00237 for (int ib = 0; ib < N; ib += 2) { 00238 vout(ib) = vin(ib) + vin(ib + 1); 00239 vout(ib + 1) = vin(ib) - vin(ib + 1); 00240 } 00241 N /= 2; 00242 00243 int l = 2; 00244 for (int i = 1; i < m; ++i) { 00245 N /= 2; 00246 int ib = 0; 00247 for (int k = 0; k < N; ++k) { 00248 for (int j = 0; j < l; ++j) { 00249 T t = vout(ib + j); 00250 vout(ib + j) += vout(ib + j + l); 00251 vout(ib + j + l) = t - vout(ib + j + l); 00252 } 00253 ib += 2 * l; 00254 } 00255 l *= 2; 00256 } 00257 00258 vout /= static_cast<T>(std::sqrt(static_cast<double>(vin.size()))); 00259 } 00260 00261 template <class T> 00262 void self_dht(Vec<T> &v) 00263 { 00264 int N = v.size(); 00265 int m = levels2bits(N); 00266 it_assert_debug((1 << m) == N, "self_dht(): The vector size must be a power " 00267 "of two"); 00268 00269 int l = 1; 00270 for (int i = 0; i < m; ++i) { 00271 N /= 2; 00272 int ib = 0; 00273 for (int k = 0; k < N; ++k) { 00274 for (int j = 0; j < l; ++j) { 00275 T t = v(ib + j); 00276 v(ib + j) += v(ib + j + l); 00277 v(ib + j + l) = t - v(ib + j + l); 00278 } 00279 ib += 2 * l; 00280 } 00281 l *= 2; 00282 } 00283 00284 v /= static_cast<T>(std::sqrt(static_cast<double>(v.size()))); 00285 } 00286 00287 template <class T> 00288 Vec<T> dwht(const Vec<T> &v) 00289 { 00290 Vec<T> ret(v.size()); 00291 dwht(v, ret); 00292 return ret; 00293 } 00294 00295 template <class T> 00296 void dwht(const Vec<T> &vin, Vec<T> &vout) 00297 { 00298 dht(vin, vout); 00299 bitrv(vout); 00300 } 00301 00302 00303 template <class T> 00304 void self_dwht(Vec<T> &v) 00305 { 00306 self_dht(v); 00307 bitrv(v); 00308 } 00309 00310 template <class T> 00311 Mat<T> dht2(const Mat<T> &m) 00312 { 00313 Mat<T> ret(m.rows(), m.cols()); 00314 Vec<T> v; 00315 00316 for (int i = 0; i < m.rows(); ++i) { 00317 v = m.get_row(i); 00318 self_dht(v); 00319 ret.set_row(i, v); 00320 } 00321 for (int i = 0; i < m.cols(); ++i) { 00322 v = ret.get_col(i); 00323 self_dht(v); 00324 ret.set_col(i, v); 00325 } 00326 00327 return transpose(ret); 00328 } 00329 00330 template <class T> 00331 Mat<T> dwht2(const Mat<T> &m) 00332 { 00333 Mat<T> ret(m.rows(), m.cols()); 00334 Vec<T> v; 00335 00336 for (int i = 0; i < m.rows(); ++i) { 00337 v = m.get_row(i); 00338 self_dwht(v); 00339 ret.set_row(i, v); 00340 } 00341 for (int i = 0; i < m.cols(); ++i) { 00342 v = ret.get_col(i); 00343 self_dwht(v); 00344 ret.set_col(i, v); 00345 } 00346 00347 return transpose(ret); 00348 } 00349 00351 00352 // ---------------------------------------------------------------------- 00353 // Instantiations 00354 // ---------------------------------------------------------------------- 00355 00356 #ifdef HAVE_EXTERN_TEMPLATE 00357 00358 extern template vec dht(const vec &v); 00359 extern template cvec dht(const cvec &v); 00360 extern template void dht(const vec &vin, vec &vout); 00361 extern template void dht(const cvec &vin, cvec &vout); 00362 00363 extern template void self_dht(vec &v); 00364 extern template void self_dht(cvec &v); 00365 00366 extern template vec dwht(const vec &v); 00367 extern template cvec dwht(const cvec &v); 00368 extern template void dwht(const vec &vin, vec &vout); 00369 extern template void dwht(const cvec &vin, cvec &vout); 00370 00371 extern template void self_dwht(vec &v); 00372 extern template void self_dwht(cvec &v); 00373 00374 extern template mat dht2(const mat &m); 00375 extern template cmat dht2(const cmat &m); 00376 00377 extern template mat dwht2(const mat &m); 00378 extern template cmat dwht2(const cmat &m); 00379 00380 #endif // HAVE_EXTERN_TEMPLATE 00381 00383 00384 } // namespace itpp 00385 00386 #endif // #ifndef TRANSFORMS_H
Generated on Fri May 1 11:09:19 2009 for IT++ by Doxygen 1.5.8