'Noise filter, averaging'
Instead of replying offline I decided to reply to the PICLIST because
this is getting interesting.
> .. From this recent post of yours, it looks like you have a
> `ready-made' routine for a median filter. Are you able to help?
Yes I do. 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. Set up a loop to read analog values into your data
array one at a time. Use the FSR to index each element of your
You can sort the array two different ways. You can sort numbers into
the array as they are read, so the zero values you started with
(don't forget to clear them next time) get kicked out the bottom of
the stack as new data is brought in.
Or you can wait till you have ten unsorted numbers, then sort them
all. You have to sort the entire stack by pairs ten times to insure
that the lowest number really ends up on the end of a ten-member
array. This seems less time efficient than the other method.
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
SUBWF INDF, F ; Y=Y-(Y-X) = X
DECF FSR, F ; SET UP TO RECALL X
ADDWF INDF, F ; X=X+(Y-X) = Y
DECF FSR, F
|Lawrence Lile <toastmaster.com> wrote: lilel
> 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.
"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.
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.
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.
SORT1: ORDER REG1,REG2
SORT2: ORDER REG2,REG3
SORT3: ORDER REG3,REG4
SORT4: ORDER REG4,REG5
=== Andrew Warren - ix.netcom.comfastfwd
=== Fast Forward Engineering - Vista, California
More... (looser matching)
- Last day of these posts
- In 1998
, 1999 only
- New search...