Blame doc/dwt.rst

Packit 67cb25
.. index::
Packit 67cb25
   single: DWT, see wavelet transforms
Packit 67cb25
   single: wavelet transforms
Packit 67cb25
   single: transforms, wavelet
Packit 67cb25
Packit 67cb25
******************
Packit 67cb25
Wavelet Transforms
Packit 67cb25
******************
Packit 67cb25
Packit 67cb25
This chapter describes functions for performing Discrete Wavelet
Packit 67cb25
Transforms (DWTs).  The library includes wavelets for real data in both
Packit 67cb25
one and two dimensions.  The wavelet functions are declared in the header
Packit 67cb25
files :file:`gsl_wavelet.h` and :file:`gsl_wavelet2d.h`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: DWT, mathematical definition
Packit 67cb25
Packit 67cb25
Definitions
Packit 67cb25
===========
Packit 67cb25
Packit 67cb25
The continuous wavelet transform and its inverse are defined by
Packit 67cb25
the relations,
Packit 67cb25
Packit 67cb25
.. math:: w(s, \tau) = \int_{-\infty}^\infty f(t) * \psi^*_{s,\tau}(t) dt
Packit 67cb25
Packit 67cb25
and,
Packit 67cb25
Packit 67cb25
.. math:: f(t) = \int_0^\infty ds \int_{-\infty}^\infty w(s, \tau) * \psi_{s,\tau}(t) d\tau
Packit 67cb25
Packit 67cb25
where the basis functions :math:`\psi_{s,\tau}`
Packit 67cb25
are obtained by scaling
Packit 67cb25
and translation from a single function, referred to as the {mother
Packit 67cb25
wavelet*.
Packit 67cb25
Packit 67cb25
The discrete version of the wavelet transform acts on equally-spaced
Packit 67cb25
samples, with fixed scaling and translation steps (:math:`s`,
Packit 67cb25
:math:`\tau`).  The frequency and time axes are sampled *dyadically*
Packit 67cb25
on scales of :math:`2^j` through a level parameter :math:`j`.
Packit 67cb25
Packit 67cb25
..  The wavelet :math:`\psi`
Packit 67cb25
..  can be expressed in terms of a scaling function :math:`\varphi`,
Packit 67cb25
..
Packit 67cb25
..  @tex
Packit 67cb25
..  \beforedisplay
Packit 67cb25
..  $$
Packit 67cb25
..  \psi(2^{j-1},t) = \sum_{k=0}^{2^j-1} g_j(k) * \bar{\varphi}(2^j t-k)
Packit 67cb25
..  $$
Packit 67cb25
..  \afterdisplay
Packit 67cb25
..  @end tex
Packit 67cb25
..  @ifinfo
Packit 67cb25
..  @example
Packit 67cb25
..  \psi(2^@{j-1@},t) = \sum_@{k=0@}^@{2^j-1@} g_j(k) * \bar@{\varphi@}(2^j t-k)
Packit 67cb25
..  @end example
Packit 67cb25
..  @end ifinfo
Packit 67cb25
..  @noindent
Packit 67cb25
..  and
Packit 67cb25
..
Packit 67cb25
..  @tex
Packit 67cb25
..  \beforedisplay
Packit 67cb25
..  $$
Packit 67cb25
..  \varphi(2^{j-1},t) = \sum_{k=0}^{2^j-1} h_j(k) * \bar{\varphi}(2^j t-k)
Packit 67cb25
..  $$
Packit 67cb25
..  \afterdisplay
Packit 67cb25
..  @end tex
Packit 67cb25
..  @ifinfo
Packit 67cb25
..  @example
Packit 67cb25
..  \varphi(2^@{j-1@},t) = \sum_@{k=0@}^@{2^j-1@} h_j(k) * \bar@{\varphi@}(2^j t-k)
Packit 67cb25
..  @end example
Packit 67cb25
..  @end ifinfo
Packit 67cb25
..  @noindent
Packit 67cb25
..  The functions :math:`\psi` and :math:`\varphi` are related through the
Packit 67cb25
..  coefficients
Packit 67cb25
..  @c{$g_{n} = (-1)^n h_{L-1-n}$}
Packit 67cb25
..  @math{g_@{n@} = (-1)^n h_@{L-1-n@}}
Packit 67cb25
..  for @c{$n=0 \dots L-1$}
Packit 67cb25
..  @math{n=0 ... L-1},
Packit 67cb25
..  where :math:`L` is the total number of coefficients.  The two sets of
Packit 67cb25
..  coefficients :math:`h_j` and :math:`g_i` define the scaling function and
Packit 67cb25
.. the wavelet.  
Packit 67cb25
Packit 67cb25
The resulting family of functions :math:`\{\psi_{j,n}\}`
Packit 67cb25
constitutes an orthonormal basis for square-integrable signals.  
Packit 67cb25
The discrete wavelet transform is an :math:`O(N)` algorithm, and is also
Packit 67cb25
referred to as the *fast wavelet transform*.
Packit 67cb25
Packit 67cb25
.. index:: DWT initialization
Packit 67cb25
Packit 67cb25
Initialization
Packit 67cb25
==============
Packit 67cb25
Packit 67cb25
.. type:: gsl_wavelet
Packit 67cb25
Packit 67cb25
   This structure contains the filter coefficients
Packit 67cb25
   defining the wavelet and any associated offset parameters.
Packit 67cb25
Packit 67cb25
.. function:: gsl_wavelet * gsl_wavelet_alloc (const gsl_wavelet_type * T, size_t k)
Packit 67cb25
Packit 67cb25
   This function allocates and initializes a wavelet object of type
Packit 67cb25
   :data:`T`.  The parameter :data:`k` selects the specific member of the
Packit 67cb25
   wavelet family.  A null pointer is returned if insufficient memory is
Packit 67cb25
   available or if a unsupported member is selected.
Packit 67cb25
Packit 67cb25
The following wavelet types are implemented:
Packit 67cb25
Packit 67cb25
.. type:: gsl_wavelet_type
Packit 67cb25
Packit 67cb25
   .. index::
Packit 67cb25
      single: Daubechies wavelets
Packit 67cb25
      single: maximal phase, Daubechies wavelets
Packit 67cb25
Packit 67cb25
   .. var:: gsl_wavelet_daubechies
Packit 67cb25
            gsl_wavelet_daubechies_centered
Packit 67cb25
Packit 67cb25
      This is the Daubechies wavelet family of maximum phase with :math:`k/2`
Packit 67cb25
      vanishing moments.  The implemented wavelets are 
Packit 67cb25
      :math:`k=4, 6, \dots, 20`, with :data:`k` even.
Packit 67cb25
Packit 67cb25
   .. index:: Haar wavelets
Packit 67cb25
Packit 67cb25
   .. var:: gsl_wavelet_haar
Packit 67cb25
            gsl_wavelet_haar_centered
Packit 67cb25
Packit 67cb25
      This is the Haar wavelet.  The only valid choice of :math:`k` for the
Packit 67cb25
      Haar wavelet is :math:`k=2`.
Packit 67cb25
Packit 67cb25
   .. index::
Packit 67cb25
      single: biorthogonal wavelets
Packit 67cb25
      single: B-spline wavelets
Packit 67cb25
Packit 67cb25
   .. var:: gsl_wavelet_bspline
Packit 67cb25
            gsl_wavelet_bspline_centered
Packit 67cb25
Packit 67cb25
      This is the biorthogonal B-spline wavelet family of order :math:`(i,j)`.  
Packit 67cb25
      The implemented values of :math:`k = 100*i + j` are 103, 105, 202, 204,
Packit 67cb25
      206, 208, 301, 303, 305 307, 309.
Packit 67cb25
Packit 67cb25
The centered forms of the wavelets align the coefficients of the various
Packit 67cb25
sub-bands on edges.  Thus the resulting visualization of the
Packit 67cb25
coefficients of the wavelet transform in the phase plane is easier to
Packit 67cb25
understand.
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_wavelet_name (const gsl_wavelet * w)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to the name of the wavelet family for
Packit 67cb25
   :data:`w`.
Packit 67cb25
Packit 67cb25
..  @deftypefun {void} gsl_wavelet_print (const gsl_wavelet * w)
Packit 67cb25
..  This function prints the filter coefficients (@code{**h1}, @code{**g1}, @code{**h2}, @code{**g2}) of the wavelet object :data:`w`.
Packit 67cb25
..  @end deftypefun
Packit 67cb25
Packit 67cb25
.. function:: void gsl_wavelet_free (gsl_wavelet * w)
Packit 67cb25
Packit 67cb25
   This function frees the wavelet object :data:`w`.
Packit 67cb25
Packit 67cb25
.. type:: gsl_wavelet_workspace
Packit 67cb25
Packit 67cb25
   This structure contains scratch space of the
Packit 67cb25
   same size as the input data and is used to hold intermediate results
Packit 67cb25
   during the transform.
Packit 67cb25
Packit 67cb25
.. function:: gsl_wavelet_workspace * gsl_wavelet_workspace_alloc (size_t n)
Packit 67cb25
Packit 67cb25
   This function allocates a workspace for the discrete wavelet transform.
Packit 67cb25
   To perform a one-dimensional transform on :data:`n` elements, a workspace
Packit 67cb25
   of size :data:`n` must be provided.  For two-dimensional transforms of
Packit 67cb25
   :data:`n`-by-:data:`n` matrices it is sufficient to allocate a workspace of
Packit 67cb25
   size :data:`n`, since the transform operates on individual rows and
Packit 67cb25
   columns. A null pointer is returned if insufficient memory is available.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_wavelet_workspace_free (gsl_wavelet_workspace * work)
Packit 67cb25
Packit 67cb25
   This function frees the allocated workspace :data:`work`.
Packit 67cb25
Packit 67cb25
Transform Functions
Packit 67cb25
===================
Packit 67cb25
Packit 67cb25
This sections describes the actual functions performing the discrete
Packit 67cb25
wavelet transform.  Note that the transforms use periodic boundary
Packit 67cb25
conditions.  If the signal is not periodic in the sample length then
Packit 67cb25
spurious coefficients will appear at the beginning and end of each level
Packit 67cb25
of the transform.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: DWT, one dimensional
Packit 67cb25
Packit 67cb25
Wavelet transforms in one dimension
Packit 67cb25
-----------------------------------
Packit 67cb25
Packit 67cb25
.. function:: int gsl_wavelet_transform (const gsl_wavelet * w, double * data, size_t stride, size_t n, gsl_wavelet_direction dir, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet_transform_forward (const gsl_wavelet * w, double * data, size_t stride, size_t n, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet_transform_inverse (const gsl_wavelet * w, double * data, size_t stride, size_t n, gsl_wavelet_workspace * work)
Packit 67cb25
Packit 67cb25
   These functions compute in-place forward and inverse discrete wavelet
Packit 67cb25
   transforms of length :data:`n` with stride :data:`stride` on the array
Packit 67cb25
   :data:`data`. The length of the transform :data:`n` is restricted to powers
Packit 67cb25
   of two.  For the :code:`transform` version of the function the argument
Packit 67cb25
   :data:`dir` can be either :code:`forward` (:math:`+1`) or :code:`backward`
Packit 67cb25
   (:math:`-1`).  A workspace :data:`work` of length :data:`n` must be provided.
Packit 67cb25
Packit 67cb25
   For the forward transform, the elements of the original array are 
Packit 67cb25
   replaced by the discrete wavelet
Packit 67cb25
   transform :math:`f_i \rightarrow w_{j,k}`
Packit 67cb25
   in a packed triangular storage layout, 
Packit 67cb25
   where :data:`j` is the index of the level 
Packit 67cb25
   :math:`j = 0 \dots J-1`
Packit 67cb25
   and
Packit 67cb25
   :data:`k` is the index of the coefficient within each level,
Packit 67cb25
   :math:`k = 0 \dots 2^j - 1`.
Packit 67cb25
   The total number of levels is :math:`J = \log_2(n)`.  The output data
Packit 67cb25
   has the following form,
Packit 67cb25
Packit 67cb25
   .. math:: (s_{-1,0}, d_{0,0}, d_{1,0}, d_{1,1}, d_{2,0},\cdots, d_{j,k},\cdots, d_{J-1,2^{J-1} - 1}) 
Packit 67cb25
Packit 67cb25
   where the first element is the smoothing coefficient :math:`s_{-1,0}`,
Packit 67cb25
   followed by the detail coefficients :math:`d_{j,k}`
Packit 67cb25
   for each level
Packit 67cb25
   :math:`j`.  The backward transform inverts these coefficients to obtain 
Packit 67cb25
   the original data.
Packit 67cb25
Packit 67cb25
   These functions return a status of :macro:`GSL_SUCCESS` upon successful
Packit 67cb25
   completion.  :macro:`GSL_EINVAL` is returned if :data:`n` is not an integer
Packit 67cb25
   power of 2 or if insufficient workspace is provided.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: DWT, two dimensional
Packit 67cb25
Packit 67cb25
Wavelet transforms in two dimension
Packit 67cb25
-----------------------------------
Packit 67cb25
Packit 67cb25
The library provides functions to perform two-dimensional discrete
Packit 67cb25
wavelet transforms on square matrices.  The matrix dimensions must be an
Packit 67cb25
integer power of two.  There are two possible orderings of the rows and
Packit 67cb25
columns in the two-dimensional wavelet transform, referred to as the
Packit 67cb25
"standard" and "non-standard" forms.
Packit 67cb25
Packit 67cb25
The "standard" transform performs a complete discrete wavelet
Packit 67cb25
transform on the rows of the matrix, followed by a separate complete
Packit 67cb25
discrete wavelet transform on the columns of the resulting
Packit 67cb25
row-transformed matrix.  This procedure uses the same ordering as a
Packit 67cb25
two-dimensional Fourier transform.
Packit 67cb25
Packit 67cb25
The "non-standard" transform is performed in interleaved passes on the
Packit 67cb25
rows and columns of the matrix for each level of the transform.  The
Packit 67cb25
first level of the transform is applied to the matrix rows, and then to
Packit 67cb25
the matrix columns.  This procedure is then repeated across the rows and
Packit 67cb25
columns of the data for the subsequent levels of the transform, until
Packit 67cb25
the full discrete wavelet transform is complete.  The non-standard form
Packit 67cb25
of the discrete wavelet transform is typically used in image analysis.
Packit 67cb25
Packit 67cb25
The functions described in this section are declared in the header file
Packit 67cb25
:file:`gsl_wavelet2d.h`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_wavelet2d_transform (const gsl_wavelet * w, double * data, size_t tda, size_t size1, size_t size2, gsl_wavelet_direction dir, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_transform_forward (const gsl_wavelet * w, double * data, size_t tda, size_t size1, size_t size2, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_transform_inverse (const gsl_wavelet * w, double * data, size_t tda, size_t size1, size_t size2, gsl_wavelet_workspace * work)
Packit 67cb25
Packit 67cb25
   These functions compute two-dimensional in-place forward and inverse
Packit 67cb25
   discrete wavelet transforms in standard form on the
Packit 67cb25
   array :data:`data` stored in row-major form with dimensions :data:`size1`
Packit 67cb25
   and :data:`size2` and physical row length :data:`tda`.  The dimensions must
Packit 67cb25
   be equal (square matrix) and are restricted to powers of two.  For the
Packit 67cb25
   :code:`transform` version of the function the argument :data:`dir` can be
Packit 67cb25
   either :code:`forward` (:math:`+1`) or :code:`backward` (:math:`-1`).  A
Packit 67cb25
   workspace :data:`work` of the appropriate size must be provided.  On exit,
Packit 67cb25
   the appropriate elements of the array :data:`data` are replaced by their
Packit 67cb25
   two-dimensional wavelet transform.
Packit 67cb25
Packit 67cb25
   The functions return a status of :macro:`GSL_SUCCESS` upon successful
Packit 67cb25
   completion.  :macro:`GSL_EINVAL` is returned if :data:`size1` and
Packit 67cb25
   :data:`size2` are not equal and integer powers of 2, or if insufficient
Packit 67cb25
   workspace is provided.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_wavelet2d_transform_matrix (const gsl_wavelet * w, gsl_matrix * m, gsl_wavelet_direction dir, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_transform_matrix_forward (const gsl_wavelet * w, gsl_matrix * m, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_transform_matrix_inverse (const gsl_wavelet * w, gsl_matrix * m, gsl_wavelet_workspace * work)
Packit 67cb25
Packit 67cb25
   These functions compute the two-dimensional in-place wavelet transform
Packit 67cb25
   on a matrix :data:`m`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_wavelet2d_nstransform (const gsl_wavelet * w, double * data, size_t tda, size_t size1, size_t size2, gsl_wavelet_direction dir, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_nstransform_forward (const gsl_wavelet * w, double * data, size_t tda, size_t size1, size_t size2, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_nstransform_inverse (const gsl_wavelet * w, double * data, size_t tda, size_t size1, size_t size2, gsl_wavelet_workspace * work)
Packit 67cb25
Packit 67cb25
   These functions compute the two-dimensional wavelet transform in
Packit 67cb25
   non-standard form.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_wavelet2d_nstransform_matrix (const gsl_wavelet * w, gsl_matrix * m, gsl_wavelet_direction dir, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_nstransform_matrix_forward (const gsl_wavelet * w, gsl_matrix * m, gsl_wavelet_workspace * work)
Packit 67cb25
              int gsl_wavelet2d_nstransform_matrix_inverse (const gsl_wavelet * w, gsl_matrix * m, gsl_wavelet_workspace * work)
Packit 67cb25
Packit 67cb25
   These functions compute the non-standard form of the two-dimensional
Packit 67cb25
   in-place wavelet transform on a matrix :data:`m`.
Packit 67cb25
Packit 67cb25
Examples
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The following program demonstrates the use of the one-dimensional
Packit 67cb25
wavelet transform functions.  It computes an approximation to an input
Packit 67cb25
signal (of length 256) using the 20 largest components of the wavelet
Packit 67cb25
transform, while setting the others to zero.
Packit 67cb25
Packit 67cb25
.. include:: examples/dwt.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The output can be used with the GNU plotutils :code:`graph` program::
Packit 67cb25
Packit 67cb25
  $ ./a.out ecg.dat > dwt.txt
Packit 67cb25
  $ graph -T ps -x 0 256 32 -h 0.3 -a dwt.txt > dwt.ps
Packit 67cb25
Packit 67cb25
:numref:`fig_dwt` shows an original and compressed version of a sample ECG
Packit 67cb25
recording from the MIT-BIH Arrhythmia Database, part of the PhysioNet
Packit 67cb25
archive of public-domain of medical datasets.
Packit 67cb25
Packit 67cb25
.. _fig_dwt:
Packit 67cb25
Packit 67cb25
.. figure:: /images/dwt.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Original (upper) and wavelet-compressed (lower) ECG signals, using the
Packit 67cb25
   20 largest components of the Daubechies(4) discrete wavelet transform.
Packit 67cb25
Packit 67cb25
References and Further Reading
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
The mathematical background to wavelet transforms is covered in the
Packit 67cb25
original lectures by Daubechies,
Packit 67cb25
Packit 67cb25
* Ingrid Daubechies.
Packit 67cb25
  Ten Lectures on Wavelets.
Packit 67cb25
  *CBMS-NSF Regional Conference Series in Applied Mathematics* (1992), 
Packit 67cb25
  SIAM, ISBN 0898712742.
Packit 67cb25
Packit 67cb25
An easy to read introduction to the subject with an emphasis on the
Packit 67cb25
application of the wavelet transform in various branches of science is,
Packit 67cb25
Packit 67cb25
* Paul S. Addison. *The Illustrated Wavelet Transform Handbook*.
Packit 67cb25
  Institute of Physics Publishing (2002), ISBN 0750306920.
Packit 67cb25
Packit 67cb25
For extensive coverage of signal analysis by wavelets, wavelet packets
Packit 67cb25
and local cosine bases see,
Packit 67cb25
Packit 67cb25
* S. G. Mallat. *A wavelet tour of signal processing* (Second
Packit 67cb25
  edition). Academic Press (1999), ISBN 012466606X.
Packit 67cb25
Packit 67cb25
The concept of multiresolution analysis underlying the wavelet transform
Packit 67cb25
is described in,
Packit 67cb25
Packit 67cb25
* S. G. Mallat.
Packit 67cb25
  Multiresolution Approximations and Wavelet Orthonormal Bases of L^2(R).
Packit 67cb25
  *Transactions of the American Mathematical Society*, 315(1), 1989, 69--87.
Packit 67cb25
Packit 67cb25
* S. G. Mallat.
Packit 67cb25
  A Theory for Multiresolution Signal Decomposition---The Wavelet Representation.
Packit 67cb25
  *IEEE Transactions on Pattern Analysis and Machine Intelligence*, 11, 1989,
Packit 67cb25
  674--693. 
Packit 67cb25
Packit 67cb25
The coefficients for the individual wavelet families implemented by the
Packit 67cb25
library can be found in the following papers,
Packit 67cb25
Packit 67cb25
* I. Daubechies.
Packit 67cb25
  Orthonormal Bases of Compactly Supported Wavelets.
Packit 67cb25
  *Communications on Pure and Applied Mathematics*, 41 (1988) 909--996.
Packit 67cb25
Packit 67cb25
* A. Cohen, I. Daubechies, and J.-C. Feauveau.
Packit 67cb25
  Biorthogonal Bases of Compactly Supported Wavelets.
Packit 67cb25
  *Communications on Pure and Applied Mathematics*, 45 (1992)
Packit 67cb25
  485--560.
Packit 67cb25
Packit 67cb25
The PhysioNet archive of physiological datasets can be found online at
Packit 67cb25
http://www.physionet.org/ and is described in the following
Packit 67cb25
paper,
Packit 67cb25
Packit 67cb25
* Goldberger et al.  
Packit 67cb25
  PhysioBank, PhysioToolkit, and PhysioNet: Components
Packit 67cb25
  of a New Research Resource for Complex Physiologic
Packit 67cb25
  Signals. 
Packit 67cb25
  *Circulation* 101(23):e215-e220 2000.