a quick and dirty real.docx

5
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. That’s the task of this post. Let’s say the digital data is over your computer’s 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.

Upload: paromarye1

Post on 20-Jul-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A Quick and Dirty Real.docx

A Quick and Dirty Real-time Digital (IIR/FIR) Filter ImplementationMAY 31ST, 2013 | COMMENTSIntroductionYou 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. That’s the task of this post.

Let’s say the digital data is over your computer’s 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, let’s look at the general form of an IIR filter

H(z)=∑Pi=0biz−i1+∑Qj=1ajz−j

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

Page 2: A Quick and Dirty Real.docx

Use the following code to implement the above filter:

12345678910111213141516171819202122

[b_lp,a_lp] = butter(8,40/(Fs/2),'low');global dbuffer ;dbuffer = zeros(length(b_lp),1); % buffer for LPF%....... Read the ADC value from the serialport...% Pass it to the filter subfunctionfiltered_value = NewFilter(b_lp, a_lp, value);%..... Plot data....

% NewFilter subfunctionfunction [data] = NewFilter(b, a,value)k = 1;global dbuffer ;while(k<length(b))dbuffer(k) = dbuffer(k+1);k=k+1;enddbuffer(length(b)) = 0;k = 1;while(k<(length(b)+1))dbuffer(k) = dbuffer(k) + value * b(k);k=k+1;end

k = 1;while(k<length(b))dbuffer(k+1) = dbuffer(k+1) - dbuffer(1) * a(k+1);k=k+1;end

data = dbuffer(1);

Page 3: A Quick and Dirty Real.docx

2324252627282930

That’s 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:

1234567891011

function [data] = MavgFilter(value) k = 1;global Dbuffer ;global N;% 1. shift by one element (discard the left most element)while(k<N)Dbuffer(k) = Dbuffer(k+1);k=k+1;endDbuffer(N) = 0;

k = 1;

Page 4: A Quick and Dirty Real.docx

1213141516171819

while(k<N+1)Dbuffer(k) = Dbuffer(k) + value;k=k+1;end

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 it’s easy to understand and quick to implement, so it’s always good for a first try.

http://www.desultoryquest.com/blog/a-quick-and-dirty-real-time-digital-iir-slash-fir-filter-implementation/