18 void Fftw::init(unsigned width, unsigned height)
25 fftw_plan_with_nthreads(omp_get_max_threads());
29 std::cout << "FFT: FFTW" << std::endl;
31 std::cout << "FFT: cuFFTW" << std::endl;
35 void Fftw::set_window(const cv::Mat &window)
40 ComplexMat Fftw::forward(const cv::Mat &input)
42 cv::Mat complex_result(m_height, m_width / 2 + 1, CV_32FC2);
43 fftwf_plan plan = fftwf_plan_dft_r2c_2d(m_height, m_width,
44 reinterpret_cast<float*>(input.data),
45 reinterpret_cast<fftwf_complex*>(complex_result.data),
48 fftwf_destroy_plan(plan);
49 return ComplexMat(complex_result);
52 ComplexMat Fftw::forward_window(const std::vector<cv::Mat> &input)
54 int n_channels = input.size();
55 cv::Mat in_all(m_height * n_channels, m_width, CV_32F);
56 for (int i = 0; i < n_channels; ++i) {
57 cv::Mat in_roi(in_all, cv::Rect(0, i*m_height, m_width, m_height));
58 in_roi = input[i].mul(m_window);
60 cv::Mat complex_result(n_channels*m_height, m_width/2+1, CV_32FC2);
63 int n[] = {(int)m_height, (int)m_width};
64 int howmany = n_channels;
65 int idist = m_height*m_width, odist = m_height*(m_width/2+1);
66 int istride = 1, ostride = 1;
67 int *inembed = NULL, *onembed = NULL;
68 float *in = reinterpret_cast<float*>(in_all.data);
69 fftwf_complex *out = reinterpret_cast<fftwf_complex*>(complex_result.data);
71 fftwf_plan plan = fftwf_plan_many_dft_r2c(rank, n, howmany,
72 in, inembed, istride, idist,
73 out, onembed, ostride, odist,
76 fftwf_destroy_plan(plan);
78 ComplexMat result(m_height, m_width/2 + 1, n_channels);
79 for (int i = 0; i < n_channels; ++i)
80 result.set_channel(i, complex_result(cv::Rect(0, i*m_height, m_width/2+1, m_height)));
85 cv::Mat Fftw::inverse(const ComplexMat &inputf)
87 int n_channels = inputf.n_channels;
88 cv::Mat real_result(m_height, m_width, CV_32FC(n_channels));
89 cv::Mat complex_vconcat = inputf.to_vconcat_mat();
92 int n[] = {(int)m_height, (int)m_width};
93 int howmany = n_channels;
94 int idist = m_height*(m_width/2+1), odist = 1;
95 int istride = 1, ostride = n_channels;
97 int *inembed = NULL, *onembed = NULL;
101 inembed[1] = m_width/2+1, onembed[1] = m_width;
103 fftwf_complex *in = reinterpret_cast<fftwf_complex*>(complex_vconcat.data);
104 float *out = reinterpret_cast<float*>(real_result.data);
106 fftwf_plan plan = fftwf_plan_many_dft_c2r(rank, n, howmany,
107 in, inembed, istride, idist,
108 out, onembed, ostride, odist,
111 fftwf_destroy_plan(plan);
113 return real_result/(m_width*m_height);