Path: funic!fuug!mcsun!uunet!elroy.jpl.nasa.gov!sdd.hp.com!hp-pcd!hpcvra.cv.hp.com!rnews!hpcvbbs!akcs.kevin
From: akcs.kevin@hpcvbbs.cv.hp.com (Kevin Jessup)
Newsgroups: comp.sys.handhelds
Subject: HP48SX DIGITAL FILTER DEMO
Keywords: filters dsp coleslaw
Message-ID: <288dfe82:3762comp.sys.handhelds@hpcvbbs.cv.hp.com>
Date: 24 Jul 91 22:40:07 GMT
Lines: 208

Hello again!

I recently got my hands on a copy of MathCAD.  I was playing
around with it's FFTs and DSP filtering routines when I decided
that after paying 275 bucks for my 48, it had damn well better be
able to do the same thing. :-)  I immediately wasted 3 hours coding
up the routines you'll see below.  What a time-saver the 48SX is!!

The routines do nothing really usefull.  It's just a demo of sorts.
Here is the sequence when you press the DEMO key...

1.) DEMO will use a predefined function (sin(x) is used,
    but you can change it) to generate an array of x and y
    coordinates using the summation function.

2.) A scatter plot of the data is drawn.

3.) Noise is added to the data.

4.) The scatter plot is redrawn.

5.) The data is filtered.

6.) The scatter plot is redrawn.

7.) Repeat steps 5 and 6 four more times.

8.) The display is frozen.  Press ON to quit.

9.) If you want to filter the same data a few more times,
    just press FDMO till you are satisfied.

Now just one damn minute here before all you DSP gurus grab
your flame throwers! :-)  MY AREA OF EXPERTISE IS NOT DSP!
My knowledge of DSP is somewhere between none and very
little and my knowledge of analog electronics does not go
beyond the typical undergraduate stuff.  I was just playing
around.

Now that you know that, feel free to modify the main filter
algorithm (called SMOOTH) to use whatever method works best
and/or fastest for you.  All I am doing is providing a bit of a
front end and user interface for demo purposes.  I'm not even
sure if the 48 could ever be put to use where the sample rate
is so high and the data quality so poor that such algorithms are
needed.  Let's just say that this is for educational purposes only.
The best thing that can come out of this posting is that those
who really know how to do this stuff will post some of their
routines!

Perhaps such routines have already been posted.  If so, sorry about
the wasted bandwidth.

One last item of interest.  If I run the BUILD routine seperately
in a directory just off of HOME (the path is {HOME DSP}), the BUILD
routine takes about 130 seconds.  If I copy the entire directory
down a level (the path is {HOME TEMP DSP}) it takes about 50
seconds.  This is repeatable.  Yet the SMOOTH algorithm takes an
identical amount of time in both directories.  What is going on
here???

Happy filtering!

Kevin Jessup
Home:   (414)355-9752
Office: (414)362-2020
BBS:    (414)362-2020 after 6PM CST

********************** CUT HERE **********************
%%HP: T(3)A(R)F(.);
DIR

  DEMO                 @ Generate the data and call the filter demo.
    \<< RAD            @ RAD will set radians mode.  You may want to
                       @ remove it if you change Bpar's function.
Bpar BUILD             @ Get BUILD parameters.  Call BUILD.
PLOT 2 WAIT TEXT 40    @ Plot the good data.  Pause a bit.
NOISE PLOT 2 WAIT      @ Add noise of amplitude 40 Peak to Peak.
                       @ Plot the noisy data and pause again.
                       @ You may want to change this noise value
                       @ if you change the function in Bpar.
FDMO                   @ Call the filter demo.
    \>>

@ Filter Demo.
@ Assumes the summation array is valid and contains the data
@ you want filtered.  Loops 5 times, redisplaying the data
@ after each loop.

  FDMO
    \<< 1 5            @ Call the filter algorith 5 times.
      FOR i TEXT
CLLCD
"Filtering data...
Loop "
i + 1 DISP \GSDAT      @ Recall sum data to stack.
SMOOTH '\GSDAT' STO    @ Filter it. Store it.
PLOT 2 WAIT            @ Plot it.
      NEXT 7 FREEZE    @ Freeze display if last pass.
    \>>

@ Bpar
@ This list defines the function that will generate the initial
@ "clean" data.  The first element of the list is a function that
@ expects the X value on the stack.  After evaluating the function,
@ the Y value is on the stack.  The 2nd element is the starting X
@ value.  The 3rd is the ending X value.

  Bpar {
    \<< SIN 100 *      @ The function used by the demo is '100 * SIN(X)'
    \>> 0 12.5 }       @ The starting X value and ending X value.

@ BUILD
@ Builds summation data array based on Bpar values.
@ Expects a list (just like Bpar) on stack level 1.

  BUILD
    \<< OBJ\-> DROP DUP @ Get the individual elements from the list.
131 / RCLF \-> f        @ Take 131 samples.  Save flags.
start end step
flags
      \<< CLLCD
"Building \GSDAT.
Patience please...
 ...I aint a Cray!"
1 DISP 1 FIX CL\GS      @ Display the prompt.  Clear sum data.
start end
        FOR i end i     @ Display our progress.
%T "%" + 4 DISP i       @ Get the current X value.
DUP f EVAL \->V2 \GS+   @ Calculate Y form a vector.  Sum it away.
step                    @ Do next X.
        STEP flags
STOF
      \>>
    \>>

@ PLOT
@ Assumes the summation array exists.
@ Will draw a scatter plot based on the X values
@ in column 1 and the Y values in column 2.

  PLOT                  @ SCATTER PLOT of summed data
    \<< 1 XCOL 2 YCOL
SCATRPLOT
    \>>

@ SMOOTH is the main filtering (or smoothing) algorithm.
@ SMOOTH takes all y values (column 2) in the array and
@ averages them with the adjacent y values.
@ SMOOTH expects the summation data array on the stack.
@ It returns the filtered array to the stack when finished.
@ All you DSP experts out there can hack away at this or provide
@ your own algorithm.  As long as it's inputs and outputs are the
@ same, it should work well with the rest of these routines.

  SMOOTH                @ The main filtering algorithm.
    \<< OBJ\-> DUP 1    @ Break up array and determine size.
GET 2 * 3 PICK \->      @ Get the number of stack elements to roll.
dim ec y                @ Save initial y endpoint
      \<< 1 ec 2 / 1    @ Average all y values in the array.
-
        START 3         @ Get adjacent y values and average them.
PICK + 5 PICK + 3 /
y SWAP 'y' STO ec       @ Save current average and restore the last one.
ROLLD ec ROLLD          @ Roll twice to get the next xy pair.
        NEXT ec
ROLLD ec ROLLD dim      @ Reassemble the array.
\->ARRY
      \>>
    \>>

@ NOISE adds random data to all y values in the
@ array.  The peak to peak noise amplitude is on
@ level 1.  It assumes a valid array is stored in
@ the summation data array variable.

  NOISE                 @ Add noise to all y data values.
    \<< CLLCD
"Adding noise..." 1
DISP \GSDAT OBJ\-> DUP
1 GET 2 * DUP 3 +
ROLL \-> dim ec noise
      \<< 1 ec 2 /
        START RAND
.5 - noise * + ec
ROLLD ec ROLLD
        NEXT dim
\->ARRY '\GSDAT' STO
      \>>
    \>>

@ LOOP calls the SMOOTH routine n times.
@ The array is on level 2 and n is on level 1.
@ LOOP is not used by the demo.  It is usefull
@ for repeating the SMOOTH algorithm without
@ the delays caused by replotting the data as
@ the demo does.

  LOOP
    \<< 1 SWAP
      FOR i CLLCD
"Filtering data...
Loop "
i + 1 DISP SMOOTH
      NEXT
    \>>

END
********************** CUT HERE **********************
