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

m
m (→‎{{header|Wren}}: Minor tidy)
 
(36 intermediate revisions by 24 users not shown)
Line 8:
 
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]
 
;See also:
[[https://en.wikipedia.org/wiki/Butterworth_filter Wikipedia on Butterworth filters]]
 
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F apply_filter(a, b, signal)
V result = [0.0] * signal.len
L(i) 0 .< signal.len
V tmp = 0.0
L(j) 0 .< min(i + 1, b.len)
tmp += b[j] * signal[i - j]
L(j) 1 .< min(i + 1, a.len)
tmp -= a[j] * result[i - j]
tmp /= a[0]
result[i] = tmp
R result
 
V a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17]
V b = [0.16666667, 0.5, 0.5, 0.16666667]
 
V signal = [-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]
 
V result = apply_filter(a, b, signal)
L(r) result
print(‘#2.8’.format(r), end' ‘’)
print(I (L.index + 1) % 5 != 0 {‘, ’} E "\n", end' ‘’)</syntaxhighlight>
 
{{out}}
<pre>
-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO;
 
procedure Apply_Filter is
 
generic
type Num is digits <>;
type Num_Array is array (Natural range <>) of Num;
A : Num_Array;
B : Num_Array;
package Direct_Form_II_Transposed is
pragma Assert (A'First = 0 and B'First = 0);
pragma Assert (A'Last = B'Last);
pragma Assert (A (0) = 1.000);
 
function Filter (X : in Num) return Num;
end Direct_Form_II_Transposed;
 
package body Direct_Form_II_Transposed
is
W : Num_Array (A'Range) := (others => 0.0);
 
function Filter (X : in Num) return Num is
Y : constant Num := X * B (0) + W (0);
begin
-- Calculate delay line for next sample
for I in 1 .. W'Last loop
W (I - 1) := X * B (I) - Y * A (I) + W (I);
end loop;
return Y;
end Filter;
 
end Direct_Form_II_Transposed;
 
type Coeff_Array is array (Natural range <>) of Float;
 
package Butterworth is
new Direct_Form_II_Transposed (Float, Coeff_Array,
A => (1.000000000000, -2.77555756e-16,
3.33333333e-01, -1.85037171e-17),
B => (0.16666667, 0.50000000,
0.50000000, 0.16666667));
 
subtype Signal_Array is Coeff_Array;
 
X_Signal : constant Signal_Array :=
(-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);
 
package Float_IO is
new Ada.Text_IO.Float_IO (Float);
Y : Float;
begin
for Sample of X_Signal loop
Y := Butterworth.Filter (Sample);
Float_IO.Put (Y, Exp => 0, Aft => 6);
Ada.Text_IO.New_Line;
end loop;
end Apply_Filter;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
{{Trans|C++}} ... via Yabasic<br>
... with the "j" loops transformed to not needlessly iterate beyond i.<br>
The default lower bound in Algol 68 arrays is 1, so the loops/subscripts have been adjusted accordingly.
<syntaxhighlight lang="algol68">
BEGIN # apply a digital filter #
# the lower bounds of a, b, signal and result must all be equal #
PROC filter = ( []REAL a, b, signal, REF[]REAL result )VOID:
IF LWB a /= LWB b OR LWB a /= LWB signal OR LWB a /= LWB result THEN
print( ( "Array lower bounds must be equal for filter", newline ) );
stop
ELSE
FOR i FROM LWB result TO UPB result DO result[ i ] := 0 OD;
FOR i FROM LWB signal TO UPB signal DO
REAL tmp := 0;
FOR j FROM LWB b TO IF i > UPB b THEN UPB b ELSE i FI DO
tmp +:= b[ j ] * signal[ LWB signal + ( i - j ) ]
OD;
FOR j FROM LWB a + 1 TO IF i > UPB a THEN UPB a ELSE i FI DO
tmp -:= a[ j ] * result[ LWB result + ( i - j ) ]
OD;
result[ i ] := tmp / a[ LWB a ]
OD
FI # filter # ;
[ 4 ]REAL a := []REAL( 1, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17 );
[ 4 ]REAL b := []REAL( 0.16666667, 0.5, 0.5, 0.16666667 );
[ 20 ]REAL signal
:= []REAL( -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
);
[ 20 ]REAL result;
filter( a, b, signal, result );
FOR i FROM LWB result TO UPB result DO
print( ( " ", fixed( result[ i ], -9, 6 ) ) );
IF i MOD 5 /= 0 THEN print( ( ", " ) ) ELSE print( ( newline ) ) FI
OD
END
</syntaxhighlight>
{{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|AppleScript}}==
{{trans|Julia}} — except that j starts from 2 in the second inner repeat, there being no point in fetching and performing math with the zero about to be overwritten. This change in turn allows the result list to be populated on the fly instead of being pre-populated with zeros.
<syntaxhighlight lang="applescript">on min(a, b)
if (b < a) then return b
return a
end min
 
on DF2TFilter(a, b, sig)
set aCount to (count a)
set bCount to (count b)
set sigCount to (count sig)
set rst to {}
repeat with i from 1 to sigCount
set tmp to 0
set iPlus1 to i + 1
repeat with j from 1 to min(i, bCount)
set tmp to tmp + (item j of b) * (item (iPlus1 - j) of sig)
end repeat
repeat with j from 2 to min(i, aCount)
set tmp to tmp - (item j of a) * (item (iPlus1 - j) of rst)
end repeat
set end of rst to tmp / (beginning of a)
end repeat
return rst
end DF2TFilter
 
local acoef, bcoef, signal
set acoef to {1.0, -2.77555756E-16, 0.333333333, -1.85037171E-17}
set bcoef to {0.16666667, 0.5, 0.5, 0.16666667}
set signal to {-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.025930339848, 0.490105989562, 0.549391221511, 0.9047198589}
DF2TFilter(acoef, bcoef, signal)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{-0.1529739895, -0.43525782905, -0.136043396988, 0.697503326548, 0.656444692469, -0.435482453256, -1.089239461153, -0.537676549563, 0.517049992313, 1.052249747155, 0.961854300374, 0.69569009401, 0.424356295096, 0.196262231822, -0.027835124463, -0.21172191545, -0.174745562223, 0.069258408901, 0.385445874308, 0.651770838819}</syntaxhighlight>
 
=={{header|C}}==
Given the number of values a coefficient or signal vector can have and the number of digits, this implementation reads data from a file and prints it to the console if no output file is specified or writes to the specified output file. Usage printed on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<string.h>
Line 125 ⟶ 318:
return 0;
}
</syntaxhighlight>
</lang>
Input file, 3 lines containing first ( a ) and second ( b ) coefficient followed by the signal, all values should be separated by a single space:
<pre>
Line 141 ⟶ 334:
-0.152973994613, -0.435257852077, -0.136043429375, 0.697503268719, 0.656444668770, -0.435482472181, -1.089239478111, -0.537676513195, 0.517050027847, 1.052249789238, 0.961854279041, 0.695690035820, 0.424356281757, 0.196262255311, -0.027835110202, -0.211721926928, -0.174745559692, 0.069258414209, 0.385445863008, 0.651770770550
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
 
namespace ApplyDigitalFilter {
class Program {
private static double[] Filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.Length];
for (int i = 0; i < signal.Length; ++i) {
double tmp = 0.0;
for (int j = 0; j < b.Length; ++j) {
if (i - j < 0) continue;
tmp += b[j] * signal[i - j];
}
for (int j = 1; j < a.Length; ++j) {
if (i - j < 0) continue;
tmp -= a[j] * result[i - j];
}
tmp /= a[0];
result[i] = tmp;
}
return result;
}
 
static void Main(string[] args) {
double[] a = new double[] { 1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17 };
double[] b = new double[] { 0.16666667, 0.5, 0.5, 0.16666667 };
 
double[] signal = new double[] {
-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
};
 
double[] result = Filter(a, b, signal);
for (int i = 0; i < result.Length; ++i) {
Console.Write("{0,11:F8}", result[i]);
Console.Write((i + 1) % 5 != 0 ? ", " : "\n");
}
}
}
}</syntaxhighlight>
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084</pre>
 
=={{header|C++}}==
Line 146 ⟶ 389:
This uses the C++11 method of initializing vectors. In g++, use the -std=c++0x compiler switch.
 
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
using namespace std;
Line 197 ⟶ 440:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
<pre>-0.152974,-0.435258,-0.136043,0.697503,0.656445,-0.435483,-1.08924,-0.537677,0.51705,1.05225,0.961854,0.69569,0.424356,0.196262,-0.0278351,-0.211722,-0.174746,0.0692584,0.385446,0.651771,</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
 
namespace ApplyDigitalFilter {
class Program {
private static double[] Filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.Length];
for (int i = 0; i < signal.Length; ++i) {
double tmp = 0.0;
for (int j = 0; j < b.Length; ++j) {
if (i - j < 0) continue;
tmp += b[j] * signal[i - j];
}
for (int j = 1; j < a.Length; ++j) {
if (i - j < 0) continue;
tmp -= a[j] * result[i - j];
}
tmp /= a[0];
result[i] = tmp;
}
return result;
}
 
static void Main(string[] args) {
double[] a = new double[] { 1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17 };
double[] b = new double[] { 0.16666667, 0.5, 0.5, 0.16666667 };
 
double[] signal = new double[] {
-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
};
 
double[] result = Filter(a, b, signal);
for (int i = 0; i < result.Length; ++i) {
Console.Write("{0,11:F8}", result[i]);
Console.Write((i + 1) % 5 != 0 ? ", " : "\n");
}
}
}
}</lang>
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084</pre>
=={{header|Common Lisp}}==
{{trans|zkl}}
<langsyntaxhighlight lang="lisp">(defparameter a #(1.00000000L0 -2.77555756L-16 3.33333333L-01 -1.85037171L-17))
(defparameter b #(0.16666667L0 0.50000000L0 0.50000000L0 0.16666667L0))
(defparameter s #(-0.917843918645 0.141984778794 1.20536903482 0.190286794412 -0.662370894973
Line 268 ⟶ 462:
when (>= i j) sum (* (svref a j) (svref out (- i j)))))
(svref a 0)))
(format t "~%~16,8F" (svref out i)))</langsyntaxhighlight>
{{out}}
<pre> -0.15297399
Line 294 ⟶ 488:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
alias T = real;
Line 340 ⟶ 534:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 347 ⟶ 541:
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925840, 0.38544587, 0.65177084</pre>
 
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<syntaxhighlight lang="freebasic">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</syntaxhighlight>
{{out}}
<pre>
-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925840, 0.38544587, 0.65177084
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 399 ⟶ 651:
fmt.Printf("%9.6f\n", v)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 423 ⟶ 675:
0.651771
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class DigitalFilter {
private static double[] filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.length]
for (int i = 0; i < signal.length; ++i) {
double tmp = 0.0
for (int j = 0; j < b.length; ++j) {
if (i - j < 0) continue
tmp += b[j] * signal[i - j]
}
for (int j = 1; j < a.length; ++j) {
if (i - j < 0) continue
tmp -= a[j] * result[i - j]
}
tmp /= a[0]
result[i] = tmp
}
return result
}
 
static void main(String[] args) {
double[] a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17] as double[]
double[] b = [0.16666667, 0.5, 0.5, 0.16666667] as double[]
double[] signal = [
-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
] as double[]
 
double[] result = filter(a, b, signal)
for (int i = 0; i < result.length; ++i) {
printf("% .8f", result[i])
print((i + 1) % 5 != 0 ? ", " : "\n")
}
}
}</syntaxhighlight>
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084</pre>
 
=={{header|Haskell}}==
The solution is based not on the explicit loops, as in strict imperative languages, but on lazy recursive trick known as "tying a knot".
<syntaxhighlight lang="haskell">import Data.List (tails)
 
-- lazy convolution of a list by given kernel
conv :: Num a => [a] -> [a] -> [a]
conv ker = map (dot (reverse ker)) . tails . pad
where
pad v = replicate (length ker - 1) 0 ++ v
dot v = sum . zipWith (*) v
 
-- The lazy digital filter
dFilter :: [Double] -> [Double] -> [Double] -> [Double]
dFilter (a0:a) b s = tail res
where
res = (/ a0) <$> 0 : zipWith (-) (conv b s) (conv a res)</syntaxhighlight>
 
=== Examples ===
 
Demonstration of convolution:
<pre>λ> take 10 $ conv [1,10,100,1000] [1..]
[1,12,123,1234,2345,3456,4567,5678,6789,7900]
</pre>
 
The given task:
<pre>λ> let a = [1, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17]
λ> let b = [0.16666667, 0.5, 0.5, 0.16666667]
λ> let s = [-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]
λ> dFilter a b s
[-0.15297398950031305,-0.4352578290502175,-0.13604339698849033,0.6975033265479628,
0.6564446924690288,-0.4354824532561055,-1.089239461152929,-0.5376765495627545,
0.517049992313214,1.0522497471553531,0.961854300373645,0.6956900940096052,
0.4243562950955321,0.19626223182178906,-2.7835124463393313e-2,-0.21172191545011776,
-0.17474556222276072,6.925840890119485e-2,0.3854458743074388,0.6517708388193053,
0.6802579154588558,0.326668188810626,-7.596599209379973e-2,-0.10888939616131928]
</pre>
The last line is redundant and appears due to the finiteness of a signal stream. The digital filter is able to handle infinite lists (as streams):
<pre>λ> take 10 $ dFilter a b $ cycle [1,-1]
[0.16666667,0.33333333000000004,0.11111111338888897,-0.11111110988888885,-3.703703775925934e-2,3.70370365925926e-2,1.2345679240740749e-2,-1.2345678851851824e-2,-4.1152264094650535e-3,4.115226279835409e-3]</pre>
 
=={{header|J}}==
 
There's probably a nicer way to do this:
 
<syntaxhighlight lang="j">Butter=: {{
t=. (#n) +/ .*&(|.n)\(}.n*0),y
A=.|.}.m
for_i.}.i.#y do.
t=. t i}~ (i{t) - (i{.t) +/ .* (-i){.A
end.
t%{.m
}}
 
 
sig=: ". rplc&('-_') {{)n
-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
}}-.LF
 
a=: 1.00000000 _2.77555756e_16 3.33333333e_01 _1.85037171e_17
b=: 0.16666667 0.5 0.5 0.16666667
 
4 5$ a Butter b sig
_0.152974 _0.435258 _0.136043 0.697503 0.656445
_0.435482 _1.08924 _0.537677 0.51705 1.05225
0.961854 0.69569 0.424356 0.196262 _0.0278351
_0.211722 _0.174746 0.0692584 0.385446 0.651771</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class DigitalFilter {
private static double[] filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.length];
Line 463 ⟶ 833:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
Line 472 ⟶ 842:
=={{header|Julia}}==
{{trans|zkl}}
<langsyntaxhighlight lang="julia">function DF2TFilter(a::Vector, b::Vector, sig::Vector)
rst = zeros(sig)
for i in eachindex(sig)
Line 489 ⟶ 859:
0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293,
0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589]
@show DF2TFilter(acoef, bcoef, signal)</langsyntaxhighlight>
{{output}}<pre>DF2TFilter(acoef, bcoef, signal) = [-0.152974, -0.435258, -0.136043, 0.697503, 0.656445, -0.435482, -1.08924, -0.537677, 0.51705, 1.05225, 0.961854, 0.69569, 0.424356, 0.196262, -0.0278351, -0.211722, -0.174746, 0.0692584, 0.385446, 0.651771]</pre>
 
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
fun filter(a: DoubleArray, b: DoubleArray, signal: DoubleArray): DoubleArray {
Line 531 ⟶ 901:
print(if ((i + 1) % 5 != 0) ", " else "\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 540 ⟶ 910:
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084
</pre>
 
=={{header|Lua}}==
{{trans|C++}}
<syntaxhighlight lang="lua">function filter(b,a,input)
local out = {}
for i=1,table.getn(input) do
local tmp = 0
local j = 0
out[i] = 0
 
for j=1,table.getn(b) do
if i - j < 0 then
--continue
else
tmp = tmp + b[j] * input[i - j + 1]
end
end
 
for j=2,table.getn(a) do
if i - j < 0 then
--continue
else
tmp = tmp - a[j] * out[i - j + 1]
end
end
 
tmp = tmp / a[1]
out[i] = tmp
end
return out
end
 
function main()
local sig = {
-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
}
 
--Constants for a Butterworth filter (order 3, low pass)
local a = {1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17}
local b = {0.16666667, 0.5, 0.5, 0.16666667}
 
local result = filter(b,a,sig)
for i=1,table.getn(result) do
io.write(result[i] .. ", ")
end
print()
 
return nil
end
 
main()</syntaxhighlight>
{{out}}
<pre>-0.15297398950031, -0.43525782905022, -0.13604339698849, 0.69750332654796, 0.65644469246903, -0.43548245325611, -1.0892394611529, -0.53767654956275, 0.51704999231321, 1.0522497471554, 0.96185430037364, 0.6956900940096, 0.42435629509553, 0.19626223182179, -0.027835124463393, -0.21172191545012, -0.17474556222276, 0.069258408901195, 0.38544587430744, 0.65177083881931,</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">b = {0.16666667, 0.5, 0.5, 0.16666667};
a = {1.00000000, -2.77555756*^-16, 3.33333333*^-01, -1.85037171*^-17};
signal = {-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};
RecurrenceFilter[{a, b}, signal]</syntaxhighlight>
{{out}}
<pre>{-0.152974,-0.435258,-0.136043,0.697503,0.656445,-0.435482,-1.08924,-0.537677,0.51705,1.05225,0.961854,0.69569,0.424356,0.196262,-0.0278351,-0.211722,-0.174746,0.0692584,0.385446,0.651771}</pre>
 
=={{header|MATLAB}}==
MATLAB is commonly used for filter design and implementation. To implement this filter, and display the original signal and the filtered result:
<syntaxhighlight lang="matlab">
<lang MATLAB>
signal = [-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];
a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
Line 560 ⟶ 994:
xlabel('n')
title('Filtered Signal')
</syntaxhighlight>
</lang>
 
{{out}}
Line 573 ⟶ 1,007:
 
0.9619 0.6957 0.4244 0.1963 -0.0278 -0.2117 -0.1747 0.0693 0.3854 0.6518
</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">
import strformat
 
func filter(a, b, signal: openArray[float]): seq[float] =
 
result.setLen(signal.len)
 
for i in 0..signal.high:
var tmp = 0.0
for j in 0..min(i, b.high):
tmp += b[j] * signal[i - j]
for j in 1..min(i, a.high):
tmp -= a[j] * result[i - j]
tmp /= a[0]
result[i] = tmp
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17]
let b = [0.16666667, 0.5, 0.5, 0.16666667]
 
let signal = [-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]
 
let result = filter(a, b, signal)
for i in 0..result.high:
stdout.write fmt"{result[i]: .8f}"
stdout.write if (i + 1) mod 5 != 0: ", " else: "\n"</syntaxhighlight>
 
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084</pre>
 
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">class DigitalFilter {
function : Main(args : String[]) ~ Nil {
a := [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
b := [0.16666667, 0.5, 0.5, 0.16666667];
signal := [-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];
 
result := Filter(a, b, signal);
each(i : result) {
System.IO.Console->Print(result[i])->Print(((i + 1) % 5 <> 0) ? ",\t" : "\n");
};
}
 
function : Filter(a : Float[], b : Float[], signal : Float[]) ~ Float[] {
result := Float->New[signal->Size()];
 
each(i : signal) {
tmp := 0.0;
 
each(j : b) {
if(i-j >= 0) {
tmp += b[j] * signal[i - j];
};
};
 
each(j : a) {
if(i-j >= 0) {
tmp -= a[j] * result[i - j];
};
};
 
tmp /= a[0];
result[i] := tmp;
};
 
return result;
}
}</syntaxhighlight>
 
{{output}}
<pre>
-0.152974, -0.435258, -0.136043, 0.697503, 0.656445
-0.435482, -1.08924, -0.537677, 0.51705, 1.05225
0.961854, 0.69569, 0.424356, 0.196262, -0.0278351
-0.211722, -0.174746, 0.0692584, 0.385446, 0.651771
</pre>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX */
a=.array~of(1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17)
b=.array~of(0.16666667, 0.5, 0.5, 0.16666667)
Line 612 ⟶ 1,138:
 
::OPTIONS digits 24 /* Numeric Digits 24, everywhere */
</syntaxhighlight>
</lang>
{{out|output}}
<pre> 1 -0.152973989500
Line 635 ⟶ 1,161:
20 0.651770838819</pre>
 
=={{header|Perl 6}}==
{{trans|Raku}}
{{works with|Rakudo|2016.11}}
<syntaxhighlight lang="perl">use strict;
{{trans|zkl}}
use List::AllUtils 'natatime';
 
sub TDF_II_filter {
<lang perl6>sub TDF-II-filter ( @signal, @a, @b ) {
my @out = 0 xx our(@signal,@a,@b);
for ^@local(*signal,*a,*b) = (shift, shift, shift); -> $i {
my @out = (0) x $#signal;
for my $i (0..@signal-1) {
my $this;
map { $this += @$b[$_] * @$signal[$i-$_] if $i-$_ >= 0 for} ^0..@b;
map { $this -= @$a[$_] * @$out[$i-$_] if $i-$_ >= 0 for} ^0..@a;
@$out[$i] = $this / @$a[0];
}
@out
}
 
my @signal = [(
-0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412,
-0.662370894973, -1.00700480494, -0.404707073677, 0.800482325044,
Line 656 ⟶ 1,185:
0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293,
0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589
]);
my @a = [( 1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17 ]);
my @b = [( 0.16666667, 0.5, 0.5, 0.16666667 ]);
 
saymy TDF-II-filter@filtered = TDF_II_filter(\@signal, \@a, \@b)».fmt("% 0.8f");
my $iter = natatime 5, @filtered;
Z~ flat (', ' xx 4, ",\n") xx *;</lang>
while( my @values = $iter->() ) {
printf(' %10.6f' x 5 . "\n", @values);
}
</syntaxhighlight>
{{out}}
<pre>( -0.15297399,152974 -0.43525783,435258 -0.13604340,136043 0.69750333,697503 0.65644469,656445
-0.43548245,435482 -1.08923946,089239 -0.53767655,537677 0.51704999,517050 1.05224975,052250
0.96185430,961854 0.69569009,695690 0.42435630,424356 0.19626223,196262 -0.02783512,027835
-0.21172192,211722 -0.17474556,174746 0.06925841,069258 0.38544587,385446 0.65177084,651771</pre>
)</pre>
 
=={{header|Phix}}==
{{trans|Julia}}
Note however that the a[j]* starts from index 2, unlike Julia/C/Perl6Raku/Rust/Sidef/zkl,
but the same as C++/C#/D/Java/Kotlin - and it does not seem to make any difference...
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function direct_form_II_transposed_filter(sequence a, b, signal)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence result = repeat(0,length(signal))
<span style="color: #008080;">function</span> <span style="color: #000000;">direct_form_II_transposed_filter</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">signal</span><span style="color: #0000FF;">)</span>
for i=1 to length(in) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">signal</span><span style="color: #0000FF;">))</span>
atom tmp = 0
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">signal</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for j=1 to min(i,length(b)) do tmp += b[j]*signal[i-j+1] end for
<span style="color: #004080;">atom</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for j=2 to min(i,length(a)) do tmp -= a[j]*result[i-j+1] end for
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">do</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">signal</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
result[i] = tmp/a[1]
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">do</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">/</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
return result
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant acoef = {1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17},
bcoef = {0.16666667, 0.5, 0.5, 0.16666667}
<span style="color: #008080;">constant</span> <span style="color: #000000;">acoef</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1.00000000</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2.77555756e-16</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3.33333333e-01</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.85037171e-17</span><span style="color: #0000FF;">},</span>
signal = {-0.917843918645,0.141984778794,1.20536903482,0.190286794412,-0.662370894973,
<span style="color: #000000;">bcoef</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0.16666667</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.16666667</span><span style="color: #0000FF;">},</span>
-1.00700480494,-0.404707073677,0.800482325044,0.743500089861,1.01090520172,
<span style="color: #000000;">signal</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">0.917843918645</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.141984778794</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1.20536903482</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.190286794412</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">0.662370894973</span><span style="color: #0000FF;">,</span>
0.741527555207,0.277841675195,0.400833448236,-0.2085993586,-0.172842103641,
<span style="color: #0000FF;">-</span><span style="color: #000000;">1.00700480494</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">0.404707073677</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.800482325044</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.743500089861</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1.01090520172</span><span style="color: #0000FF;">,</span>
-0.134316096293,0.0259303398477,0.490105989562,0.549391221511,0.9047198589},
<span style="color: #000000;">0.741527555207</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.277841675195</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.400833448236</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">0.2085993586</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">0.172842103641</span><span style="color: #0000FF;">,</span>
 
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.134316096293</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.0259303398477</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.490105989562</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.549391221511</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.9047198589</span><span style="color: #0000FF;">}</span>
pp(direct_form_II_transposed_filter(acoef, bcoef, signal),{pp_FltFmt,"%9.6f",pp_Maxlen,110})</lang>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">direct_form_II_transposed_filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">acoef</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bcoef</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">signal</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_FltFmt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%9.6f"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_Maxlen</span><span style="color: #0000FF;">,</span><span style="color: #000000;">110</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 697 ⟶ 1,232:
0.961854, 0.695690, 0.424356, 0.196262,-0.027835,-0.211722,-0.174746, 0.069258, 0.385446, 0.651771}
</pre>
 
=={{header|Phixmonti}}==
{{trans|Phix}}
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt
 
( 1.00000000 -2.77555756e-16 3.33333333e-01 -1.85037171e-17 ) var a
( 0.16666667 0.5 0.5 0.16666667 ) var b
( -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 )
len dup 0 swap repeat >ps
 
for var i
0 >ps
b len i min for var j
j get rot i j - 1 + get rot * ps> + >ps swap
endfor
drop
a len i min for var j
j get ps> tps i j - 1 + get nip rot * - >ps
endfor
drop
ps> a 1 get nip / ps> swap i set >ps
endfor
drop ps> ?</syntaxhighlight>
{{out}}
<pre>[-0.152973989500313, -0.435257829050217, -0.13604339698849, 0.697503326547963, 0.656444692469029, -0.435482453256106, -1.089239461152929, -0.537676549562755, 0.517049992313214, 1.052249747155353, 0.961854300373645, 0.695690094009605, 0.424356295095532, 0.196262231821789, -0.0278351244633933, -0.211721915450118, -0.174745562222761, 0.0692584089011949, 0.385445874307439, 0.651770838819305]
 
=== Press any key to exit ===</pre>
 
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">#!/bin/python
from __future__ import print_function
from scipy import signal
Line 722 ⟶ 1,288:
plt.plot(sig, 'b')
plt.plot(filt, 'r--')
plt.show()</langsyntaxhighlight>
 
{{out}}
Line 729 ⟶ 1,295:
0.4243563 0.19626223 -0.02783512 -0.21172192 -0.17474556 0.06925841
0.38544587 0.65177084]</pre>
 
=={{header|Racket}}==
{{trans|C}} Strangely, C was more informative than Common Lisp in helping figure out what was going on here.
 
<syntaxhighlight lang="racket">#lang racket
 
(define a (vector 1.00000000E0 -2.77555756E-16 3.33333333E-01 -1.85037171E-17))
(define b (vector 0.16666667E0 0.50000000E0 0.50000000E0 0.16666667E0))
(define s (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))
 
(define (filter-signal-direct-form-ii-transposed coeff1 coeff2 signal)
(define signal-size (vector-length signal))
(define filtered-signal (make-vector signal-size 0))
(for ((i signal-size))
(vector-set! filtered-signal
i
(/ (for/fold ((s (for/fold ((s 0)) ((j (vector-length coeff2)) #:when (>= i j))
(+ s (* (vector-ref coeff2 j) (vector-ref signal (- i j)))))))
((j (vector-length coeff1)) #:when (>= i j))
(- s (* (vector-ref coeff1 j) (vector-ref filtered-signal (- i j)))))
(vector-ref coeff1 0))))
filtered-signal)
 
(filter-signal-direct-form-ii-transposed a b s)</syntaxhighlight>
 
{{out}}
<pre>'#(-0.15297398950031305
-0.4352578290502175
-0.13604339698849033
0.6975033265479628
0.6564446924690288
-0.4354824532561056
-1.0892394611529292
-0.5376765495627545
0.5170499923132141
1.0522497471553531
0.9618543003736449
0.6956900940096049
0.42435629509553213
0.19626223182178917
-0.027835124463393326
-0.21172191545011781
-0.17474556222276072
0.06925840890119488
0.3854458743074388
0.6517708388193052)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2016.11}}
{{trans|zkl}}
 
<syntaxhighlight lang="raku" line>sub TDF-II-filter ( @signal, @a, @b ) {
my @out = 0 xx @signal;
for ^@signal -> $i {
my $this;
$this += @b[$_] * @signal[$i-$_] if $i-$_ >= 0 for ^@b;
$this -= @a[$_] * @out[$i-$_] if $i-$_ >= 0 for ^@a;
@out[$i] = $this / @a[0];
}
@out
}
 
my @signal = [
-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
];
my @a = [ 1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17 ];
my @b = [ 0.16666667, 0.5, 0.5, 0.16666667 ];
 
say TDF-II-filter(@signal, @a, @b)».fmt("% 0.8f")
Z~ flat (', ' xx 4, ",\n") xx *;</syntaxhighlight>
{{out}}
<pre>(-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469,
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975,
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512,
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084,
)</pre>
 
=={{header|REXX}}==
===version 1===
{{trans|Julia}}
<langsyntaxhighlight REXXlang="rexx">/*REXX pgm filters a signal with a order3 lowpass Butterworth, direct form II transposed*/
numeric digits 24 /*use 20 decimal digs*/
@a= '1 -2.77555756e-16 3.33333333e-1 -1.85037171e-17' /*filter coefficients*/
@b= 0.16666667 0.5 0.5 0.16666667 /* " " */
Line 741 ⟶ 1,390:
' 0.741527555207 0.277841675195 0.400833448236 -0.2085993586 -0.172842103641' ,
'-0.134316096293 0.0259303398477 0.490105989562 0.549391221511 0.9047198589 '
$.=0; N= words(@s); w= length(n); numeric digits 24 /* [↑] signal vector*/
do i=1 for N; #=0 /*process each of the vector elements. */
do j=1 for words(@b); if i-j >= 0 then #= # + word(@b, j) * word(@s, i-j+1); end
do k=1 for words(@a); _= i -k +1; if i-k >= 0 then #= # - word(@a, k) * $._; end
Line 749 ⟶ 1,398:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: numeric digits digits()%2; say right(i, w) " " left('', $.i>=0)$.i /1; return</langsyntaxhighlight>
{{out|output}}
<pre>
Line 776 ⟶ 1,425:
===version 2===
{{trans|Julia}}
<langsyntaxhighlight REXXlang="rexx">/* REXX */
Numeric Digits 24
acoef = '1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17'
Line 807 ⟶ 1,456:
ret.i=temp/a.1
Say format(i,2) format(ret.i,2,12)
End</langsyntaxhighlight>
{{out|output}}
<pre> 1 -0.152973989500
Line 830 ⟶ 1,479:
20 0.651770838819
</pre>
 
=={{header|RPL}}==
We use here useful list handling functions that are available from HP48G or newer models.
{| class="wikitable"
! RPL code
! Comment
|-
|
ROT REVLIST ROT REVLIST → signal a b
≪ { } 1 signal SIZE '''FOR''' j
signal j b SIZE - 1 + j SUB
'''WHILE''' DUP SIZE b SIZE < '''REPEAT''' 0 SWAP + '''END'''
b * ∑LIST
OVER j a SIZE - 1 + j 1 - SUB 0 +
'''WHILE''' DUP SIZE a SIZE < '''REPEAT''' 0 SWAP + '''END'''
a * ∑LIST -
a DUP SIZE GET / +
'''NEXT'''
≫ ≫ '<span style="color:blue">'''FILTR'''</span>' STO
|
<span style="color:blue">'''FILTR'''</span> ''( {a} {b} {signal} → {filtered} ) ''
Reverse a and b
For j = 1 to last signal item
extract what to multiply to b
prepend 0's if necessary
multiply by b and sum
extract what to multiply by a except a[0]
prepend 0's if necessary
multiply by a, sum and substract
divide by a[0] which is the last item once a reversed
|}
Figures have been rounded to 3 digits after the decimal point to ease the 100% manual data transfer.
{ 1 -2.778E-16 0.3333 -1.851E-17 }
{ 0.1667 0.5 0.5 0.1667}
{ -0.9178 0.1420 1.2054 0.1903 -0.6624
-1.007 -0.4047 0.8005 0.7435 1.0109
0.7415 0.2778 0.4008 -0.2086 -0.17281 -
-0.1343 0.02593 0.4901 0.5494 0.90472 }
<span style="color:blue">'''FILTR'''</span> 3 RND
'''Output:'''
<span style="color:grey"> 1:</span> { -.153 -.435 -.136 .697 .656
-.436 -1.089 -.538 .517 1.052
.962 .696 .425 .196 .028
-.212 -.175 .069 .386 .652 }
 
=={{header|Ruby}}==
{{trans|C#}}
<syntaxhighlight lang="ruby">def filter(a,b,signal)
result = Array.new(signal.length(), 0.0)
for i in 0..signal.length()-1 do
tmp = 0.0
for j in 0 .. b.length()-1 do
if i - j < 0 then next end
tmp += b[j] * signal[i - j]
end
for j in 1 .. a.length()-1 do
if i - j < 0 then next end
tmp -= a[j] * result[i - j]
end
tmp /= a[0]
result[i] = tmp
end
return result
end
 
def main
a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17]
b = [0.16666667, 0.5, 0.5, 0.16666667]
signal = [
-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
]
 
result = filter(a,b,signal)
for i in 0 .. result.length() - 1 do
print "%11.8f" % [result[i]]
if (i + 1) % 5 == 0 then
print "\n"
else
print ", "
end
end
end
 
main()</syntaxhighlight>
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084</pre>
 
=={{header|Rust}}==
{{trans|Java}}
<langsyntaxhighlight Rustlang="rust">use std::cmp::Ordering;
 
struct IIRFilter<'f>(&'f [f32], &'f [f32]);
Line 934 ⟶ 1,679:
}
println!();
}</langsyntaxhighlight>
{{out|output}}
<pre>
Line 942 ⟶ 1,687:
-0.21172196, -0.17474557, 0.06925842, 0.38544586, 0.65177077
</pre>
 
=={{header|Scala}}==
{{Out}}See it yourself by running in your browser either by [https://scalafiddle.io/sf/0D4zyWF/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/mUPbvXTQTXijp2DV1piQgQ Scastie (remote JVM)].
{{libheader|Scala Tail recursion}}
{{libheader|Scala Digital Signal Processing}}
{{libheader|ScalaFiddle qualified}}
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<syntaxhighlight lang="scala">object ButterworthFilter extends App {
private def filter(a: Vector[Double],
b: Vector[Double],
signal: Vector[Double]): Vector[Double] = {
 
@scala.annotation.tailrec
def outer(i: Int, acc: Vector[Double]): Vector[Double] = {
if (i >= signal.length) acc
else {
@scala.annotation.tailrec
def inner0(j: Int, tmp: Double): Double = if (j >= b.length) tmp
else if ((i - j) >= 0) inner0(j + 1, tmp + b(j) * signal(i - j)) else inner0(j + 1, tmp)
 
@scala.annotation.tailrec
def inner1(j: Int, tmp: Double): Double = if (j >= a.length) tmp
else if (i - j >= 0) inner1(j + 1, tmp - a(j) * acc(i - j)) else inner1(j + 1, tmp)
 
outer(i + 1, acc :+ inner1(1, inner0(0, 0D)) / a(0))
}
}
 
outer(0, Vector())
}
 
filter(Vector[Double](1, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17),
Vector[Double](0.16666667, 0.5, 0.5, 0.16666667),
Vector[Double](
-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)
).grouped(5)
.map(_.map(x => f"$x% .8f"))
.foreach(line => println(line.mkString(" ")))
 
}</syntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">func TDF_II_filter(signal, a, b) {
var out = [0]*signal.len
for i in ^signal {
Line 970 ⟶ 1,759:
say "["
say f.map { "% 0.8f" % _ }.slices(5).map{.join(', ')}.join(",\n")
say "]"</langsyntaxhighlight>
{{out}}
<pre>
Line 978 ⟶ 1,767:
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512,
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084
]</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function Filter(a As Double(), b As Double(), signal As Double()) As Double()
Dim result(signal.Length - 1) As Double
For i = 1 To signal.Length
Dim tmp = 0.0
For j = 1 To b.Length
If i - j < 0 Then
Continue For
End If
tmp += b(j - 1) * signal(i - j)
Next
For j = 2 To a.Length
If i - j < 0 Then
Continue For
End If
tmp -= a(j - 1) * result(i - j)
Next
tmp /= a(0)
result(i - 1) = tmp
Next
Return result
End Function
 
Sub Main()
Dim a() As Double = {1.0, -0.000000000000000277555756, 0.333333333, -1.85037171E-17}
Dim b() As Double = {0.16666667, 0.5, 0.5, 0.16666667}
 
Dim signal() As Double = {
-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
}
 
Dim result = Filter(a, b, signal)
For i = 1 To result.Length
Console.Write("{0,11:F8}", result(i - 1))
Console.Write(If(i Mod 5 <> 0, ", ", vbNewLine))
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">struct Filter {
b []f64
a []f64
}
fn (f Filter) filter(inp []f64) []f64 {
mut out := []f64{len: inp.len}
s := 1.0 / f.a[0]
for i in 0..inp.len {
mut tmp := 0.0
mut b := f.b
if i+1 < b.len {
b = b[..i+1]
}
for j, bj in b {
tmp += bj * inp[i-j]
}
mut a := f.a[1..]
if i < a.len {
a = a[..i]
}
for j, aj in a {
tmp -= aj * out[i-j-1]
}
out[i] = tmp * s
}
return out
}
//Constants for a Butterworth Filter (order 3, low pass)
const bwf = Filter{
a: [f64(1.00000000), -2.77555756e-16, 3.33333333e-01, -1.85037171e-17],
b: [f64(0.16666667), 0.5, 0.5, 0.16666667],
}
const sig = [
f64(-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,
]
fn main() {
for v in bwf.filter(sig) {
println("${v:9.6}")
}
}</syntaxhighlight>
 
{{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|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var filter = Fn.new { |a, b, signal|
var result = List.filled(signal.count, 0)
for (i in 0...signal.count) {
var tmp = 0
for (j in 0...b.count) {
if (i - j < 0) continue
tmp = tmp + b[j] * signal[i - j]
}
for (j in 1...a.count) {
if (i - j < 0) continue
tmp = tmp - a[j] * result[i - j]
}
tmp = tmp / a[0]
result[i] = tmp
}
return result
}
 
var a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17]
var b = [0.16666667, 0.5, 0.5, 0.16666667]
 
var signal = [
-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
]
 
var result = filter.call(a, b, signal)
for (i in 0...result.count) {
Fmt.write("$11.8f", result[i])
System.write(((i + 1) % 5 != 0) ? ", " : "\n")
}</syntaxhighlight>
 
{{out}}
<pre>
-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">real A, B, Signal, Temp, Result(20);
int I, J;
[A:= [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
B:= [0.16666667, 0.5, 0.5, 0.16666667];
Signal:= [
-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 ];
Format(2, 8);
for I:= 0 to 20-1 do
[Temp:= 0.;
for J:= 0 to 4-1 do
if I-J >= 0 then
Temp:= Temp + B(J)*Signal(I-J);
for J:= 1 to 4-1 do
if I-J >= 0 then
Temp:= Temp - A(J)*Result(I-J);
Result(I):= Temp / A(0);
RlOut(0, Result(I));
Text(0, if rem(I/5) = 4 then "^m^j" else ", ");
];
]</syntaxhighlight>
{{out}}
<pre>
-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
-0.43548245, -1.08923946, -0.53767655, 0.51704999, 1.05224975
0.96185430, 0.69569009, 0.42435630, 0.19626223, -0.02783512
-0.21172192, -0.17474556, 0.06925841, 0.38544587, 0.65177084
</pre>
 
=={{header|Yabasic}}==
{{trans|D}}
<langsyntaxhighlight Yabasiclang="yabasic">sub filter(a(), b(), signal(), result())
local i, j, tmp
Line 1,027 ⟶ 2,023:
print
end if
next</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|C++}}
<langsyntaxhighlight lang="zkl">fcn direct_form_II_transposed_filter(b,a,signal){
out:=List.createLong(signal.len(),0.0); // vector of zeros
foreach i in (signal.len()){
Line 1,040 ⟶ 2,036:
}
out
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">signal:=T(-0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412,
-0.662370894973,-1.00700480494, -0.404707073677, 0.800482325044,
0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195,
Line 1,049 ⟶ 2,045:
b:=T(0.16666667, 0.5, 0.5, 0.16666667 );
result:=direct_form_II_transposed_filter(b,a,signal);
println(result);</langsyntaxhighlight>
{{out}}
<pre>
9,482

edits