-
Notifications
You must be signed in to change notification settings - Fork 2
/
filtic.go
105 lines (85 loc) · 2.31 KB
/
filtic.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
package gdsp
// Filtic creates the initial condition vector for the filter function. x (optional)
// and y contain the last input and output from the filter function. a and b are
// the filter coefficients.
func Filtic(b Vector, a Vector, y Vector, x Vector) Vector {
na := len(a)
nb := len(b)
m := MaxI(na, nb) - 1
if m < 1 {
return MakeVector(0.0, 0)
}
xc := x.Copy()
if len(xc) < nb-1 {
xc = xc.PaddedTrailing(0.0, nb-1-len(x))
}
yc := y.Copy()
if len(yc) < na-1 {
yc = yc.PaddedTrailing(0.0, na-1-len(y))
}
vinit := MakeVector(0.0, m)
vx := MakeVector(0.0, m)
if na-1 > 0 {
bf := VSDiv(VNeg(a.Reverse(na-1, 1)), a[0])
af := MakeVector(1.0, 1).PaddedTrailing(0.0, len(bf)-1)
yf := yc.SubVector(0, na-1)
f, _ := Filter(bf, af, yf, nil)
for i := 0; i < len(f); i++ {
vIndex := na - 2 - i
vinit[vIndex] = f[i]
}
}
if nb-1 > 0 {
bf := VSDiv(b.Reverse(nb-1, 1), a[0])
af := MakeVector(1.0, 1).PaddedTrailing(0.0, len(bf)-1)
xf := xc.SubVector(0, nb-1)
f, _ := Filter(bf, af, xf, nil)
for i := 0; i < len(f); i++ {
vIndex := nb - 2 - i
vx[vIndex] = f[i]
}
}
return VAdd(vinit, vx)
}
// FilticC creates the initial condition vector for the filter function. x (optional)
// and y contain the last input and output from the filter function. a and b are
// the filter coefficients.
func FilticC(b VectorComplex, a VectorComplex, y VectorComplex, x VectorComplex) VectorComplex {
na := len(a)
nb := len(b)
m := MaxI(na, nb) - 1
if m < 1 {
return MakeVectorComplex(0.0, 0)
}
xc := x.Copy()
if len(xc) < nb-1 {
xc = xc.PaddedTrailing(0.0, nb-1-len(x))
}
yc := y.Copy()
if len(yc) < na-1 {
yc = yc.PaddedTrailing(0.0, na-1-len(y))
}
vinit := MakeVectorComplex(0.0, m)
vx := MakeVectorComplex(0.0, m)
if na-1 > 0 {
bf := VSDivC(VNegC(a.Reverse(na-1, 1)), a[0])
af := MakeVectorComplex(1.0, 1).PaddedTrailing(0.0, len(bf)-1)
yf := yc.SubVector(0, na-1)
f, _ := FilterC(bf, af, yf, nil)
for i := 0; i < len(f); i++ {
vIndex := na - 2 - i
vinit[vIndex] = f[i]
}
}
if nb-1 > 0 {
bf := VSDivC(b.Reverse(nb-1, 1), a[0])
af := MakeVectorComplex(1.0, 1).PaddedTrailing(0.0, len(bf)-1)
xf := xc.SubVector(0, nb-1)
f, _ := FilterC(bf, af, xf, nil)
for i := 0; i < len(f); i++ {
vIndex := nb - 2 - i
vx[vIndex] = f[i]
}
}
return VAddC(vinit, vx)
}