git-svn-id: svn://db.shs.com.ru/pip@331 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-04-04 21:55:29 +00:00
parent d37c981d13
commit b6d35cf05b
5 changed files with 220 additions and 58 deletions

143
src/math/pifft_p.h Normal file
View File

@@ -0,0 +1,143 @@
/*! \file pifft_p.h
* \brief Class for FFT, IFFT and Hilbert transformations
*/
/*
PIP - Platform Independent Primitives
Private header for fftw3
Copyright (C) 2017 Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIFFT_P_H
#define PIFFT_P_H
#include "pivector.h"
#include "pimutex.h"
#include <complex.h>
#ifdef PIP_FFTW
# include "fftw3.h"
#else
# define FFTW_FORWARD 0
# define FFTW_BACKWARD 0
# define FFTW_ESTIMATE 0
# define FFTW_MEASURE 0
#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);}
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;
}
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;
}
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;
}
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();
switch ((FFT_Operation)op) {
case fo_real:
createPlan_r2c_1d(plan, in.size_s(), inr.data(), out.data(), FFTW_MEASURE);
break;
case fo_complex:
createPlan_c_1d(plan, in.size_s(), in.data(), out.data(), FFTW_FORWARD, FFTW_MEASURE);
break;
case fo_inverse:
createPlan_c_1d(plan, in.size_s(), in.data(), out.data(), FFTW_BACKWARD, FFTW_MEASURE);
break;
}
__pip_fft_plan_mutex.unlock();
}
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) {}
PIVector<complex<T> > result;
void * plan;
};
#ifdef PIP_FFTW
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>::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
#endif // PIFFT_H