File: modes.rst

package info (click to toggle)
pywavelets 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,488 kB
  • ctags: 2,909
  • sloc: ansic: 3,616; python: 2,782; makefile: 87
file content (122 lines) | stat: -rw-r--r-- 4,349 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
.. _reg-modes:

.. currentmodule:: pywt


Signal Extension Modes
======================

Import :mod:`pywt` first

    >>> import pywt

    >>> def format_array(a):
    ...     """Consistent array representation across different systems"""
    ...     import numpy
    ...     a = numpy.where(numpy.abs(a) < 1e-5, 0, a)
    ...     return numpy.array2string(a, precision=5, separator=' ', suppress_small=True)

List of available signal extension :ref:`modes <MODES>`:

    >>> print pywt.MODES.modes
    ['zpd', 'cpd', 'sym', 'ppd', 'sp1', 'per']


Test that :func:`dwt` and :func:`idwt` can be performed using every mode:

    >>> x = [1,2,1,5,-1,8,4,6]
    >>> for mode in pywt.MODES.modes:
    ...     cA, cD = pywt.dwt(x, 'db2', mode)
    ...     print "Mode:", mode
    ...     print "cA:", format_array(cA)
    ...     print "cD:", format_array(cD)
    ...     print "Reconstruction:", pywt.idwt(cA, cD, 'db2', mode)
    Mode: zpd
    cA: [-0.03468  1.73309  3.40612  6.32929  6.95095]
    cD: [-0.12941 -2.156   -5.95035 -1.21545 -1.8625 ]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: cpd
    cA: [ 1.2848   1.73309  3.40612  6.32929  7.51936]
    cD: [-0.48296 -2.156   -5.95035 -1.21545  0.25882]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: sym
    cA: [ 1.76777  1.73309  3.40612  6.32929  7.77817]
    cD: [-0.61237 -2.156   -5.95035 -1.21545  1.22474]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: ppd
    cA: [ 6.91627  1.73309  3.40612  6.32929  6.91627]
    cD: [-1.99191 -2.156   -5.95035 -1.21545 -1.99191]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: sp1
    cA: [-0.51764  1.73309  3.40612  6.32929  7.45001]
    cD: [ 0.      -2.156   -5.95035 -1.21545  0.     ]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: per
    cA: [ 4.05317  3.05257  2.85381  8.42522]
    cD: [ 0.18947  4.18258  4.33738  2.60428]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]


Invalid mode name should rise a :exc:`ValueError`:

    >>> pywt.dwt([1,2,3,4], 'db2', 'invalid')
    Traceback (most recent call last):
    ...
    ValueError: Unknown mode name 'invalid'.


You can also refer to modes via :ref:`MODES <MODES>` class attributes:

    >>> for mode_name in ['zpd', 'cpd', 'sym', 'ppd', 'sp1', 'per']:
    ...     mode = getattr(pywt.MODES, mode_name)
    ...     cA, cD = pywt.dwt([1,2,1,5,-1,8,4,6], 'db2', mode)
    ...     print "Mode:", mode, "(%s)" % mode_name
    ...     print "cA:", format_array(cA)
    ...     print "cD:", format_array(cD)
    ...     print "Reconstruction:", pywt.idwt(cA, cD, 'db2', mode)
    Mode: 0 (zpd)
    cA: [-0.03468  1.73309  3.40612  6.32929  6.95095]
    cD: [-0.12941 -2.156   -5.95035 -1.21545 -1.8625 ]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: 2 (cpd)
    cA: [ 1.2848   1.73309  3.40612  6.32929  7.51936]
    cD: [-0.48296 -2.156   -5.95035 -1.21545  0.25882]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: 1 (sym)
    cA: [ 1.76777  1.73309  3.40612  6.32929  7.77817]
    cD: [-0.61237 -2.156   -5.95035 -1.21545  1.22474]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: 4 (ppd)
    cA: [ 6.91627  1.73309  3.40612  6.32929  6.91627]
    cD: [-1.99191 -2.156   -5.95035 -1.21545 -1.99191]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: 3 (sp1)
    cA: [-0.51764  1.73309  3.40612  6.32929  7.45001]
    cD: [ 0.      -2.156   -5.95035 -1.21545  0.     ]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]
    Mode: 5 (per)
    cA: [ 4.05317  3.05257  2.85381  8.42522]
    cD: [ 0.18947  4.18258  4.33738  2.60428]
    Reconstruction: [ 1.  2.  1.  5. -1.  8.  4.  6.]


The default mode is :ref:`sym <MODES.sym>`:

    >>> cA, cD = pywt.dwt(x, 'db2')
    >>> print cA
    [ 1.76776695  1.73309178  3.40612438  6.32928585  7.77817459]
    >>> print cD
    [-0.61237244 -2.15599552 -5.95034847 -1.21545369  1.22474487]
    >>> print pywt.idwt(cA, cD, 'db2')
    [ 1.  2.  1.  5. -1.  8.  4.  6.]


And using a keyword argument:

    >>> cA, cD = pywt.dwt(x, 'db2', mode='sym')
    >>> print cA
    [ 1.76776695  1.73309178  3.40612438  6.32928585  7.77817459]
    >>> print cD
    [-0.61237244 -2.15599552 -5.95034847 -1.21545369  1.22474487]
    >>> print pywt.idwt(cA, cD, 'db2')
    [ 1.  2.  1.  5. -1.  8.  4.  6.]