9 #if !defined(ASYNC) && !defined(OPENMP) && !defined(CUFFTW)
10 #define FFTW_PLAN_WITH_THREADS() fftw_plan_with_nthreads(4);
12 #define FFTW_PLAN_WITH_THREADS()
17 void Fftw::init(unsigned width, unsigned height, unsigned num_of_feats, unsigned num_of_scales)
19 Fft::init(width, height, num_of_feats, num_of_scales);
21 #if (!defined(ASYNC) && !defined(CUFFTW)) && defined(OPENMP)
26 std::cout << "FFT: FFTW" << std::endl;
28 std::cout << "FFT: cuFFTW" << std::endl;
31 // FFT forward one scale
33 cv::Mat in_f = cv::Mat::zeros(int(m_height), int(m_width), CV_32FC1);
34 ComplexMat out_f(int(m_height), m_width / 2 + 1, 1);
35 plan_f = fftwf_plan_dft_r2c_2d(int(m_height), int(m_width), reinterpret_cast<float *>(in_f.data),
36 reinterpret_cast<fftwf_complex *>(out_f.get_p_data()), FFTW_PATIENT);
39 // FFT forward all scales
40 if (m_num_of_scales > 1) {
41 cv::Mat in_f_all = cv::Mat::zeros(m_height * m_num_of_scales, m_width, CV_32F);
42 ComplexMat out_f_all(m_height, m_width / 2 + 1, m_num_of_scales);
43 float *in = reinterpret_cast<float *>(in_f_all.data);
44 fftwf_complex *out = reinterpret_cast<fftwf_complex *>(out_f_all.get_p_data());
46 int n[] = {(int)m_height, (int)m_width};
47 int howmany = m_num_of_scales;
48 int idist = m_height * m_width, odist = m_height * (m_width / 2 + 1);
49 int istride = 1, ostride = 1;
50 int *inembed = NULL, *onembed = NULL;
52 FFTW_PLAN_WITH_THREADS();
53 plan_f_all_scales = fftwf_plan_many_dft_r2c(rank, n, howmany, in, inembed, istride, idist, out, onembed,
54 ostride, odist, FFTW_PATIENT);
57 // FFT forward window one scale
59 cv::Mat in_fw = cv::Mat::zeros(int(m_height * m_num_of_feats), int(m_width), CV_32F);
60 ComplexMat out_fw(int(m_height), m_width / 2 + 1, int(m_num_of_feats));
61 float *in = reinterpret_cast<float *>(in_fw.data);
62 fftwf_complex *out = reinterpret_cast<fftwf_complex *>(out_fw.get_p_data());
64 int n[] = {int(m_height), int(m_width)};
65 int howmany = int(m_num_of_feats);
66 int idist = int(m_height * m_width), odist = int(m_height * (m_width / 2 + 1));
67 int istride = 1, ostride = 1;
68 int *inembed = nullptr, *onembed = nullptr;
70 FFTW_PLAN_WITH_THREADS();
71 plan_fw = fftwf_plan_many_dft_r2c(rank, n, howmany, in, inembed, istride, idist, out, onembed, ostride, odist,
75 // FFT forward window all scales all feats
76 if (m_num_of_scales > 1) {
77 cv::Mat in_all = cv::Mat::zeros(m_height * (m_num_of_scales * m_num_of_feats), m_width, CV_32F);
78 ComplexMat out_all(m_height, m_width / 2 + 1, m_num_of_scales * m_num_of_feats);
79 float *in = reinterpret_cast<float *>(in_all.data);
80 fftwf_complex *out = reinterpret_cast<fftwf_complex *>(out_all.get_p_data());
82 int n[] = {(int)m_height, (int)m_width};
83 int howmany = m_num_of_scales * m_num_of_feats;
84 int idist = m_height * m_width, odist = m_height * (m_width / 2 + 1);
85 int istride = 1, ostride = 1;
86 int *inembed = NULL, *onembed = NULL;
88 FFTW_PLAN_WITH_THREADS();
89 plan_fw_all_scales = fftwf_plan_many_dft_r2c(rank, n, howmany, in, inembed, istride, idist, out, onembed,
90 ostride, odist, FFTW_PATIENT);
94 // FFT inverse all scales
96 ComplexMat in_i_all(m_height, m_width / 2 + 1, m_num_of_scales);
97 cv::Mat out_i_all = cv::Mat::zeros(m_height, m_width, CV_32FC(m_num_of_scales));
98 fftwf_complex *in = reinterpret_cast<fftwf_complex *>(in_i_all.get_p_data());
99 float *out = reinterpret_cast<float *>(out_i_all.data);
101 int n[] = {(int)m_height, (int)m_width};
102 int howmany = m_num_of_scales;
103 int idist = m_height * (m_width / 2 + 1), odist = m_height * m_width;
104 int istride = 1, ostride = 1;
105 int inembed[] = {(int)m_height, (int)m_width / 2 + 1}, *onembed = n;
107 FFTW_PLAN_WITH_THREADS();
108 plan_i_all_scales = fftwf_plan_many_dft_c2r(rank, n, howmany, in, inembed, istride, idist, out,
109 onembed, ostride, odist, FFTW_PATIENT);
112 // FFT inverse one channel
114 ComplexMat in_i1(int(m_height), int(m_width), 1);
115 cv::Mat out_i1 = cv::Mat::zeros(int(m_height), int(m_width), CV_32FC1);
116 fftwf_complex *in = reinterpret_cast<fftwf_complex *>(in_i1.get_p_data());
117 float *out = reinterpret_cast<float *>(out_i1.data);
119 int n[] = {int(m_height), int(m_width)};
121 int idist = m_height * (m_width / 2 + 1), odist = 1;
122 int istride = 1, ostride = 1;
123 int inembed[] = {int(m_height), int(m_width / 2 + 1)}, *onembed = n;
125 FFTW_PLAN_WITH_THREADS();
126 plan_i_1ch = fftwf_plan_many_dft_c2r(rank, n, howmany, in, inembed, istride, idist, out, onembed, ostride,
127 odist, FFTW_PATIENT);
131 void Fftw::set_window(const MatDynMem &window)
133 Fft::set_window(window);
137 void Fftw::forward(const MatScales &real_input, ComplexMat &complex_result)
139 Fft::forward(real_input, complex_result);
141 if (real_input.size[0] == 1)
142 fftwf_execute_dft_r2c(plan_f, reinterpret_cast<float *>(real_input.data),
143 reinterpret_cast<fftwf_complex *>(complex_result.get_p_data()));
146 fftwf_execute_dft_r2c(plan_f_all_scales, reinterpret_cast<float *>(real_input.data),
147 reinterpret_cast<fftwf_complex *>(complex_result.get_p_data()));
151 void Fftw::forward_window(MatScaleFeats &feat, ComplexMat & complex_result, MatScaleFeats &temp)
153 Fft::forward_window(feat, complex_result, temp);
155 uint n_scales = feat.size[0];
156 for (uint s = 0; s < n_scales; ++s) {
157 for (uint ch = 0; ch < uint(feat.size[1]); ++ch) {
158 cv::Mat feat_plane = feat.plane(s, ch);
159 cv::Mat temp_plane = temp.plane(s, ch);
160 temp_plane = feat_plane.mul(m_window);
164 float *in = temp.ptr<float>();
165 fftwf_complex *out = reinterpret_cast<fftwf_complex *>(complex_result.get_p_data());
168 fftwf_execute_dft_r2c(plan_fw, in, out);
170 fftwf_execute_dft_r2c(plan_fw_all_scales, in, out);
174 void Fftw::inverse(ComplexMat &complex_input, MatScales &real_result)
176 Fft::inverse(complex_input, real_result);
178 int n_channels = complex_input.n_channels;
179 fftwf_complex *in = reinterpret_cast<fftwf_complex *>(complex_input.get_p_data());
180 float *out = real_result.ptr<float>();
183 fftwf_execute_dft_c2r(plan_i_1ch, in, out);
186 fftwf_execute_dft_c2r(plan_i_all_scales, in, out);
188 real_result *= 1.0 / (m_width * m_height);
193 fftwf_destroy_plan(plan_f);
194 fftwf_destroy_plan(plan_fw);
195 fftwf_destroy_plan(plan_i_1ch);
197 if (BIG_BATCH_MODE) {
198 fftwf_destroy_plan(plan_f_all_scales);
199 fftwf_destroy_plan(plan_i_all_scales);
200 fftwf_destroy_plan(plan_fw_all_scales);