You are on page 1of 5

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];
}
}
}

void filter(const int *x, int *y, int n)

static float x_2 = 0.0f; // delayed x, y samples

static float x_1 = 0.0f;

static float y_1 = 0.0f;

for (i = 0; i < n; ++i)


{

y[i] = a0 * x[i] + a1 * x_1 + a2 * x_2 // IIR difference equation

+ b1 * y_1 + b2 * y_2;

x_2 = x_1; // shift delayed x, y samples

x_1 = x[i];

y_1 = y[i];

Another approach is using the coefficients as was suggested above.Once you have those you can
use the following formula:

output[i] = A[0] * in[2] + A[1] * in[1] + A[2] * in[0] - B[1] * out[1] - B[2] * out[0];

Where A/B are you coefficients, in[0,1,2] are 3 points of you original file and out [0,1,2] is the filtered
one. Of-course for the first few points there are no values of out[0,1] and you should erase them
after filtration.

By running this formula and shifting both in and out at each point you will result in your filtered signal.

A Quick and Dirty Real-time Digital


(IIR/FIR) Filter Implementation
May 31st, 2013 | Comments

Introduction
You have this Data Acquisition System that coughs out data in real time, and you want to add a
digital filter that takes each filter sample, throws out the filtered output. Thats the task of this
post.
Lets say the digital data is over your computers serial port, and you have set up some Matlab
code to plot it already, now we need to filter each sample before storing it for plotting.

First of all you need to decide what kind of filter you want to use low pass/bandpass etc,
butterworth/elliptic etc, the filters order, cutoff frequency etc. And depending on all that use the
matlab functions like butter(), ellip() to get the coefficients of the filter you want to use. You
could calculate the coefficients by any other method also. This post assumes that you already
have the filter coefficients and you just need some simple code to implement the filter.

Now that you have the coefficients usually denoted by the vectors a and b for the denominator
and numerator respectively, lets look at the general form of an IIR filter

H(z) = P i=0 b i z i 1+ Q j=1 a j z j

Make sure that in your coefficients, a(0)=1 .

Use the following code to implement the above filter:

1 [b_lp,a_lp] = butter(8,40/(Fs/2),'low');
2 global dbuffer ;
3 dbuffer = zeros(length(b_lp),1); % buffer for LPF
4 %....... Read the ADC value from the serialport...
5 % Pass it to the filter subfunction
6 filtered_value = NewFilter(b_lp, a_lp, value);
7 %..... Plot data....
8
9 % NewFilter subfunction
10 function [data] = NewFilter(b, a,value)
11 k = 1;
12 global dbuffer ;
13 while(k<length(b))
14 dbuffer(k) = dbuffer(k+1);
15 k=k+1;
16 end
17 dbuffer(length(b)) = 0;
18 k = 1;
19 while(k<(length(b)+1))
20 dbuffer(k) = dbuffer(k) + value * b(k);
21 k=k+1;
22 end
23
24 k = 1;
25 while(k<length(b))
26 dbuffer(k+1) = dbuffer(k+1) - dbuffer(1) * a(k+1);
27 k=k+1;
28 end
29
30 data = dbuffer(1);

Thats all to it! Now consider an FIR filter, this is easier because now we do not have the vector
a , so just delete the Multiply and accumulate loop for the a terms in the above code.
A moving average filter can also be implemented in a similar fashion. The code looks like this:

1 function [data] = MavgFilter(value)


2
3 k = 1;
4 global Dbuffer ;
5 global N;
6 % 1. shift by one element (discard the left most element)
7 while(k<N)
8 Dbuffer(k) = Dbuffer(k+1);
9 k=k+1;
10 end
11 Dbuffer(N) = 0;
12
13 k = 1;
14 while(k<N+1)
15 Dbuffer(k) = Dbuffer(k) + value;
16 k=k+1;
17 end
18
19 data = Dbuffer(1)/N;

Obviously, the same code can be used in any other language like C, or C# if you want to do
analysis using them. And also note that this is not the most efficient implementation, but its easy
to understand and quick to implement, so its always good for a first try.

You might also like