Wavelets in sage

There are no native wavelet packages in sage. But there is a great module in python that contains, among other things, forward and inverse discrete wavelet transforms (for one and two dimensions). It comes bundled with seventy-six wavelet filters, and allows support to build your own! The name is PyWavelets, written by Tariq Rashid, and can be retrieved from pypi.python.org/pypi/PyWavelets. In order to install it in sage, take the following steps:

  1. Download the latest version, and unpack it.
    tar xvfz PyWavelets-0.2.0.tar.bz2
  2. Navigate to the top-level directory of the newly created PyWavelets folder, and install the package in sage.
    /Applications/sage/sage -python setup.py install
  3. This should be enough! Open a sage session, and import the pywt module:
    import pywt
    dir(pywt)

    [‘BaseNode’, ‘MODES’, ‘Node’, ‘Node2D’, ‘Wavelet’, ‘WaveletPacket’, ‘Wavelet
    Packet2D’, ‘__all__’, ‘__author__’, ‘__builtins__’, ‘__doc__’, ‘__file__’,
    ‘__license__’, ‘__name__’, ‘__package__’, ‘__path__’, ‘__version__’, ‘_pywt’
    , ‘centfrq’, ‘downcoef’, ‘dwt’, ‘dwt2’, ‘dwt_coeff_len’, ‘dwt_max_level’, ‘d
    wtn’, ‘families’, ‘functions’, ‘idwt’, ‘idwt2’, ‘intwave’, ‘numerix’, ‘orthf
    ilt’, ‘qmf’, ‘release_details’, ‘scal2frq’, ‘swt’, ‘swt2’, ‘swt_max_level’,
    ‘thresholding’, ‘upcoef’, ‘wavedec’, ‘wavedec2’, ‘wavelist’, ‘waverec’, ‘wav
    erec2’]

  4. It might happen that the installation is not very clean:
    import pywt

    —————————————————————————
    ImportError Traceback (most recent call last)

    /Users/blanco/Documents/Research/PyWavelets-0.2.0/ in ()

    /Users/blanco/Documents/Research/PyWavelets-0.2.0/pywt/__init__.py in ()
    11 “””
    12
    —> 13 from _pywt import *
    14 from multilevel import *
    15 from multidim import *

    ImportError: No module named _pywt

    In that event, simply find where the _pywt.so library is in your PyWavelet package, and place a link in your sage libraries:

    sudo ln -sv ./PyWavelets-0.2.0/build/lib.macosx-10.6-i386-2.6/pywt/_pywt.so
    /Applications/sage/local/lib/.

Once installed, you can get a flavor of what it means to work with this module by exploring a little. For example, to find out how many different families of wavelets are implemented, or the names of each of the filters present in the package, simply issue

pywt.families()

[‘haar’, ‘db’, ‘sym’, ‘coif’, ‘bior’, ‘rbio’, ‘dmey’]

pywt.wavelist()

[‘bior1.1’, ‘bior1.3’, ‘bior1.5’, ‘bior2.2’, ‘bior2.4’, ‘bior2.6’, ‘bior2.8’, ‘
bior3.1’, ‘bior3.3’, ‘bior3.5’, ‘bior3.7’, ‘bior3.9’, ‘bior4.4’, ‘bior5.5’, ‘bi
or6.8’, ‘coif1’, ‘coif2’, ‘coif3’, ‘coif4’, ‘coif5’, ‘db1’, ‘db2’, ‘db3’, ‘db4’
, ‘db5’, ‘db6’, ‘db7’, ‘db8’, ‘db9’, ‘db10’, ‘db11’, ‘db12’, ‘db13’, ‘db14’, ‘d
b15’, ‘db16’, ‘db17’, ‘db18’, ‘db19’, ‘db20’, ‘dmey’, ‘haar’, ‘rbio1.1’, ‘rbio1
.3’, ‘rbio1.5’, ‘rbio2.2’, ‘rbio2.4’, ‘rbio2.6’, ‘rbio2.8’, ‘rbio3.1’, ‘rbio3.3
‘, ‘rbio3.5’, ‘rbio3.7’, ‘rbio3.9’, ‘rbio4.4’, ‘rbio5.5’, ‘rbio6.8’, ‘sym2’, ‘s
ym3’, ‘sym4’, ‘sym5’, ‘sym6’, ‘sym7’, ‘sym8’, ‘sym9’, ‘sym10’, ‘sym11’, ‘sym12’
, ‘sym13’, ‘sym14’, ‘sym15’, ‘sym16’, ‘sym17’, ‘sym18’, ‘sym19’, ‘sym20’]

To compute one level of the Haar-wavelet coefficients of a given image (the top level alone), we proceed as follows:

from numpy import *
import scipy

image=scipy.misc.lena().astype(float32)

Aprox, Details = pywt.dwt2( image, pywt.Wavelet('haar') )
HorizDetail, VertDetail, DiagDetail = Details
Approximation Horizontal Detail
Vertical Detail Diagonal Detail

References

Programming Python (See all Computer Programmer Books) Ten Lectures on Wavelets (CBMS-NSF Regional Conference Series in Applied Mathematics) (See all Waves & Wave Mechanics Books)
  1. No comments yet.
  1. January 23, 2011 at 12:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: