-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
192 lines (169 loc) · 5.14 KB
/
filter.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package eewgo
import (
"embed"
"fmt"
"github.com/dop251/goja"
)
//go:embed js
var oregonDspScript embed.FS
func NewFilter() (Filter, error) {
jsRuntime := goja.New()
// console.log() and console.assert() are not defined in goja by default,
// but we need make them to be defined, so that the filter script can work.
obj := jsRuntime.NewObject()
obj.Set("log", func(msg ...any) { fmt.Println(msg...) })
obj.Set("assert", func(msg ...any) {})
jsRuntime.Set("console", obj)
// Load kotlin-kotlin-stdlib-js-ir.js
scriptData, err := oregonDspScript.ReadFile("js/kotlin-kotlin-stdlib-js-ir.js")
if err != nil {
return Filter{}, err
}
script, err := stripSourceMap(string(scriptData))
if err != nil {
return Filter{}, err
}
_, err = jsRuntime.RunString(script)
if err != nil {
return Filter{}, err
}
// Load oregondsp.js
scriptData, err = oregonDspScript.ReadFile("js/oregondsp.js")
if err != nil {
return Filter{}, err
}
script, err = stripSourceMap(string(scriptData))
if err != nil {
return Filter{}, err
}
_, err = jsRuntime.RunString(script)
if err != nil {
return Filter{}, err
}
// Extract OregonDSP object
_, err = jsRuntime.RunString("const oregonDspObj = oregondsp.com.oregondsp.signalProcessing;")
if err != nil {
panic(err)
}
// Extract the filter passband types
_, err = jsRuntime.RunString("const lowPass = oregonDspObj.filter.iir.PassbandType.LOWPASS;")
if err != nil {
return Filter{}, err
}
_, err = jsRuntime.RunString("const highPass = oregonDspObj.filter.iir.PassbandType.HIGHPASS;")
if err != nil {
return Filter{}, err
}
_, err = jsRuntime.RunString("const bandPass = oregonDspObj.filter.iir.PassbandType.BANDPASS;")
if err != nil {
return Filter{}, err
}
// Create filter handler
_, err = jsRuntime.RunString(`
const ButterWorth = oregonDspObj.filter.iir.Butterworth;
const handleButterWorth = (record, poles, passbandType, lowCorner, highCorner, delta) => {
const filterObj = new ButterWorth(poles, passbandType, lowCorner, highCorner, delta);
filterObj.filterInPlace(record);
return record;
}
const ChebyshevI = oregonDspObj.filter.iir.ChebyshevI;
const handleChebyshevI = (record, poles, passbandType, lowCorner, highCorner, delta, ripple) => {
const filterObj = new ChebyshevI(poles, ripple, passbandType, lowCorner, highCorner, delta);
filterObj.filterInPlace(record);
return record;
}
const ChebyshevII = oregonDspObj.filter.iir.ChebyshevII;
const handleChebyshevII = (record, poles, passbandType, lowCorner, highCorner, delta, ripple) => {
const filterObj = new ChebyshevII(poles, ripple, passbandType, lowCorner, highCorner, delta);
filterObj.filterInPlace(record);
return record;
}
`)
if err != nil {
return Filter{}, err
}
return Filter{vm: jsRuntime}, nil
}
func (f *Filter) Butterworth(data []float64, poles int, delta, lowCorner, highCorner float64, passbandType PassbandType) ([]float64, error) {
passbandTypeJs := f.vm.Get("bandPass")
if passbandType == LOWPASS {
passbandTypeJs = f.vm.Get("lowPass")
} else if passbandType == HIGHPASS {
passbandTypeJs = f.vm.Get("highPass")
}
filterHandler, ok := goja.AssertFunction(f.vm.Get("handleButterWorth"))
if !ok {
return nil, fmt.Errorf("failed to assert handleButterWorth function")
}
res, err := filterHandler(
goja.Undefined(),
f.vm.ToValue(data),
f.vm.ToValue(poles),
passbandTypeJs,
f.vm.ToValue(lowCorner),
f.vm.ToValue(highCorner),
f.vm.ToValue(delta),
)
if err != nil {
return nil, err
}
var resArr []float64
f.vm.ExportTo(res, &resArr)
return resArr, nil
}
func (f *Filter) ChebyshevI(data []float64, poles int, delta, lowCorner, highCorner, ripple float64, passbandType PassbandType) ([]float64, error) {
passbandTypeJs := f.vm.Get("bandPass")
if passbandType == LOWPASS {
passbandTypeJs = f.vm.Get("lowPass")
} else if passbandType == HIGHPASS {
passbandTypeJs = f.vm.Get("highPass")
}
filterHandler, ok := goja.AssertFunction(f.vm.Get("handleChebyshevI"))
if !ok {
return nil, fmt.Errorf("failed to assert handleChebyshevI function")
}
res, err := filterHandler(
goja.Undefined(),
f.vm.ToValue(data),
f.vm.ToValue(poles),
passbandTypeJs,
f.vm.ToValue(lowCorner),
f.vm.ToValue(highCorner),
f.vm.ToValue(delta),
f.vm.ToValue(ripple),
)
if err != nil {
return nil, err
}
var resArr []float64
f.vm.ExportTo(res, &resArr)
return resArr, nil
}
func (f *Filter) ChebyshevII(data []float64, poles int, delta, lowCorner, highCorner, ripple float64, passbandType PassbandType) ([]float64, error) {
passbandTypeJs := f.vm.Get("bandPass")
if passbandType == LOWPASS {
passbandTypeJs = f.vm.Get("lowPass")
} else if passbandType == HIGHPASS {
passbandTypeJs = f.vm.Get("highPass")
}
filterHandler, ok := goja.AssertFunction(f.vm.Get("handleChebyshevII"))
if !ok {
return nil, fmt.Errorf("failed to assert handleChebyshevII function")
}
res, err := filterHandler(
goja.Undefined(),
f.vm.ToValue(data),
f.vm.ToValue(poles),
passbandTypeJs,
f.vm.ToValue(lowCorner),
f.vm.ToValue(highCorner),
f.vm.ToValue(delta),
f.vm.ToValue(ripple),
)
if err != nil {
return nil, err
}
var resArr []float64
f.vm.ExportTo(res, &resArr)
return resArr, nil
}