IT++ 4.3.1
transforms.h
Go to the documentation of this file.
1
29
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#include <itpp/itexports.h>
37
38
39namespace itpp
40{
41
81
84
86ITPP_EXPORT bool have_fourier_transforms();
88ITPP_EXPORT void fft(const cvec &in, cvec &out);
90ITPP_EXPORT cvec fft(const cvec &in);
97ITPP_EXPORT cvec fft(const cvec &in, const int N);
99ITPP_EXPORT void ifft(const cvec &in, cvec &out);
101ITPP_EXPORT cvec ifft(const cvec &in);
108ITPP_EXPORT cvec ifft(const cvec &in, const int N);
109
111ITPP_EXPORT void fft_real(const vec& in, cvec &out);
113ITPP_EXPORT cvec fft_real(const vec& in);
120ITPP_EXPORT cvec fft_real(const vec &in, const int N);
127ITPP_EXPORT void ifft_real(const cvec &in, vec &out);
134ITPP_EXPORT vec ifft_real(const cvec &in);
144ITPP_EXPORT vec ifft_real(const cvec &in, const int N);
146
147
188
191
193ITPP_EXPORT bool have_cosine_transforms();
195ITPP_EXPORT void dct(const vec &in, vec &out);
197ITPP_EXPORT vec dct(const vec &in);
204ITPP_EXPORT vec dct(const vec &in, const int N);
206ITPP_EXPORT void idct(const vec &in, vec &out);
208ITPP_EXPORT vec idct(const vec &in);
215ITPP_EXPORT vec idct(const vec &in, const int N);
217
218
221
223template <class T> Vec<T> dht(const Vec<T> &v);
225template <class T> void dht(const Vec<T> &vin, Vec<T> &vout);
227template <class T> void self_dht(Vec<T> &v);
228
230template <class T> Vec<T> dwht(const Vec<T> &v);
232template <class T> void dwht(const Vec<T> &vin, Vec<T> &vout);
234template <class T> void self_dwht(Vec<T> &v);
235
237template <class T> Mat<T> dht2(const Mat<T> &m);
239template <class T> Mat<T> dwht2(const Mat<T> &m);
241
242template <class T>
244{
245 Vec<T> ret(v.size());
246 dht(v, ret);
247 return ret;
248}
249
251template <class T>
252void bitrv(Vec<T> &out)
253{
254 int N = out.size();
255 int j = 0;
256 int N1 = N - 1;
257 for(int i = 0; i < N1; ++i) {
258 if(i < j) {
259 T temp = out[j];
260 out[j] = out[i];
261 out[i] = temp;
262 }
263 int K = N / 2;
264 while(K <= j) {
265 j -= K;
266 K /= 2;
267 }
268 j += K;
269 }
270}
271
272template <class T>
273void dht(const Vec<T> &vin, Vec<T> &vout)
274{
275 int N = vin.size();
276 int m = levels2bits(N);
277 it_assert_debug((1 << m) == N, "dht(): The vector size must be a power of two");
278
279 vout.set_size(N);
280
281 // This step is separated because it copies vin to vout
282 for(int ib = 0; ib < N; ib += 2) {
283 vout(ib) = vin(ib) + vin(ib + 1);
284 vout(ib + 1) = vin(ib) - vin(ib + 1);
285 }
286 N /= 2;
287
288 int l = 2;
289 for(int i = 1; i < m; ++i) {
290 N /= 2;
291 int ib = 0;
292 for(int k = 0; k < N; ++k) {
293 for(int j = 0; j < l; ++j) {
294 T t = vout(ib + j);
295 vout(ib + j) += vout(ib + j + l);
296 vout(ib + j + l) = t - vout(ib + j + l);
297 }
298 ib += 2 * l;
299 }
300 l *= 2;
301 }
302
303 vout /= static_cast<T>(std::sqrt(static_cast<double>(vin.size())));
304}
305
306template <class T>
308{
309 int N = v.size();
310 int m = levels2bits(N);
311 it_assert_debug((1 << m) == N, "self_dht(): The vector size must be a power "
312 "of two");
313
314 int l = 1;
315 for(int i = 0; i < m; ++i) {
316 N /= 2;
317 int ib = 0;
318 for(int k = 0; k < N; ++k) {
319 for(int j = 0; j < l; ++j) {
320 T t = v(ib + j);
321 v(ib + j) += v(ib + j + l);
322 v(ib + j + l) = t - v(ib + j + l);
323 }
324 ib += 2 * l;
325 }
326 l *= 2;
327 }
328
329 v /= static_cast<T>(std::sqrt(static_cast<double>(v.size())));
330}
331
332template <class T>
334{
335 Vec<T> ret(v.size());
336 dwht(v, ret);
337 return ret;
338}
339
340template <class T>
341void dwht(const Vec<T> &vin, Vec<T> &vout)
342{
343 dht(vin, vout);
344 bitrv(vout);
345}
346
347
348template <class T>
350{
351 self_dht(v);
352 bitrv(v);
353}
354
355template <class T>
357{
358 Mat<T> ret(m.rows(), m.cols());
359 Vec<T> v;
360
361 for(int i = 0; i < m.rows(); ++i) {
362 v = m.get_row(i);
363 self_dht(v);
364 ret.set_row(i, v);
365 }
366 for(int i = 0; i < m.cols(); ++i) {
367 v = ret.get_col(i);
368 self_dht(v);
369 ret.set_col(i, v);
370 }
371
372 return transpose(ret);
373}
374
375template <class T>
377{
378 Mat<T> ret(m.rows(), m.cols());
379 Vec<T> v;
380
381 for(int i = 0; i < m.rows(); ++i) {
382 v = m.get_row(i);
383 self_dwht(v);
384 ret.set_row(i, v);
385 }
386 for(int i = 0; i < m.cols(); ++i) {
387 v = ret.get_col(i);
388 self_dwht(v);
389 ret.set_col(i, v);
390 }
391
392 return transpose(ret);
393}
394
396
397// ----------------------------------------------------------------------
398// Instantiations
399// ----------------------------------------------------------------------
400
401ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec dht(const vec &v);
402ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec dht(const cvec &v);
403ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void dht(const vec &vin, vec &vout);
404ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void dht(const cvec &vin, cvec &vout);
405
406ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void self_dht(vec &v);
407ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void self_dht(cvec &v);
408
409ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec dwht(const vec &v);
410ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec dwht(const cvec &v);
411ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void dwht(const vec &vin, vec &vout);
412ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void dwht(const cvec &vin, cvec &vout);
413
414ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void self_dwht(vec &v);
415ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void self_dwht(cvec &v);
416
417ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat dht2(const mat &m);
418ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat dht2(const cmat &m);
419
420ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat dwht2(const mat &m);
421ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat dwht2(const cmat &m);
422
424
425} // namespace itpp
426
427#endif // #ifndef TRANSFORMS_H
Matrix Class (Templated)
Definition mat.h:202
Vec< Num_T > get_row(int r) const
Get row r.
Definition mat.h:852
void set_col(int c, const Vec< Num_T > &v)
Set column c to vector v.
Definition mat.h:936
int rows() const
The number of rows.
Definition mat.h:237
int cols() const
The number of columns.
Definition mat.h:235
void set_row(int r, const Vec< Num_T > &v)
Set row r to vector v.
Definition mat.h:927
Vec< Num_T > get_col(int c) const
Get column c.
Definition mat.h:889
Vector Class (Templated)
Definition vec.h:245
int size() const
The size of the vector.
Definition vec.h:271
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition vec.h:663
ITPP_EXPORT void idct(const vec &in, vec &out)
Inverse Discrete Cosine Transform (IDCT)
ITPP_EXPORT void dct(const vec &in, vec &out)
Discrete Cosine Transform (DCT)
ITPP_EXPORT bool have_cosine_transforms()
Run-time test if library is built with cosine transforms enabled.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
ITPP_EXPORT void ifft(const cvec &in, cvec &out)
Inverse Fast Fourier Transform.
ITPP_EXPORT void fft_real(const vec &in, cvec &out)
Real Fast Fourier Transform.
ITPP_EXPORT void ifft_real(const cvec &in, vec &out)
Inverse Real Fast Fourier Transform.
ITPP_EXPORT bool have_fourier_transforms()
Run-time test if library is built with Fast Fourier Transforms enabled.
ITPP_EXPORT void fft(const cvec &in, cvec &out)
Fast Fourier Transform.
Mat< T > dwht2(const Mat< T > &m)
Fast 2D Walsh Hadamard Transform.
Definition transforms.h:376
Vec< T > dwht(const Vec< T > &v)
Fast Walsh Hadamard Transform.
Definition transforms.h:333
Vec< T > dht(const Vec< T > &v)
Fast Hadamard Transform.
Definition transforms.h:243
void self_dht(Vec< T > &v)
Fast Hadamard Transform - memory efficient. Stores the result in v.
Definition transforms.h:307
Mat< T > dht2(const Mat< T > &m)
Fast 2D Hadamard Transform.
Definition transforms.h:356
void self_dwht(Vec< T > &v)
Fast Walsh Hadamard Transform - memory efficient (result in v)
Definition transforms.h:349
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Definition log_exp.h:92
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Definition matfunc.h:308
Matrix Class Definitions.
Various functions on vectors and matrices - header file.
itpp namespace
Definition itmex.h:37
void bitrv(Vec< T > &out)
Bit reverse.
Definition transforms.h:252
Templated Vector Class Definitions.