Lawrence Lile <spam_OUTlilelTakeThisOuTtoastmaster.com> wrote:
> Yes I do [have a median-filter routine]. The key to this is to be
> able to sort data, and I was stumped until Bob Fehrenbach posted
> this routine. (Thanks, Bob.)
>
> You might designate, say, 5 or 10 locations for your (tiny little)
> array of data.
Lawrence:
"5 or 11 locations" would be better... It's easiest to pick the
median value when your array has an odd number of elements.
> You can sort the array two different ways. You can sort numbers
> into the array as they are read
> ....
> Or you can wait till you have ten unsorted numbers, then sort them
> all .... This seems less time efficient than the other method.
Correct; INSERTING a value into an already-sorted partial
list can be done much more quickly than sorting a list.
{Quote hidden}> The following snippet of code is the key to this, an algorithm that
> sorts a pair of numbers. To use it, set FSR register to the first
> location in a small bank of RAM. It decides whether the location
> referenced by FSR is greater or less than the next location, FSR+1,
> and swaps them if not. It cleans house by leaving FSR at the same
> place it started.
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> ; ORDER sorts a pair of numbers indexed by FSR and FSR+1
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> ORDER ; SUBROUTINE
>
>
> MOVF INDF, W ; RECALL X
> INCF FSR, F ; SET UP TO RECALL Y
> SUBWF INDF, W ; Y >= X?
> BTFSC STATUS, C ; Y<X C=0, NO SKIP
> ; Y >=X C=1 SKIP
> GOTO NOSWAP
> SUBWF INDF, F ; Y=Y-(Y-X) = X
> DECF FSR, F ; SET UP TO RECALL X
> ADDWF INDF, F ; X=X+(Y-X) = Y
> RETLW 0
> NOSWAP
> DECF FSR, F
> RETLW 0
Personally, I'd leave out the "house-cleaning" step at the end,
since a bubble-sort will generally want to call the subroutine
repeatedly, incrementing the FSR by one at each call.
And actually... If the array were small, I probably wouldn't use
the FSR at all; it's much faster to use a macro like the
following, which I first posted to the list two years ago:
; THIS MACRO COMPARES THE VALUES IN REGISTERS X AND Y.
; IF Y < X, IT SWAPS THEM.
ORDER MACRO X,Y
MOVF X,W ;GRAB X.
SUBWF Y,W ;Y >= X?
BC $+3 ;IF SO, JUMP AHEAD.
ADDWF X ;OTHERWISE, X = X + (Y-X) = Y,
SUBWF Y ; AND Y = Y - (Y-X) = X.
ENDM
For a 5-element array, the "sort" subroutine would occupy 28
words of program space and would look like this:
; SORT REG1-REG5 IN ASCENDING ORDER.
SORTSUB:
CALL SORT4
CALL SORT3
CALL SORT2
SORT1: ORDER REG1,REG2
SORT2: ORDER REG2,REG3
SORT3: ORDER REG3,REG4
SORT4: ORDER REG4,REG5
RETURN
-Andy
=== Andrew Warren - .....fastfwdKILLspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499