Apply a digital filter (direct form II transposed): Difference between revisions
Apply a digital filter (direct form II transposed) (view source)
Revision as of 01:04, 26 August 2022
, 1 year agosyntax highlighting fixup automation
(Added Algol 68) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12:
{{trans|Nim}}
<
V result = [0.0] * signal.len
L(i) 0 .< signal.len
Line 36:
L(r) result
print(‘#2.8’.format(r), end' ‘’)
print(I (L.index + 1) % 5 != 0 {‘, ’} E "\n", end' ‘’)</
{{out}}
Line 47:
=={{header|Ada}}==
<
procedure Apply_Filter is
Line 106:
Ada.Text_IO.New_Line;
end loop;
end Apply_Filter;</
=={{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.
<
PROC filter = ( []REAL a, b, signal, REF[]REAL result )VOID:
BEGIN
Line 141:
IF i MOD 5 /= 0 THEN print( ( ", " ) ) ELSE print( ( newline ) ) FI
OD
END</
{{out}}
<pre>
Line 151:
=={{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.
<
if (b < a) then return b
return a
Line 185:
0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293, ¬
0.025930339848, 0.490105989562, 0.549391221511, 0.9047198589}
DF2TFilter(acoef, bcoef, signal)</
{{output}}
<
=={{header|C}}==
Given the number of values a coefficient or signal vector can have and the number of digits, this implementation reads data from a file and prints it to the console if no output file is specified or writes to the specified output file. Usage printed on incorrect invocation.
<syntaxhighlight lang=C>
#include<stdlib.h>
#include<string.h>
Line 306:
return 0;
}
</syntaxhighlight>
Input file, 3 lines containing first ( a ) and second ( b ) coefficient followed by the signal, all values should be separated by a single space:
<pre>
Line 325:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace ApplyDigitalFilter {
Line 366:
}
}
}</
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
Line 377:
This uses the C++11 method of initializing vectors. In g++, use the -std=c++0x compiler switch.
<
#include <iostream>
using namespace std;
Line 428:
return 0;
}</
{{out}}
Line 435:
=={{header|Common Lisp}}==
{{trans|zkl}}
<
(defparameter b #(0.16666667L0 0.50000000L0 0.50000000L0 0.16666667L0))
(defparameter s #(-0.917843918645 0.141984778794 1.20536903482 0.190286794412 -0.662370894973
Line 450:
when (>= i j) sum (* (svref a j) (svref out (- i j)))))
(svref a 0)))
(format t "~%~16,8F" (svref out i)))</
{{out}}
<pre> -0.15297399
Line 476:
=={{header|D}}==
{{trans|Kotlin}}
<
alias T = real;
Line 522:
}
}
}</
{{out}}
Line 532:
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<
Dim As Integer j, k
Dim As Double tmp
Line 579:
Data 0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589
Sleep</
{{out}}
<pre>
Line 589:
=={{header|Go}}==
<
import "fmt"
Line 639:
fmt.Printf("%9.6f\n", v)
}
}</
{{out}}
<pre>
Line 666:
=={{header|Groovy}}==
{{trans|Java}}
<
private static double[] filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.length]
Line 702:
}
}
}</
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
Line 711:
=={{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".
<
-- lazy convolution of a list by given kernel
Line 724:
dFilter (a0:a) b s = tail res
where
res = (/ a0) <$> 0 : zipWith (-) (conv b s) (conv a res)</
=== Examples ===
Line 753:
There's probably a nicer way to do this:
<
t=. (#n) +/ .*&(|.n)\(}.n*0),y
A=.|.}.m
Line 780:
_0.435482 _1.08924 _0.537677 0.51705 1.05225
0.961854 0.69569 0.424356 0.196262 _0.0278351
_0.211722 _0.174746 0.0692584 0.385446 0.651771</
=={{header|Java}}==
{{trans|Kotlin}}
<
private static double[] filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.length];
Line 821:
}
}
}</
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
Line 830:
=={{header|Julia}}==
{{trans|zkl}}
<
rst = zeros(sig)
for i in eachindex(sig)
Line 847:
0.400833448236, -0.2085993586, -0.172842103641, -0.134316096293,
0.0259303398477, 0.490105989562, 0.549391221511, 0.9047198589]
@show DF2TFilter(acoef, bcoef, signal)</
{{output}}<pre>DF2TFilter(acoef, bcoef, signal) = [-0.152974, -0.435258, -0.136043, 0.697503, 0.656445, -0.435482, -1.08924, -0.537677, 0.51705, 1.05225, 0.961854, 0.69569, 0.424356, 0.196262, -0.0278351, -0.211722, -0.174746, 0.0692584, 0.385446, 0.651771]</pre>
=={{header|Kotlin}}==
{{trans|C++}}
<
fun filter(a: DoubleArray, b: DoubleArray, signal: DoubleArray): DoubleArray {
Line 889:
print(if ((i + 1) % 5 != 0) ", " else "\n")
}
}</
{{out}}
Line 901:
=={{header|Lua}}==
{{trans|C++}}
<
local out = {}
for i=1,table.getn(input) do
Line 951:
end
main()</
{{out}}
<pre>-0.15297398950031, -0.43525782905022, -0.13604339698849, 0.69750332654796, 0.65644469246903, -0.43548245325611, -1.0892394611529, -0.53767654956275, 0.51704999231321, 1.0522497471554, 0.96185430037364, 0.6956900940096, 0.42435629509553, 0.19626223182179, -0.027835124463393, -0.21172191545012, -0.17474556222276, 0.069258408901195, 0.38544587430744, 0.65177083881931,</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
a = {1.00000000, -2.77555756*^-16, 3.33333333*^-01, -1.85037171*^-17};
signal = {-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};
RecurrenceFilter[{a, b}, signal]</
{{out}}
<pre>{-0.152974,-0.435258,-0.136043,0.697503,0.656445,-0.435482,-1.08924,-0.537677,0.51705,1.05225,0.961854,0.69569,0.424356,0.196262,-0.0278351,-0.211722,-0.174746,0.0692584,0.385446,0.651771}</pre>
Line 965:
=={{header|MATLAB}}==
MATLAB is commonly used for filter design and implementation. To implement this filter, and display the original signal and the filtered result:
<
signal = [-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];
a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
Line 982:
xlabel('n')
title('Filtered Signal')
</syntaxhighlight>
{{out}}
Line 999:
=={{header|Nim}}==
{{trans|Kotlin}}
<
import strformat
Line 1,029:
for i in 0..result.high:
stdout.write fmt"{result[i]: .8f}"
stdout.write if (i + 1) mod 5 != 0: ", " else: "\n"</
{{out}}
Line 1,039:
=={{header|Objeck}}==
{{trans|Java}}
<
function : Main(args : String[]) ~ Nil {
a := [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
Line 1,079:
return result;
}
}</
{{output}}
Line 1,090:
=={{header|ooRexx}}==
<
a=.array~of(1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17)
b=.array~of(0.16666667, 0.5, 0.5, 0.16666667)
Line 1,126:
::OPTIONS digits 24 /* Numeric Digits 24, everywhere */
</syntaxhighlight>
{{out|output}}
<pre> 1 -0.152973989500
Line 1,151:
=={{header|Perl}}==
{{trans|Raku}}
<
use List::AllUtils 'natatime';
Line 1,182:
printf(' %10.6f' x 5 . "\n", @values);
}
</syntaxhighlight>
{{out}}
<pre> -0.152974 -0.435258 -0.136043 0.697503 0.656445
Line 1,193:
Note however that the a[j]* starts from index 2, unlike Julia/C/Raku/Rust/Sidef/zkl,
but the same as C++/C#/D/Java/Kotlin - and it does not seem to make any difference...
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">direct_form_II_transposed_filter</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">signal</span><span style="color: #0000FF;">)</span>
Line 1,214:
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">direct_form_II_transposed_filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">acoef</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bcoef</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">signal</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_FltFmt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%9.6f"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_Maxlen</span><span style="color: #0000FF;">,</span><span style="color: #000000;">110</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,223:
=={{header|Phixmonti}}==
{{trans|Phix}}
<
( 1.00000000 -2.77555756e-16 3.33333333e-01 -1.85037171e-17 ) var a
Line 1,246:
ps> a 1 get nip / ps> swap i set >ps
endfor
drop ps> ?</
{{out}}
<pre>[-0.152973989500313, -0.435257829050217, -0.13604339698849, 0.697503326547963, 0.656444692469029, -0.435482453256106, -1.089239461152929, -0.537676549562755, 0.517049992313214, 1.052249747155353, 0.961854300373645, 0.695690094009605, 0.424356295095532, 0.196262231821789, -0.0278351244633933, -0.211721915450118, -0.174745562222761, 0.0692584089011949, 0.385445874307439, 0.651770838819305]
Line 1,254:
=={{header|Python}}==
<
from __future__ import print_function
from scipy import signal
Line 1,276:
plt.plot(sig, 'b')
plt.plot(filt, 'r--')
plt.show()</
{{out}}
Line 1,287:
{{trans|C}} Strangely, C was more informative than Common Lisp in helping figure out what was going on here.
<
(define a (vector 1.00000000E0 -2.77555756E-16 3.33333333E-01 -1.85037171E-17))
Line 1,309:
filtered-signal)
(filter-signal-direct-form-ii-transposed a b s)</
{{out}}
Line 1,338:
{{trans|zkl}}
<
my @out = 0 xx @signal;
for ^@signal -> $i {
Line 1,360:
say TDF-II-filter(@signal, @a, @b)».fmt("% 0.8f")
Z~ flat (', ' xx 4, ",\n") xx *;</
{{out}}
<pre>(-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469,
Line 1,371:
===version 1===
{{trans|Julia}}
<
@a= '1 -2.77555756e-16 3.33333333e-1 -1.85037171e-17' /*filter coefficients*/
@b= 0.16666667 0.5 0.5 0.16666667 /* " " */
Line 1,386:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: numeric digits digits()%2; say right(i, w) " " left('', $.i>=0)$.i /1; return</
{{out|output}}
<pre>
Line 1,413:
===version 2===
{{trans|Julia}}
<
Numeric Digits 24
acoef = '1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17'
Line 1,444:
ret.i=temp/a.1
Say format(i,2) format(ret.i,2,12)
End</
{{out|output}}
<pre> 1 -0.152973989500
Line 1,470:
=={{header|Ruby}}==
{{trans|C#}}
<
result = Array.new(signal.length(), 0.0)
for i in 0..signal.length()-1 do
Line 1,510:
end
main()</
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
Line 1,519:
=={{header|Rust}}==
{{trans|Java}}
<
struct IIRFilter<'f>(&'f [f32], &'f [f32]);
Line 1,620:
}
println!();
}</
{{out|output}}
<pre>
Line 1,636:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<
private def filter(a: Vector[Double],
b: Vector[Double],
Line 1,671:
.foreach(line => println(line.mkString(" ")))
}</
=={{header|Sidef}}==
{{trans|Raku}}
<
var out = [0]*signal.len
for i in ^signal {
Line 1,700:
say "["
say f.map { "% 0.8f" % _ }.slices(5).map{.join(', ')}.join(",\n")
say "]"</
{{out}}
<pre>
Line 1,712:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Function Filter(a As Double(), b As Double(), signal As Double()) As Double()
Line 1,755:
End Sub
End Module</
{{out}}
<pre>-0.15297399, -0.43525783, -0.13604340, 0.69750333, 0.65644469
Line 1,764:
=={{header|Vlang}}==
{{trans|Go}}
<
b []f64
a []f64
Line 1,811:
println("${v:9.6}")
}
}</
{{out}}
Line 1,840:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var filter = Fn.new { |a, b, signal|
Line 1,875:
Fmt.write("$11.8f", result[i])
System.write(((i + 1) % 5 != 0) ? ", " : "\n")
}</
{{out}}
Line 1,887:
=={{header|Yabasic}}==
{{trans|D}}
<
local i, j, tmp
Line 1,931:
print
end if
next</
=={{header|zkl}}==
{{trans|C++}}
<
out:=List.createLong(signal.len(),0.0); // vector of zeros
foreach i in (signal.len()){
Line 1,944:
}
out
}</
<
-0.662370894973,-1.00700480494, -0.404707073677, 0.800482325044,
0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195,
Line 1,953:
b:=T(0.16666667, 0.5, 0.5, 0.16666667 );
result:=direct_form_II_transposed_filter(b,a,signal);
println(result);</
{{out}}
<pre>
|