From ea705aedc18ef80ed3cf3fe1cc076b41c5453544 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Sun, 1 Dec 2013 21:27:25 +0100 Subject: [PATCH] Adds complete wavelet algorithm --- wavelet/wavelet2.hpp | 21 ++++++++++++++++++--- wavelet/wavelet_test.cpp | 0 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 wavelet/wavelet_test.cpp diff --git a/wavelet/wavelet2.hpp b/wavelet/wavelet2.hpp index 8fe0c88..2b176aa 100644 --- a/wavelet/wavelet2.hpp +++ b/wavelet/wavelet2.hpp @@ -13,17 +13,18 @@ * * But also less abstract (which can be both a good thing and bad thing) * + * wavelet function does not shuffle! */ namespace wvlt{ inline namespace V2 { - double inner_product(double* x, double const* coef, unsigned int stride){ + inline double inner_product(double* x, double const* coef, unsigned int stride){ return x[0] * coef[0] + x[stride] * coef[1] + x[2*stride] * coef[2] + x[3*stride] * coef[3]; } // will overwrite x, x1 and x2 are next elements, or wrap around // size is size of vector x (so x[size-1] is valid) - void wavelet_mul(double* x, double x1, double x2, unsigned int size, unsigned int stride){ + inline void wavelet_mul(double* x, double x1, double x2, unsigned int size, unsigned int stride){ assert(is_pow_of_two(size) && is_pow_of_two(stride) && 4*stride <= size); for(int i = 0; i < size - 2*stride; i += 2*stride){ @@ -42,7 +43,7 @@ namespace wvlt{ // will overwrite x, x2 and x1 are previous elements, or wrap around // size is size of vector x (so x[size-1] is valid) - void wavelet_inv(double* x, double x1, double x2, unsigned int size, unsigned int stride){ + inline void wavelet_inv(double* x, double x1, double x2, unsigned int size, unsigned int stride){ assert(is_pow_of_two(size) && is_pow_of_two(stride) && 4*stride <= size); for(int i = size - 2*stride; i >= 2*stride; i -= 2*stride){ @@ -58,5 +59,19 @@ namespace wvlt{ x[i] = y1; x[i+stride] = y2; } + + inline void wavelet(double* x, unsigned int size){ + assert(is_pow_of_two(size) && size >= 4); + for(int i = 1; i <= size / 4; i <<= 1){ + wavelet_mul(x, x[0], x[i], size, i); + } + } + + inline void unwavelet(double* x, unsigned int size){ + assert(is_pow_of_two(size) && size >= 4); + for(int i = size / 4; i >= 1; i >>= 1){ + wavelet_inv(x, x[size-i], x[size-2*i], size, i); + } + } } } diff --git a/wavelet/wavelet_test.cpp b/wavelet/wavelet_test.cpp new file mode 100644 index 0000000..e69de29