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

Added Algol 68
No edit summary
(Added Algol 68)
Line 108:
end Apply_Filter;</lang>
 
=={{header|ALGOL 68}}==
{{Trans|C++}} ... via Yabasic<br>
The default lower bound in Algol 68 arrays is 1, so the loops/subscripts have been adjusted accordingly.
<lang algol68>BEGIN # apply a digital filter #
PROC filter = ( []REAL a, b, signal, REF[]REAL result )VOID:
BEGIN
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 UPB b DO
IF i >= j THEN tmp +:= b[ j ] * signal[ LWB signal + ( i - j ) ] FI
OD;
FOR j FROM LWB a TO UPB a DO
IF i >= j THEN tmp -:= a[ j ] * result[ LWB result + ( i - j ) ] FI
OD;
result[ i ] := tmp / a[ LWB a ]
OD
END # 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</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|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.
3,032

edits