Newsgroups: comp.speech,comp.dsp,comp.robotics
Path: lyra.csx.cam.ac.uk!warwick!slxsys!pipex!howland.reston.ans.net!vixen.cso.uiuc.edu!news.uoregon.edu!cs.uoregon.edu!reuter.cse.ogi.edu!hp-cv!hp-pcd!hpcvsnz!not-for-mail
From: ericb@lsid.hp.com (Eric Backus)
Subject: Re: calc. of long & short term avgs.
Sender: news@hpcvsnz.cv.hp.com (News )
Message-ID: <CtoCMz.5un@hpcvsnz.cv.hp.com>
Date: Thu, 28 Jul 1994 23:44:58 GMT
Distribution: usa
References: <315v47$qb2@jaws.cs.hmc.edu>
Organization: Hewlett-Packard
X-Newsreader: TIN [UNIX 1.3 310394BETA PL0]
Followup-To: comp.speech,comp.dsp,comp.robotics
Lines: 37
Xref: lyra.csx.cam.ac.uk comp.speech:2980 comp.dsp:8625 comp.robotics:9727

Thomas Chong (tchong@osiris.hmc.edu) wrote:
> Help!  I am trying to calculate a long-term average at each point of
> a continuous data stream.  The following algorithm works fine when using
> floating point type values but does not for integer types (e.g. 8 or 16 bit
> integer) because of loss of precision when the oldLTA(see eq.) approaches
> the current data value.
>
>           n-1                1
> newLTA = ----- * oldLTA  +  --- * current_value
>            n                 n
>
> where
> newLTA = new long term average
> oldLTA = old       "
> n = integer greater than 1

> When using non-floating point types and having a constant value data stream,
> the newLTA fails to converge to the constant value.

> I need to apply this in assembly on the Motorola HC11.  I am trying to
> detect an event by measuring the short term to long term average ratio.  Any
> assistance would be appreciated.


First, you should re-write the equation as follows:

                   1
newLTA = oldLTA + --- (current_value - oldLTA)
                   n

This has the advantage of requiring only one multiply instead of two.
To get better convergence, you may want to compute the (current-old)/n
part with greater precision.
-- 
				Eric Backus
				ericb@lsid.hp.com
				(206) 335-2495
