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