-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalculate.go
45 lines (37 loc) · 1.06 KB
/
calculate.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
package gohaar // import "github.com/oskanberg/gohaar"
import (
"errors"
"math"
)
// Transform takes the Haar wavelet transform on f
// It returns the result in two slices:
// the approximation coefficients, and the detail coefficients.
func Transform(f []float64) ([]float64, []float64) {
nl := len(f) / 2
app := make([]float64, nl)
det := make([]float64, nl)
for i := 0; i < nl; i++ {
j := f[2*i]
k := f[(2*i)+1]
app[i] = (j + k) / math.Sqrt2
det[i] = (j - k) / math.Sqrt2
}
return app, det
}
// ConverseTransform takes a slice of approximation coefficients, and
// a slice of detail coefficients reconstructs the original signal.
// It will return an error if the two supplied slices are not equal in length.
func ConverseTransform(app []float64, det []float64) ([]float64, error) {
l := len(app)
if len(det) != l {
return []float64{}, errors.New("arguments should be the same length")
}
f := make([]float64, l*2)
for i := 0; i < l; i++ {
j := app[i]
k := det[i]
f[2*i] = (j + k) / math.Sqrt2
f[2*i+1] = (j - k) / math.Sqrt2
}
return f, nil
}