libsdr  0.1.0
A simple SDR library
fftplan_fftw3.hh
1 #ifndef __SDR_FFTPLAN_FFTW3_HH__
2 #define __SDR_FFTPLAN_FFTW3_HH__
3 
4 #include "fftplan.hh"
5 #include <fftw3.h>
6 
7 
8 namespace sdr {
9 
11 template<>
12 class FFTPlan<double>
13 {
14 public:
16  FFTPlan(const Buffer< std::complex<double> > &in, const Buffer< std::complex<double> > &out,
17  FFT::Direction dir)
18  : _in(in), _out(out)
19  {
20  if (in.size() != out.size()) {
21  ConfigError err;
22  err << "Can not construct FFT plan: input & output buffers are of different size!";
23  throw err;
24  }
25  if (in.isEmpty() || out.isEmpty()) {
26  ConfigError err;
27  err << "Can not construct FFT plan: input or output buffer is empty!";
28  throw err;
29  }
30 
31  int fftw_dir = FFTW_FORWARD;
32  if (FFT::BACKWARD == dir) { fftw_dir = FFTW_BACKWARD; }
33 
34  _plan = fftw_plan_dft_1d(
35  in.size(), (fftw_complex *)in.data(), (fftw_complex *)out.data(),
36  fftw_dir, FFTW_ESTIMATE);
37  }
38 
40  FFTPlan(const Buffer< std::complex<double> > &inplace, FFT::Direction dir)
41  : _in(inplace), _out(inplace)
42  {
43  if (inplace.isEmpty()) {
44  ConfigError err;
45  err << "Can not construct FFT plan: Buffer is empty!";
46  throw err;
47  }
48 
49  int fftw_dir = FFTW_FORWARD;
50  if (FFT::BACKWARD == dir) { fftw_dir = FFTW_BACKWARD; }
51 
52  _plan = fftw_plan_dft_1d(
53  inplace.size(), (fftw_complex *)inplace.data(), (fftw_complex *)inplace.data(),
54  fftw_dir, FFTW_ESTIMATE);
55  }
56 
58  virtual ~FFTPlan() {
59  fftw_destroy_plan(_plan);
60  }
61 
63  void operator() () {
64  fftw_execute(_plan);
65  }
66 
67 protected:
73  fftw_plan _plan;
74 };
75 
76 
78 template<>
79 class FFTPlan<float>
80 {
81 public:
83  FFTPlan(const Buffer< std::complex<float> > &in, const Buffer< std::complex<float> > &out,
84  FFT::Direction dir)
85  : _in(in), _out(out)
86  {
87  if (in.size() != out.size()) {
88  ConfigError err;
89  err << "Can not construct FFT plan: input & output buffers are of different size!";
90  throw err;
91  }
92 
93  if (in.isEmpty() || out.isEmpty()) {
94  ConfigError err;
95  err << "Can not construct FFT plan: input or output buffer is empty!";
96  throw err;
97  }
98 
99  int fftw_dir = FFTW_FORWARD;
100  if (FFT::BACKWARD == dir) { fftw_dir = FFTW_BACKWARD; }
101 
102  _plan = fftwf_plan_dft_1d(
103  in.size(), (fftwf_complex *)in.data(), (fftwf_complex *)out.data(),
104  fftw_dir, FFTW_ESTIMATE);
105  }
106 
108  FFTPlan(const Buffer< std::complex<float> > &inplace, FFT::Direction dir)
109  : _in(inplace), _out(inplace)
110  {
111  if (inplace.isEmpty()) {
112  ConfigError err;
113  err << "Can not construct FFT plan: Buffer is empty!";
114  throw err;
115  }
116 
117  int fftw_dir = FFTW_FORWARD;
118  if (FFT::BACKWARD == dir) { fftw_dir = FFTW_BACKWARD; }
119 
120  _plan = fftwf_plan_dft_1d(
121  inplace.size(), (fftwf_complex *)inplace.data(), (fftwf_complex *)inplace.data(),
122  fftw_dir, FFTW_ESTIMATE);
123  }
124 
126  virtual ~FFTPlan() {
127  fftwf_destroy_plan(_plan);
128  }
129 
131  void operator() () {
132  fftwf_execute(_plan);
133  }
134 
135 protected:
141  fftwf_plan _plan;
142 };
143 
144 
145 
146 
147 }
148 
149 #endif // __SDR_FFTPLAN_FFTW3_HH__
FFTPlan(const Buffer< std::complex< double > > &in, const Buffer< std::complex< double > > &out, FFT::Direction dir)
Constructor.
Definition: fftplan_fftw3.hh:16
FFTPlan(const Buffer< std::complex< float > > &in, const Buffer< std::complex< float > > &out, FFT::Direction dir)
Constructor.
Definition: fftplan_fftw3.hh:83
void operator()()
Performs the FFT.
Definition: fftplan_native.hh:53
Direction
Direction type.
Definition: fftplan.hh:17
Definition: autocast.hh:8
Buffer< std::complex< Scalar > > _in
The input buffer.
Definition: fftplan_native.hh:79
Buffer< std::complex< float > > _out
Output buffer.
Definition: fftplan_fftw3.hh:139
Buffer< std::complex< double > > _in
Input buffer.
Definition: fftplan_fftw3.hh:69
virtual ~FFTPlan()
Destructor.
Definition: fftplan_fftw3.hh:126
Buffer< std::complex< Scalar > > _out
The output buffer.
Definition: fftplan_native.hh:81
FFTPlan(const Buffer< std::complex< float > > &inplace, FFT::Direction dir)
Constructor.
Definition: fftplan_fftw3.hh:108
fftw_plan _plan
The FFT plan.
Definition: fftplan_fftw3.hh:73
Buffer< std::complex< float > > _in
Input buffer.
Definition: fftplan_fftw3.hh:137
Buffer< std::complex< double > > _out
Output buffer.
Definition: fftplan_fftw3.hh:71
Trivial FFT implementation for buffer sizes of N=2**K.
Definition: fftplan.hh:11
fftwf_plan _plan
The fft plan.
Definition: fftplan_fftw3.hh:141
The configuration error class.
Definition: exception.hh:24
virtual ~FFTPlan()
Destructor.
Definition: fftplan_fftw3.hh:58
FFTPlan(const Buffer< std::complex< double > > &inplace, FFT::Direction dir)
Constructor.
Definition: fftplan_fftw3.hh:40
A typed buffer.
Definition: buffer.hh:111