-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests.py
144 lines (94 loc) · 3.34 KB
/
tests.py
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
136
137
138
139
140
141
142
143
144
import tfwavelets as tfw
import numpy as np
def check_orthonormality_1d(wavelet, tol=1e-5, N=8):
matrix = np.zeros((N, N))
for i in range(N):
unit = np.zeros(N)
unit[i] = 1
matrix[:, i] = tfw.wrappers.dwt1d(unit, wavelet)
error1 = np.mean(np.abs(matrix.T @ matrix - np.eye(N)))
error2 = np.mean(np.abs(matrix @ matrix.T - np.eye(N)))
assert error1 < tol, "Mean error: %g" % error1
assert error2 < tol, "Mean error: %g" % error2
def check_linearity_1d(wavelet, tol=1e-5, N=256):
x1 = np.random.random(N)
x2 = np.random.random(N)
c1 = np.random.random(1)
c2 = np.random.random(1)
test1 = tfw.wrappers.dwt1d(c1 * x1 + c2 * x2)
test2 = c1 * tfw.wrappers.dwt1d(x1) + c2 * tfw.wrappers.dwt1d(x2)
error = np.mean(np.abs(test1 - test2))
assert error < tol, "Mean error: %g" % error
def check_linearity_2d(wavelet, tol=1e-5, N=256):
x1 = np.random.random((N, N))
x2 = np.random.random((N, N))
c1 = np.random.random(1)
c2 = np.random.random(1)
test1 = tfw.wrappers.dwt2d(c1 * x1 + c2 * x2)
test2 = c1 * tfw.wrappers.dwt2d(x1) + c2 * tfw.wrappers.dwt2d(x2)
error = np.mean(np.abs(test1 - test2))
assert error < tol, "Mean error: %g" % error
def check_inverse_1d(wavelet, levels=1, tol=1e-4, N=256):
signal = np.random.random(N)
reconstructed = tfw.wrappers.idwt1d(
tfw.wrappers.dwt1d(signal, levels=levels),
levels=levels
)
error = np.mean(np.abs(signal - reconstructed))
assert error < tol, "Mean error: %g" % error
def check_inverse_2d(wavelet, levels=1, tol=1e-4, N=256):
signal = np.random.random((N, N))
reconstructed = tfw.wrappers.idwt2d(
tfw.wrappers.dwt2d(signal, levels=levels),
levels=levels
)
error = np.mean(np.abs(signal - reconstructed))
assert error < tol, "Mean error: %g" % error
def test_ortho_haar():
check_orthonormality_1d("haar")
def test_linear_haar_1d():
check_linearity_1d("haar")
def test_linear_haar_2d():
check_linearity_2d("haar")
def test_inverse_haar_1d():
check_inverse_1d("haar", levels=1)
def test_inverse_haar_1d_level2():
check_inverse_1d("haar", levels=2)
def test_inverse_haar_2d():
check_inverse_2d("haar", levels=2)
def test_ortho_db2():
check_orthonormality_1d("db2")
def test_linear_db2_2d():
check_linearity_2d("db2")
def test_linear_db2_1d():
check_linearity_1d("db2")
def test_inverse_db2_1d():
check_inverse_1d("db2", levels=1)
def test_inverse_db2_1d_level2():
check_inverse_1d("db2", levels=2)
def test_inverse_db2_2d():
check_inverse_2d("db2", levels=2)
def test_ortho_db3():
check_orthonormality_1d("db3")
def test_linear_db3_2d():
check_linearity_2d("db3")
def test_linear_db3_1d():
check_linearity_1d("db3")
def test_inverse_db3_1d():
check_inverse_1d("db3", levels=1)
def test_inverse_db3_1d_level2():
check_inverse_1d("db3", levels=2)
def test_inverse_db3_2d():
check_inverse_2d("db3", levels=2)
def test_ortho_db4():
check_orthonormality_1d("db4")
def test_linear_db4_2d():
check_linearity_2d("db4")
def test_linear_db4_1d():
check_linearity_1d("db4")
def test_inverse_db4_1d():
check_inverse_1d("db4", levels=1)
def test_inverse_db4_1d_level2():
check_inverse_1d("db4", levels=2)
def test_inverse_db4_2d():
check_inverse_2d("db4", levels=2)