|
|
|
|
@@ -25,7 +25,7 @@
|
|
|
|
|
|
|
|
|
|
#include "pivector.h"
|
|
|
|
|
#include "pimutex.h"
|
|
|
|
|
#include <complex.h>
|
|
|
|
|
#include "picout.h"
|
|
|
|
|
#ifdef PIP_FFTW
|
|
|
|
|
# include "fftw3.h"
|
|
|
|
|
#else
|
|
|
|
|
@@ -36,106 +36,136 @@
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static PIMutex __pip_fft_plan_mutex;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
class PIFFTW_Private
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit PIFFTW_Private() {plan = 0; newPlan(plan);}
|
|
|
|
|
~PIFFTW_Private() {deletePlan(plan);}
|
|
|
|
|
explicit PIFFTW_Private() {
|
|
|
|
|
plan = 0;
|
|
|
|
|
#ifdef PIP_FFTW
|
|
|
|
|
// fftwf_m
|
|
|
|
|
#else
|
|
|
|
|
piCout << "[PIFFTW]" << "Warning: PIFFTW is disabled, to enable install libfftw3-dev library and build pip with -DFFTW=1";
|
|
|
|
|
#endif
|
|
|
|
|
p_makeThreadSafe();
|
|
|
|
|
}
|
|
|
|
|
~PIFFTW_Private() {p_destroyPlan(plan);}
|
|
|
|
|
|
|
|
|
|
const PIVector<complex<T> > & calcFFT(const PIVector<complex<T> > & in) {
|
|
|
|
|
result.resize(in.size());
|
|
|
|
|
__pip_fft_plan_mutex.lock();
|
|
|
|
|
createPlan_c_1d(plan, in.size_s(), in.data(), result.data(), FFTW_FORWARD, FFTW_ESTIMATE);
|
|
|
|
|
__pip_fft_plan_mutex.unlock();
|
|
|
|
|
executePlan(plan);
|
|
|
|
|
destroyPlan(plan);
|
|
|
|
|
return result;
|
|
|
|
|
if (prepare != PlanParams(in.size(), fo_complex)) {
|
|
|
|
|
p_out.resize(in.size());
|
|
|
|
|
piCout << "[PIFFTW]" << "creating plan";
|
|
|
|
|
p_createPlan_c2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_FORWARD, FFTW_ESTIMATE | FFTW_UNALIGNED);
|
|
|
|
|
prepare = PlanParams(in.size(), fo_complex);
|
|
|
|
|
}
|
|
|
|
|
p_executePlan_c2c(plan, in.data(), p_out.data());
|
|
|
|
|
return p_out;
|
|
|
|
|
}
|
|
|
|
|
const PIVector<complex<T> > & calcFFT(const PIVector<T> & in) {
|
|
|
|
|
result.resize(in.size());
|
|
|
|
|
__pip_fft_plan_mutex.lock();
|
|
|
|
|
createPlan_r2c_1d(plan, in.size_s(), in.data(), result.data(), FFTW_ESTIMATE);
|
|
|
|
|
__pip_fft_plan_mutex.unlock();
|
|
|
|
|
executePlan(plan);
|
|
|
|
|
destroyPlan(plan);
|
|
|
|
|
return result;
|
|
|
|
|
if (prepare != PlanParams(in.size(), fo_real)) {
|
|
|
|
|
p_out.resize(in.size());
|
|
|
|
|
piCout << "[PIFFTW]" << "creating plan";
|
|
|
|
|
p_createPlan_r2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_ESTIMATE | FFTW_UNALIGNED);
|
|
|
|
|
prepare = PlanParams(in.size(), fo_real);
|
|
|
|
|
}
|
|
|
|
|
p_executePlan_r2c(plan, in.data(), p_out.data());
|
|
|
|
|
return p_out;
|
|
|
|
|
}
|
|
|
|
|
const PIVector<complex<T> > & calcFFTinverse(const PIVector<complex<T> > & in) {
|
|
|
|
|
result.resize(in.size());
|
|
|
|
|
__pip_fft_plan_mutex.lock();
|
|
|
|
|
createPlan_c_1d(plan, in.size_s(), in.data(), result.data(), FFTW_BACKWARD, FFTW_ESTIMATE);
|
|
|
|
|
__pip_fft_plan_mutex.unlock();
|
|
|
|
|
executePlan(plan);
|
|
|
|
|
destroyPlan(plan);
|
|
|
|
|
return result;
|
|
|
|
|
if (prepare != PlanParams(in.size(), fo_inverse)) {
|
|
|
|
|
p_out.resize(in.size());
|
|
|
|
|
piCout << "[PIFFTW]" << "creating plan";
|
|
|
|
|
p_createPlan_c2c_1d(plan, in.size(), in.data(), p_out.data(), FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_UNALIGNED);
|
|
|
|
|
prepare = PlanParams(in.size(), fo_inverse);
|
|
|
|
|
}
|
|
|
|
|
p_executePlan_c2c(plan, in.data(), p_out.data());
|
|
|
|
|
return p_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum FFT_Operation {fo_real, fo_complex, fo_inverse};
|
|
|
|
|
|
|
|
|
|
void preparePlan(int size, int op) {
|
|
|
|
|
PIVector<complex<T> > in(size), out(size);
|
|
|
|
|
PIVector<T> inr(size);
|
|
|
|
|
__pip_fft_plan_mutex.lock();
|
|
|
|
|
p_inr.clear();
|
|
|
|
|
p_in.clear();
|
|
|
|
|
p_out.clear();
|
|
|
|
|
switch ((FFT_Operation)op) {
|
|
|
|
|
case fo_real:
|
|
|
|
|
createPlan_r2c_1d(plan, in.size_s(), inr.data(), out.data(), FFTW_MEASURE);
|
|
|
|
|
case fo_real:
|
|
|
|
|
p_inr.resize(size);
|
|
|
|
|
p_out.resize(size);
|
|
|
|
|
p_createPlan_r2c_1d(plan, size, p_inr.data(), p_out.data(), FFTW_MEASURE | FFTW_UNALIGNED);
|
|
|
|
|
break;
|
|
|
|
|
case fo_complex:
|
|
|
|
|
createPlan_c_1d(plan, in.size_s(), in.data(), out.data(), FFTW_FORWARD, FFTW_MEASURE);
|
|
|
|
|
case fo_complex:
|
|
|
|
|
p_in.resize(size);
|
|
|
|
|
p_out.resize(size);
|
|
|
|
|
p_createPlan_c2c_1d(plan, size, p_in.data(), p_out.data(), FFTW_FORWARD, FFTW_MEASURE | FFTW_UNALIGNED);
|
|
|
|
|
break;
|
|
|
|
|
case fo_inverse:
|
|
|
|
|
createPlan_c_1d(plan, in.size_s(), in.data(), out.data(), FFTW_BACKWARD, FFTW_MEASURE);
|
|
|
|
|
case fo_inverse:
|
|
|
|
|
p_in.resize(size);
|
|
|
|
|
p_out.resize(size);
|
|
|
|
|
p_createPlan_c2c_1d(plan, size, p_in.data(), p_out.data(), FFTW_BACKWARD, FFTW_MEASURE | FFTW_UNALIGNED);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
size = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
__pip_fft_plan_mutex.unlock();
|
|
|
|
|
prepare = PlanParams(size, (FFT_Operation)op);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void createPlan_c_1d(void * plan, int size, const void * in, void * out, int dir, int flags) {}
|
|
|
|
|
inline void createPlan_r2c_1d(void * plan, int size, const void * in, void * out, int flags) {}
|
|
|
|
|
inline void executePlan(void * plan) {}
|
|
|
|
|
inline void destroyPlan(void * plan) {}
|
|
|
|
|
inline void newPlan(void *& plan) {}
|
|
|
|
|
inline void deletePlan(void *& plan) {}
|
|
|
|
|
inline void p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {}
|
|
|
|
|
inline void p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {}
|
|
|
|
|
inline void p_executePlan_c2c(void * plan, const void * in, void * out) {}
|
|
|
|
|
inline void p_executePlan_r2c(void * plan, const void * in, void * out) {}
|
|
|
|
|
inline void p_destroyPlan(void *& plan) {}
|
|
|
|
|
inline void p_makeThreadSafe() {}
|
|
|
|
|
|
|
|
|
|
PIVector<complex<T> > result;
|
|
|
|
|
struct PlanParams {
|
|
|
|
|
PlanParams() {size = 0; op = fo_complex;}
|
|
|
|
|
PlanParams(int size_, FFT_Operation op_) {size = size_; op = op_;}
|
|
|
|
|
bool isValid() {return size > 0;}
|
|
|
|
|
bool operator ==(const PlanParams & v) const {return (v.size == size) && (v.op == op);}
|
|
|
|
|
bool operator !=(const PlanParams & v) const {return !(*this == v);}
|
|
|
|
|
int size;
|
|
|
|
|
FFT_Operation op;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PIVector<complex<T> > p_in;
|
|
|
|
|
PIVector<T> p_inr;
|
|
|
|
|
PIVector<complex<T> > p_out;
|
|
|
|
|
void * plan;
|
|
|
|
|
PlanParams prepare;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PIP_FFTW
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
|
|
|
|
|
plan = fftwf_plan_dft_1d(size, (fftwf_complex *)in, (fftwf_complex *)out, dir, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
|
|
|
|
|
plan = fftwf_plan_dft_r2c_1d(size, (float *)in, (fftwf_complex *)out, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::p_executePlan_c2c(void * plan, const void * in, void * out) {fftwf_execute_dft((fftwf_plan)plan, (fftwf_complex *)in, (fftwf_complex *)out);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::p_executePlan_r2c(void * plan, const void * in, void * out) {fftwf_execute_dft_r2c((fftwf_plan)plan, (float *)in, (fftwf_complex *)out);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::p_destroyPlan(void *& plan) {if (plan) fftwf_destroy_plan((fftwf_plan)plan); plan = 0;}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::p_makeThreadSafe() {fftwf_make_planner_thread_safe();}
|
|
|
|
|
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::createPlan_c_1d(void * plan, int size, const void * in, void * out, int dir, int flags) {
|
|
|
|
|
*(fftwf_plan*)plan = fftwf_plan_dft_1d(size, (fftwf_complex *)in, (fftwf_complex *)out, dir, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::createPlan_r2c_1d(void * plan, int size, const void * in, void * out, int flags) {
|
|
|
|
|
*(fftwf_plan*)plan = fftwf_plan_dft_r2c_1d(size, (float *)in, (fftwf_complex *)out, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::executePlan(void * plan) {fftwf_execute(*(fftwf_plan*)plan);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::destroyPlan(void * plan) {fftwf_destroy_plan(*(fftwf_plan*)plan);}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::newPlan(void *& plan) {plan = new fftwf_plan();}
|
|
|
|
|
template<> inline void PIFFTW_Private<float>::deletePlan(void *& plan) {if (plan) delete (fftwf_plan*)plan; plan = 0;}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
|
|
|
|
|
plan = fftw_plan_dft_1d(size, (fftw_complex *)in, (fftw_complex *)out, dir, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
|
|
|
|
|
plan = fftw_plan_dft_r2c_1d(size, (double *)in, (fftw_complex *)out, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::p_executePlan_c2c(void * plan, const void * in, void * out) {fftw_execute_dft((fftw_plan)plan, (fftw_complex *)in, (fftw_complex *)out);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::p_executePlan_r2c(void * plan, const void * in, void * out) {fftw_execute_dft_r2c((fftw_plan)plan, (double *)in, (fftw_complex *)out);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::p_destroyPlan(void *& plan) {if (plan) fftw_destroy_plan((fftw_plan)plan); plan = 0;}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::p_makeThreadSafe() {fftw_make_planner_thread_safe();}
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::p_createPlan_c2c_1d(void *& plan, int size, const void * in, void * out, int dir, int flags) {
|
|
|
|
|
plan = fftwl_plan_dft_1d(size, (fftwl_complex *)in, (fftwl_complex *)out, dir, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::p_createPlan_r2c_1d(void *& plan, int size, const void * in, void * out, int flags) {
|
|
|
|
|
plan = fftwl_plan_dft_r2c_1d(size, (ldouble *)in, (fftwl_complex *)out, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::p_executePlan_c2c(void * plan, const void * in, void * out) {fftwl_execute_dft((fftwl_plan)plan, (fftwl_complex *)in, (fftwl_complex *)out);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::p_executePlan_r2c(void * plan, const void * in, void * out) {fftwl_execute_dft_r2c((fftwl_plan)plan, (ldouble *)in, (fftwl_complex *)out);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::p_destroyPlan(void *& plan) {if (plan) fftwl_destroy_plan((fftwl_plan)plan); plan = 0;}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::p_makeThreadSafe() {fftwl_make_planner_thread_safe();}
|
|
|
|
|
#endif // WINDOWS
|
|
|
|
|
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::createPlan_c_1d(void * plan, int size, const void * in, void * out, int dir, int flags) {
|
|
|
|
|
*(fftw_plan*)plan = fftw_plan_dft_1d(size, (fftw_complex *)in, (fftw_complex *)out, dir, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::createPlan_r2c_1d(void * plan, int size, const void * in, void * out, int flags) {
|
|
|
|
|
*(fftw_plan*)plan = fftw_plan_dft_r2c_1d(size, (double *)in, (fftw_complex *)out, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::executePlan(void * plan) {fftw_execute(*(fftw_plan*)plan);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::destroyPlan(void * plan) {fftw_destroy_plan(*(fftw_plan*)plan);}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::newPlan(void *& plan) {plan = new fftw_plan();}
|
|
|
|
|
template<> inline void PIFFTW_Private<double>::deletePlan(void *& plan) {if (plan) delete (fftw_plan*)plan; plan = 0;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::createPlan_c_1d(void * plan, int size, const void * in, void * out, int dir, int flags) {
|
|
|
|
|
*(fftwl_plan*)plan = fftwl_plan_dft_1d(size, (fftwl_complex *)in, (fftwl_complex *)out, dir, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::createPlan_r2c_1d(void * plan, int size, const void * in, void * out, int flags) {
|
|
|
|
|
*(fftwl_plan*)plan = fftwl_plan_dft_r2c_1d(size, (ldouble *)in, (fftwl_complex *)out, flags);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::executePlan(void * plan) {fftwl_execute(*(fftwl_plan*)plan);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::destroyPlan(void * plan) {fftwl_destroy_plan(*(fftwl_plan*)plan);}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::newPlan(void *& plan) {plan = new fftwl_plan();}
|
|
|
|
|
template<> inline void PIFFTW_Private<ldouble>::deletePlan(void *& plan) {if (plan) delete (fftwl_plan*)plan; plan = 0;}
|
|
|
|
|
|
|
|
|
|
#endif // PIP_FFTW
|
|
|
|
|
|
|
|
|
|
|