From a600e8e91e46d0cba477190689c0c027b173ed11 Mon Sep 17 00:00:00 2001 From: Guillaume Courrier Date: Tue, 26 Nov 2019 13:24:33 +0100 Subject: [PATCH] =?UTF-8?q?correction=20des=20bugs=20de=20compilation=20et?= =?UTF-8?q?=20premi=C3=A8re=20impl=C3=A9mentation=20de=20la=20fft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/examples/test-fft.cpp | 52 +++------------------- tests/src/CMakeLists.txt | 7 +-- tests/src/math.hpp | 86 ++++++++++++++++++------------------- tests/src/test-fft.cpp | 13 ------ 4 files changed, 51 insertions(+), 107 deletions(-) delete mode 100644 tests/src/test-fft.cpp diff --git a/tests/examples/test-fft.cpp b/tests/examples/test-fft.cpp index 17db501..1531742 100644 --- a/tests/examples/test-fft.cpp +++ b/tests/examples/test-fft.cpp @@ -1,51 +1,13 @@ -#include +#include #include -#include - -double pi() { - return 3.1415; -} - -std::vector tfd2vect(fftw_complex* tfd, int N) { - std::vector res; - auto it = tfd; - for (int i = 0; i != N; ++i) { - fftw_complex c = {*it[0], *it[1]}; - res.push_back(sqrt(c[0]*c[0] + c[1]*c[1])); - it++; - } - - return res; -} int main(int argc, char** argv) { - QApplication app(argc, argv); - fftw_complex *in, *out; - fftw_plan p; + math::csignal s; - int N = 500; - in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); - out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); - p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_MEASURE); + for (int i=0; i<100; ++i) { + s.push_back(std::sin(2*math::pi()*50*i/100)); + } + math::csignal tfd = math::fft(s); - std::vector xx; - for (int i = 0; i != N; ++i) { - xx.push_back(i); - } - - for (int i = 0; i != N; ++i) { - in[i][0] = sin(2*pi()*50*i/N); - } - - fftw_execute(p); /* repeat as needed */ - - std::vector res = tfd2vect(out, N); - PlotCpp g; - g.plot(xx, res); - g.draw(); - - - fftw_destroy_plan(p); - fftw_free(in); fftw_free(out); - return app.exec(); + return 0; } diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt index ce8f025..cac1f0d 100644 --- a/tests/src/CMakeLists.txt +++ b/tests/src/CMakeLists.txt @@ -1,11 +1,8 @@ # file(GLOB headers *.hpp) # file(GLOB lib_files *.cpp) -# add_executable(traitement traitement.cpp) -# target_link_libraries(traitement ${OpenCV_LIBS} fftw3) - -add_executable(test-fft test-fft.cpp) -target_link_libraries(test-fft) +add_executable(traitement traitement.cpp) +target_link_libraries(traitement ${OpenCV_LIBS} fftw3) # target_include_directories(blk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) # target_compile_options (blk PUBLIC -std=c++11 ) diff --git a/tests/src/math.hpp b/tests/src/math.hpp index ef2c90a..fbfaf00 100644 --- a/tests/src/math.hpp +++ b/tests/src/math.hpp @@ -8,28 +8,42 @@ namespace math { - using complex = std::complex; + using complex = std::complex; using signal = std::vector; using csignal = std::vector; using contour = std::vector; constexpr double pi() {return std::atan(1)*4;} - //TODO implémenter la fft - csignal fft(const signal& input) { - //TODO: s'assurer que le signal est bien formé (i.e. bonne taille) - return fft_rec(input); + csignal cont2sig(const contour& cont) { + csignal sig; + auto sig_it = sig.begin(); + auto cont_it = cont.begin(); + + for (auto cont_it = cont.begin(); cont_it != cont.end(); ++cont_it) { + *(sig_it++) = complex((*cont_it).x, (*cont_it).y); + } + return sig; }; - csignal fft_rec(const signal& input) { + complex mean(const csignal& sig) { + complex res = 0; + for (auto x: sig) { + res += x; + } + return complex(res.real()/sig.size(), res.imag()/sig.size()); + }; + + //TODO implémenter la fft + csignal fft_rec(const csignal& input) { int size = input.size(); if (size == 1) { - return input; + return csignal(); } else { - signal odd; - signal even; - std::back_insert_iterator odd_back_it(odd); - std::back_insert_iterator even_back_it(even); + csignal odd; + csignal even; + std::back_insert_iterator odd_back_it(odd); + std::back_insert_iterator even_back_it(even); bool insert_in_even = false; for (auto it = input.begin(); it != input.end(); ++it) { @@ -42,13 +56,13 @@ namespace math { } } - signal odd_fft = fft_rec(odd); - signal even_fft = fft_rec(even); - signal res; + csignal odd_fft = fft_rec(odd); + csignal even_fft = fft_rec(even); + csignal res; res.reserve(size); for (int k = 0; k& contours) { int max = 0; int id = 0; @@ -99,13 +106,4 @@ namespace math { } return id; }; - - contour simplify_contour(const contour& cont, int cmax) { - contour res; - signal z = cont2sig(cont); - complex zm = mean(z); - signal tfd = fft(z); - res = coef2cont(tfd, zm, 0, cmax); - return res; - }; } diff --git a/tests/src/test-fft.cpp b/tests/src/test-fft.cpp deleted file mode 100644 index cd382a0..0000000 --- a/tests/src/test-fft.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include - -int main(int argc, char** argv) { - math::signal s; - - for (int i=0; i<100; ++i) { - s.push_back(std::sin(2*math::pi()*50*i/100)); - } - math::fft(s); - - return 0; -}