Announcement

Collapse
No announcement yet.

FIR Filter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • FIR Filter

    Hi,
    I know PICs aren't the best for this but they are what I'm set up for.

    I'm thinking of trying an 8 tap averaging filter to reduce some noise on a PI.
    I've gained some bits by a novel sampling method, so I don't mind loosing a few,
    maybe I could even go to a 16 or 32 point average.

    I need to know if my algorithm is correct before beginning.
    Now with the 8 point model, would it work if I right shifted each new sample by 3 bits
    (n/ eight) then added all 8 right shifted samples together for an output result?

    I know dynamic loss will result as each new sample is summed with the previous 7,
    but if the sample rate is high enough as is the PRR, target traces should emerge more
    from the noise?
    what do you think?

    Cheers
    Kev.

  • #2
    Kev,

    If you're using a dsPIC, here's a chunk of code that I wrote for such a purpose. I named it CalculateEMA for Exponential Moving Average, but I believe the calculation is a tad different from the typical EMA calculation. Feel free to use my code however you wish.

    Code:
            .equ EmaShiftFactor, 8        ;Amount to right shift values in EMA calc
                                        ;Change to 16 for live test
    Code:
    ;..............................................................................
    ;Calculate a new EMA value and save it back to the EMA memory location
    ;    W4 should contain the address of the current EMA
    ;    W5 should contain the new value to avarage in
    ;..............................................................................
    CalculateEMA:
            MOV [W4], W0            ;Load accumulator A with previous EMA
            MOV W0, ACCAU
            MOV [W4+2], W0
            MOV W0, ACCAH
            MOV [W4+4], W0
            MOV W0, ACCAL
            SAC A, W0                ;Subtract fraction of previous EMA value
            LAC W0, B
            SFTAC B, #EmaShiftFactor
            SUB A
            LAC W5, B                ;Add new value to average in
            SFTAC B, #EmaShiftFactor
            ADD A
            MOV ACCAU, W0            ;Save accumulator A back to the EMA memory 
                                    ; location
            MOV W0, [W4]
            MOV ACCAH, W0
            MOV W0, [W4+2]
            MOV ACCAL, W0
            MOV W0, [W4+4]
            RETURN

    Comment


    • #3
      Thank you Hobbes,
      I'm using a 16F88 but that's not a problem, perhaps Calvin can transmogrify your algorithm to suit, even though the A2D is a mud pie 10 bit one!

      Very generous of you to reply, I was beginning to think everyone passing by only wore analog hats.

      Regards
      Kev.

      Comment


      • #4
        I did this a couple of years ago on a PIC 18F4550, had to dig out my code to see what I did.

        I used the internal 10b ADC, saved the samples as 16-bit ints in a 16-point array with rotating pointer. I then summed all 16 numbers, and did a 4-bit right shift on the result.

        Then, took that result and saved it in a 32-point tracking array to establish a slowly varying baseline to compare against.

        The results were surprisingly decent. Not great, but better than I expected.

        - Carl

        Comment


        • #5
          Thanks Carl,
          That's a similar averaging to what I'm looking at, but what I don't fully understand is why not right shift the samples as they become available and then sum the results, Is there something important I'm missing?

          With 4 samples it equates to less than 1/5 of one percent difference between right shifts before summing and after, e.g. 1.7nV on a 1uV signal. Since we can't rely that much on the LSB anyway, why not bin it at the start?

          I'm sorry, I'm a bit thick, but I'm keen to learn but by not getting into a shamozzle jumping in with boots on and getting totally confused. It's bad enough trying to comprehend the simplest filter ideas, let alone the sigma expression and resulting polynomials, plus the diagrams, whew.
          I guess it's simply like electronic schema once you know the symbols?

          Cheers
          Kev.

          P.S. "32-point tracking array" that sounds interesting Carl, can you explain the fundamentals of that for me please?
          Last edited by Kev; 03-30-2011, 10:33 PM. Reason: Post script

          Comment


          • #6
            Kev,

            Shift right 4 bits = divide by 16. So I think if I understood Carl correctly he was taking 16 adc readings, summing them and dividing by 16. This would give the average for those 16 readings. And If I understood what you were stating. Shift each sample first and then sum them? So I guess you are trimming the lsb's then calculating your average?

            Regards
            Mark

            Comment


            • #7
              It doesn't make a lot of difference whether you shift the samples or shift the average... shifting the samples results in slightly more rounding errors but probably not noticeable.

              Actually, if you save 10-bit samples into 16-bit vars then there is no need to rt-shift unless you average more than 64 samples.

              The tracking array holds an "average of averages." Use this as a long-term signal to compare the sample-by-sample average against; it's just like an analog SAT.

              - Carl

              Comment


              • #8
                &

                Originally posted by Altra View Post
                Kev,

                Shift each sample first and then sum them? So I guess you are trimming the lsb's then calculating your average?

                Regards
                Mark
                Hi Mark,
                Because I was initially going to use 4 poles, I would've right shifted the samples 2 bits before summing to make the arithmetic a touch more 8 bit friendly.
                Originally posted by Carl
                The tracking array holds an "average of averages." Use this as a long-term signal to compare the sample-by-sample average against; it's just like an analog SAT.
                Thanks Carl that's well worth remembering, and with a mud pie A2D as well.
                Were you running it at 40MHz?

                I've just found out my development kit will handle dsPICs
                I like the look of the 30F2011, so fast and such wide accumulators and heaps of them, & lots of math functions per cycle !!!!
                Thanks Hobbes for getting me curious about them

                But there's one thing I don't understand, the minimum A2D window reference voltage?
                The 16f88 is 2 Volts making the res 2/1024 ~ 2mV
                The datasheet is not that clear, I come up with 2V7 (659uV) but that seems way too high for such a device I must be way off.
                Do any of you Guys know the minimum weight per bit please?
                I don't want to over amplify and loose dynamic range, sorry probably not making sense here, getting into deep water I think.

                Cheers
                Kev.
                Last edited by Kev; 03-31-2011, 08:23 AM. Reason: Clarification

                Comment


                • #9
                  The F88 ADC ref is 2V min, so the smallest resolution is 2mV.

                  Comment


                  • #10
                    Originally posted by Carl-NC View Post
                    The F88 ADC ref is 2V min, so the smallest resolution is 2mV.
                    Sorry Carl I didn't make myself very clear, it's the dspic30F2011 that's got me confused, it appears to be 2V7, but surely it wouldn't have a wider window than the 16 Family?

                    Cheers
                    Kev.
                    Last edited by Kev; 04-01-2011, 03:21 AM. Reason: BoBo

                    Comment


                    • #11
                      The data sheet doesn't spec a min for Vref, but INL & DNL are spec'd for 3V. I would assume it would run OK with 2V, perhaps with some degradation in linearity. Of course, it's a 12b ADC so the resolution would then be 0.5mV.

                      Comment


                      • #12
                        Microchip hides the important specs in the "electrical charactoristics" section. I'm not sure why they could'nt mention it in the ADC section?

                        Vref min is specified at 2.7v

                        I am experimenting with the Dspic30f6014A. Maybe we could have a place on the forum to discuss programming and uP for metal detectors?

                        Regards
                        Mark

                        Comment


                        • #13
                          Originally posted by Altra View Post
                          Microchip hides the important specs in the "electrical charactoristics" section. I'm not sure why they could'nt mention it in the ADC section?

                          Vref min is specified at 2.7v

                          I am experimenting with the Dspic30f6014A. Maybe we could have a place on the forum to discuss programming and uP for metal detectors?

                          Regards
                          Mark
                          IMHO I don't think it's necessary to create a special place for discussing uprocs and programming. The Tech Forum seems as good a place as any.

                          Comment


                          • #14
                            I was impressed at how well The Wizard's dspic detector reportedly performed 40 cms (16") on a one euro indoors!
                            http://www.geotech1.com/forums/showp...6&postcount=24

                            He was sadly sent packing by a certain other calling the processors he was using a toy.

                            Comment


                            • #15
                              Even lighter weight PICs (18F and 24F) are used in several well-regarded hobby detectors. If it does the job...

                              Comment

                              Working...
                              X