-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickavg.js
59 lines (46 loc) · 1.47 KB
/
quickavg.js
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
// Original Morse Runner use two different Filter.
// Likely for performance reasons, the QuickAverage is used for the QSB filter.
export class QuickAvg {
constructor() {
this.FPoints = 128
this.FPasses = 4
this.Reset()
}
Filter(ARe, AIm) {
let re = this._DoFilter(ARe, this.ReBufs)
let im = this._DoFilter(AIm, this.ImBufs)
this.PrevIdx = this.Idx
this.Idx = (this.Idx + 1) % this.FPoints
return { Re: re, Im: im }
}
Reset() {
const number_of_buffers = this.FPasses + 1
this.ReBufs = Array.from(Array(number_of_buffers), _ => new Float32Array(this.FPoints))
this.ImBufs = Array.from(Array(number_of_buffers), _ => new Float32Array(this.FPoints))
this.FScale = Math.pow(this.FPoints, -this.FPasses)
this.Idx = 0
this.PrevIdx = this.FPoints - 1
}
set passes(value) {
this.FPasses = Math.max(1, Math.min(8, value))
this.Reset()
}
set points(value) {
this.FPoints = Math.max(1, value)
this.Reset()
}
get points() {
return this.FPoints
}
_DoFilter(V, Buffers) {
let result = V
for (let p = 1; p <= this.FPasses; p++) {
V = result
result = Buffers[p][this.PrevIdx] - Buffers[p - 1][this.Idx] + V
Buffers[p - 1][this.Idx] = V
}
Buffers[this.FPasses][this.Idx] = result
result *= this.FScale
return result
}
}