13 Replies Latest reply on Aug 26, 2019 1:06 AM by aswinvenu

    Implementing a digital Filter

    farcane

      Hallo Guys,

       

      I want to implement a digital filter using an Atmega16 Microcontroller, I am getting the Input signal from an 8 bit  ADC "AD9057" to  the Atmega 16  "port C" then I converted my input signal to ASCii Characters ( 0-128)  which are displayed in my Terminal every 1 second , but there is some noise in my output signal and I want to get rid of it, so I want to use a digital filter for this purpose, but I am confused which digital filter may I implement  to get the desired result.

       

      just to give you an example:

       

      When I apply a constant voltage to my  ADC analog input; I expect a  single digital output representation. but I am getting a range of data.

       

      like when I apply 2 V I get a digital output  (0 - 3)

                                 2.2 V I get ( 40- 44)

                                 2.3 V I get ( 65-70)

                                 2.5 V I get ( 118-124)

       

      And in each case the desiried output should be just one value " a single digital output representation, lets say the average  " and not a range of data.

       

      Any ideas for this issue?

       

      regards

      Imed

        • Re: Implementing a digital Filter
          manlong

          Hello,

          Could you tell me how you apply a constant voltage?

          Is it a sensor? a power supply? a voltage divider?

          If you constant voltage is not constant because of noise, it is normal you have some bit variation.

          Your component have also a noise conversion that generate noise at the output which can varie about 1 bit to 2 bit.

          Regards

          Man Long

          1 of 1 people found this helpful
            • Re: Implementing a digital Filter
              farcane

              Hello Man Long,

               

              Thanks for your reply, I am applying a constant voltage from a potentiometer( 0 to +0.5V) throught an inverting amplifier ( AD8041)  which yields an output of ( 2.5V - 2V ) respectively; which is directly connected to the ADC analog input.

               

              Yes you are right that can be the reason why I am getting a range of output digital representation, but how do you think may I implement a digital filter in this case ? should I change the source of the analog input ? knowing that my ADC ( AD9057)  works only with an analag input of 1V pp and its related to the ADC reference voltage, which is in my case 2.5V,  so the analog input range is between ( 2- 3V), have a look at the ADC data sheet page 4 http://www.analog.com/media/en/technical-documentation/data-sheets/AD9057.pdf

               

              regards

              Imed

              1 of 1 people found this helpful
                • Re: Implementing a digital Filter
                  manlong

                  Imed,

                  First I think you have to add an anti-aliasing filter at the input of your ADC.

                  The cuttoff frequency should be twice as high as the signal frequency.

                  Regards

                  Man Long
                  1 of 1 people found this helpful
                    • Re: Implementing a digital Filter
                      michaelkellett

                      @ Man Lomg Lay

                      If Imed is measuring DC then an anti aliasing filter will make no difference at all.

                       

                      @Imed,

                       

                      Your ADC looks very very noisy, much worse than it should be.

                      You are using a fast ADC and I think from my recollection of your pcb you don't have very good ground planes (I think none at all) - this may hurt at fast sampling rates.

                      Check the noise on your power supplies to the ADC using a scope and possibly increase the values of any decoupling capacitors.

                      Check the noise on the ADC input if your scope is good enough - you should be looking for peak - peak noise less than 4mV.

                       

                      When you get the noise to a reasonable level I can help you with a digital filter, but I would need to know the sampling rate and the bandwidth of the signals you want to measure.

                       

                      MK

                      1 of 1 people found this helpful
                        • Re: Implementing a digital Filter
                          manlong

                          When you input to an ADC you have to put before an anti aliasing filter.

                          The aim is to reduce the noise. Even you want to measure DC signal.

                          You have to filter and delete all signal above DC.

                          The ADC acquire all signal between DC to 120MHz. If you want only DC signal you have to cutoff the signal above DC.

                          1 of 1 people found this helpful
                            • Re: Implementing a digital Filter
                              michaelkellett

                              @ Man Long Lay

                              I'm sorry but this is NOT the function of an anti-alias filter.

                              The clue is in the name - an anti-alias filter is a low pass filter placed before the ADC to stop frequencies above one half of the sampling frequency from appearing at the input. This is because if you, for example, sample a 1.1kHZ sine wave at 2kHz you will see a 100Hz signal in the ADC output  - this effect is called 'aliasing'. If you stop any frequencies above 1000Hz getting into your 2kHz sampling system the effect will not occur.

                              Noise is different, there will be noise generated in the ADC itself and noise presented at its input. You can reduce random broadband noise at the input by reducing the bandwidth but this is very unlikely to be Imed's problem, and we know from his earlier posts that he wants eventually to sample faster signals than DC.

                               

                              @Imed

                              4 or 5 bits noise is a bit higher than I might have hoped for but not too dreadful.

                              At 5MHz your ATmega16 is not going to cope:

                               

                              Digital filtering is  a big subject but about the simplest filter you can use is a rolling average:

                               

                              Add together the last 2,4, 8 or 16 samples and then divide the total by 2,4,8 or 16. The use of powers of two means that the divide can be a shift operation (keep to unsigned data). An you don't need to add up 16 samples each time, just add in the new one and take off the oldest one.

                               

                              Another kind of filter is a simulated RC

                               

                              filtered_data = filtered_data + ((new sample - filtered_data)/k)

                               

                              If you are careful about the value of k then division can be a shift but arithmetic must be signed. For an 8 bit ADC you will need to use at least 16 and quite possibly 32 bit precision for the maths. A typical value for k would be 8.

                               

                              The first filter is more deterministic than the second, any change in data is worked though in the fixed number of samples you average, the second filter takes (in theory) infinite time to work though a disturbance although in pracitce the precision of the maths controls this.

                               

                              I don't think an ATmega 16 is capable of implementing either filter at 5MHz sample rate - I'll be impressed if you manage 1MHz.

                               

                              An ARM Cortex M4 can do either in floating point or 32 bit integer precision at the required speed.

                               

                              There are much more sophisticated filters possible.

                               

                              MK

                              1 of 1 people found this helpful
                                • Re: Implementing a digital Filter
                                  farcane

                                  michaelkellett ok, you mean the sampling frequency of my analog input signal right ? which is sampled using the ENCODE pinheader input pf my PCB, that one is 5 MHz.

                                  but I am getting the digital output on my Terminal every second which means at a freqency of 1 Hz.

                                   

                                  Are these algorithms that you told me about digital filtering?  can I implement a typical Digital filter ( like low-pass or High-pass filter FIR or IIR ) with my DC analog input signal and if not with a different signal ?

                                   

                                  regards

                                  Imed

                                    • Re: Implementing a digital Filter
                                      michaelkellett

                                      Imed, you need to read up on digital signal processing.

                                       

                                      If you sample at 5MHz your filter needs to run at 5MHz.

                                       

                                      Why not set up your ATmega to control the Encode signal, use a timer and make something like 1000 conversions per second, then it will be easy to make a filter such as I describe.

                                       

                                      There is no point trying to implement a complex filter until you have  a better feel for what's going on.

                                       

                                      The two filters I described are FIR (the first one) and IIR (the second) and both are low pass.

                                       

                                      MK

                                      1 of 1 people found this helpful
                                    • Re: Implementing a digital Filter
                                      abrain

                                      Hi Michael, I stumbled across this old thread, and I have a feeling that 1.1kHz sampled at 2kHz looks more like 900Hz than 100Hz due the apparent frequencies involved? As you get closer to the sampling frequency, the signal actually appears as a lower and lower frequency, until the two are at the same frequency and the signal looks like dc again.

                                       

                                      You can do some neat tricks if you actually filter out below half the sampling frequency, and above the sampling frequency, as you can then sample in that middle range, but working out what on earth you’re looking at gets a little complicated and needs a clear head!

                                       

                                      A

                                  • Re: Implementing a digital Filter
                                    farcane

                                    Hello Michael,

                                     

                                    which ADC noise do you mean? because I solved the problem that I told you about before, and now I am getting a reasonble digital output range of 4 to 5 Bits difference only, so for example if I apply 2V to AIN of my ADC I get ( 0-3 ) digital output converted to ASCII, and if I apply 2.2V to AIN I get (40-44), for 2.5V I get (120- 125) knowing that my reference voltage is exactly 2.52V, so I think my ADC is not so noisy and that digital output bits difference is due to a small alternance of 10mV  in the AIN voltage because as you know this voltage is coming from an inverting amplifer output.

                                    The sampling frequency of my Analog signal is 5MHz, so what kind of digital Filter may I implement with an Atmega16  please ?

                                     

                                    Best regards

                                    Imed Oussalah

                                    1 of 1 people found this helpful
                            • Re: Implementing a digital Filter
                              vu2iti

                              I think the problem is not due to noise. So you cannot "filter out" the problem.  Please check up your routine to read/ configuration of  the ADC and the hardware circuit to interface the ADC to your Micro controller.

                               

                              -Mani

                              • Re: Implementing a digital Filter
                                clem57

                                Here is the crux of the matter. Processors like Atmel deal with ones and zeros while signals are analog in nature. An ADC will convert the approximate value of the signal at one point in time. But we know that signal may have bounce or noise if you will. If you want to you can give a range for 1-5 lets say and assume it is 3 in the middle. Of course 1,2,4,5 will never be yielded. This lets the digital world "filter" the real world.

                                Clem

                                • Re: Implementing a digital Filter
                                  aswinvenu

                                  Hi Imed,

                                   

                                  Can you tell me what is the sampling rate ?

                                   

                                  This is  a very normal issue we face while dealing with ADCs and Analog signals.

                                   

                                  The easiest way to get rid of this is to over-sample and decimate.

                                   

                                  For example if your sampling rate is 128 sps, sample the signal at 4x which is 512 and average out the 4 samples at a time. So effectively you will get 128 samples.

                                  Which set the sampling frequency at 512, buffer 4 samples and take an average of those four samples and store.

                                   

                                  Second way is to design a filter ( can be FIR (Finite impluse response ) or IIR  (Infinite impulse response)

                                   

                                  Since you are using Atmega chip I would prefer IIR because it doesn't require too much sample buffering.

                                   

                                  1) understand the noise characteristics ( I your case its high frequency compared to the information you want to extract)

                                  2) Decide the high frequency - above you need to clean the data (say cutoff frequency)

                                  3) Design a low pass filter ( using the cutoff frequency )

                                  4) Use python https://stackoverflow.com/questions/25191620/creating-lowpass-filter-in-scipy-understanding-methods-and-units

                                      to get the filter coefficients

                                  5) Implement it in C

                                   

                                  Please let me know if you need any further help.

                                   

                                  Aswin