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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
/*!
* \file
* \brief Fourier, Cosine, Hadamard, Walsh-Hadamard, and 2D Hadamard
* transforms - source file
* \author Tony Ottosson, Thomas Eriksson, Simon Wood and Adam Piatyszek
*
* -------------------------------------------------------------------------
*
* IT++ - C++ library of mathematical, signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 1995-2008 (see AUTHORS file for a list of contributors)
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
#ifndef _MSC_VER
# include <itpp/config.h>
#else
# include <itpp/config_msvc.h>
#endif
#if defined(HAVE_FFT_MKL)
namespace mkl {
# include <mkl_dfti.h>
}
#elif defined(HAVE_FFT_ACML)
namespace acml {
# include <acml.h>
}
#elif defined(HAVE_FFTW3)
# include <fftw3.h>
#endif
#include <itpp/signal/transforms.h>
//! \cond
namespace itpp {
#if defined(HAVE_FFT_MKL)
//---------------------------------------------------------------------------
// FFT/IFFT based on MKL
//---------------------------------------------------------------------------
void fft(const cvec &in, cvec &out)
{
static mkl::DFTI_DESCRIPTOR* fft_handle = NULL;
static int N;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (fft_handle != NULL) mkl::DftiFreeDescriptor(&fft_handle);
mkl::DftiCreateDescriptor(&fft_handle, mkl::DFTI_DOUBLE, mkl::DFTI_COMPLEX, 1, N);
mkl::DftiSetValue(fft_handle, mkl::DFTI_PLACEMENT, mkl::DFTI_NOT_INPLACE);
mkl::DftiCommitDescriptor(fft_handle);
}
mkl::DftiComputeForward(fft_handle, (void *)in._data(), out._data());
}
void ifft(const cvec &in, cvec &out)
{
static mkl::DFTI_DESCRIPTOR* fft_handle = NULL;
static int N;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (fft_handle != NULL) mkl::DftiFreeDescriptor(&fft_handle);
mkl::DftiCreateDescriptor(&fft_handle, mkl::DFTI_DOUBLE, mkl::DFTI_COMPLEX, 1, N);
mkl::DftiSetValue(fft_handle, mkl::DFTI_PLACEMENT, mkl::DFTI_NOT_INPLACE);
mkl::DftiSetValue(fft_handle, mkl::DFTI_BACKWARD_SCALE, 1.0/N);
mkl::DftiCommitDescriptor(fft_handle);
}
mkl::DftiComputeBackward(fft_handle, (void *)in._data(), out._data());
}
void fft_real(const vec &in, cvec &out)
{
static mkl::DFTI_DESCRIPTOR* fft_handle = NULL;
static int N;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (fft_handle != NULL) mkl::DftiFreeDescriptor(&fft_handle);
mkl::DftiCreateDescriptor(&fft_handle, mkl::DFTI_DOUBLE, mkl::DFTI_REAL, 1, N);
mkl::DftiSetValue(fft_handle, mkl::DFTI_PLACEMENT, mkl::DFTI_NOT_INPLACE);
mkl::DftiCommitDescriptor(fft_handle);
}
mkl::DftiComputeForward(fft_handle, (void *)in._data(), out._data());
// Real FFT does not compute the 2nd half of the FFT points because it
// is redundant to the 1st half. However, we want all of the data so we
// fill it in. This is consistent with Matlab's functionality
int istart = ceil_i(in.size() / 2.0);
int iend = in.size() - 1;
int idelta = iend - istart + 1;
out.set_subvector(istart, iend, reverse(conj(out(1, idelta))));
}
void ifft_real(const cvec &in, vec &out)
{
static mkl::DFTI_DESCRIPTOR* fft_handle = NULL;
static int N;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (fft_handle != NULL) mkl::DftiFreeDescriptor(&fft_handle);
mkl::DftiCreateDescriptor( &fft_handle, mkl::DFTI_DOUBLE, mkl::DFTI_REAL, 1, N);
mkl::DftiSetValue(fft_handle, mkl::DFTI_PLACEMENT, mkl::DFTI_NOT_INPLACE);
mkl::DftiSetValue(fft_handle, mkl::DFTI_BACKWARD_SCALE, 1.0/N);
mkl::DftiCommitDescriptor(fft_handle);
}
mkl::DftiComputeBackward(fft_handle, (void *)in._data(), out._data());
}
#endif // #ifdef HAVE_FFT_MKL
#if defined(HAVE_FFT_ACML)
//---------------------------------------------------------------------------
// FFT/IFFT based on ACML
//---------------------------------------------------------------------------
void fft(const cvec &in, cvec &out)
{
static int N = 0;
static cvec *comm_ptr = NULL;
int info;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (comm_ptr != NULL)
delete comm_ptr;
comm_ptr = new cvec(5 * in.size() + 100);
acml::zfft1dx(0, 1.0, false, N, (acml::doublecomplex *)in._data(), 1,
(acml::doublecomplex *)out._data(), 1,
(acml::doublecomplex *)comm_ptr->_data(), &info);
}
acml::zfft1dx(-1, 1.0, false, N, (acml::doublecomplex *)in._data(), 1,
(acml::doublecomplex *)out._data(), 1,
(acml::doublecomplex *)comm_ptr->_data(), &info);
}
void ifft(const cvec &in, cvec &out)
{
static int N = 0;
static cvec *comm_ptr = NULL;
int info;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (comm_ptr != NULL)
delete comm_ptr;
comm_ptr = new cvec(5 * in.size() + 100);
acml::zfft1dx(0, 1.0/N, false, N, (acml::doublecomplex *)in._data(), 1,
(acml::doublecomplex *)out._data(), 1,
(acml::doublecomplex *)comm_ptr->_data(), &info);
}
acml::zfft1dx(1, 1.0/N, false, N, (acml::doublecomplex *)in._data(), 1,
(acml::doublecomplex *)out._data(), 1,
(acml::doublecomplex *)comm_ptr->_data(), &info);
}
void fft_real(const vec &in, cvec &out)
{
static int N = 0;
static double factor = 0;
static vec *comm_ptr = NULL;
int info;
vec out_re = in;
if (N != in.size()) {
N = in.size();
factor = std::sqrt(static_cast<double>(N));
if (comm_ptr != NULL)
delete comm_ptr;
comm_ptr = new vec(5 * in.size() + 100);
acml::dzfft(0, N, out_re._data(), comm_ptr->_data(), &info);
}
acml::dzfft(1, N, out_re._data(), comm_ptr->_data(), &info);
// Normalise output data
out_re *= factor;
// Convert the real Hermitian DZFFT's output to the Matlab's complex form
vec out_im(in.size()); out_im.zeros();
out.set_size(in.size(), false);
out_im.set_subvector(1, reverse(out_re(N/2 + 1, N-1)));
out_im.set_subvector(N/2 + 1, -out_re(N/2 + 1, N-1));
out_re.set_subvector(N/2 + 1, reverse(out_re(1, (N-1)/2)));
out = to_cvec(out_re, out_im);
}
void ifft_real(const cvec &in, vec &out)
{
static int N = 0;
static double factor = 0;
static vec *comm_ptr = NULL;
int info;
// Convert Matlab's complex input to the real Hermitian form
out.set_size(in.size());
out.set_subvector(0, real(in(0, in.size()/2)));
out.set_subvector(in.size()/2 + 1, -imag(in(in.size()/2 + 1, in.size()-1)));
if (N != in.size()) {
N = in.size();
factor = 1.0 / std::sqrt(static_cast<double>(N));
if (comm_ptr != NULL)
delete comm_ptr;
comm_ptr = new vec(5 * in.size() + 100);
acml::zdfft(0, N, out._data(), comm_ptr->_data(), &info);
}
acml::zdfft(1, N, out._data(), comm_ptr->_data(), &info);
out.set_subvector(1, reverse(out(1, N-1)));
// Normalise output data
out *= factor;
}
#endif // defined(HAVE_FFT_ACML)
#if defined(HAVE_FFTW3)
//---------------------------------------------------------------------------
// FFT/IFFT based on FFTW
//---------------------------------------------------------------------------
void fft(const cvec &in, cvec &out)
{
static int N = 0;
static fftw_plan p = NULL;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (p != NULL)
fftw_destroy_plan(p); // destroy the previous plan
// create a new plan
p = fftw_plan_dft_1d(N, (fftw_complex *)in._data(),
(fftw_complex *)out._data(),
FFTW_FORWARD, FFTW_ESTIMATE);
}
// compute FFT using the GURU FFTW interface
fftw_execute_dft(p, (fftw_complex *)in._data(),
(fftw_complex *)out._data());
}
void ifft(const cvec &in, cvec &out)
{
static int N = 0;
static double inv_N;
static fftw_plan p = NULL;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
inv_N = 1.0/N;
if (p != NULL)
fftw_destroy_plan(p); // destroy the previous plan
// create a new plan
p = fftw_plan_dft_1d(N, (fftw_complex *)in._data(),
(fftw_complex *)out._data(),
FFTW_BACKWARD, FFTW_ESTIMATE);
}
// compute IFFT using the GURU FFTW interface
fftw_execute_dft(p, (fftw_complex *)in._data(),
(fftw_complex *)out._data());
// scale output
out *= inv_N;
}
void fft_real(const vec &in, cvec &out)
{
static int N = 0;
static fftw_plan p = NULL;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (p!= NULL)
fftw_destroy_plan(p); //destroy the previous plan
// create a new plan
p = fftw_plan_dft_r2c_1d(N, (double *)in._data(),
(fftw_complex *)out._data(),
FFTW_ESTIMATE);
}
// compute FFT using the GURU FFTW interface
fftw_execute_dft_r2c(p, (double *)in._data(),
(fftw_complex *)out._data());
// Real FFT does not compute the 2nd half of the FFT points because it
// is redundant to the 1st half. However, we want all of the data so we
// fill it in. This is consistent with Matlab's functionality
int offset = ceil_i(in.size() / 2.0);
int n_elem = in.size() - offset;
for (int i = 0; i < n_elem; ++i) {
out(offset + i) = std::conj(out(n_elem - i));
}
}
void ifft_real(const cvec &in, vec & out)
{
static int N = 0;
static double inv_N;
static fftw_plan p = NULL;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
inv_N = 1.0/N;
if (p != NULL)
fftw_destroy_plan(p); // destroy the previous plan
// create a new plan
p = fftw_plan_dft_c2r_1d(N, (fftw_complex *)in._data(),
(double *)out._data(),
FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
}
// compute IFFT using the GURU FFTW interface
fftw_execute_dft_c2r(p, (fftw_complex *)in._data(),
(double *)out._data());
out *= inv_N;
}
//---------------------------------------------------------------------------
// DCT/IDCT based on FFTW
//---------------------------------------------------------------------------
void dct(const vec &in, vec &out)
{
static int N;
static fftw_plan p = NULL;
out.set_size(in.size(), false);
if (N != in.size()) {
N = in.size();
if (p!= NULL)
fftw_destroy_plan(p); // destroy the previous plan
// create a new plan
p = fftw_plan_r2r_1d(N, (double *)in._data(),
(double *)out._data(),
FFTW_REDFT10, FFTW_ESTIMATE);
}
// compute FFT using the GURU FFTW interface
fftw_execute_r2r(p, (double *)in._data(), (double *)out._data());
// Scale to matlab definition format
out /= std::sqrt(2.0 * N);
out(0) /= std::sqrt(2.0);
}
// IDCT
void idct(const vec &in, vec &out)
{
static int N;
static fftw_plan p = NULL;
out = in;
// Rescale to FFTW format
out(0) *= std::sqrt(2.0);
out /= std::sqrt(2.0 * in.size());
if (N != in.size()) {
N = in.size();
if (p != NULL)
fftw_destroy_plan(p); // destroy the previous plan
// create a new plan
p = fftw_plan_r2r_1d(N, (double *)out._data(),
(double *)out._data(),
FFTW_REDFT01, FFTW_ESTIMATE);
}
// compute FFT using the GURU FFTW interface
fftw_execute_r2r(p, (double *)out._data(), (double *)out._data());
}
#endif // defined(HAVE_FFTW3)
#if defined(HAVE_FFT_MKL) || defined(HAVE_FFT_ACML)
//---------------------------------------------------------------------------
// DCT/IDCT based on MKL or ACML
//---------------------------------------------------------------------------
void dct(const vec &in, vec &out)
{
int N = in.size();
if (N == 1)
out = in;
else {
cvec c = fft_real(concat(in, reverse(in)));
c.set_size(N, true);
for (int i = 0; i < N; i++) {
c(i) *= std::complex<double>(std::cos(pi*i/N/2), std::sin(-pi*i/N/2))
/ std::sqrt(2.0 * N);
}
out = real(c);
out(0) /= std::sqrt(2.0);
}
}
void idct(const vec &in, vec &out)
{
int N = in.size();
if (N == 1)
out = in;
else {
cvec c = to_cvec(in);
c.set_size(2*N, true);
c(0) *= std::sqrt(2.0);
for (int i = 0; i < N; i++) {
c(i) *= std::complex<double>(std::cos(pi*i/N/2), std::sin(pi*i/N/2))
* std::sqrt(2.0 * N);
}
for (int i = N - 1; i >= 1; i--) {
c(c.size() - i) = c(i) * std::complex<double>(std::cos(pi*i/N),
std::sin(-pi*i/N));
}
out = ifft_real(c);
out.set_size(N, true);
}
}
#endif // defined(HAVE_FFT_MKL) || defined(HAVE_FFT_ACML)
#if !defined(HAVE_FFT)
void fft(const cvec &in, cvec &out)
{
it_error("FFT library is needed to use fft() function");
}
void ifft(const cvec &in, cvec &out)
{
it_error("FFT library is needed to use ifft() function");
}
void fft_real(const vec &in, cvec &out)
{
it_error("FFT library is needed to use fft_real() function");
}
void ifft_real(const cvec &in, vec & out)
{
it_error("FFT library is needed to use ifft_real() function");
}
void dct(const vec &in, vec &out)
{
it_error("FFT library is needed to use dct() function");
}
void idct(const vec &in, vec &out)
{
it_error("FFT library is needed to use idct() function");
}
#endif // !defined(HAVE_FFT)
cvec fft(const cvec &in)
{
cvec out;
fft(in, out);
return out;
}
cvec fft(const cvec &in, const int N)
{
cvec in2 = in;
cvec out;
in2.set_size(N, true);
fft(in2, out);
return out;
}
cvec ifft(const cvec &in)
{
cvec out;
ifft(in, out);
return out;
}
cvec ifft(const cvec &in, const int N)
{
cvec in2 = in;
cvec out;
in2.set_size(N, true);
ifft(in2, out);
return out;
}
cvec fft_real(const vec& in)
{
cvec out;
fft_real(in, out);
return out;
}
cvec fft_real(const vec& in, const int N)
{
vec in2 = in;
cvec out;
in2.set_size(N, true);
fft_real(in2, out);
return out;
}
vec ifft_real(const cvec &in)
{
vec out;
ifft_real(in, out);
return out;
}
vec ifft_real(const cvec &in, const int N)
{
cvec in2 = in;
in2.set_size(N, true);
vec out;
ifft_real(in2, out);
return out;
}
vec dct(const vec &in)
{
vec out;
dct(in, out);
return out;
}
vec idct(const vec &in)
{
vec out;
idct(in, out);
return out;
}
// ----------------------------------------------------------------------
// Instantiation
// ----------------------------------------------------------------------
template vec dht(const vec &v);
template cvec dht(const cvec &v);
template void dht(const vec &vin, vec &vout);
template void dht(const cvec &vin, cvec &vout);
template void self_dht(vec &v);
template void self_dht(cvec &v);
template vec dwht(const vec &v);
template cvec dwht(const cvec &v);
template void dwht(const vec &vin, vec &vout);
template void dwht(const cvec &vin, cvec &vout);
template void self_dwht(vec &v);
template void self_dwht(cvec &v);
template mat dht2(const mat &m);
template cmat dht2(const cmat &m);
template mat dwht2(const mat &m);
template cmat dwht2(const cmat &m);
} // namespace itpp
//! \endcond
|