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

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 12: Line 12:
{{trans|Nim}}
{{trans|Nim}}


<syntaxhighlight lang=11l>F apply_filter(a, b, signal)
<syntaxhighlight lang="11l">F apply_filter(a, b, signal)
V result = [0.0] * signal.len
V result = [0.0] * signal.len
L(i) 0 .< signal.len
L(i) 0 .< signal.len
Line 47: Line 47:


=={{header|Ada}}==
=={{header|Ada}}==
<syntaxhighlight lang=Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Apply_Filter is
procedure Apply_Filter is
Line 111: Line 111:
{{Trans|C++}} ... via Yabasic<br>
{{Trans|C++}} ... via Yabasic<br>
The default lower bound in Algol 68 arrays is 1, so the loops/subscripts have been adjusted accordingly.
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 #
<syntaxhighlight lang="algol68">BEGIN # apply a digital filter #
PROC filter = ( []REAL a, b, signal, REF[]REAL result )VOID:
PROC filter = ( []REAL a, b, signal, REF[]REAL result )VOID:
BEGIN
BEGIN
Line 151: Line 151:
=={{header|AppleScript}}==
=={{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.
{{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.
<syntaxhighlight lang=applescript>on min(a, b)
<syntaxhighlight lang="applescript">on min(a, b)
if (b < a) then return b
if (b < a) then return b
return a
return a
Line 188: Line 188:


{{output}}
{{output}}
<syntaxhighlight 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}</syntaxhighlight>
<syntaxhighlight 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}</syntaxhighlight>


=={{header|C}}==
=={{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.
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>
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<stdlib.h>
#include<string.h>
#include<string.h>
Line 325: Line 325:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;


namespace ApplyDigitalFilter {
namespace ApplyDigitalFilter {
Line 377: Line 377:
This uses the C++11 method of initializing vectors. In g++, use the -std=c++0x compiler switch.
This uses the C++11 method of initializing vectors. In g++, use the -std=c++0x compiler switch.


<syntaxhighlight lang=cpp>#include <vector>
<syntaxhighlight lang="cpp">#include <vector>
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
Line 435: Line 435:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
{{trans|zkl}}
{{trans|zkl}}
<syntaxhighlight lang=lisp>(defparameter a #(1.00000000L0 -2.77555756L-16 3.33333333L-01 -1.85037171L-17))
<syntaxhighlight lang="lisp">(defparameter a #(1.00000000L0 -2.77555756L-16 3.33333333L-01 -1.85037171L-17))
(defparameter b #(0.16666667L0 0.50000000L0 0.50000000L0 0.16666667L0))
(defparameter b #(0.16666667L0 0.50000000L0 0.50000000L0 0.16666667L0))
(defparameter s #(-0.917843918645 0.141984778794 1.20536903482 0.190286794412 -0.662370894973
(defparameter s #(-0.917843918645 0.141984778794 1.20536903482 0.190286794412 -0.662370894973
Line 476: Line 476:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=D>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


alias T = real;
alias T = real;
Line 532: Line 532:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
{{trans|Yabasic}}
<syntaxhighlight lang=freebasic>Sub Filtro(a() As Double, b() As Double, senal() As Double, resultado() As Double)
<syntaxhighlight lang="freebasic">Sub Filtro(a() As Double, b() As Double, senal() As Double, resultado() As Double)
Dim As Integer j, k
Dim As Integer j, k
Dim As Double tmp
Dim As Double tmp
Line 589: Line 589:


=={{header|Go}}==
=={{header|Go}}==
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 666: Line 666:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=groovy>class DigitalFilter {
<syntaxhighlight lang="groovy">class DigitalFilter {
private static double[] filter(double[] a, double[] b, double[] signal) {
private static double[] filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.length]
double[] result = new double[signal.length]
Line 711: Line 711:
=={{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".
<syntaxhighlight lang=Haskell>import Data.List (tails)
<syntaxhighlight lang="haskell">import Data.List (tails)


-- lazy convolution of a list by given kernel
-- lazy convolution of a list by given kernel
Line 753: Line 753:
There's probably a nicer way to do this:
There's probably a nicer way to do this:


<syntaxhighlight lang=J>Butter=: {{
<syntaxhighlight lang="j">Butter=: {{
t=. (#n) +/ .*&(|.n)\(}.n*0),y
t=. (#n) +/ .*&(|.n)\(}.n*0),y
A=.|.}.m
A=.|.}.m
Line 784: Line 784:
=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=Java>public class DigitalFilter {
<syntaxhighlight lang="java">public class DigitalFilter {
private static double[] filter(double[] a, double[] b, double[] signal) {
private static double[] filter(double[] a, double[] b, double[] signal) {
double[] result = new double[signal.length];
double[] result = new double[signal.length];
Line 830: Line 830:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|zkl}}
{{trans|zkl}}
<syntaxhighlight lang=julia>function DF2TFilter(a::Vector, b::Vector, sig::Vector)
<syntaxhighlight lang="julia">function DF2TFilter(a::Vector, b::Vector, sig::Vector)
rst = zeros(sig)
rst = zeros(sig)
for i in eachindex(sig)
for i in eachindex(sig)
Line 852: Line 852:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


fun filter(a: DoubleArray, b: DoubleArray, signal: DoubleArray): DoubleArray {
fun filter(a: DoubleArray, b: DoubleArray, signal: DoubleArray): DoubleArray {
Line 901: Line 901:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=lua>function filter(b,a,input)
<syntaxhighlight lang="lua">function filter(b,a,input)
local out = {}
local out = {}
for i=1,table.getn(input) do
for i=1,table.getn(input) do
Line 956: Line 956:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>b = {0.16666667, 0.5, 0.5, 0.16666667};
<syntaxhighlight lang="mathematica">b = {0.16666667, 0.5, 0.5, 0.16666667};
a = {1.00000000, -2.77555756*^-16, 3.33333333*^-01, -1.85037171*^-17};
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};
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};
Line 965: Line 965:
=={{header|MATLAB}}==
=={{header|MATLAB}}==
MATLAB is commonly used for filter design and implementation. To implement this filter, and display the original signal and the filtered result:
MATLAB is commonly used for filter design and implementation. To implement this filter, and display the original signal and the filtered result:
<syntaxhighlight lang=MATLAB>
<syntaxhighlight lang="matlab">
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];
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];
a = [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
Line 999: Line 999:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=Nim>
<syntaxhighlight lang="nim">
import strformat
import strformat


Line 1,039: Line 1,039:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=objeck>class DigitalFilter {
<syntaxhighlight lang="objeck">class DigitalFilter {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
a := [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
a := [1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17];
Line 1,090: Line 1,090:


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<syntaxhighlight lang=oorexx>/* REXX */
<syntaxhighlight lang="oorexx">/* REXX */
a=.array~of(1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17)
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)
b=.array~of(0.16666667, 0.5, 0.5, 0.16666667)
Line 1,151: Line 1,151:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=perl>use strict;
<syntaxhighlight lang="perl">use strict;
use List::AllUtils 'natatime';
use List::AllUtils 'natatime';


Line 1,193: Line 1,193:
Note however that the a[j]* starts from index 2, unlike Julia/C/Raku/Rust/Sidef/zkl,
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...
but the same as C++/C#/D/Java/Kotlin - and it does not seem to make any difference...
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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,223: Line 1,223:
=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
{{trans|Phix}}
{{trans|Phix}}
<syntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


( 1.00000000 -2.77555756e-16 3.33333333e-01 -1.85037171e-17 ) var a
( 1.00000000 -2.77555756e-16 3.33333333e-01 -1.85037171e-17 ) var a
Line 1,254: Line 1,254:
=={{header|Python}}==
=={{header|Python}}==


<syntaxhighlight lang=python>#!/bin/python
<syntaxhighlight lang="python">#!/bin/python
from __future__ import print_function
from __future__ import print_function
from scipy import signal
from scipy import signal
Line 1,287: Line 1,287:
{{trans|C}} Strangely, C was more informative than Common Lisp in helping figure out what was going on here.
{{trans|C}} Strangely, C was more informative than Common Lisp in helping figure out what was going on here.


<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define a (vector 1.00000000E0 -2.77555756E-16 3.33333333E-01 -1.85037171E-17))
(define a (vector 1.00000000E0 -2.77555756E-16 3.33333333E-01 -1.85037171E-17))
Line 1,338: Line 1,338:
{{trans|zkl}}
{{trans|zkl}}


<syntaxhighlight lang=perl6>sub TDF-II-filter ( @signal, @a, @b ) {
<syntaxhighlight lang="raku" line>sub TDF-II-filter ( @signal, @a, @b ) {
my @out = 0 xx @signal;
my @out = 0 xx @signal;
for ^@signal -> $i {
for ^@signal -> $i {
Line 1,371: Line 1,371:
===version 1===
===version 1===
{{trans|Julia}}
{{trans|Julia}}
<syntaxhighlight lang=REXX>/*REXX pgm filters a signal with a order3 lowpass Butterworth, direct form II transposed*/
<syntaxhighlight lang="rexx">/*REXX pgm filters a signal with a order3 lowpass Butterworth, direct form II transposed*/
@a= '1 -2.77555756e-16 3.33333333e-1 -1.85037171e-17' /*filter coefficients*/
@a= '1 -2.77555756e-16 3.33333333e-1 -1.85037171e-17' /*filter coefficients*/
@b= 0.16666667 0.5 0.5 0.16666667 /* " " */
@b= 0.16666667 0.5 0.5 0.16666667 /* " " */
Line 1,413: Line 1,413:
===version 2===
===version 2===
{{trans|Julia}}
{{trans|Julia}}
<syntaxhighlight lang=REXX>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
Numeric Digits 24
Numeric Digits 24
acoef = '1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17'
acoef = '1.00000000, -2.77555756e-16, 3.33333333e-01, -1.85037171e-17'
Line 1,470: Line 1,470:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=ruby>def filter(a,b,signal)
<syntaxhighlight lang="ruby">def filter(a,b,signal)
result = Array.new(signal.length(), 0.0)
result = Array.new(signal.length(), 0.0)
for i in 0..signal.length()-1 do
for i in 0..signal.length()-1 do
Line 1,519: Line 1,519:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=Rust>use std::cmp::Ordering;
<syntaxhighlight lang="rust">use std::cmp::Ordering;


struct IIRFilter<'f>(&'f [f32], &'f [f32]);
struct IIRFilter<'f>(&'f [f32], &'f [f32]);
Line 1,636: Line 1,636:
{{libheader|Scastie qualified}}
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
{{works with|Scala|2.13}}
<syntaxhighlight lang=Scala>object ButterworthFilter extends App {
<syntaxhighlight lang="scala">object ButterworthFilter extends App {
private def filter(a: Vector[Double],
private def filter(a: Vector[Double],
b: Vector[Double],
b: Vector[Double],
Line 1,675: Line 1,675:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=ruby>func TDF_II_filter(signal, a, b) {
<syntaxhighlight lang="ruby">func TDF_II_filter(signal, a, b) {
var out = [0]*signal.len
var out = [0]*signal.len
for i in ^signal {
for i in ^signal {
Line 1,712: Line 1,712:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Function Filter(a As Double(), b As Double(), signal As Double()) As Double()
Function Filter(a As Double(), b As Double(), signal As Double()) As Double()
Line 1,764: Line 1,764:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=vlang>struct Filter {
<syntaxhighlight lang="vlang">struct Filter {
b []f64
b []f64
a []f64
a []f64
Line 1,840: Line 1,840:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var filter = Fn.new { |a, b, signal|
var filter = Fn.new { |a, b, signal|
Line 1,887: Line 1,887:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|D}}
{{trans|D}}
<syntaxhighlight lang=Yabasic>sub filter(a(), b(), signal(), result())
<syntaxhighlight lang="yabasic">sub filter(a(), b(), signal(), result())
local i, j, tmp
local i, j, tmp
Line 1,935: Line 1,935:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=zkl>fcn direct_form_II_transposed_filter(b,a,signal){
<syntaxhighlight lang="zkl">fcn direct_form_II_transposed_filter(b,a,signal){
out:=List.createLong(signal.len(),0.0); // vector of zeros
out:=List.createLong(signal.len(),0.0); // vector of zeros
foreach i in (signal.len()){
foreach i in (signal.len()){
Line 1,945: Line 1,945:
out
out
}</syntaxhighlight>
}</syntaxhighlight>
<syntaxhighlight lang=zkl>signal:=T(-0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412,
<syntaxhighlight lang="zkl">signal:=T(-0.917843918645, 0.141984778794, 1.20536903482, 0.190286794412,
-0.662370894973,-1.00700480494, -0.404707073677, 0.800482325044,
-0.662370894973,-1.00700480494, -0.404707073677, 0.800482325044,
0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195,
0.743500089861, 1.01090520172, 0.741527555207, 0.277841675195,