2022-10-12 13:02:42 +00:00
|
|
|
/* #define DEBUGTRACE_ENABLED */
|
2022-08-07 19:13:45 +00:00
|
|
|
#include <memory>
|
2022-09-03 18:59:14 +00:00
|
|
|
#include <cassert>
|
2022-08-07 19:13:45 +00:00
|
|
|
#include "debugtrace.hpp"
|
|
|
|
#include "lasp_config.h"
|
2022-10-12 13:02:42 +00:00
|
|
|
#include "lasp_fft.h"
|
2022-09-03 18:59:14 +00:00
|
|
|
using rte = std::runtime_error;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
#if LASP_FFT_BACKEND == Armadillo
|
|
|
|
class Fft_impl {
|
2022-10-12 13:02:42 +00:00
|
|
|
public:
|
|
|
|
us nfft;
|
|
|
|
Fft_impl(const us nfft) : nfft(nfft) { DEBUGTRACE_ENTER; }
|
|
|
|
|
|
|
|
vc fft(const vd &time) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
vc res = arma::fft(time);
|
|
|
|
return res.rows(0, nfft / 2);
|
|
|
|
}
|
|
|
|
vd ifft(const vc &f) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
vc f_full(nfft);
|
|
|
|
vc f_above_nyq = arma::conj(arma::reverse(f));
|
|
|
|
f_full.rows(0, nfft / 2) = f;
|
|
|
|
f_full.rows(nfft / 2, nfft - 1) = f_above_nyq.rows(0, nfft/2-1);
|
|
|
|
return arma::real(arma::ifft(f_full));
|
|
|
|
}
|
2022-08-07 19:13:45 +00:00
|
|
|
};
|
|
|
|
#elif LASP_FFT_BACKEND == FFTW
|
|
|
|
#include <fftw3.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FFTW implementation
|
|
|
|
*/
|
|
|
|
class Fft_impl {
|
2022-10-12 13:02:42 +00:00
|
|
|
public:
|
|
|
|
us nfft;
|
|
|
|
fftw_plan forward_plan = nullptr;
|
|
|
|
fftw_plan reverse_plan = nullptr;
|
|
|
|
fftw_complex *frequencyDomain = nullptr;
|
|
|
|
d *timeDomain = nullptr;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
Fft_impl(const us nfft) : nfft(nfft) {
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
timeDomain = (d *)fftw_malloc(sizeof(d) * nfft);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
frequencyDomain =
|
2022-08-07 19:13:45 +00:00
|
|
|
(fftw_complex *)fftw_malloc(sizeof(fftw_complex) * (nfft / 2 + 1));
|
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
forward_plan =
|
2022-08-07 19:13:45 +00:00
|
|
|
fftw_plan_dft_r2c_1d(nfft, timeDomain, frequencyDomain, FFTW_MEASURE);
|
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
reverse_plan =
|
2022-08-07 19:13:45 +00:00
|
|
|
fftw_plan_dft_c2r_1d(nfft, frequencyDomain, timeDomain, FFTW_MEASURE);
|
2022-10-12 13:02:42 +00:00
|
|
|
if (!forward_plan || !reverse_plan || !timeDomain || !frequencyDomain) {
|
|
|
|
throw rte("Error allocating FFT");
|
|
|
|
}
|
|
|
|
};
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
~Fft_impl() {
|
|
|
|
fftw_destroy_plan(forward_plan);
|
|
|
|
fftw_destroy_plan(reverse_plan);
|
|
|
|
fftw_free(frequencyDomain);
|
|
|
|
fftw_free(timeDomain);
|
|
|
|
}
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
vc fft(const vd &time) {
|
|
|
|
if (time.n_rows != nfft) {
|
|
|
|
throw rte("Invalid size of input vector, should be equal to nfft");
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
vc res(nfft / 2 + 1);
|
|
|
|
memcpy(timeDomain, time.memptr(), sizeof(d) * nfft);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
fftw_execute(forward_plan);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
memcpy(reinterpret_cast<void *>(res.memptr()), frequencyDomain,
|
|
|
|
sizeof(c) * (nfft / 2 + 1));
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
vd ifft(const vc &f) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
if (f.n_rows != nfft / 2 + 1) {
|
|
|
|
throw rte("Invalid size of input vector, should be equal to nfft/2+1");
|
2022-09-03 18:59:14 +00:00
|
|
|
}
|
2022-10-12 13:02:42 +00:00
|
|
|
memcpy(frequencyDomain,
|
|
|
|
reinterpret_cast<void *>(const_cast<c *>(f.memptr())),
|
|
|
|
(nfft / 2 + 1) * sizeof(c));
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
fftw_execute(reverse_plan);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
vd res(nfft);
|
|
|
|
memcpy(res.memptr(), timeDomain, nfft * sizeof(d));
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
/* Scale by dividing by nfft. Checked with numpy implementation
|
|
|
|
* that this indeed needs to be done for FFTW. */
|
|
|
|
res *= 1.0 / nfft;
|
2022-09-03 18:59:14 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
2022-08-07 19:13:45 +00:00
|
|
|
};
|
|
|
|
#else
|
|
|
|
#error \
|
2022-10-12 13:02:42 +00:00
|
|
|
"Cannot compile lasp_ffc.c, no FFT backend specified. Should either be FFTPack, or FFTW"
|
2022-08-07 19:13:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Fft::Fft(const us nfft) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
if (nfft == 0) {
|
2022-09-03 18:59:14 +00:00
|
|
|
throw rte("Invalid nfft: 0");
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|
|
|
|
if (nfft >= LASP_MAX_NFFT) {
|
2022-09-03 18:59:14 +00:00
|
|
|
throw rte("Invalid nfft, should be smaller than: " +
|
2022-10-12 13:02:42 +00:00
|
|
|
std::to_string(LASP_MAX_NFFT));
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|
|
|
|
_impl = std::make_unique<Fft_impl>(nfft);
|
|
|
|
}
|
2022-10-12 13:02:42 +00:00
|
|
|
Fft::~Fft() {}
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
us Fft::nfft() const {
|
|
|
|
assert(_impl);
|
|
|
|
return _impl->nfft;
|
|
|
|
}
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
vc Fft::fft(const vd &timedata) {
|
|
|
|
assert(_impl);
|
|
|
|
return _impl->fft(timedata);
|
|
|
|
}
|
2022-09-03 18:59:14 +00:00
|
|
|
|
|
|
|
cmat Fft::fft(const dmat &freqdata) {
|
2022-08-07 19:13:45 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2022-10-12 13:02:42 +00:00
|
|
|
assert(_impl);
|
|
|
|
cmat res(_impl->nfft / 2 + 1, freqdata.n_cols);
|
2022-10-05 12:57:39 +00:00
|
|
|
/// * WARNING *. This was source of a serious bug. It is not possible to run
|
|
|
|
/// FFT's and IFFT's on the same _impl, as it overwrites the same memory.
|
|
|
|
/// Uncommenting the line below results in faulty results.
|
2023-01-20 14:59:08 +00:00
|
|
|
// #pragma omp parallel for
|
2022-09-03 18:59:14 +00:00
|
|
|
for (us colno = 0; colno < freqdata.n_cols; colno++) {
|
|
|
|
res.col(colno) = _impl->fft(freqdata.col(colno));
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|
2022-09-03 18:59:14 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
vd Fft::ifft(const vc &freqdata) {
|
|
|
|
DEBUGTRACE_ENTER;
|
2022-10-12 13:02:42 +00:00
|
|
|
assert(_impl);
|
2022-08-07 19:13:45 +00:00
|
|
|
return _impl->ifft(freqdata);
|
|
|
|
}
|
|
|
|
dmat Fft::ifft(const cmat &freqdata) {
|
|
|
|
dmat res(_impl->nfft, freqdata.n_cols);
|
2022-10-05 12:57:39 +00:00
|
|
|
/// * WARNING *. This was source of a serious bug. It is not possible to run
|
|
|
|
/// FFT's and IFFT's on the same _impl, as it overwrites the same memory.
|
|
|
|
/// Uncommenting the line below results in faulty results.
|
2023-01-20 14:59:08 +00:00
|
|
|
// #pragma omp parallel for
|
2022-10-05 12:57:39 +00:00
|
|
|
|
2022-08-07 19:13:45 +00:00
|
|
|
for (us colno = 0; colno < freqdata.n_cols; colno++) {
|
|
|
|
res.col(colno) = _impl->ifft(freqdata.col(colno));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-10-12 13:02:42 +00:00
|
|
|
void Fft::load_fft_wisdom(const std::string &wisdom) {
|
2022-08-07 19:13:45 +00:00
|
|
|
#if LASP_FFT_BACKEND == Armadillo
|
|
|
|
#elif LASP_FFT_BACKEND == FFTW
|
2022-09-22 08:18:38 +00:00
|
|
|
if (wisdom.length() > 0) {
|
|
|
|
int rv = fftw_import_wisdom_from_string(wisdom.c_str());
|
2022-08-07 19:13:45 +00:00
|
|
|
if (rv != 1) {
|
2022-09-22 08:18:38 +00:00
|
|
|
throw rte("Error loading FFTW wisdom");
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-09-22 08:18:38 +00:00
|
|
|
std::string Fft::store_fft_wisdom() {
|
2022-08-07 19:13:45 +00:00
|
|
|
#if LASP_FFT_BACKEND == Armadillo
|
2022-09-22 08:18:38 +00:00
|
|
|
return "";
|
2022-08-07 19:13:45 +00:00
|
|
|
#elif LASP_FFT_BACKEND == FFTW
|
2022-09-22 08:18:38 +00:00
|
|
|
// It is not possible to let FFTW directly return a C++ string. We have to
|
|
|
|
// put it in this container by copying in. Not a good solution if this has to
|
|
|
|
// happen often.
|
|
|
|
// Fortunately, this function is only called at the end of the program.
|
2022-10-12 13:02:42 +00:00
|
|
|
char *wis = fftw_export_wisdom_to_string();
|
|
|
|
std::string res{wis};
|
2022-09-22 08:18:38 +00:00
|
|
|
free(wis);
|
|
|
|
return res;
|
2022-08-07 19:13:45 +00:00
|
|
|
#endif
|
|
|
|
}
|