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

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 2 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}}==
Line 110 ⟶ 114:
=={{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 #
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
BEGIN
print( ( "Array lower bounds must be equal for filter", newline ) );
stop
BEGINELSE
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
IF i >= j THEN tmp +:= b[ j ] * signal[ LWB signal + ( i - j ) ] FI
OD;
FOR j FROM LWB a + 1 TO IF i > UPB a THEN UPB a ELSE i FI DO
IF i >= j THEN tmp -:= a[ j ] * result[ LWB result + ( i - j ) ] FI
OD;
result[ i ] := tmp / a[ LWB a ]
OD
ENDFI # 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 );
Line 141 ⟶ 151:
IF i MOD 5 /= 0 THEN print( ( ", " ) ) ELSE print( ( newline ) ) FI
OD
END
END</syntaxhighlight>
{{out}}
<pre>
Line 149 ⟶ 160:
-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.
Line 1,887 ⟶ 1,899:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var filter = Fn.new { |a, b, signal|
9,485

edits