Apply a digital filter (direct form II transposed): Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: minor details) |
|||
Line 670: | Line 670: | ||
=={{header|Haskell}}== |
=={{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". |
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> |
<lang Haskell>import Data.List (tails) |
||
import Data.List (inits, tails) |
|||
-- lazy convolution of a list by given kernel |
-- lazy convolution of a list by given kernel |
||
Line 684: | Line 683: | ||
dFilter (a0:a) b s = tail res |
dFilter (a0:a) b s = tail res |
||
where |
where |
||
res = (/ a0) <$> 0 : zipWith (-) (conv b s) (conv a res) |
res = (/ a0) <$> 0 : zipWith (-) (conv b s) (conv a res)</lang> |
||
⚫ | |||
=== Examples === |
|||
'''Example''' |
|||
<pre> |
|||
Demonstration of convolution: |
|||
λ> take 10 $ conv [1,10,100,1000] [1..] |
<pre>λ> take 10 $ conv [1,10,100,1000] [1..] |
||
[1,12,123,1234,2345,3456,4567,5678,6789,7900] |
[1,12,123,1234,2345,3456,4567,5678,6789,7900] |
||
⚫ | |||
The given task: |
|||
λ> let a = [1, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17] |
<pre>λ> let a = [1, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17] |
||
λ> let b = [0.16666667, 0.5, 0.5, 0.16666667] |
λ> 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] |
λ> 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] |
||
Line 703: | Line 704: | ||
0.6802579154588558,0.326668188810626,-7.596599209379973e-2,-0.10888939616131928] |
0.6802579154588558,0.326668188810626,-7.596599209379973e-2,-0.10888939616131928] |
||
</pre> |
</pre> |
||
The last line is redundant and appears due to the finiteness of a signal stream. The digital filter is able to handle infinite lists (as streams): |
|||
<pre>λ> take 10 $ dFilter a b $ cycle [1,-1] |
|||
[0.16666667,0.33333333000000004,0.11111111338888897,-0.11111110988888885,-3.703703775925934e-2,3.70370365925926e-2,1.2345679240740749e-2,-1.2345678851851824e-2,-4.1152264094650535e-3,4.115226279835409e-3]</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |