-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
44 lines (35 loc) · 901 Bytes
/
window.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
package main
import "math"
// available windowing functions
var windows = map[string]windowFunc{
"boxcar": boxcar,
"hamming": hamming,
"hann": hann,
"welch": welch,
}
// returns weight for element n in array of N
type windowFunc func(n, N float32) float32
// multiply all elements by window functions
func applyWindow(data []float32, window windowFunc) {
N := float32(len(data))
for i := range data {
n := float32(i)
data[i] *= window(n, N)
}
}
func boxcar(n, N float32) float32 {
return 1
}
func welch(n, N float32) float32 {
return 1 - sqr((n-(N-1)/2)/((N-1)/2))
}
func hann(n, N float32) float32 {
return 0.5 * (1 + cos((2*math.Pi*n)/(N-1)))
}
func hamming(n, N float32) float32 {
const a = 0.54
const b = 1 - a
return a + b*cos((2*math.Pi*n)/(N-1))
}
func sqr(x float32) float32 { return x * x }
func cos(x float32) float32 { return float32(math.Cos(float64(x))) }