From 65a656f2573d61010d7e8ba8ccc94e5fa10c6831 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Thu, 17 Feb 2022 19:24:34 +0000 Subject: [PATCH] first skeleton for DWT --- lib/Makefile.am | 4 + lib/Numerical/DWT.cpp | 57 ++++ lib/Numerical/DWT.hpp | 52 ++++ lib/Numerical/DWTFilters.cpp | 528 +++++++++++++++++++++++++++++++++++ lib/Numerical/DWTFilters.hpp | 53 ++++ 5 files changed, 694 insertions(+) create mode 100644 lib/Numerical/DWT.cpp create mode 100644 lib/Numerical/DWT.hpp create mode 100644 lib/Numerical/DWTFilters.cpp create mode 100644 lib/Numerical/DWTFilters.hpp diff --git a/lib/Makefile.am b/lib/Makefile.am index 9fb5e8c..8f33d71 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -48,6 +48,8 @@ libLatAnalyze_la_SOURCES = \ Io/XmlReader.cpp \ Io/Xml/tinyxml2.cpp \ Numerical/Derivative.cpp \ + Numerical/DWT.cpp \ + Numerical/DWTFilters.cpp \ Numerical/GslFFT.cpp \ Numerical/GslHybridRootFinder.cpp\ Numerical/GslMinimizer.cpp \ @@ -92,6 +94,8 @@ HPPFILES = \ Io/IoObject.hpp \ Io/XmlReader.hpp \ Numerical/Derivative.hpp \ + Numerical/DWT.hpp \ + Numerical/DWTFilters.hpp \ Numerical/FFT.hpp \ Numerical/GslFFT.hpp \ Numerical/GslHybridRootFinder.hpp\ diff --git a/lib/Numerical/DWT.cpp b/lib/Numerical/DWT.cpp new file mode 100644 index 0000000..b5c49a7 --- /dev/null +++ b/lib/Numerical/DWT.cpp @@ -0,0 +1,57 @@ +/* + * DWT.cpp, part of LatAnalyze + * + * Copyright (C) 2013 - 2020 Antonin Portelli + * + * LatAnalyze is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LatAnalyze is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LatAnalyze. If not, see . + */ + +#include +#include + +using namespace std; +using namespace Latan; + +/****************************************************************************** + * DWT implementation * + ******************************************************************************/ +// constructor ///////////////////////////////////////////////////////////////// +DWT::DWT(const DWTFilter &filter) +: filter_(filter) +{} + +// convolution primitive /////////////////////////////////////////////////////// +DVec DWT::filterConvolution(const DVec &data, const DWTFilter &filter, + const Index offset) +{ + DVec res(data.size()); + + return res; +} + +// DWT ///////////////////////////////////////////////////////////////////////// +std::vector +DWT::forward(const DVec &data, const unsigned int level) const +{ + std::vector dwt(level); + + return dwt; +} + +DVec DWT::backward(const std::vector& dwt) const +{ + DVec res; + + return res; +} diff --git a/lib/Numerical/DWT.hpp b/lib/Numerical/DWT.hpp new file mode 100644 index 0000000..f1ee646 --- /dev/null +++ b/lib/Numerical/DWT.hpp @@ -0,0 +1,52 @@ +/* + * DWT.hpp, part of LatAnalyze + * + * Copyright (C) 2013 - 2020 Antonin Portelli + * + * LatAnalyze is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LatAnalyze is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LatAnalyze. If not, see . + */ + +#ifndef Latan_DWT_hpp_ +#define Latan_DWT_hpp_ + +#include +#include + +BEGIN_LATAN_NAMESPACE + +/****************************************************************************** + * Discrete wavelet transform class * + ******************************************************************************/ +class DWT +{ +public: + typedef std::pair DWTLevel; +public: + // constructor + DWT(const DWTFilter &filter); + // destructor + virtual ~DWT(void) = default; + // convolution primitive + static DVec filterConvolution(const DVec &data, const DWTFilter &filter, + const Index offset); + // DWT + std::vector forward(const DVec &data, const unsigned int level) const; + DVec backward(const std::vector& dwt) const; +private: + DWTFilter filter_; +}; + +END_LATAN_NAMESPACE + +#endif // Latan_DWT_hpp_ diff --git a/lib/Numerical/DWTFilters.cpp b/lib/Numerical/DWTFilters.cpp new file mode 100644 index 0000000..be3accf --- /dev/null +++ b/lib/Numerical/DWTFilters.cpp @@ -0,0 +1,528 @@ +/* + * DWTFilters.cpp, part of LatAnalyze + * + * Copyright (C) 2013 - 2020 Antonin Portelli + * + * LatAnalyze is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LatAnalyze is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LatAnalyze. If not, see . + */ + +#include +#include + +// cf. http://wavelets.pybytes.com +// *here we implement the reverse filters more convenient for convolutions* + +using namespace std; +using namespace Latan; + +#define FILTDICT(x) {#x, &DWTFilters::x} + +std::map DWTFilters::fromName = { + FILTDICT(haar), + FILTDICT(db2), + FILTDICT(db3), + FILTDICT(db4), + FILTDICT(db5), + FILTDICT(db6), + FILTDICT(bior13), + FILTDICT(bior15), + FILTDICT(bior22), + FILTDICT(bior24), + FILTDICT(bior31), + FILTDICT(bior33), + FILTDICT(bior35) +}; + +DWTFilter DWTFilters::haar = { + // fwdL + {0.7071067811865476, + 0.7071067811865476}, + // fwdH + {0.7071067811865476, + -0.7071067811865476}, + // bwdL + {0.7071067811865476, + 0.7071067811865476}, + // bwdH + {-0.7071067811865476, + 0.7071067811865476} +}; + +DWTFilter DWTFilters::db2 = { + // fwdL + {0.48296291314469025, + 0.836516303737469, + 0.22414386804185735, + -0.12940952255092145}, + // fwdH + {-0.12940952255092145, + -0.22414386804185735, + 0.836516303737469, + -0.48296291314469025}, + // bwdL + {-0.12940952255092145, + 0.22414386804185735, + 0.836516303737469, + 0.48296291314469025}, + // bwdH + {-0.48296291314469025, + 0.836516303737469, + -0.22414386804185735, + -0.12940952255092145} +}; + +DWTFilter DWTFilters::db3 = { + // fwdL + {0.3326705529509569, + 0.8068915093133388, + 0.4598775021193313, + -0.13501102001039084, + -0.08544127388224149, + 0.035226291882100656}, + // fwdH + {0.035226291882100656, + 0.08544127388224149, + -0.13501102001039084, + -0.4598775021193313, + 0.8068915093133388, + -0.3326705529509569}, + // bwdL + {0.035226291882100656, + -0.08544127388224149, + -0.13501102001039084, + 0.4598775021193313, + 0.8068915093133388, + 0.3326705529509569}, + // bwdH + {-0.3326705529509569, + 0.8068915093133388, + -0.4598775021193313, + -0.13501102001039084, + 0.08544127388224149, + 0.035226291882100656} +}; + +DWTFilter DWTFilters::db4 = { + // fwdL + {0.23037781330885523, + 0.7148465705525415, + 0.6308807679295904, + -0.02798376941698385, + -0.18703481171888114, + 0.030841381835986965, + 0.032883011666982945, + -0.010597401784997278}, + // fwdH + {-0.010597401784997278, + -0.032883011666982945, + 0.030841381835986965, + 0.18703481171888114, + -0.02798376941698385, + -0.6308807679295904, + 0.7148465705525415, + -0.23037781330885523}, + // bwdL + {-0.010597401784997278, + 0.032883011666982945, + 0.030841381835986965, + -0.18703481171888114, + -0.02798376941698385, + 0.6308807679295904, + 0.7148465705525415, + 0.23037781330885523}, + // bwdH + {-0.23037781330885523, + 0.7148465705525415, + -0.6308807679295904, + -0.02798376941698385, + 0.18703481171888114, + 0.030841381835986965, + -0.032883011666982945, + -0.010597401784997278} +}; + +DWTFilter DWTFilters::db5 = { + // fwdL + {0.160102397974125, + 0.6038292697974729, + 0.7243085284385744, + 0.13842814590110342, + -0.24229488706619015, + -0.03224486958502952, + 0.07757149384006515, + -0.006241490213011705, + -0.012580751999015526, + 0.003335725285001549}, + // fwdH + {0.003335725285001549, + 0.012580751999015526, + -0.006241490213011705, + -0.07757149384006515, + -0.03224486958502952, + 0.24229488706619015, + 0.13842814590110342, + -0.7243085284385744, + 0.6038292697974729, + -0.160102397974125}, + // bwdL + {0.003335725285001549, + -0.012580751999015526, + -0.006241490213011705, + 0.07757149384006515, + -0.03224486958502952, + -0.24229488706619015, + 0.13842814590110342, + 0.7243085284385744, + 0.6038292697974729, + 0.160102397974125}, + // bwdH + {-0.160102397974125, + 0.6038292697974729, + -0.7243085284385744, + 0.13842814590110342, + 0.24229488706619015, + -0.03224486958502952, + -0.07757149384006515, + -0.006241490213011705, + 0.012580751999015526, + 0.003335725285001549} +}; + +DWTFilter DWTFilters::db6 = { + // fwdL + {0.11154074335008017, + 0.4946238903983854, + 0.7511339080215775, + 0.3152503517092432, + -0.22626469396516913, + -0.12976686756709563, + 0.09750160558707936, + 0.02752286553001629, + -0.031582039318031156, + 0.0005538422009938016, + 0.004777257511010651, + -0.00107730108499558}, + // fwdH + {-0.00107730108499558, + -0.004777257511010651, + 0.0005538422009938016, + 0.031582039318031156, + 0.02752286553001629, + -0.09750160558707936, + -0.12976686756709563, + 0.22626469396516913, + 0.3152503517092432, + -0.7511339080215775, + 0.4946238903983854, + -0.11154074335008017}, + // bwdL + {-0.00107730108499558, + 0.004777257511010651, + 0.0005538422009938016, + -0.031582039318031156, + 0.02752286553001629, + 0.09750160558707936, + -0.12976686756709563, + -0.22626469396516913, + 0.3152503517092432, + 0.7511339080215775, + 0.4946238903983854, + 0.11154074335008017}, + // bwdH + {-0.11154074335008017, + 0.4946238903983854, + -0.7511339080215775, + 0.3152503517092432, + 0.22626469396516913, + -0.12976686756709563, + -0.09750160558707936, + 0.02752286553001629, + 0.031582039318031156, + 0.0005538422009938016, + -0.004777257511010651, + -0.00107730108499558} +}; + +DWTFilter DWTFilters::bior13 = { + // fwdL + {-0.08838834764831845, + 0.08838834764831845, + 0.7071067811865476, + 0.7071067811865476, + 0.08838834764831845, + -0.08838834764831845}, + // fwdH + {0.0, + 0.0, + 0.7071067811865476, + -0.7071067811865476, + 0.0, + 0.0}, + // bwdL + {0.0, + 0.0, + 0.7071067811865476, + 0.7071067811865476, + 0.0, + 0.0}, + // bwdH + {0.08838834764831845, + 0.08838834764831845, + -0.7071067811865476, + 0.7071067811865476, + -0.08838834764831845, + -0.08838834764831845} +}; + +DWTFilter DWTFilters::bior15 = { + // fwdL + {0.01657281518405971, + -0.01657281518405971, + -0.12153397801643787, + 0.12153397801643787, + 0.7071067811865476, + 0.7071067811865476, + 0.12153397801643787, + -0.12153397801643787, + -0.01657281518405971, + 0.01657281518405971}, + // fwdH + {0.0, + 0.0, + 0.0, + 0.0, + 0.7071067811865476, + -0.7071067811865476, + 0.0, + 0.0, + 0.0, + 0.0}, + // bwdL + {0.0, + 0.0, + 0.0, + 0.0, + 0.7071067811865476, + 0.7071067811865476, + 0.0, + 0.0, + 0.0, + 0.0}, + // bwdH + {-0.01657281518405971, + -0.01657281518405971, + 0.12153397801643787, + 0.12153397801643787, + -0.7071067811865476, + 0.7071067811865476, + -0.12153397801643787, + -0.12153397801643787, + 0.01657281518405971, + 0.01657281518405971} +}; + +DWTFilter DWTFilters::bior22 = { + // fwdL + {-0.1767766952966369, + 0.3535533905932738, + 1.0606601717798214, + 0.3535533905932738, + -0.1767766952966369, + 0.0}, + // fwdH + {0.0, + 0.0, + 0.3535533905932738, + -0.7071067811865476, + 0.3535533905932738, + 0.0}, + // bwdL + {0.0, + 0.0, + 0.3535533905932738, + 0.7071067811865476, + 0.3535533905932738, + 0.0}, + // bwdH + {0.1767766952966369, + 0.3535533905932738, + -1.0606601717798214, + 0.3535533905932738, + 0.1767766952966369, + 0.0} +}; + +DWTFilter DWTFilters::bior24 = { + // fwdL + {0.03314563036811942, + -0.06629126073623884, + -0.1767766952966369, + 0.4198446513295126, + 0.9943689110435825, + 0.4198446513295126, + -0.1767766952966369, + -0.06629126073623884, + 0.03314563036811942, + 0.0}, + // fwdH + {0.0, + 0.0, + 0.0, + 0.0, + 0.3535533905932738, + -0.7071067811865476, + 0.3535533905932738, + 0.0, + 0.0, + 0.0}, + // bwdL + {0.0, + 0.0, + 0.0, + 0.0, + 0.3535533905932738, + 0.7071067811865476, + 0.3535533905932738, + 0.0, + 0.0, + 0.0}, + // bwdH + {-0.03314563036811942, + -0.06629126073623884, + 0.1767766952966369, + 0.4198446513295126, + -0.9943689110435825, + 0.4198446513295126, + 0.1767766952966369, + -0.06629126073623884, + -0.03314563036811942, + 0.0} +}; + +DWTFilter DWTFilters::bior31 = { + // fwdL + {-0.3535533905932738, + 1.0606601717798214, + 1.0606601717798214, + -0.3535533905932738}, + // fwdH + {0.1767766952966369, + -0.5303300858899107, + 0.5303300858899107, + -0.1767766952966369}, + // bwdL + {0.1767766952966369, + 0.5303300858899107, + 0.5303300858899107, + 0.1767766952966369}, + // bwdH + {0.3535533905932738, + 1.0606601717798214, + -1.0606601717798214, + -0.3535533905932738} +}; + +DWTFilter DWTFilters::bior33 = { + // fwdL + {0.06629126073623884, + -0.19887378220871652, + -0.15467960838455727, + 0.9943689110435825, + 0.9943689110435825, + -0.15467960838455727, + -0.19887378220871652, + 0.06629126073623884}, + // fwdH + {0.0, + 0.0, + 0.1767766952966369, + -0.5303300858899107, + 0.5303300858899107, + -0.1767766952966369, + 0.0, + 0.0}, + // bwdL + {0.0, + 0.0, + 0.1767766952966369, + 0.5303300858899107, + 0.5303300858899107, + 0.1767766952966369, + 0.0, + 0.0}, + // bwdH + {-0.06629126073623884, + -0.19887378220871652, + 0.15467960838455727, + 0.9943689110435825, + -0.9943689110435825, + -0.15467960838455727, + 0.19887378220871652, + 0.06629126073623884} +}; + +DWTFilter DWTFilters::bior35 = { + // fwdL + {-0.013810679320049757, + 0.04143203796014927, + 0.052480581416189075, + -0.26792717880896527, + -0.07181553246425874, + 0.966747552403483, + 0.966747552403483, + -0.07181553246425874, + -0.26792717880896527, + 0.052480581416189075, + 0.04143203796014927, + -0.013810679320049757}, + // fwdH + {0.0, + 0.0, + 0.0, + 0.0, + 0.1767766952966369, + -0.5303300858899107, + 0.5303300858899107, + -0.1767766952966369, + 0.0, + 0.0, + 0.0, + 0.0}, + // bwdL + {0.0, + 0.0, + 0.0, + 0.0, + 0.1767766952966369, + 0.5303300858899107, + 0.5303300858899107, + 0.1767766952966369, + 0.0, + 0.0, + 0.0, + 0.0}, + // bwdH + {0.013810679320049757, + 0.04143203796014927, + -0.052480581416189075, + -0.26792717880896527, + 0.07181553246425874, + 0.966747552403483, + -0.966747552403483, + -0.07181553246425874, + 0.26792717880896527, + 0.052480581416189075, + -0.04143203796014927, + -0.013810679320049757} +}; diff --git a/lib/Numerical/DWTFilters.hpp b/lib/Numerical/DWTFilters.hpp new file mode 100644 index 0000000..8ceab9a --- /dev/null +++ b/lib/Numerical/DWTFilters.hpp @@ -0,0 +1,53 @@ +/* + * DWTFilters.hpp, part of LatAnalyze + * + * Copyright (C) 2013 - 2020 Antonin Portelli + * + * LatAnalyze is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LatAnalyze is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LatAnalyze. If not, see . + */ + +#ifndef Latan_DWTFilters_hpp_ +#define Latan_DWTFilters_hpp_ + +#include + +BEGIN_LATAN_NAMESPACE + +struct DWTFilter +{ + const std::vector fwdL, fwdH, bwdL, bwdH; +}; + +namespace DWTFilters +{ + extern DWTFilter haar; + extern DWTFilter db2; + extern DWTFilter db3; + extern DWTFilter db4; + extern DWTFilter db5; + extern DWTFilter db6; + extern DWTFilter bior13; + extern DWTFilter bior15; + extern DWTFilter bior22; + extern DWTFilter bior24; + extern DWTFilter bior31; + extern DWTFilter bior33; + extern DWTFilter bior35; + + extern std::map fromName; +} + +END_LATAN_NAMESPACE + +#endif // Latan_DWTFilters_hpp_