Permalink
Browse files

Done refactoring main algorithm into smaller classes

  • Loading branch information...
1 parent d5d0ef5 commit 3789fc4ee9099e174b20c80d02736823b2de0ec4 @prasko committed May 17, 2011
Showing with 162 additions and 67 deletions.
  1. +80 −53 src/adwt.cpp
  2. +43 −8 src/adwt.h
  3. +1 −1 src/denoise.h
  4. +3 −4 src/main.cpp
  5. +30 −0 src/sysutil.cpp
  6. +5 −1 src/sysutil.h
View
@@ -10,19 +10,19 @@
#include <iterator>
#include <utility>
+#include "adwt.h"
#include "lpw.h"
#include "combine.h"
#include "denoise.h"
#include "sysutil.h"
+
/*
Main function for ADWT calculation.
*/
-void adwt(signal &input, signal &A, signal &D) {
-
- Multiply2 mul;
- Add2 adder;
- Decimator decim;
+void adwt(signal &input, signal &res) {
+ Add2 sys_add;
+ Decimator sys_dec;
// *** PREPARATION ***
@@ -34,81 +34,108 @@ void adwt(signal &input, signal &A, signal &D) {
const int zeroes_front = 2;
const int zeroes_back = 1;
- signal xe(zeroes_front); // Xe(z)
- signal xo(zeroes_front); // Xo(z)
+ signal sig_xe(zeroes_front); // Xe(z)
+ signal sig_xo(zeroes_front); // Xo(z)
- decim.decimate(input, xe, xo);
+ sys_dec.decimate(input, sig_xe, sig_xo);
for(int i = 0; i < zeroes_back; ++i) {
- xe.push_back(0);
- xo.push_back(0);
+ sig_xe.push_back(0);
+ sig_xo.push_back(0);
}
// *** INTERPOLATION ***
- signal x2, x4, yd;
-
- // linear interpolation
- PolySystem systwin1(0.5);
- systwin1.addMember(0, 1);
- systwin1.addMember(-1, 1);
-
- systwin1.process(xe, x2);
-
- // cubic interpolation
- PolySystem systriple1(1.0/8);
- systriple1.addMember(-1, 1);
- systriple1.addMember(0, -2);
- systriple1.addMember(1, 1);
+ ADWTInterpolate sys_ip;
+ sys_ip.process(sig_xe);
+
+ signal sig_yd;
+ sys_add.add(sig_xo, sys_ip.sig_x2(), -1.0, sig_yd);
- systriple1.process(x2, x4);
+ // *** PARAMETER GUESSING ***
- // calculate Yd(z)
- adder.add(xo, x2, -1.0, yd);
+ Lpw *lsw = new Lsw(sig_yd, sys_ip.sig_x4());
+ //WindowCombiner *wc = new DenoiserWindowCombiner(*lsw);
+ WindowCombiner *wc = new ICIWindowCombiner(*lsw);
- // guess parameter b = Yd(z) / U(z)
-
- Lpw *lsw = new Lsw(yd, x4);
- // ConfidenceInterval *ci = new ConfidenceInterval(0.005);
- // ConfidenceInterval *ci = new TunnelInterval(0.05, 0.005, 0.005);
- // WindowCombiner *wc = new SimpleWindowCombiner(*lsw, *ci);
- //WindowCombiner *wc = new ICIWindowCombiner(*lsw);
- WindowCombiner *wc = new DenoiserWindowCombiner(*lsw);
-
- signal b_res;
+ signal b_res, b_dns;
wc->combine(b_res);
- signal b_dns;
Denoiser *dns = new TwoWayICIDenoiser(4.4, 0.7, 0.2);
dns->denoise(b_res, b_dns);
- for(int i = 0; i < (int)b_res.size(); ++i)
+ for(int i = 0; i < (int)b_res.size(); ++i) {
printf("(%lf %lf)", b_res[i], b_dns[i]);
- printf("\n");
+ }
- signal ub;
+ // add up
- mul.multiply(x4, b_res, ub);
+ signal sig_d;
+
+ sys_ip.finalize(sig_xo, true, &b_dns, sig_d);
+
+ // *** UPDATE ***
- // calculate D(z)
- adder.add(ub, yd, 1.0, D);
+ signal sig_a;
- // *** UPDATE ***
+ ADWTUpdate sys_up;
+ sys_up.process(sig_d);
+ sys_up.finalize(sig_xe, true, NULL, sig_a);
+}
- signal u2, u4, ya;
+void ADWTSubSystem::finalize(const signal &input, bool positive,
+ const signal *param, signal &res) {
+ assert(res.size() == 0);
+ assert(input.size() == x2_.size());
+ assert(param == NULL || param->size() == input.size());
+ Sumator sys_sum(res);
+ sys_sum.sumSignal(input, true);
+ sys_sum.sumSignal(x2_, x2_prefix());
+
+ if(param) {
+ sys_sum.sumMultiply(x4_, *param, x4_prefix());
+ } else {
+ sys_sum.sumSignal(x4_, x4_prefix());
+ }
+}
+
+void ADWTInterpolate::process_(const signal &input) {
+ // linear interpolation
+ PolySystem systwin1(0.5);
+ systwin1.addMember(0, 1);
+ systwin1.addMember(-1, 1);
+
+ systwin1.process(input, x2_);
+
+ // cubic interpolation
+ PolySystem systriple1(1.0/8);
+ systriple1.addMember(-1, 1);
+ systriple1.addMember(0, -2);
+ systriple1.addMember(1, 1);
+
+ systriple1.process(x2_, x4_);
+}
+
+bool ADWTInterpolate::x2_prefix() { return false; }
+bool ADWTInterpolate::x4_prefix() { return true; }
+
+void ADWTUpdate::process_(const signal &input) {
// linear update
PolySystem systwin2(0.25);
systwin2.addMember(0, 1);
systwin2.addMember(1, 1);
- systwin2.process(D, u2);
- // cubic update
- systriple1.process(u2, u4);
+ systwin2.process(input, x2_);
- // calculate Ya(z)
- adder.add(xe, u2, 1.0, ya);
+ // cubic update
+ PolySystem systriple1(1.0/8);
+ systriple1.addMember(-1, 1);
+ systriple1.addMember(0, -2);
+ systriple1.addMember(1, 1);
- // calculate A(z)
- adder.add(ya, u4, -1.0, A);
+ systriple1.process(x2_, x4_);
}
+
+bool ADWTUpdate::x2_prefix() { return true; }
+bool ADWTUpdate::x4_prefix() { return false; }
View
@@ -5,16 +5,51 @@
#ifndef ADWT_ADWT_H
#define ADWT_ADWT_H
-class WaveletTransform {
-public:
-
+#include <vector>
+
+typedef std::vector<double> signal;
+
+struct ADWTSubSystem {
+ ADWTSubSystem() {}
+
+ void process(const signal &input) {
+ x2_.clear();
+ x4_.clear();
+ process_(input);
+ }
+
+ const signal &sig_x2() const { return x2_; }
+ const signal &sig_x4() const { return x4_; }
+
+ void finalize(const signal &input, bool positive,
+ const signal *param, signal &res);
+
+protected:
+ virtual void process_(const signal &input) = 0;
+ virtual bool x2_prefix() = 0;
+ virtual bool x4_prefix() = 0;
+
+ signal x2_;
+ signal x4_;
+};
+
+struct ADWTInterpolate : public ADWTSubSystem {
+ ADWTInterpolate() {}
+ void process_(const signal &input);
+
+private:
+ bool x2_prefix();
+ bool x4_prefix();
};
-class AdaptiveWT {
-public:
- AdaptiveWT(Lpw &lpw) : lpw_(lpw) {}
- void doo() {}
-}
+struct ADWTUpdate : public ADWTSubSystem {
+ ADWTUpdate() {}
+ void process_(const signal &input);
+
+private:
+ bool x2_prefix();
+ bool x4_prefix();
+};
#endif // ADWT_ADWT_H
View
@@ -10,6 +10,7 @@
typedef std::vector<double> signal;
+
struct Denoiser {
Denoiser() : result_(NULL) {}
virtual void denoise(const signal &sig, signal &res) = 0;
@@ -19,7 +20,6 @@ struct Denoiser {
};
struct ICIDenoiser : public Denoiser {
-public:
ICIDenoiser(double gama=4.4, double rc=0.85, double sigma=0.2) :
Denoiser(), gama_(gama), rc_(rc), sigma_(sigma) {}
View
@@ -10,7 +10,7 @@
typedef std::vector<double> signal;
-void adwt(signal &input, signal &A, signal &D);
+void adwt(signal &input, signal &result);
int main() {
int width;
@@ -31,9 +31,8 @@ int main() {
// printf("%lf ", res[i]);
// printf("\n");
- signal resultA;
- signal resultD;
- adwt(input, resultA, resultD);
+ signal result;
+ adwt(input, result);
return 0;
View
@@ -68,3 +68,33 @@ void Sumator::sumSignal(const signal &signal, const double coef) {
for(int i = 0; i < (int)signal.size(); ++i)
*(result_->begin() + i) += coef * signal[i];
}
+
+void Sumator::sumSignal(const signal &signal, const bool prefix) {
+ if(signal.size() > result_->size())
+ result_->resize(signal.size());
+
+ if(prefix) {
+ for(int i = 0; i < (int)signal.size(); ++i) {
+ *(result_->begin() + i) += signal[i];
+ }
+ } else {
+ for(int i = 0; i < (int)signal.size(); ++i) {
+ *(result_->begin() + i) -= signal[i];
+ }
+ }
+}
+
+void Sumator::sumMultiply(const signal &s1, const signal &s2, bool prefix) {
+ if(s1.size() > result_->size() || s2.size() > result_->size())
+ result_->resize(std::max(s1.size(), s2.size()));
+
+ if(prefix) {
+ for(int i = 0; i < (int)std::min(s1.size(), s2.size()); ++i) {
+ *(result_->begin() + i) += s1[i] * s2[i];
+ }
+ } else {
+ for(int i = 0; i < (int)std::min(s1.size(), s2.size()); ++i) {
+ *(result_->begin() + i) -= s1[i] * s2[i];
+ }
+ }
+}
View
@@ -37,7 +37,9 @@ class PolySystem : public System {
class Multiply2 : public System {
public:
- Multiply2(const double constant=1.0) : System(constant) {}
+ Multiply2(const double constant) : System(constant) {}
+ Multiply2(const bool constant=true) : System(constant ? 1.0 : -1.0) {}
+
void multiply(const signal &s1, const signal &s2, signal &res);
};
@@ -52,6 +54,8 @@ class Sumator {
Sumator(signal &result) : result_(&result) {}
void init(signal &result);
void sumSignal(const signal &signal, const double coef);
+ void sumSignal(const signal &signal, const bool prefix);
+ void sumMultiply(const signal &s1, const signal &s2, const bool prefix);
private:
signal *result_;

0 comments on commit 3789fc4

Please sign in to comment.