The problem with Allen's filter is that it doesn't take into account the scan time. This will cause Allen's filter to respond differently ( at different rates ) depending on how often Allen's filter is updated. This means the filter constants must be updated as more code is added to the PLC and the PLC scan time becomes longer. These crude filters may be OK for many applications but wouldn't it be nice to be able to calculate how a low pass filter will respond? OH YEAH, we all assume we were asked about how to do a low pass filter.Low pass filters are usually described as having a time constant or cut off frequency.
Time constants describe how quickly a filter will respond to a step input. Let's assume the the signal we are filtering is at 0 volts and then it takes a step jump to 10 volts. It takes five time constants for the signal to rise from 0 to within 1% of 10 volts. Therefore if the time constant is 1 second, it will take the filtered signal 5 seconds to get within 1% of the signal.
Y = 10 ( 1 - exp ( -t / TC ))
BTW if this were a RC electrical circuit then TC = RC or
Y = 10 ( 1 - exp ( -t / RC ))
The cut off frequency, roll off frequency or pole frequeny of a filter is the frequency where the amplitude of the AC or noise components are reduced by 3db or .707 of the 0 HZ signal. Beyond the pole frequency the noise drops off at 6db per octave or 20db per decade.
For most applications I like to think in terms of time constants. I want to filter the data as much as possible but not too much. If I need the input to be accurate within 1% in 1 second then the time constant needs to be 200 milliseconds ( remember it takes 5 time constants to get within 1%).
Now that I know the time constant I need to calculate the filter coefficient that will yield a time constant of 200 milliseconds.
The formula is:
K is the filter coefficient
TC is the time constant
ST is the scan time or period
Y is the Output or filtered value
X is the Input or signal or error
K = ST / ( TC + ST )
Now the filter formula becomes
Y = Y + K * ( X - Y )
This formula is accurates as long as the scan time ST is small compared to the time constant TC. If ST is <= TC /10 then an analog system can be approximated. So if the PLC scan time is 10 milliseconds, the TC can be as low as 100 and the results will be pretty good.
Normally K is computed only once when the PLC starts but it may need to be updated before it is used eached scan if the scan time can vary a lot.
In this case the filter is
Y = Y + ST * ( X - Y) / ( ST + TC )
Many PLCs perform A to D conversions synchrously to the scan and the period between scans or scan time is known to a millisecond.
Compensating for the scan time and knowing how safe ratio between the ST and TC keep the wierd stuff from happening that Terry mentioned in the PID thread.
The relationship between the TC and pole frequency is simple:
F is the pole frequency where the noise is reduced to .707
F = 1/(2*PI*TC)
In the example where TC = .2 seconds the pole frequency is 1.256 HZ which is pretty low. At 12.56HZ or one decade above 1.256 hz the noise reduced to a tenth.
Now you might have an application where you have 5 HZ signal that you want to measure but want to start to filter noise above 10 HZ. From the equation
TC = 1/(2*PI*f) = .0159 seconds or 16 milliseconds. This filter would be hard to do accurately because the scan time is not 10 times less than the time constant. The PLC scan time would have to be less than two milliseconds. There are other ways of calculating the filter coefficient K that would return adequate results with much lower scan times.
Now one can predict how a filter will respond to noise.
Get a spread sheet a try it.