File: test_wavelets.py

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (135 lines) | stat: -rw-r--r-- 5,273 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
134
135
from __future__ import division, print_function, absolute_import

import numpy as np
from numpy.testing import TestCase, run_module_suite, assert_equal, \
    assert_array_equal, assert_array_almost_equal, assert_array_less, assert_
from scipy._lib.six import xrange

from scipy.signal import wavelets


class TestWavelets(TestCase):
    def test_qmf(self):
        assert_array_equal(wavelets.qmf([1, 1]), [1, -1])

    def test_daub(self):
        for i in xrange(1, 15):
            assert_equal(len(wavelets.daub(i)), i * 2)

    def test_cascade(self):
        for J in xrange(1, 7):
            for i in xrange(1, 5):
                lpcoef = wavelets.daub(i)
                k = len(lpcoef)
                x, phi, psi = wavelets.cascade(lpcoef, J)
                assert_(len(x) == len(phi) == len(psi))
                assert_equal(len(x), (k - 1) * 2 ** J)

    def test_morlet(self):
        x = wavelets.morlet(50, 4.1, complete=True)
        y = wavelets.morlet(50, 4.1, complete=False)
        # Test if complete and incomplete wavelet have same lengths:
        assert_equal(len(x), len(y))
        # Test if complete wavelet is less than incomplete wavelet:
        assert_array_less(x, y)

        x = wavelets.morlet(10, 50, complete=False)
        y = wavelets.morlet(10, 50, complete=True)
        # For large widths complete and incomplete wavelets should be
        # identical within numerical precision:
        assert_equal(x, y)

        # miscellaneous tests:
        x = np.array([1.73752399e-09 + 9.84327394e-25j,
                      6.49471756e-01 + 0.00000000e+00j,
                      1.73752399e-09 - 9.84327394e-25j])
        y = wavelets.morlet(3, w=2, complete=True)
        assert_array_almost_equal(x, y)

        x = np.array([2.00947715e-09 + 9.84327394e-25j,
                      7.51125544e-01 + 0.00000000e+00j,
                      2.00947715e-09 - 9.84327394e-25j])
        y = wavelets.morlet(3, w=2, complete=False)
        assert_array_almost_equal(x, y, decimal=2)

        x = wavelets.morlet(10000, s=4, complete=True)
        y = wavelets.morlet(20000, s=8, complete=True)[5000:15000]
        assert_array_almost_equal(x, y, decimal=2)

        x = wavelets.morlet(10000, s=4, complete=False)
        assert_array_almost_equal(y, x, decimal=2)
        y = wavelets.morlet(20000, s=8, complete=False)[5000:15000]
        assert_array_almost_equal(x, y, decimal=2)

        x = wavelets.morlet(10000, w=3, s=5, complete=True)
        y = wavelets.morlet(20000, w=3, s=10, complete=True)[5000:15000]
        assert_array_almost_equal(x, y, decimal=2)

        x = wavelets.morlet(10000, w=3, s=5, complete=False)
        assert_array_almost_equal(y, x, decimal=2)
        y = wavelets.morlet(20000, w=3, s=10, complete=False)[5000:15000]
        assert_array_almost_equal(x, y, decimal=2)

        x = wavelets.morlet(10000, w=7, s=10, complete=True)
        y = wavelets.morlet(20000, w=7, s=20, complete=True)[5000:15000]
        assert_array_almost_equal(x, y, decimal=2)

        x = wavelets.morlet(10000, w=7, s=10, complete=False)
        assert_array_almost_equal(x, y, decimal=2)
        y = wavelets.morlet(20000, w=7, s=20, complete=False)[5000:15000]
        assert_array_almost_equal(x, y, decimal=2)

    def test_ricker(self):
        w = wavelets.ricker(1.0, 1)
        expected = 2 / (np.sqrt(3 * 1.0) * (np.pi ** 0.25))
        assert_array_equal(w, expected)

        lengths = [5, 11, 15, 51, 101]
        for length in lengths:
            w = wavelets.ricker(length, 1.0)
            assert_(len(w) == length)
            max_loc = np.argmax(w)
            assert_(max_loc == (length // 2))

        points = 100
        w = wavelets.ricker(points, 2.0)
        half_vec = np.arange(0, points // 2)
        #Wavelet should be symmetric
        assert_array_almost_equal(w[half_vec], w[-(half_vec + 1)])

        #Check zeros
        aas = [5, 10, 15, 20, 30]
        points = 99
        for a in aas:
            w = wavelets.ricker(points, a)
            vec = np.arange(0, points) - (points - 1.0) / 2
            exp_zero1 = np.argmin(np.abs(vec - a))
            exp_zero2 = np.argmin(np.abs(vec + a))
            assert_array_almost_equal(w[exp_zero1], 0)
            assert_array_almost_equal(w[exp_zero2], 0)

    def test_cwt(self):
        widths = [1.0]
        delta_wavelet = lambda s, t: np.array([1])
        len_data = 100
        test_data = np.sin(np.pi * np.arange(0, len_data) / 10.0)

        #Test delta function input gives same data as output
        cwt_dat = wavelets.cwt(test_data, delta_wavelet, widths)
        assert_(cwt_dat.shape == (len(widths), len_data))
        assert_array_almost_equal(test_data, cwt_dat.flatten())

        #Check proper shape on output
        widths = [1, 3, 4, 5, 10]
        cwt_dat = wavelets.cwt(test_data, wavelets.ricker, widths)
        assert_(cwt_dat.shape == (len(widths), len_data))

        widths = [len_data * 10]
        #Note: this wavelet isn't defined quite right, but is fine for this test
        flat_wavelet = lambda l, w: np.ones(w) / w
        cwt_dat = wavelets.cwt(test_data, flat_wavelet, widths)
        assert_array_almost_equal(cwt_dat, np.mean(test_data))


if __name__ == "__main__":
    run_module_suite()