-
Notifications
You must be signed in to change notification settings - Fork 0
/
swt1d_test.go
82 lines (64 loc) · 1.33 KB
/
swt1d_test.go
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
package wavelib
import (
"fmt"
"math"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
// analog of https://github.com/rafat/wavelib/wiki/SWT-Example-Code
func ExampleSWT() {
rand.Seed(0)
obj := WaveInit("bior3.5")
defer obj.Free()
input := make([]float64, 256)
for idx := range input {
input[idx] = rand.Float64()
}
wt := WTInit(obj, "swt", 256, 1)
defer wt.Free()
SetWTConv(wt, "direct")
SWT(wt, input)
for _, value := range wt.Output() {
fmt.Printf("%g\n", value)
}
output := make([]float64, 256)
ISWT(wt, output)
diff := make([]float64, 256)
for i := 0; i < wt.SigLength(); i++ {
diff[i] = output[i] - input[i]
}
fmt.Printf("\n MAX %g \n", absMax(diff))
WTSummary(wt)
}
func absMax(s []float64) float64 {
max := float64(0)
for _, v := range s {
abs := math.Abs(v)
if abs > max {
max = abs
}
}
return max
}
func TestSWT1D(t *testing.T) {
rand.Seed(0)
obj := WaveInit("bior3.5")
defer obj.Free()
input := make([]float64, 256)
for idx := range input {
input[idx] = rand.Float64()
}
wt := WTInit(obj, "swt", 256, 1)
defer wt.Free()
SetWTConv(wt, "direct")
SWT(wt, input)
output := make([]float64, 256)
ISWT(wt, output)
diff := make([]float64, 256)
for i := 0; i < wt.SigLength(); i++ {
diff[i] = output[i] - input[i]
}
require.Less(t, absMax(diff), 1e-10)
WTSummary(wt)
}