Averaging AtoD samples


Closed Thread
Results 1 to 40 of 40

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    We choose to go to the moon. We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win, and the others, too.
                John F. Kennedy
    Name:  AvgReject.JPG
Views: 2926
Size:  196.7 KB

    Notice in the image that all spurious samples were rejected.
    And where the samples changed from 6798 to 3899, the average caught up after 2 samples (adjustable with a constant).
    Code:
    DEFINE OSC 20
    
    DEFINE LCD_DREG PORTB 'LCD data port 
    DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4 
    DEFINE LCD_RSREG PORTB 'LCD register select port 
    DEFINE LCD_RSBIT 4 'LCD register select bit 
    DEFINE LCD_EREG PORTB 'LCD enable port 
    DEFINE LCD_EBIT 5 'LCD enable bit 
    DEFINE LCD_BITS 4 'LCD bus size 4 or 8 
    DEFINE LCD_LINES 2 'Number lines on LCD 
    DEFINE LCD_COMMANDUS 2000 'Command delay time in us 
    DEFINE LCD_DATAUS 50 'Data delay time in us 
    
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    '----[Averaging Options]------------------------------------------------------------------
    AvgCount	CON  16		' Number of samples to average
    FAspread	CON  50 	' Fast Average threshold +/-
    Reject          CON  2          ' Spurious Rejection level
    
    ;----[Variables]--------------------------------------------------------------------------
    Value		VAR  WORD
    RejCount        VAR  BYTE
    ADavg		VAR  WORD
    
    ;----[Initialize]-------------------------------------------------------------------------
    RejCount = 0
    ADavg = 0
    
    ;----[Main Program Loop]------------------------------------------------------------------
    Main:
        HSERIN [wait($FF),DEC Value]                                   ; get a sample
        HSEROUT ["Smpl= ",DEC Value]
        LCDOUT $FE,$80,"Smpl= ",DEC Value,"    "
        GOSUB Average                                                  ; average it
        HSEROUT ["  Avg= ",DEC ADavg] ;   ,"  Avg2= ",DEC ADavg2,13,10]
        IF RejCount > 0 THEN HSEROUT ["  Rejected"]                    ; indicate if rejected
        HSEROUT [13,10]
        LCDOUT $FE,$C0,"Avg = ",DEC Value,"    "
        LCDOUT $FE,$94,"FC  = ",DEC RejCount,"    "
    GOTO Main
    
    ' -=-=-=-=-=-=  Average Sample values -=-=-=-=-=-=-=-=-=-=
    Average:
        IF Value = ADavg Then RejCount = 0 : RETURN  ; NoChange
        IF ABS (Value - ADavg) > FAspread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)
        ADavg = ADavg + (Value/AvgCount)
        RejCount = 0
        GoTo AVGok
      FastAvg:
        RejCount = RejCount + 1
        IF RejCount = Reject + 1 THEN 
            ADavg = Value
            RejCount = 0
        ENDIF
        GoTo AVGok
      RealClose:
        RejCount = 0
        ADavg = ADavg - (ADavg/(AvgCount/4))
        ADavg = ADavg + (Value/(AvgCount/4))
      AVGok:
        Value = ADavg			' Put Average back into Value
      NoChange:
    Return
    DT

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default

    What filters and what rules?

    Darrel rules!

    A masterpiece, if I may say.

    Ioannis

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Thanks Ioannis.

    And I'll say ... cncmachineguy's rules were pretty darn close to how I modified the averaging routine.
    DT

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Wow ...what can I say?!!

    DT you truly rock...and without a doubt you're my most favouritiest E-personage on the whole of this global network!

    I'll give this runout at the weekend...once again, thanks....just brilliant!!

    cnc machine guy - great input too ...many thanks.. re the arriving frequencies (or more importantly, what the PIC sees for incoming signal frequency - comparator 'count'), well, on a std tuned guitar, the lowest note is 82.4Hz...which @20Mhz clock gives a comparator interrupt 'count' of about 60700 per 82.4Hz period' ...the highest note on a guitar is about 1.4Khz & gives a comparator count of about 3500 1.4khz 'period'. Each musical note has about 5% difference...but the problem with guitar 'pitch detection' is you can bend a note, which will obviously fall somewhere in between the 5% between notes.

    I'd like to be able to eventually get the PIC discern not just which note is being played but also when it is s being bent - but for that I need good solid incoming samples to process...and these spurious samples were really screwing me up - hence I relish having a dabble with what DT has just posted!
    Last edited by HankMcSpank; - 21st October 2010 at 15:57.

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Averaging AtoD samples

    An old thread & just revisiting this issue again ...re the spurious rejection, there's a setting as follows....

    Code:
    Reject          CON  2          ' Spurious Rejection level
    Presumably that setting is the amount of samples, before the averaging code accepts/adopts the new incoming sample (vs just throwing away a sample that diverges too much) ....but what is deemed a 'spurious' sample ..in other words what in the Darrel's code a couple of posts above decides that the incoming sample was spurious ...and can this 'sprurious diversion amount' be adjusted?

    To help explain, consider the following trail of ADC samples...

    99
    101
    100
    91 ------ not spurious, just a low sample
    97
    101
    50 -----------spurious
    98
    100

    How is the spurious rejection set so as to accept the ADC rsample of 91, but to just reject the sample of 50?
    Last edited by HankMcSpank; - 29th September 2011 at 11:24.

  6. #6
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Averaging AtoD samples

    Ok Hank, I'll give this a go. BTW, it only took me 15 mins to remember what I was thinking when I posted almost a year ago!

    Anyway, this is what I think is going on. Every sample with a difference greater then 50 in DT's code gets rejected the first and second time through. The 50 can be set with the FAspread constant. the number of times it get rejected is set by Reject constant.

    The way it gets rejected is really pretty. Int the avg routine, the value gets tested against FAspread, if it is bigger, the flow jumps to FastAvg, where reject count gets incremented by 1. then reject count gets tested to see if it is now 3
    Code:
    IF RejCount = Reject + 1 THEN
    if it is, the new value is accepted as good. It also becomes the new average. and everything moves on from there.
    If it is not, then the running average is left as is and the current value is rejected.

    BTW, I think using DT's FAspread con 50, 91 would never be considered as spurious since it is within 50

    Does this help?
    Last edited by cncmachineguy; - 29th September 2011 at 12:56.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Averaging AtoD samples

    Thanks Bert....let me see - so it's the FAspread that sets how far the deviation from mean that can be accepted .... with the RejCount counting how many 'deviations from mean' there have been - once the Rejcount number is met, then the new level of incoming sample becomes a valid sample to average against?

Similar Threads

  1. Darrel's latest 16 bit averaging routine?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 17th October 2009, 01:57
  2. ADC Averaging
    By Sach_1979 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st September 2009, 06:53
  3. Atod Digital Filter
    By GeoJoe in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd April 2008, 17:04
  4. Microchip free samples in UK?
    By zoki008 in forum Off Topic
    Replies: 1
    Last Post: - 21st March 2006, 16:06
  5. Averaging & 16 maths
    By paul.mcallister in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2005, 18:17

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts