Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[9770d9]: itpp / signal / transforms.h Maximize Restore History

Download this file

transforms.h    411 lines (344 with data), 12.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*!
* \file
* \brief Fourier, Hadamard, Walsh-Hadamard, and 2D Hadamard transforms -
* header file
* \author Tony Ottosson, Thomas Eriksson, Simon Wood and Adam Piatyszek
*
* -------------------------------------------------------------------------
*
* Copyright (C) 1995-2010 (see AUTHORS file for a list of contributors)
*
* This file is part of IT++ - a C++ library of mathematical, signal
* processing, speech processing, and communications classes and functions.
*
* IT++ is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with IT++. If not, see <http://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#ifndef TRANSFORMS_H
#define TRANSFORMS_H
#include <itpp/base/vec.h>
#include <itpp/base/mat.h>
#include <itpp/base/matfunc.h>
namespace itpp
{
/*!
\addtogroup fft
\brief One dimensional fast fourier transform
\author Tony Ottosson and Adam Piatyszek
The functions \code X = fft(x) \endcode and \code x = ifft(X) \endcode are
the fourier and inverse fourier transforms of size \a N defined as:
\f[
X(k) = \sum_{j=0}^{N-1} x(j) e^{-2\pi j k \cdot i / N}
\f]
\f[
x(j) = \frac{1}{N} \sum_{k=0}^{N-1} X(k) e^{2\pi j k \cdot i / N}
\f]
\code Y = fft(X, N) \endcode performs zero-padding up to size N and then
performs an N-size fft.
The implementation is built upon one of the following libraries:
- FFTW (version 3.0.0 or higher)
- MKL (version 8.0.0 or higher)
- ACML (version 2.5.3 or higher).
\note FFTW-based implementation is the fastest for powers of two.
Furthermore, the second time you call the routine with the same size,
the calculation is much faster due to many things were calculated and
stored the first time the routine was called.
\note Achieving maximum runtime efficiency with the FFTW library on some
computer architectures requires that data are stored in the memory with
a special alignment (to 16-byte boundaries). The IT++ memory management
functions and container classes do not generally allocate memory aligned
this way, and as a result calling FFTW via the IT++ interface (i.e. the
fft() function) may be slower than using the FFTW library directly.
Therefore, FFTW users concerned about maximum possible performance may
want to consider the possibility of calling the FFTW library and its
memory management/allocation routines directly, bypassing the IT++
storage classes and the fft() interface to FFTW.
*/
//!\addtogroup fft
//!@{
//! Run-time check if library is built with Fast Fourier Transforms enabled
bool have_fourier_transforms();
//! Fast Fourier Transform
void fft(const cvec &in, cvec &out);
//! Fast Fourier Transform
cvec fft(const cvec &in);
//! Fast Fourier Transform of subvector or with zero-padding up to size N
cvec fft(const cvec &in, const int N);
//! Inverse Fast Fourier Transform
void ifft(const cvec &in, cvec &out);
//! Inverse Fast Fourier Transform
cvec ifft(const cvec &in);
//! Inverse Fast Fourier Transform of subvector or with zero-padding up to size N
cvec ifft(const cvec &in, const int N);
//! Real Fast Fourier Transform
void fft_real(const vec& in, cvec &out);
//! Real Fast Fourier Transform
cvec fft_real(const vec& in);
/*!
\brief Real Fast Fourier Transform of subvector or zero-padding up to size N
First N points of input vector are used to perform the transform if N < length(in). Padding with 0's is
performed if N > length(in).
*/
cvec fft_real(const vec &in, const int N);
/*!
\brief Inverse Real Fast Fourier Transform.
Underlying implementation assumes Hermitian symmetry of the input spectra. Results are
unpredictable and depending on the implementation (MKL/ACML/FFTW) if this requirement is not met.
*/
void ifft_real(const cvec &in, vec &out);
/*!
\brief Inverse Real Fast Fourier Transform.
Underlying implementation assumes Hermittian symmetry of the input spectra. Results are
unpredictable and depending on the implementation (MKL/ACML/FFTW) if this requirement is not met.
*/
vec ifft_real(const cvec &in);
/*!
\brief Inverse Real Fast Fourier Transformon of N-length subvector or zero-padded input sequence.
First N points of input vector are used to perform the transform if N < length(in). Padding with 0's is
performed if N > length(in).
Underlying implementation assumes Hermitian symmetry of the input subvector/padded sequence. Results are
unpredictable and depending on the implementation (MKL/ACML/FFTW) if this requirement is not met.
*/
vec ifft_real(const cvec &in, const int N);
//!@}
/*!
\addtogroup dct
\brief One dimensional Dicrete Cosine Transform
\author Tony Ottosson and Adam Piatyszek
The functions \code X = dct(x) \endcode and \code x = idct(X) \endcode
are the dicrete cosine and inverse discrete cosine transforms of size \a
N defined as:
\f[
X(k) = w(k) \sum_{j=0}^{N-1} x(j) \cos \left(\frac{(2j+1)k \pi}{2N} \right)
\f]
\f[
x(j) = \sum_{k=0}^{N-1} w(k) X(k) \cos \left(\frac{(2j+1)k \pi}{2N} \right)
\f]
where \f$w(k) = 1/sqrt{N}\f$ for \f$k=0\f$ and
\f$w(k) = sqrt{2/N}\f$ for \f$k\geq 1\f$.
The implementation is built upon one of the following libraries:
- FFTW (version 3.0.0 or higher)
- MKL (version 10.0.0 or higher)
- ACML (version 4.4.0 or higher).
\note FFTW-based implementation is the fastest for powers of two.
Furthermore, the second time you call the routine with the same size,
the calculation is much faster due to many things were calculated and
stored the first time the routine was called.
\note Achieving maximum runtime efficiency with the FFTW library on some
computer architectures requires that data are stored in the memory with
a special alignment (to 16-byte boundaries). The IT++ memory management
functions and container classes do not generally allocate memory aligned
this way, and as a result calling FFTW via the IT++ interface (i.e. the
dct()/idct() function) may be slower than using the FFTW library
directly. Therefore, FFTW users concerned about maximum possible
performance may want to consider the possibility of calling the FFTW
library and its memory management/allocation routines directly,
bypassing the IT++ storage classes and the dct()/idct() interface to
FFTW.
*/
//!\addtogroup dct
//!@{
//! Run-time check if library is built with cosine transfroms enabled
bool have_cosine_transforms();
//! Discrete Cosine Transform (DCT)
void dct(const vec &in, vec &out);
//! Discrete Cosine Transform (DCT)
vec dct(const vec &in);
//! Discrete Cosine Transform (DCT) of subvector or with zero-padding up to size N
vec dct(const vec &in, int N);
//! Inverse Discrete Cosine Transform (IDCT)
void idct(const vec &in, vec &out);
//! Inverse Discrete Cosine Transform (IDCT)
vec idct(const vec &in);
//! Inverse Discrete Cosine Transform (IDCT) of subvector or with zero-padding up to size N
vec idct(const vec &in, int N);
//!@}
//!\addtogroup fht
//!@{
//! Fast Hadamard Transform
template <class T> Vec<T> dht(const Vec<T> &v);
//! Fast Hadamard Transform
template <class T> void dht(const Vec<T> &vin, Vec<T> &vout);
//! Fast Hadamard Transform - memory efficient. Stores the result in \c v
template <class T> void self_dht(Vec<T> &v);
//! Fast Walsh Hadamard Transform
template <class T> Vec<T> dwht(const Vec<T> &v);
//! Fast Walsh Hadamard Transform
template <class T> void dwht(const Vec<T> &vin, Vec<T> &vout);
//! Fast Walsh Hadamard Transform - memory efficient (result in \c v)
template <class T> void self_dwht(Vec<T> &v);
//! Fast 2D Hadamard Transform
template <class T> Mat<T> dht2(const Mat<T> &m);
//! Fast 2D Walsh Hadamard Transform
template <class T> Mat<T> dwht2(const Mat<T> &m);
//!@}
template <class T>
Vec<T> dht(const Vec<T> &v)
{
Vec<T> ret(v.size());
dht(v, ret);
return ret;
}
//! Bit reverse
template <class T>
void bitrv(Vec<T> &out)
{
int N = out.size();
int j = 0;
int N1 = N - 1;
for(int i = 0; i < N1; ++i) {
if(i < j) {
T temp = out[j];
out[j] = out[i];
out[i] = temp;
}
int K = N / 2;
while(K <= j) {
j -= K;
K /= 2;
}
j += K;
}
}
template <class T>
void dht(const Vec<T> &vin, Vec<T> &vout)
{
int N = vin.size();
int m = levels2bits(N);
it_assert_debug((1 << m) == N, "dht(): The vector size must be a power of two");
vout.set_size(N);
// This step is separated because it copies vin to vout
for(int ib = 0; ib < N; ib += 2) {
vout(ib) = vin(ib) + vin(ib + 1);
vout(ib + 1) = vin(ib) - vin(ib + 1);
}
N /= 2;
int l = 2;
for(int i = 1; i < m; ++i) {
N /= 2;
int ib = 0;
for(int k = 0; k < N; ++k) {
for(int j = 0; j < l; ++j) {
T t = vout(ib + j);
vout(ib + j) += vout(ib + j + l);
vout(ib + j + l) = t - vout(ib + j + l);
}
ib += 2 * l;
}
l *= 2;
}
vout /= static_cast<T>(std::sqrt(static_cast<double>(vin.size())));
}
template <class T>
void self_dht(Vec<T> &v)
{
int N = v.size();
int m = levels2bits(N);
it_assert_debug((1 << m) == N, "self_dht(): The vector size must be a power "
"of two");
int l = 1;
for(int i = 0; i < m; ++i) {
N /= 2;
int ib = 0;
for(int k = 0; k < N; ++k) {
for(int j = 0; j < l; ++j) {
T t = v(ib + j);
v(ib + j) += v(ib + j + l);
v(ib + j + l) = t - v(ib + j + l);
}
ib += 2 * l;
}
l *= 2;
}
v /= static_cast<T>(std::sqrt(static_cast<double>(v.size())));
}
template <class T>
Vec<T> dwht(const Vec<T> &v)
{
Vec<T> ret(v.size());
dwht(v, ret);
return ret;
}
template <class T>
void dwht(const Vec<T> &vin, Vec<T> &vout)
{
dht(vin, vout);
bitrv(vout);
}
template <class T>
void self_dwht(Vec<T> &v)
{
self_dht(v);
bitrv(v);
}
template <class T>
Mat<T> dht2(const Mat<T> &m)
{
Mat<T> ret(m.rows(), m.cols());
Vec<T> v;
for(int i = 0; i < m.rows(); ++i) {
v = m.get_row(i);
self_dht(v);
ret.set_row(i, v);
}
for(int i = 0; i < m.cols(); ++i) {
v = ret.get_col(i);
self_dht(v);
ret.set_col(i, v);
}
return transpose(ret);
}
template <class T>
Mat<T> dwht2(const Mat<T> &m)
{
Mat<T> ret(m.rows(), m.cols());
Vec<T> v;
for(int i = 0; i < m.rows(); ++i) {
v = m.get_row(i);
self_dwht(v);
ret.set_row(i, v);
}
for(int i = 0; i < m.cols(); ++i) {
v = ret.get_col(i);
self_dwht(v);
ret.set_col(i, v);
}
return transpose(ret);
}
//! \cond
// ----------------------------------------------------------------------
// Instantiations
// ----------------------------------------------------------------------
#ifndef _MSC_VER
extern template vec dht(const vec &v);
extern template cvec dht(const cvec &v);
extern template void dht(const vec &vin, vec &vout);
extern template void dht(const cvec &vin, cvec &vout);
extern template void self_dht(vec &v);
extern template void self_dht(cvec &v);
extern template vec dwht(const vec &v);
extern template cvec dwht(const cvec &v);
extern template void dwht(const vec &vin, vec &vout);
extern template void dwht(const cvec &vin, cvec &vout);
extern template void self_dwht(vec &v);
extern template void self_dwht(cvec &v);
extern template mat dht2(const mat &m);
extern template cmat dht2(const cmat &m);
extern template mat dwht2(const mat &m);
extern template cmat dwht2(const cmat &m);
#endif // _MSC_VER
//! \endcond
} // namespace itpp
#endif // #ifndef TRANSFORMS_H