NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
wavelet.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2018 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "wavelet.hpp"
20#include "../ui/error.hpp"
21#include "resampler.h"
22#include <gsl/gsl_wavelet.h>
23
24#include <cmath>
25
26void calculateWavelet(std::vector<double>& data, WaveletType _type, int k, int dir)
27{
28 gsl_wavelet* wavelet = nullptr;
29 gsl_wavelet_workspace* workspace = nullptr;
30
31 size_t sze = 1 << (size_t)std::log2(data.size());
32
33 // assure that the data size is fitting (power of 2)
34 if (sze != data.size())
36
37 // allocate the wavelet type data
38 switch (_type)
39 {
40 case Daubechies:
41 if (k == 2) // special case: Daubechies with k = 2 is the haar wavelet
42 {
43 wavelet = gsl_wavelet_alloc(gsl_wavelet_haar, 2);
44 break;
45 }
46 if (k < 4 || k > 20 || k % 2) // implemented values for k according https://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html
48 wavelet = gsl_wavelet_alloc(gsl_wavelet_daubechies, k);
49 break;
51 if (k == 2) // special case: Daubechies with k = 2 is the haar wavelet
52 {
53 wavelet = gsl_wavelet_alloc(gsl_wavelet_haar_centered, 2);
54 break;
55 }
56 if (k < 4 || k > 20 || k % 2) // implemented values for k according https://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html
58 wavelet = gsl_wavelet_alloc(gsl_wavelet_daubechies_centered, k);
59 break;
60 case Haar:
61 wavelet = gsl_wavelet_alloc(gsl_wavelet_haar, 2); // implemented values for k according https://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html
62 break;
63 case CenteredHaar:
64 wavelet = gsl_wavelet_alloc(gsl_wavelet_haar_centered, 2); // implemented values for k according https://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html
65 break;
66 case BSpline:
67 if (k != 103 && k != 105 && !(k >= 202 && k <= 208 && !(k % 2)) && !(k >= 301 && k <= 309 && k % 2)) // implemented values for k according https://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html
69 wavelet = gsl_wavelet_alloc(gsl_wavelet_bspline, k);
70 break;
71 case CenteredBSpline:
72 if (k != 103 && k != 105 && !(k >= 202 && k <= 208 && !(k % 2)) && !(k >= 301 && k <= 309 && k % 2)) // implemented values for k according https://www.gnu.org/software/gsl/manual/html_node/DWT-Initialization.html
74 wavelet = gsl_wavelet_alloc(gsl_wavelet_bspline_centered, k);
75 break;
76 }
77
78 // assure that the combination is possible
79 if (wavelet == nullptr)
80 {
82 }
83
84 // allocate the workspace
85 workspace = gsl_wavelet_workspace_alloc(data.size());
86
87 // transform the data (done in the array itself)
88 if (dir > 0)
89 gsl_wavelet_transform_forward(wavelet, &data[0], 1, data.size(), workspace);
90 else
91 gsl_wavelet_transform_inverse(wavelet, &data[0], 1, data.size(), workspace);
92
93 // free the data
94 gsl_wavelet_free(wavelet);
95 gsl_wavelet_workspace_free(workspace);
96}
97
98
99NumeRe::Table decodeWaveletData(const std::vector<double>& vWaveletData, const std::vector<double>& vAxisData)
100{
101 int nLevels = log2(vWaveletData.size());
102 int nTimePoints = vWaveletData.size() / 2;
103 NumeRe::Table wavelet;
104 wavelet.setSize(nTimePoints, nLevels+3);
105 double* data = new double[nTimePoints];
106 const double* output = nullptr;
107
108 // write the axes
109 for (int i = -1; i < nLevels; i++)
110 {
111 wavelet.setValue(i+1, 1, i);
112 }
113 // Resample the axisdata
114 Resampler axis(vAxisData.size(), 1, nTimePoints, 1, Resampler::BOUNDARY_CLAMP, 1.0, 0.0, "lanczos6");
115 axis.put_line(&vAxisData[0]);
116 const double* axis_out = axis.get_line();
117 for (int i = 0; i < nTimePoints; i++)
118 {
119 wavelet.setValue(i, 0, axis_out[i]);
120 }
121
122 // write the data
123 for (int i = 0; i <= nLevels; i++)
124 {
125 if (i < 2)
126 {
127 // this case means only to write the data to a whole line
128 for (int j = 0; j < nTimePoints; j++)
129 {
130 wavelet.setValue(j, nLevels-i+2, vWaveletData[i]);
131 }
132 }
133 else
134 {
135 // copy the coefficients of the current level
136 for (int k = 0; k < (1 << ((i-1))); k++)
137 {
138 data[k] = vWaveletData[(1 << (i-1))+k];
139 }
140 // resample them
141 Resampler res((1 << ((i-1))), 1, nTimePoints, 1, Resampler::BOUNDARY_CLAMP, 1.0, 0.0, "lanczos6");
142 res.put_line(data);
143
144 output = res.get_line();
145 // write the output to the table
146 for (int j = 0; j < nTimePoints; j++)
147 {
148 wavelet.setValue(j, nLevels-i+2, output[j]);
149 }
150 }
151
152 }
153
154 delete[] data;
155 return wavelet;
156}
157
158
This data container is a copy- efficient table to interchange data between Kernel and GUI.
Definition: table.hpp:87
void setValue(size_t i, size_t j, const mu::value_type &_dValue)
This member function sets the data to the table. It will resize the table automatically,...
Definition: table.cpp:291
void setSize(size_t i, size_t j)
This member function simply redirects the control to setMinSize().
Definition: table.cpp:165
const Sample * get_line()
Definition: resampler.cpp:924
@ BOUNDARY_CLAMP
Definition: resampler.h:35
bool put_line(const Sample *Psrc)
Definition: resampler.cpp:848
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ WRONG_DATA_SIZE
Definition: error.hpp:229
@ INVALID_WAVELET_COEFFICIENT
Definition: error.hpp:136
@ INVALID_WAVELET_TYPE
Definition: error.hpp:135
static size_t invalid_position
Definition: error.hpp:235
void calculateWavelet(std::vector< double > &data, WaveletType _type, int k, int dir)
Definition: wavelet.cpp:26
NumeRe::Table decodeWaveletData(const std::vector< double > &vWaveletData, const std::vector< double > &vAxisData)
Definition: wavelet.cpp:99
WaveletType
Definition: wavelet.hpp:27
@ Daubechies
Definition: wavelet.hpp:28
@ CenteredDaubechies
Definition: wavelet.hpp:29
@ CenteredBSpline
Definition: wavelet.hpp:33
@ BSpline
Definition: wavelet.hpp:32
@ CenteredHaar
Definition: wavelet.hpp:31
@ Haar
Definition: wavelet.hpp:30