This is an c# implementation of the matlab function Filter, which is a digital infinite impulse response IIR filter. The code is fairly similar to the C++ version, which I use as an reference. Using the class below is much easier than use signal processing library. The default coefficient was the taken from Matlab documentation.
class Filter
{
private List<double> a;
private List<double> b;
//default Filter
public Filter()
{
b = new List<double>();
b.Add(0.002899695497431);
b.Add(-0.006626465760968);
b.Add(0.004033620976099);
b.Add(0.004033620976099);
b.Add(-0.006626465760968);
b.Add(0.002899695497431);
a = new List<double>();
a.Add(1.000000000000000);
a.Add(-4.229081817661462);
a.Add(7.205853343227314);
a.Add(-6.177477993982333);
a.Add(2.662714482809827);
a.Add(-0.461394312968222);
}
public Filter(List<double> a, List<double> b)
{
this.a = a;
this.b = b;
}
public void Applyfilter(List<double> x, out List<double> y)
{
int ord = a.Count -1;
int np = x.Count -1;
if (np < ord)
{
for(int k=0;k<ord-np;k++)
x.Add(0.0);
np = ord;
}
y = new List<double>();
for(int k=0;k<np+1;k++)
{
y.Add(0.0);
}
int i, j;
y[0] = b[0] * x[0];
for (i = 1; i < ord + 1; i++)
{
y[i] = 0.0;
for (j = 0; j < i + 1; j++)
y[i] = y[i] + b[j] * x[i - j];
for (j = 0; j < i; j++)
y[i] = y[i] - a[j + 1] * y[i - j - 1];
}
/* end of initial part */
for (i = ord + 1; i < np +1; i++)
{
y[i] = 0.0;
for (j = 0; j < ord + 1; j++)
y[i] = y[i] + b[j] * x[i - j];
for (j = 0; j < ord; j++)
y[i] = y[i] - a[j + 1] * y[i - j - 1];
}
}
}
5 comments:
It not work... :(
Is this working ??
Kill yourself with a lava bucket. This article is as useful as an asshole in the elbow
13 years later... I've tried it and it works just fine in my application. Thanks.
Post a Comment