Hi, due to several questions for the Code I will post the code again to the list. It would be great if somebody would put the Basic and the ASM to his PIC - Resources web-page. As usual the code is provided without any warranties. Kind regards, Stefan BASIC: init: REM following defines the circular buffer 10 DIM a(16) REM for clearer view 2 tag "lines" one held the 4 most higest, the other REM the 4 most lowest tag's REM While they are Int-values in Basic - they are stuffed into four bytes REM in the assembler code for space-saving. 20 DIM flagH(16) 21 DIM flagL(16) 22 buf = 0: REM Buffer-Pointer newvalue: REM clrf tag bytes FOR i = 0 TO 15: flagL(i) = 0: NEXT i FOR i = 0 TO 15: flagH(i) = 0: NEXT i extrema = 0 median = 0 k = 0 PRINT "Buffer:"; buf; " Wert:": INPUT in REM insert new value in circular buffer a(buf) = in REM housekeeping buf = buf + 1 IF buf > 15 THEN buf = 0 i = 0 outerloop: n = 0 T3 = 0 T5 = 255 innerloop: IF flagL(n) = 0 THEN IF a(n) >= T3 THEN T3 = a(n) T4 = n END IF END IF IF flagH(n) = 0 THEN IF a(n) <= T5 THEN T5 = a(n) t6 = n END IF END IF n = n + 1 IF n <= 15 THEN GOTO innerloop END IF extrema = extrema + a(T4) + a(t6) flagL(T4) = flagL(T4) OR 1 flagH(t6) = flagH(t6) OR 1 i = i + 1 IF i <= 3 THEN GOTO outerloop: END IF FOR i = 0 TO 15: median = median + a(i): NEXT i median = median - extrema median = median / 8 FOR i = 0 TO 15: PRINT USING "### "; i; : NEXT: PRINT FOR i = 0 TO 15: PRINT USING "### "; a(i); : NEXT: PRINT : PRINT FOR i = 0 TO 15: PRINT USING "### "; flagL(i); : NEXT: PRINT FOR i = 0 TO 15: PRINT USING "### "; flagH(i); : NEXT: PRINT : PRINT PRINT "median :"; median: PRINT "extrema:"; extrema: PRINT : 140 GOTO newvalue ASSEMBLER: ;Maybe there is a better solution as ae DEFBYTE-Macro , but I use this to automatically ;assign a memory-location to a label ( I later discovered that RES will do something similar ); ; -------------------------------------------------------------------------------- ------------ ; -------------------------- Variablen fuer Sort Routine ----------------------------------- DEFBYTE _pBuffer DEFBYTE _n DEFBYTE _i DEFBYTE T3 DEFBYTE T4 DEFBYTE T5 DEFBYTE T6 DEFBYTE _NewValue DEFBYTE _MedianH DEFBYTE _MedianL DEFBYTE _ExtremaH DEFBYTE _ExtremaL DEFBYTE _TagHH DEFBYTE _TagHL DEFBYTE _TagLH DEFBYTE _TagLL DEFBYTE _CBuffer_0 ; [0] DEFBYTE _CBuffer_1 ; [1] DEFBYTE _CBuffer_2 ; [2] DEFBYTE _CBuffer_3 ; [3] DEFBYTE _CBuffer_4 ; [4] DEFBYTE _CBuffer_5 ; [5] DEFBYTE _CBuffer_6 ; [6] DEFBYTE _CBuffer_7 ; [7] DEFBYTE _CBuffer_8 ; [8] DEFBYTE _CBuffer_9 ; [9] DEFBYTE _CBuffer_A ; [A] DEFBYTE _CBuffer_B ; [B] DEFBYTE _CBuffer_C ; [C] DEFBYTE _CBuffer_D ; [D] DEFBYTE _CBuffer_E ; [E] DEFBYTE _CBuffer_F ; [F] ; Subroutine used by median filter ----------------------------------------------------------------- ; converts numerical bitposition e.g. 2 into "00000100" BitPos: addwf PCL, F retlw 0x01 ; retlw 0x02 ; retlw 0x04 ; retlw 0x08 ; retlw 0x10 ; retlw 0x20 ; retlw 0x40 ; retlw 0x80 ; ; Medianfilter ------------------------------------------------------------------------ filter: movwf _NewValue clrf _TagHH ; clear Tag-Bytes clrf _TagHL clrf _TagLH clrf _TagLL clrf _MedianH clrf _MedianL clrf _ExtremaH clrf _ExtremaL movlw _CBuffer_0 ; houskeeping for circular buffer starts her e addwf _pBuffer,W movwf FSR movf _NewValue, W movwf INDF incf _pBuffer, F ; next circular buffer movlw D'16' ; If pbuffer < 16 subwf _pBuffer, W ; btfsc STATUS, C clrf _pBuffer ; Then pbuffer = 0 clrf _i ; i = 0 outerloop: clrf _n ; n = 0 clrf T3 ; T3 = 0 movlw 0xFF ; T5 = 255 movwf T5 movlw _CBuffer_0 ; W = &filter[0] movwf FSR ; innerloop: btfsc _TagHL, 0 ; if TagHL(n) then goto il1 ; goto test TagH(n) ; else movf T3, W ; if filter[n] >= T3 subwf INDF, W ; btfss STATUS,C goto il1 movf INDF, W ; T3 = filter[n] movwf T3 movf _n, W ; T4 = n ( which Bit to tag movwf T4 ; in _TagHH:_TagHL il1: btfsc _TagLL, 0 ; if TagLL(n) then goto il2 ; goto shift Tags ; else movf T5, W ; if filter[n] <= T5 subwf INDF, W ; btfss STATUS, C ; goto $+3 ; no, goto shift Tags btfss STATUS, Z goto il2 movf INDF, W ; T5 = filter[n] movwf T5 movf _n, W ; T6 = n ( which Bit to tag ) movwf T6 ; in _TagLH:_TagLL il2: ; Shift Tags rrf _TagLH, W ; MSB -> C rrf _TagLL, F ; rrf _TagLH, F ; rrf _TagHH, W ; MSB -> C rrf _TagHL, F ; rrf _TagHH, F ; incf FSR, F incf _n, F ; n++ ; movlw D'16' ; if n < 16 {n: 0..15} ; subwf _n, W ; btfss STATUS, C ; goto innerloop ; then goto innerloop ; the 4 lines above are replaced by the two lines below for speedup btfss _n, 4 ; if n == 16 goto innerloop ; then goto innerloop ; else movf T3, W ; Extrema = Extrema + T3 addwf _ExtremaL, F btfsc STATUS, C incf _ExtremaH, F movf T5, W ; Extrema = Extrema + T5 addwf _ExtremaL, F btfsc STATUS, C incf _ExtremaH, F movf T4, W ; Update TagHH:TagHL Bitfield movwf T3 ; save T4 movlw 0x07 ; "00000111" -> W andwf T3, W ; call BitPos ; btfss T4, 3 iorwf _TagHL, F btfsc T4, 3 iorwf _TagHH, F movf T6, W ; Update TagLH:TagLL Bitfield movwf T5 ; save T5 movlw 0x07 ; "00000111" -> W andwf T5, W ; call BitPos ; evaluate Bitposition btfss T6, 3 iorwf _TagLL, F ; btfsc T6, 3 iorwf _TagLH, F incf _i, F ; i++ ; movlw D'4' ; if i < 4 {i: 0..3} ; subwf _i, W ; btfss STATUS, C ; goto outerloop ; then goto outerloop ; the 4 lines above are replaced by the two lines below for speedup btfss _i, 2 ; if i = 4 {i: 0..3} goto outerloop ; then goto outerloop ; else clrf _i movlw _CBuffer_0 ; W = &filter[0] movwf FSR ; set FSR to start of buffer sumloop: movf INDF, W ; addwf _MedianL, F btfsc STATUS, C incf _MedianH, F incf FSR, F ; inc pointer to buffer incf _i, F ; i++ ; movlw D'16' ; if i < 16 {i: 0..15} ; subwf _i, W ; btfss STATUS, C ; goto sumloop ; then goto ... ; bussines as usal for speedup btfss _i, 4 ; if i = 16 {i: 0..15} goto sumloop ; then goto ... ; else movf _ExtremaL, W ; Median = Median - Extrema subwf _MedianL, F btfss STATUS, C decf _MedianH, F movf _ExtremaH, W subwf _MedianH, F rrf _MedianH, F ; Median = Median / 8 rrf _MedianL, F rrf _MedianH, F rrf _MedianL, F rrf _MedianH, F rrf _MedianL, F ; _MedianL contains resu lt. return