rgsl/types/wavelet_transforms.rs
1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5/*!
6# Wavelet Transforms
7
8This chapter describes functions for performing Discrete Wavelet Transforms (DWTs). The library includes wavelets for real data in both
9one and two dimensions.
10
11## Definitions
12
13The continuous wavelet transform and its inverse are defined by the relations,
14
15w(s,\tau) = \int f(t) * \psi^*_{s,\tau}(t) dt
16and,
17
18f(t) = \int \int_{-\infty}^\infty w(s, \tau) * \psi_{s,\tau}(t) d\tau ds
19where the basis functions \psi_{s,\tau} are obtained by scaling and translation from a single function, referred to as the mother wavelet.
20
21The discrete version of the wavelet transform acts on equally-spaced samples, with fixed scaling and translation steps (s, \tau). The
22frequency and time axes are sampled dyadically on scales of 2^j through a level parameter j. The resulting family of functions
23{\psi_{j,n}} constitutes an orthonormal basis for square-integrable signals.
24
25The discrete wavelet transform is an O(N) algorithm, and is also referred to as the fast wavelet transform.
26
27## References and Further Reading
28
29The mathematical background to wavelet transforms is covered in the original lectures by Daubechies,
30
31Ingrid Daubechies. Ten Lectures on Wavelets. CBMS-NSF Regional Conference Series in Applied Mathematics (1992), SIAM, ISBN 0898712742.
32An easy to read introduction to the subject with an emphasis on the application of the wavelet transform in various branches of science is,
33
34Paul S. Addison. The Illustrated Wavelet Transform Handbook. Institute of Physics Publishing (2002), ISBN 0750306920.
35For extensive coverage of signal analysis by wavelets, wavelet packets and local cosine bases see,
36
37S. G. Mallat. A wavelet tour of signal processing (Second edition). Academic Press (1999), ISBN 012466606X.
38The concept of multiresolution analysis underlying the wavelet transform is described in,
39
40S. G. Mallat. Multiresolution Approximations and Wavelet Orthonormal Bases of L^2(R). Transactions of the American Mathematical Society,
41315(1), 1989, 69–87.
42S. G. Mallat. A Theory for Multiresolution Signal Decomposition—The Wavelet Representation. IEEE Transactions on Pattern Analysis and
43Machine Intelligence, 11, 1989, 674–693.
44The coefficients for the individual wavelet families implemented by the library can be found in the following papers,
45
46I. Daubechies. Orthonormal Bases of Compactly Supported Wavelets. Communications on Pure and Applied Mathematics, 41 (1988) 909–996.
47A. Cohen, I. Daubechies, and J.-C. Feauveau. Biorthogonal Bases of Compactly Supported Wavelets. Communications on Pure and Applied
48Mathematics, 45 (1992) 485–560.
49The PhysioNet archive of physiological datasets can be found online at http://www.physionet.org/ and is described in the following paper,
50
51Goldberger et al. PhysioBank, PhysioToolkit, and PhysioNet: Components of a New Research Resource for Complex Physiologic Signals.
52Circulation 101(23):e215-e220 2000.
53!*/
54
55use ffi::FFI;
56
57ffi_wrapper!(
58 Wavelet,
59 *mut sys::gsl_wavelet,
60 gsl_wavelet_free,
61 "The Wavelet structure contains the filter coefficients defining the wavelet and any associated
62offset parameters."
63);
64
65impl Wavelet {
66 /// This function allocates and initializes a wavelet object of type T. The parameter k selects the specific member of the wavelet
67 /// family. A null pointer is returned if insufficient memory is available or if a unsupported member is selected.
68 #[doc(alias = "gsl_wavelet_alloc")]
69 pub fn new(t: WaveletType, k: usize) -> Option<Wavelet> {
70 let tmp = unsafe { sys::gsl_wavelet_alloc(t.unwrap_shared(), k) };
71
72 if tmp.is_null() {
73 None
74 } else {
75 Some(Wavelet::wrap(tmp))
76 }
77 }
78
79 /// This function returns a pointer to the name of the wavelet family for w.
80 #[doc(alias = "gsl_wavelet_name")]
81 pub fn name(&self) -> Option<String> {
82 let tmp = unsafe { sys::gsl_wavelet_name(self.unwrap_shared()) };
83
84 if tmp.is_null() {
85 None
86 } else {
87 unsafe {
88 Some(
89 String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string(),
90 )
91 }
92 }
93 }
94}
95
96ffi_wrapper!(WaveletType, *const sys::gsl_wavelet_type,
97"The centered forms of the wavelets align the coefficients of the various sub-bands on edges. Thus
98the resulting visualization of the coefficients of the wavelet transform in the phase plane is
99easier to understand.");
100
101impl WaveletType {
102 /// This is the Daubechies wavelet family of maximum phase with k/2 vanishing moments. The implemented wavelets are k=4, 6, …, 20, with
103 /// k even.
104 #[doc(alias = "gsl_wavelet_daubechies")]
105 pub fn daubechies() -> WaveletType {
106 ffi_wrap!(gsl_wavelet_daubechies)
107 }
108
109 /// This is the Daubechies wavelet family of maximum phase with k/2 vanishing moments. The implemented wavelets are k=4, 6, …, 20, with
110 /// k even.
111 #[doc(alias = "gsl_wavelet_daubechies_centered")]
112 pub fn daubechies_centered() -> WaveletType {
113 ffi_wrap!(gsl_wavelet_daubechies_centered)
114 }
115
116 /// This is the Haar wavelet. The only valid choice of k for the Haar wavelet is k=2.
117 #[doc(alias = "gsl_wavelet_haar")]
118 pub fn haar() -> WaveletType {
119 ffi_wrap!(gsl_wavelet_haar)
120 }
121
122 /// This is the Haar wavelet. The only valid choice of k for the Haar wavelet is k=2.
123 #[doc(alias = "gsl_wavelet_haar_centered")]
124 pub fn haar_centered() -> WaveletType {
125 ffi_wrap!(gsl_wavelet_haar_centered)
126 }
127
128 /// This is the biorthogonal B-spline wavelet family of order (i,j). The implemented values of k = 100*i + j are 103, 105, 202, 204,
129 /// 206, 208, 301, 303, 305 307, 309.
130 #[doc(alias = "gsl_wavelet_bspline")]
131 pub fn bspline() -> WaveletType {
132 ffi_wrap!(gsl_wavelet_bspline)
133 }
134
135 /// This is the biorthogonal B-spline wavelet family of order (i,j). The implemented values of k = 100*i + j are 103, 105, 202, 204,
136 /// 206, 208, 301, 303, 305 307, 309.
137 #[doc(alias = "gsl_wavelet_bspline_centered")]
138 pub fn bspline_centered() -> WaveletType {
139 ffi_wrap!(gsl_wavelet_bspline_centered)
140 }
141}
142
143ffi_wrapper!(WaveletWorkspace, *mut sys::gsl_wavelet_workspace, gsl_wavelet_workspace_free,
144"The WaveletWorkspace structure contains scratch space of the same size as the input data and is
145used to hold intermediate results during the transform.");
146
147impl WaveletWorkspace {
148 /// This function allocates a workspace for the discrete wavelet transform. To perform a one-dimensional transform on n elements, a
149 /// workspace of size n must be provided. For two-dimensional transforms of n-by-n matrices it is sufficient to allocate a workspace
150 /// of size n, since the transform operates on individual rows and columns. A null pointer is returned if insufficient memory is
151 /// available.
152 #[doc(alias = "gsl_wavelet_workspace_alloc")]
153 pub fn new(n: usize) -> Option<WaveletWorkspace> {
154 let tmp = unsafe { sys::gsl_wavelet_workspace_alloc(n) };
155
156 if tmp.is_null() {
157 None
158 } else {
159 Some(WaveletWorkspace::wrap(tmp))
160 }
161 }
162}