Jump to content

Apply a digital filter (direct form II transposed): Difference between revisions

Go solution
(Added Java)
(Go solution)
Line 259:
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925840, 0.38544587, 0.65177084</pre>
 
=={{header|Go}}==
<lang go>package main
 
import "fmt"
 
type filter struct {
b, a []float64
}
 
func (f filter) filter(in []float64) []float64 {
out := make([]float64, len(in))
s := 1. / f.a[0]
for i := range in {
tmp := 0.
b := f.b
if i+1 < len(b) {
b = b[:i+1]
}
for j, bj := range b {
tmp += bj * in[i-j]
}
a := f.a[1:]
if i < len(a) {
a = a[:i]
}
for j, aj := range a {
tmp -= aj * out[i-j-1]
}
out[i] = tmp * s
}
return out
}
 
//Constants for a Butterworth filter (order 3, low pass)
var bwf = filter{
a: []float64{1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17},
b: []float64{0.16666667, 0.5, 0.5, 0.16666667},
}
 
var sig = []float64{
-0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412,
-0.662370894973, -1.00700480494, -0.404707073677, 0.800482325044,
0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195,
0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293,
0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589,
}
 
func main() {
for _, v := range bwf.filter(sig) {
fmt.Printf("%9.6f\n", v)
}
}</lang>
{{out}}
<pre>
-0.152974
-0.435258
-0.136043
0.697503
0.656445
-0.435482
-1.089239
-0.537677
0.517050
1.052250
0.961854
0.695690
0.424356
0.196262
-0.027835
-0.211722
-0.174746
0.069258
0.385446
0.651771
</pre>
 
=={{header|Java}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.