diff --git a/Homework3/src/fft.hh b/Homework3/src/fft.hh index 92907e0..7889044 100644 --- a/Homework3/src/fft.hh +++ b/Homework3/src/fft.hh @@ -1,51 +1,75 @@ #ifndef FFT_HH #define FFT_HH /* ------------------------------------------------------ */ #include "matrix.hh" #include "my_types.hh" #include "fftw3.h" /* ------------------------------------------------------ */ struct FFT { static Matrix transform(Matrix& m); static Matrix itransform(Matrix& m); static Matrix> computeFrequencies(int size); }; /* ------------------------------------------------------ */ inline Matrix FFT::transform(Matrix& m_in) { - fftw_complex* C_in; - fftw_complex* C_out; + fftw_complex *C_in; + fftw_complex *C_out; + fftw_plan forward_plan; UInt length = m_in.size()*m_in.size(); UInt rows = m_in.rows(); UInt columns = m_in.cols(); - + Matrix m_out(rows); C_in = new fftw_complex [length]; C_out = new fftw_complex [length]; - - fftw_plan forward_plan; - forward_plan = fftw_plan_dft_2d(rows, columns, C_in, C_out); - + + for (auto&& entry : index(m_in)) + { + int i = std::get<0>(entry); + int j = std::get<1>(entry); + auto& val = std::get<2>(entry); + C_in[i*rows+j][0]=val.real(); + C_in[i*rows+j][1]=0.0; + } + + forward_plan = fftw_plan_dft_2d(rows, columns, C_in, C_out, FFTW_FORWARD, FFTW_ESTIMATE); + fftw_execute(forward_plan); + + for (auto&& entry : index(m_out)) + { + int i = std::get<0>(entry); + int j = std::get<1>(entry); + auto& val = std::get<2>(entry); + val=complex(C_out[i*rows+j][0], C_out[i*rows+j][1]); + } + + + fftw_destroy_plan(forward_plan); + fftw_free(C_in); + fftw_free(C_out); + + return m_out; } /* ------------------------------------------------------ */ inline Matrix FFT::itransform(Matrix& m_in) { } /* ------------------------------------------------------ */ /* ------------------------------------------------------ */ inline Matrix> FFT::computeFrequencies(int size) { } #endif // FFT_HH