embedded systems developers tools, cross compilers
  Home  |   SwiftX Archive  |   SwiftForth Archive  |

Re: Running average

From: <wheath_at_comcast.net>
Date: Sun, 27 Aug 2006 15:42:01 +0000

Try this
\ MAVE.F
\ MOVING AVERAGE
\ FOR 16 BIT INDIVIDUAL NUMBERS MXRDS MUST BE < 32768
\ SO THAT MSUM DOES NOT OVERFLOW
128 CONSTANT MXRDS ( READS IN MOVING AVERAGE )
    VARIABLE SVQF ( FLAG 1 = QUE FULL )
    VARIABLE QPTR ( QUE PTR TO LAST RD SVD IN MAVSV )
   2VARIABLE MSUM ( MAVE SUM DOUBLE PRECISION )
\ ASSUMES 2 BYTE INDIVIDUAL NUMBERS
MXRDS CELLS BUFFER: MAVSV ( MAVE QUE )

: 2+! ( D A -> - ) -ROT third 2@ D+ ROT 2! ;

: INITQ ( - ) 0 SVQF ! 0 QPTR ! 0. MSUM 2! ;

: SMA ( I -- AMVSV ) CELLS MAVSV + ;

\ IF QUE FULL 1ST RD = NEXT READ
: 1STRD ( -- NCTS )
   QPTR @ 1+ MXRDS MOD SMA @ ;

\ AVE = SUM/MXRDS
: AVERD ( NCTS -- NAVE T or F | F= NOT FULL YET )
   S>D MSUM 2+! \ ADD NEW READ
   SVQF @
   IF 1STRD NEGATE S>D MSUM 2+! \ REMOVE OLDEST READ
       MSUM 2@ MXRDS M/ \ CALC NEW AVE
       TRUE
   ELSE 0 \ NO AVE TILL QUE FULL
   THEN ;

: SVQ ( NCTS -- )
   QPTR @ 1+ DUP MXRDS 1- > ( NCTS PTRNX TF )
   IF 1 SVQF ! \ QUE FULL
   THEN MXRDS MOD DUP QPTR ! SMA ! ; \ ADD TO QUE

: MAVE ( NCTS -- NAVE T or F | F= NOT FULL YET )
    DUP AVERD ( NCTS NAVE T or NCTS F )
    DUP IF ROT
        ELSE SWAP
        THEN SVQ ; \ MUST DO SVQ LAST TO INC AFTER SAVE
\\
\ TO USE
   INITQ ( - )
 BEGIN
   READ A/D ( -- NCTS )
   MAVE ( NTCS -- NAVE T or F )

Warren Heath
-------------- Original message --------------
From: Jim Hartman <coast1jim_at_ispwest.com>

> Has anyone done a simple double precision running average. I'm working
> with about 100 samples max. The platform is the hc9s12.
> Thanks, Jim Hartman
> ----------------------------------------------------------------------
> swiftx_at_forth.com The SwiftX programming discussion email list
> To unsubscribe, send subject "unsubscribe" to swiftx-request_at_forth.com
> For list command help, send subject "help" to swiftx-request_at_forth.com
> Message archives are located at http://www.forth.com/archive/swiftx
> ----------------------------------------------------------------------
> This list is a forum for SwiftX users. For product support and bug
> reports, please send email to support_at_forth.com
> ----------------------------------------------------------------------
>

----------------------------------------------------------------------
swiftx_at_forth.com The SwiftX programming discussion email list
To unsubscribe, send subject "unsubscribe" to swiftx-request_at_forth.com
For list command help, send subject "help" to swiftx-request_at_forth.com
Message archives are located at http://www.forth.com/archive/swiftx
----------------------------------------------------------------------
This list is a forum for SwiftX users. For product support and bug
reports, please send email to support_at_forth.com
----------------------------------------------------------------------
Received on Sun Aug 27 2006 - 08:42:25 PDT

This archive was generated by hypermail 2.2.0 : Tue Dec 02 2008 - 03:04:51 PST