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

(Added AppleScript.)
Line 667:
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".
<lang Haskell>
import Data.List (inits, 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)
</lang>
 
'''Example'''
<pre>
λ> take 10 $ conv [1,10,100,1000] [1..]
[1,12,123,1234,2345,3456,4567,5678,6789,7900]
 
λ> 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>
 
=={{header|Java}}==
Anonymous user