PDA

View Full Version : Problem with Tracy Allen digital low pass filter?



DSaum
- 26th November 2012, 19:09
I would like to do an RC digital low pass filter on some 16 bit adc data with an RC time constant of about 1000 samples.

Guru Tracy Allen seems to have some code for just such a filter:

---- from http://www.emesystems.com/BS2math5.htm

Here is a filter that works for larger numbers, such as barometer data that has to be filtered for altimetry. This filter uses a double precision accumulator, it too converges well without numerical problems. This can be used for small numbers too.

example with time constant kR=8:
Y0 = X0*65536 initial accumulator =216* initial datum
Yn = Yn-1 * 7/8 + Xn*65536/8 iteration adds new value to accumulator
Zn = Yn/65536 filter output ' Double precision filter for input numbers up to 65535
kR con 3 ' the filter time constant is 23=8, done a shift right kR
kL con 16-kR ' for the computation shift lefts
X var word ' current value, filter input
Y0 var word ' smoothed value, accumulator low word
Y1 var word ' smoothed value, accumulator high word
Z var word ' scratch variable for calculations
' also final filter output
inititalize:
gosub getX ' get current value, not shown
Y1=X 'initialize, note effective multipication *65536
loop:
gosub getX ' get current value, not shown
Z=(Y0>>kR)|(Y1<<kL) ' form low word of accumulator, divided by (2^kR).
Y0=Y0-Z ' subtract it from low word of accumulator
' next do the same for high word, minus borrow from low word
Y1=Y1-(Y1>>kR)+(Y0 max -Z +Z max 1 -1)
Z=X<<kL ' form input data times divided by (2^kR), part to low word
Y0= Y0 + Z ' add it to low word of the accumulator
Y1= Y1+(X>>kR)+(Y0 max Z -Z max 1) ' do the same for the high word, plus carry
Z = Y1 + (X0 max 1) ' filter output, high word of accumulator with adjustment.
debug dec X, tab, dec Z, CR ' show the raw value and the filtered value
goto loop

------------ end of http://www.emesystems.com/BS2math5.htm code

However, when I try to compile this code, there is an undefined variable X0 in the next to last
line. It seems like the "adjustment" this line refers to is rounding, but I am having trouble
understanding all the neat tricks in this code.

I have not been able to contact Tracy

Has anyone else ever got this code working?

TIA

Dave

DSaum
- 26th November 2012, 19:22
It figures, as soon as I posted my question, I heard back from Tracy:

Hi Dave,

That is as puzzling to me too! My first reaction is that it should say
Z = Y1 + (Y0.bit15)
the purpose being to round off the value to output. That should be done based on the
high bit of the low word of the accumulation. It is not usually going to be
important to round it off. With Z = Y1, it is always rounding down.

-- best regards,
Tracy