Digital filters are used to apply a mathematical operation to a sampled signal. One of the common formulations is the "direct form II transposed" which can represent both infinite impulse response (IIR) and finite impulse response (FIR) filters, as well as being more numerically stable than other forms. [1]

Task

Filter a signal using an order 3 low-pass Butterworth filter. The coefficients for the filter are a=[1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17] and b = [0.16666667, 0.5, 0.5, 0.16666667]

The signal that needs filtering is the following vector: [-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]

Sub Filtro(a() As Double, b() As Double, senal() As Double, resultado() As Double)
    Dim As Integer j, k
    Dim As Double tmp
 
    For j = 0 To Ubound(senal)
        tmp = 0
        For k = 0 To Ubound(b)
            If (j-k < 0) Then Continue For
            tmp = tmp + b(k) * senal(j-k)
        Next k
        For k = 0 To Ubound(a)
            If (j-k < 0) Then Continue For
            tmp = tmp - a(k) * resultado(j-k)
        Next k
        tmp /= a(0)
        resultado(j) = tmp
    Next j
End Sub
 
Dim Shared As Double a(4), b(4), senal(20), resultado(20)
Dim As Integer i
For i = 0 To 3 : Read a(i) : Next i
For i = 0 To 3 : Read b(i) : Next i
For i = 0 To 19 : Read senal(i) : Next i
 
Filtro(a(),b(),senal(),resultado())
 
For i = 0 To 19
    Print Using "###.########"; resultado(i);
    If (i+1) Mod 5 <> 0 Then
        Print ", ";
    Else
        Print
    End If
Next i
 
'' a()
Data 1, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17
'' b()
Data 0.16666667, 0.5, 0.5, 0.16666667
'' senal()
Data -0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412
Data -0.662370894973, -1.00700480494, -0.404707073677, 0.800482325044
Data 0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195
Data 0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293
Data 0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589
 
Sleep