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

Added AppleScript.
(Added 11l)
(Added AppleScript.)
Line 107:
end loop;
end Apply_Filter;</lang>
 
=={{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.
<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)</lang>
 
{{output}}
<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}</lang>
 
=={{header|C}}==
557

edits