如题:
.h 文件
#include <vector>
#include <complex>
#include "../../3rdLibs/FFTW3.3.8/fftw3.h"
using namespace std;
class DFT_1D_TASK {
public:
DFT_1D_TASK();
DFT_1D_TASK(size_t N);
void ResetNfft(size_t N);
~DFT_1D_TASK();
bool DFT_R2C (const vector<double> &input, vector<complex<double>> &output);
bool IDFT_C2R(const vector<complex<double>> &input, vector<double> &output);
bool DFT_C2C (const vector<complex<double>> &input, vector<complex<double>> &output);
bool IDFT_C2C(const vector<complex<double>> &input, vector<complex<double>> &output);
private:
size_t Nfft;
fftw_complex *_fftIn;
fftw_complex *_fftOut;
fftw_plan _fftPlan;
fftw_plan _ifftPlan;
void ClearBuffer(); };
.cpp 实现
'''c++
DFT_1D_TASK::DFT_1D_TASK()
{
Nfft = 0;
_fftIn = nullptr;
_fftOut = nullptr;
}
DFT_1D_TASK::DFT_1D_TASK(size_t N)
{
Nfft = N;
_fftIn = fftw_alloc_complex(Nfft);
_fftOut = fftw_alloc_complex(Nfft);
_fftPlan = fftw_plan_dft_1d(static_cast(Nfft), _fftIn, _fftOut, FFTW_FORWARD, FFTW_ESTIMATE);
_ifftPlan = fftw_plan_dft_1d(static_cast(Nfft), _fftIn, _fftOut, FFTW_BACKWARD, FFTW_ESTIMATE);
}
void DFT_1D_TASK::ResetNfft(size_t N)
{
ClearBuffer();
Nfft = N;
_fftIn = fftw_alloc_complex(Nfft);
_fftOut = fftw_alloc_complex(Nfft);
_fftPlan = fftw_plan_dft_1d(static_cast(Nfft), _fftIn, _fftOut, FFTW_FORWARD, FFTW_ESTIMATE);
_ifftPlan = fftw_plan_dft_1d(static_cast(Nfft), _fftIn, _fftOut, FFTW_BACKWARD, FFTW_ESTIMATE);
}
DFT_1D_TASK::~DFT_1D_TASK()
{
ClearBuffer();
}
bool DFT_1D_TASK::DFT_R2C(const vector &input, vector> &output){
if(input.size()!= this->Nfft)
return false;
for(size_t i = 0;i
_fftIn[i][0] = input[i];
_fftIn[i][1] = 0;
}
fftw_execute(_fftPlan);
if(output.size()!=this->Nfft)
output.resize(Nfft);
for(size_t i = 0;i<(Nfft);i++)
{
output[i].real( _fftOut[i][0]);
output[i].imag( _fftOut[i][1]);
}
return true;
}
bool DFT_1D_TASK::IDFT_C2R(const vector> &input, vector &output)
{
if(input.size()!= this->Nfft)
return false;
for(size_t i = 0;i
_fftIn[i][0] = input[i].real();
_fftIn[i][1] = input[i].imag();
}
fftw_execute(_ifftPlan);
if(output.size()!=this->Nfft)
output.resize(Nfft);
for(size_t i = 0;i<(Nfft);i++)
{
output[i] = _fftOut[i][0]/Nfft;
}
return true;
}
bool DFT_1D_TASK::DFT_C2C (const vector> &input, vector> &output)
{
if(input.size()!= this->Nfft)
return false;
for(size_t i = 0;i
_fftIn[i][0] = input[i].real();
_fftIn[i][1] = input[i].imag();
}
fftw_execute(_fftPlan);
if(output.size()!=this->Nfft)
output.resize(Nfft);
for(size_t i = 0;i<(Nfft);i++)
{
output[i].real( _fftOut[i][0]);
output[i].imag( _fftOut[i][1]);
}
return true;
}
bool DFT_1D_TASK::IDFT_C2C(const vector> &input, vector> &output){
if(input.size()!= this->Nfft)
return false;
for(size_t i = 0;i
_fftIn[i][0] = input[i].real();
_fftIn[i][1] = input[i].imag();
}
fftw_execute(_ifftPlan);
if(output.size()!=this->Nfft)
output.resize(Nfft);
for(size_t i = 0;i<(Nfft);i++)
{
output[i].real( _fftOut[i][0]/Nfft);
output[i].imag( _fftOut[i][1]/Nfft);
}
return true;
}
void DFT_1D_TASK::ClearBuffer()
{
if(_fftIn != nullptr)
fftw_free(_fftIn);
if(_fftOut != nullptr)
fftw_free(_fftOut);
fftw_destroy_plan(_fftPlan);
fftw_destroy_plan(_ifftPlan);
}
'''