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

m
moved RUST to proper location.
m (→‎version 1: changed some comments and whitespace.)
m (moved RUST to proper location.)
Line 612:
0.4243563 0.19626223 -0.02783512 -0.21172192 -0.17474556 0.06925841
0.38544587 0.65177084]</pre>
 
=={{header|REXX}}==
===version 1===
{{trans|Julia}}
<lang 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 /* " " */
@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 '
$.=0; N=words(@s); w=length(n) /* [↑] signal vector*/
do i=1 for N /*process each of the vector elements. */
#=0 /*temp variable used in calculations. */
do j=1 for words(@b); if i-j >= 0 then #= # + word(@b, j) * word(@s, i-j+1)
end /*j*/ /* [↑] process all the B coefficients.*/
 
do k=1 for words(@a); _=i - k + 1; if i-k >= 0 then #=# - word(@a, k) * $._
end /*k*/ /* [↑] process all the A coefficients.*/
$.i= # / word(@a ,1); call tell /*only display using half the dec digs.*/
end /*i*/
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</lang>
{{out|output}}
<pre>
1 -0.1529739895
2 -0.43525782905
3 -0.136043396988
4 0.697503326548
5 0.656444692469
6 -0.435482453256
7 -1.08923946115
8 -0.537676549563
9 0.517049992313
10 1.05224974716
11 0.961854300374
12 0.69569009401
13 0.424356295096
14 0.196262231822
15 -0.0278351244634
16 -0.21172191545
17 -0.174745562223
18 0.0692584089012
19 0.385445874307
20 0.651770838819
</pre>
 
===version 2===
{{trans|Julia}}
<lang REXX>/* REXX */
Numeric Digits 24
acoef = '1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17'
bcoef = '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'
 
Do i=1 By 1 While acoef>''; Parse Var acoef a.i . ',' acoef; End; a.0=i-1
Do i=1 By 1 While bcoef>''; Parse Var bcoef b.i . ',' bcoef; End; b.0=i-1
Do i=1 By 1 While signal>''; Parse Var signal s.i . ',' signal; End; s.0=i-1
 
ret.=0
Do i=1 To s.0
temp=0.0
Do j=1 To b.0
if i-j>=0 Then Do
u=i-j+1
temp=temp+b.j*s.u
End
End
Do j=1 To a.0
if i-j>=0 Then Do
u=i-j+1
temp=temp-a.j*ret.u
End
End
ret.i=temp/a.1
Say format(i,2) format(ret.i,2,12)
End</lang>
{{out|output}}
<pre> 1 -0.152973989500
2 -0.435257829050
3 -0.136043396988
4 0.697503326548
5 0.656444692469
6 -0.435482453256
7 -1.089239461153
8 -0.537676549563
9 0.517049992313
10 1.052249747155
11 0.961854300374
12 0.695690094010
13 0.424356295096
14 0.196262231822
15 -0.027835124463
16 -0.211721915450
17 -0.174745562223
18 0.069258408901
19 0.385445874307
20 0.651770838819
</pre>
 
=={{header|Rust}}==
===version 1===
{{trans|Java}}
<lang Rust>use std::cmp::Ordering;
Line 724 ⟶ 828:
0.96185434, 0.69568992, 0.42435625, 0.19626230, -0.02783510
-0.21172196, -0.17474557, 0.06925842, 0.38544586, 0.65177077
</pre>
 
=={{header|REXX}}==
===version 1===
{{trans|Julia}}
<lang 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 /* " " */
@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 '
$.=0; N=words(@s); w=length(n) /* [↑] signal vector*/
do i=1 for N /*process each of the vector elements. */
#=0 /*temp variable used in calculations. */
do j=1 for words(@b); if i-j >= 0 then #= # + word(@b, j) * word(@s, i-j+1)
end /*j*/ /* [↑] process all the B coefficients.*/
 
do k=1 for words(@a); _=i - k + 1; if i-k >= 0 then #=# - word(@a, k) * $._
end /*k*/ /* [↑] process all the A coefficients.*/
$.i= # / word(@a ,1); call tell /*only display using half the dec digs.*/
end /*i*/
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</lang>
{{out|output}}
<pre>
1 -0.1529739895
2 -0.43525782905
3 -0.136043396988
4 0.697503326548
5 0.656444692469
6 -0.435482453256
7 -1.08923946115
8 -0.537676549563
9 0.517049992313
10 1.05224974716
11 0.961854300374
12 0.69569009401
13 0.424356295096
14 0.196262231822
15 -0.0278351244634
16 -0.21172191545
17 -0.174745562223
18 0.0692584089012
19 0.385445874307
20 0.651770838819
</pre>
 
===version 2===
{{trans|Julia}}
<lang REXX>/* REXX */
Numeric Digits 24
acoef = '1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17'
bcoef = '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'
 
Do i=1 By 1 While acoef>''; Parse Var acoef a.i . ',' acoef; End; a.0=i-1
Do i=1 By 1 While bcoef>''; Parse Var bcoef b.i . ',' bcoef; End; b.0=i-1
Do i=1 By 1 While signal>''; Parse Var signal s.i . ',' signal; End; s.0=i-1
 
ret.=0
Do i=1 To s.0
temp=0.0
Do j=1 To b.0
if i-j>=0 Then Do
u=i-j+1
temp=temp+b.j*s.u
End
End
Do j=1 To a.0
if i-j>=0 Then Do
u=i-j+1
temp=temp-a.j*ret.u
End
End
ret.i=temp/a.1
Say format(i,2) format(ret.i,2,12)
End</lang>
{{out|output}}
<pre> 1 -0.152973989500
2 -0.435257829050
3 -0.136043396988
4 0.697503326548
5 0.656444692469
6 -0.435482453256
7 -1.089239461153
8 -0.537676549563
9 0.517049992313
10 1.052249747155
11 0.961854300374
12 0.695690094010
13 0.424356295096
14 0.196262231822
15 -0.027835124463
16 -0.211721915450
17 -0.174745562223
18 0.069258408901
19 0.385445874307
20 0.651770838819
</pre>