File: wavelet.cc

package info (click to toggle)
mia 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,532 kB
  • ctags: 16,800
  • sloc: cpp: 137,909; python: 1,057; ansic: 998; sh: 146; xml: 127; csh: 24; makefile: 13
file content (133 lines) | stat: -rw-r--r-- 3,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis 
 * Copyright (c) Leipzig, Madrid 1999-2014 Gert Wollny
 *
 * MIA 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.
 *
 * This program 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 MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdexcept>
#include <iostream>
#include <gsl++/wavelet.hh>
#include <gsl/gsl_wavelet.h>

using std::invalid_argument; 
using std::vector; 
using std::copy; 

namespace gsl {


struct C1DWaveletImpl {
public:
	C1DWaveletImpl(EWaveletType wt, size_t k); 

	~C1DWaveletImpl(); 

	template <typename T> 
	vector<double> forward(const vector<T>& x) const; 
	
	vector<double> backward(const vector<double>& x) const; 

private: 
	gsl_wavelet *m_wavelet;
}; 


C1DWavelet::C1DWavelet(EWaveletType wt, size_t k):
	impl(new C1DWaveletImpl(wt,k))
{
}

C1DWavelet::~C1DWavelet()
{
	delete impl; 
}

std::vector<double> C1DWavelet::backward(const std::vector<double>& x) const
{
	return impl->forward(x); 
}

std::vector<double> C1DWavelet::forward(const std::vector<double>& x) const
{
	return impl->forward(x); 
}

std::vector<double> C1DWavelet::forward(const std::vector<float>& x) const
{
	return impl->forward(x); 
}

C1DWaveletImpl::C1DWaveletImpl(EWaveletType wt, size_t k):
	m_wavelet(NULL)
{
	switch (wt) {
	case wt_haar:         m_wavelet = gsl_wavelet_alloc (gsl_wavelet_haar_centered, k); break; 
	case wt_haar_centered:m_wavelet = gsl_wavelet_alloc (gsl_wavelet_haar_centered, k); break; 
	case wt_daubechies:          m_wavelet = gsl_wavelet_alloc (gsl_wavelet_daubechies, k); break; 
	case wt_daubechies_centered: m_wavelet = gsl_wavelet_alloc (gsl_wavelet_daubechies_centered, k); break; 
	case wt_bspline:         m_wavelet = gsl_wavelet_alloc (gsl_wavelet_bspline_centered, k); break; 
	case wt_bspline_centered:m_wavelet = gsl_wavelet_alloc (gsl_wavelet_bspline_centered, k); break; 
	default:
		throw invalid_argument("C1DWavelet: Unknown wavelet type requested");
	}
	if (!m_wavelet) 
		throw invalid_argument("C1DWavelet: Unsupported member or insufficienr memory");
}


C1DWaveletImpl::~C1DWaveletImpl()
{
	gsl_wavelet_free(m_wavelet); 
}

vector<double> C1DWaveletImpl::backward(const vector<double>& x) const
{
	vector<double> result(x); 

	gsl_wavelet_workspace * ws = gsl_wavelet_workspace_alloc (x.size()); 
	gsl_wavelet_transform_inverse(m_wavelet, &result[0], 1, x.size(), ws);
	gsl_wavelet_workspace_free (ws); 

	return result;
}

template <typename T> 
vector<double> C1DWaveletImpl::forward(const vector<T>& x) const
{
	size_t s = x.size(); 
	size_t s1 = 1; 
	while (s) {
		s /= 2; 
		s1 *=2; 
	}
	
	if (s1 < x.size()) 
		s1 *=2;
		
	vector<double> x_size_to_pow2(s1); 
	copy(x.begin(), x.end(), x_size_to_pow2.begin());

	
	gsl_wavelet_workspace * ws = gsl_wavelet_workspace_alloc (s1); 
	gsl_wavelet_transform_forward (m_wavelet, &x_size_to_pow2[0], 1, s1, ws); 
	gsl_wavelet_workspace_free (ws); 
	return x_size_to_pow2; 
}



}