Software filter algorithm

A

Thread Starter

Anonymous

I have a serial interface between my PC and a servo motor controller. I am using VB as the GUI, and am reading the actual velocity at a 1Hz rate. The problem is that the velocity reading jumps +/- 5rpm (the controller I have to use doesn't really have velocity feedback; I am just reading the encoder position, waiting x seconds, reading the encoder position a second time, and taking the difference; so there is some inaccuracy). I would like to decrease this fluctuation in the reading. I have tried an averaging equation, but it doesn't decrease the fluctuation enough. If I increase the number of samples in the averaging equation it takes too long to get the result.

I know there is a standard software filter algorithm, but I can't remember what it is. Any help would be appreciated!

Thanks.
 
J
To implement a first order filter in software
do this...

a=0.1

do
read new data x
x1 = x0 + a*(x-x0)
x0 = x1
loop


x is the raw data
x1 is the filtered value
x0 is the previous filtered value
a is the filter factor (alpha)

You need to choose a value of a to suit your application. A value of 1 will give no filtering.
A value of 0.1 will filter the data a lot but will also put in a lot of lag into the value (slow response time).

Jamie.
 
P

Pasi Airikka

Hello,

If you just want to get rid of noise in your measurements (those velocity reading jumps), the low-pass filter should do fine. You mentioned that averaging takes too long to get the result. It seems that you are not applying a recursive filtering in that case? Because filtering is not about how long it takes to get the result but rather how many samples to use in filtering.

An exponentially weighted moving average (EWMA) filter in time domain is as follows:

xf(k) = a * xf(k-1) + (1-a) * x(k)

where k = 1,2,3,... is time instant and k-1 is preceding time instant, x is the signal to be filtered (in your case, velocity) and xf is the EWMA filtered signal. The parameter a = (N-1)/N where N is a number of signal samples used in averaging. N is a design parameter by which you can put more or less weight on filtering. The greater N is, the less weight the new value of x has and therefore, the signal is more filtered.

Note, however, that this filtering equation is totally recursive! All you need to do at every sampling (and filtering) instant is two multiplications and one addition! Parameters a and (1-a) can be calculated beforehand so that you do not need to calculate them at every sampling period.

EWMA filter is exactly similar to low-pass filter. In other words, the high frequencies are filtered out. Therefore, by setting the parameter a you define your bandwidth. The greater a is (a must be within range 0...1), the lower the bandwidth of the low-pass filter.

Hoping this will help you,
-Pasi A.
 
J

John G. Boland

Hi, Anonymous,

You are computing the ratio of (the difference between two position measurements) to (an approximate delta time).

+/- one count could be significant. Keep your counts per computation cycle up.

Both the serial interface timing indeterminacy and "waiting x seconds in VB" reduce the accuracy of the approximate delta time.

Also, as you mentioned, be a little wary of filter algorithms in closed-loop systems. With a simple average, the filtered value lags the
current reading by one-half the filter interval and reduces the system response.

You can achieve the same simple average by extending your computation interval, unless frequent reads are needed to avoid missing a position counter rollover. This increases both the delta counts and the delta time without increasing the measurement uncertainty in each interval, decreasing the ratio error.

Try reading the position (counts), then the datetime to the greatest resolution your can achieve (and in the immediately *following*
instruction). Reset the timer, which only triggers the next computation cycle. Compute the ratio of the delta position to the delta time. Extend the computation interval if still necessary.

Best,

John G. Boland, president
[email protected]
Strateg!c Method$ Corporation
One Parker Square Suite 408
2525 Kell Boulevard
Wichita Falls, Texas 76308
940.322.9922
940.723.1478 fax
 
A

Anthony Kerstens

A useful set of filters are a family of 5-term symmetric linear formulas of the form:

gk = a*fk-2 + b*fk-1 + c*fk + b*fk+1 + a*fk+2

These are non-recursive filters, meaning they don't use previously calculated values which makes them a little simpler. Note that fk+2 is the most recent data value, and gk is the calculated value lagging by two measurements.

There are three requirement to using these equations.

1. The sampling frequency should be set to at least 2x the bit rate. An additional 2x is required for each additional filter cascaded, if any.

2. The sampling interval must be as accurate as possible. Any variation in the sample interval will appear as noise.

3. Don't be tempted to use a simple average. Just taking 5 numbers and averaging them produces a transfer function that actually attenuates
certain lower frequencies and allows noise frequencies to pass.


Some good low pass filter values are as follows:

a = 1/16 b = 1/4 c = 3/8
gk = 1/16 (fk-2 + fk+2) + 1/4 (fk-1 + fk+1) + 3/8 fk
Flat response at high frequencies.
Good for removing noise.
Tends to excessively dampen sharp signal changes.

a = - 1/16 b = 1/4 c = 5/8
gk = - 1/16 (fk-2 + fk+2) + 1/4 (fk-1 + fk+1) + 5/8 fk
Flat response at low frequencies.
Good for dampening small oscillations in clean signals.
Not as good at removing signal noise.

a = 0 b = 1/4 c = 1/2
gk = 1/4 (fk-1 + fk+1) + 1/2 fk
Compromise between a=1/16 and a=-1/6.


However, since you need to calculate speed you may want to use a derivative filter instead.

fk’ = ( -a*fk-2 - b*fk-1 + b*fk+1 + a*fk+2 ) / delta-k
delta-k = sampling interval in unit measure of time

a = - 1/12 b = 2/3
fk’ = [-1/12 (fk+2 - fk-2) + 2/3 (fk+1 - fk-1) ] / delta-k
Flat response at low frequencies.
High accuracy for differentiating clean signals.
Highly succeptable to signal noise and process oscillations.


a = 0 b = 1/2 (simple derivative)
fk’ = 1/2 (fk+1 - fk-1) ] / delta-k
Moderate accuracy for differentiating clean signals.
Moderately succeptable to signal noise and process oscillations.

a = 1/8 b = 1/4
fk’ = [1/8 (fk+2 - fk-2) + 1/4 (fk+1 - fk-1) ] / delta-k
Flat response at high frequencies.
Best for differentiating noisy signals.
Not as good accuracy for differentiating clean signals.
Minimal succeptability to signal noise and process oscillations.

Anthony Kerstens P.Eng.
 
R

Robert Scott

Your situation is the victim of "garbage in, garbage out". There is no magic solution. Your biggest problem is the quality of the data itself. Focus on ways to improve the resolution of the velocity reading itself, rather than trying to process faulty data into something it can never be. Since you are unable to tolerate longer delays, filtering is of little use.
 
Top