IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
transforms.h
Go to the documentation of this file.
1 
30 #ifndef TRANSFORMS_H
31 #define TRANSFORMS_H
32 
33 #include <itpp/base/vec.h>
34 #include <itpp/base/mat.h>
35 #include <itpp/base/matfunc.h>
36 
37 
38 namespace itpp
39 {
40 
81 
82 
83 
85 void fft(const cvec &in, cvec &out);
87 cvec fft(const cvec &in);
89 cvec fft(const cvec &in, const int N);
91 void ifft(const cvec &in, cvec &out);
93 cvec ifft(const cvec &in);
95 cvec ifft(const cvec &in, const int N);
96 
98 void fft_real(const vec& in, cvec &out);
100 cvec fft_real(const vec& in);
102 cvec fft_real(const vec &in, const int N);
104 void ifft_real(const cvec &in, vec &out);
106 vec ifft_real(const cvec &in);
108 vec ifft_real(const cvec &in, const int N);
110 
111 
153 
154 
155 
157 void dct(const vec &in, vec &out);
159 vec dct(const vec &in);
161 void idct(const vec &in, vec &out);
163 vec idct(const vec &in);
165 
166 
169 
171 template <class T> Vec<T> dht(const Vec<T> &v);
173 template <class T> void dht(const Vec<T> &vin, Vec<T> &vout);
175 template <class T> void self_dht(Vec<T> &v);
176 
178 template <class T> Vec<T> dwht(const Vec<T> &v);
180 template <class T> void dwht(const Vec<T> &vin, Vec<T> &vout);
182 template <class T> void self_dwht(Vec<T> &v);
183 
185 template <class T> Mat<T> dht2(const Mat<T> &m);
187 template <class T> Mat<T> dwht2(const Mat<T> &m);
189 
190 template <class T>
191 Vec<T> dht(const Vec<T> &v)
192 {
193  Vec<T> ret(v.size());
194  dht(v, ret);
195  return ret;
196 }
197 
199 template <class T>
200 void bitrv(Vec<T> &out)
201 {
202  int N = out.size();
203  int j = 0;
204  int N1 = N - 1;
205  for (int i = 0; i < N1; ++i) {
206  if (i < j) {
207  T temp = out[j];
208  out[j] = out[i];
209  out[i] = temp;
210  }
211  int K = N / 2;
212  while (K <= j) {
213  j -= K;
214  K /= 2;
215  }
216  j += K;
217  }
218 }
219 
220 template <class T>
221 void dht(const Vec<T> &vin, Vec<T> &vout)
222 {
223  int N = vin.size();
224  int m = levels2bits(N);
225  it_assert_debug((1 << m) == N, "dht(): The vector size must be a power of two");
226 
227  vout.set_size(N);
228 
229  // This step is separated because it copies vin to vout
230  for (int ib = 0; ib < N; ib += 2) {
231  vout(ib) = vin(ib) + vin(ib + 1);
232  vout(ib + 1) = vin(ib) - vin(ib + 1);
233  }
234  N /= 2;
235 
236  int l = 2;
237  for (int i = 1; i < m; ++i) {
238  N /= 2;
239  int ib = 0;
240  for (int k = 0; k < N; ++k) {
241  for (int j = 0; j < l; ++j) {
242  T t = vout(ib + j);
243  vout(ib + j) += vout(ib + j + l);
244  vout(ib + j + l) = t - vout(ib + j + l);
245  }
246  ib += 2 * l;
247  }
248  l *= 2;
249  }
250 
251  vout /= static_cast<T>(std::sqrt(static_cast<double>(vin.size())));
252 }
253 
254 template <class T>
255 void self_dht(Vec<T> &v)
256 {
257  int N = v.size();
258  int m = levels2bits(N);
259  it_assert_debug((1 << m) == N, "self_dht(): The vector size must be a power "
260  "of two");
261 
262  int l = 1;
263  for (int i = 0; i < m; ++i) {
264  N /= 2;
265  int ib = 0;
266  for (int k = 0; k < N; ++k) {
267  for (int j = 0; j < l; ++j) {
268  T t = v(ib + j);
269  v(ib + j) += v(ib + j + l);
270  v(ib + j + l) = t - v(ib + j + l);
271  }
272  ib += 2 * l;
273  }
274  l *= 2;
275  }
276 
277  v /= static_cast<T>(std::sqrt(static_cast<double>(v.size())));
278 }
279 
280 template <class T>
281 Vec<T> dwht(const Vec<T> &v)
282 {
283  Vec<T> ret(v.size());
284  dwht(v, ret);
285  return ret;
286 }
287 
288 template <class T>
289 void dwht(const Vec<T> &vin, Vec<T> &vout)
290 {
291  dht(vin, vout);
292  bitrv(vout);
293 }
294 
295 
296 template <class T>
298 {
299  self_dht(v);
300  bitrv(v);
301 }
302 
303 template <class T>
304 Mat<T> dht2(const Mat<T> &m)
305 {
306  Mat<T> ret(m.rows(), m.cols());
307  Vec<T> v;
308 
309  for (int i = 0; i < m.rows(); ++i) {
310  v = m.get_row(i);
311  self_dht(v);
312  ret.set_row(i, v);
313  }
314  for (int i = 0; i < m.cols(); ++i) {
315  v = ret.get_col(i);
316  self_dht(v);
317  ret.set_col(i, v);
318  }
319 
320  return transpose(ret);
321 }
322 
323 template <class T>
324 Mat<T> dwht2(const Mat<T> &m)
325 {
326  Mat<T> ret(m.rows(), m.cols());
327  Vec<T> v;
328 
329  for (int i = 0; i < m.rows(); ++i) {
330  v = m.get_row(i);
331  self_dwht(v);
332  ret.set_row(i, v);
333  }
334  for (int i = 0; i < m.cols(); ++i) {
335  v = ret.get_col(i);
336  self_dwht(v);
337  ret.set_col(i, v);
338  }
339 
340  return transpose(ret);
341 }
342 
344 
345 // ----------------------------------------------------------------------
346 // Instantiations
347 // ----------------------------------------------------------------------
348 
349 #ifndef _MSC_VER
350 
351 extern template vec dht(const vec &v);
352 extern template cvec dht(const cvec &v);
353 extern template void dht(const vec &vin, vec &vout);
354 extern template void dht(const cvec &vin, cvec &vout);
355 
356 extern template void self_dht(vec &v);
357 extern template void self_dht(cvec &v);
358 
359 extern template vec dwht(const vec &v);
360 extern template cvec dwht(const cvec &v);
361 extern template void dwht(const vec &vin, vec &vout);
362 extern template void dwht(const cvec &vin, cvec &vout);
363 
364 extern template void self_dwht(vec &v);
365 extern template void self_dwht(cvec &v);
366 
367 extern template mat dht2(const mat &m);
368 extern template cmat dht2(const cmat &m);
369 
370 extern template mat dwht2(const mat &m);
371 extern template cmat dwht2(const cmat &m);
372 
373 #endif // _MSC_VER
374 
376 
377 } // namespace itpp
378 
379 #endif // #ifndef TRANSFORMS_H
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:14 for IT++ by Doxygen 1.8.1.2