<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Moving Average for ADC in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857444#M16155</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;Sorry for not providing all the necessary information.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;I am using &lt;/SPAN&gt;&lt;STRONG style="border: 0px; font-weight: bold;"&gt;Mc9s12XEP100&lt;/STRONG&gt;&lt;SPAN&gt; microcontroller, and ADC is on chip, having 12 bit resolution. &amp;nbsp;I want to implement it in a embedded C code. So, how these concepts can be applied?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;Well thank you for your explaination.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;Madhura&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 13 Feb 2019 03:43:57 GMT</pubDate>
    <dc:creator>madhura_bhandig</dc:creator>
    <dc:date>2019-02-13T03:43:57Z</dc:date>
    <item>
      <title>Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857440#M16151</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am working on moving average code for ADC. I got 8 samples from ADC and as per my knowledge, while moving average: (1+2+.....8)/8 will be first average, (2+3+....9)/8 will be second ...&lt;/P&gt;&lt;P&gt;Is this a correct way of implementation?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;If anyone has a sample code, then please provide.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Madhura&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Feb 2019 08:12:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857440#M16151</guid>
      <dc:creator>madhura_bhandig</dc:creator>
      <dc:date>2019-02-12T08:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857441#M16152</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Madhura, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you please specify the product that you are using (NXP part number)?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Paulina&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Feb 2019 23:00:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857441#M16152</guid>
      <dc:creator>lisettelozano</dc:creator>
      <dc:date>2019-02-12T23:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857442#M16153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Madhura,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I know of two algorithms that might satisfy your requirements, although I do not know what you need to accomplish. Without knowing the processor you wish to use, I can't provide code. The implementations could vary widely - I've implemented the second algorithm one way in an 8-bit MCU, and in a very different way in a DSP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The first algorithm is what you describe - It's called a Boxcar filter. It is a special case of a FIR filter (finite-impulse response filter), where the coefficients are all the same. It will produce a true average over the most recent N samples. It is computationally inexpensive: For memory, it just uses a table for the most recent N samples and the running average, and the math requires only an add and a subtract and maybe a shift.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second is called an exponential filter, and is a type of IIR filter (infinite-impulse response filter). It is different than a true average in that the most recent value has the most weight in the final result, the second most recent has less weight, and on down. It's response is identical to an electrical RC filter. For memory, it only needs the new sample and the previous output. It can be implemented as two multiplies and an add, or it can be implemented as a subtract, a shift and an add.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you describe the problem better, we may be able to help you better.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Feb 2019 23:33:25 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857442#M16153</guid>
      <dc:creator>rocco</dc:creator>
      <dc:date>2019-02-12T23:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857443#M16154</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;Sorry for not providing all the necessary information.&lt;/P&gt;&lt;P&gt;I am using &lt;STRONG&gt;Mc9s12XEP100&lt;/STRONG&gt; microcontroller, and ADC is on chip, having 12 bit resolution.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Madhura&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Feb 2019 03:34:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857443#M16154</guid>
      <dc:creator>madhura_bhandig</dc:creator>
      <dc:date>2019-02-13T03:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857444#M16155</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;Sorry for not providing all the necessary information.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;I am using &lt;/SPAN&gt;&lt;STRONG style="border: 0px; font-weight: bold;"&gt;Mc9s12XEP100&lt;/STRONG&gt;&lt;SPAN&gt; microcontroller, and ADC is on chip, having 12 bit resolution. &amp;nbsp;I want to implement it in a embedded C code. So, how these concepts can be applied?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;Well thank you for your explaination.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff; font-size: 14px;"&gt;&lt;SPAN&gt;Madhura&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Feb 2019 03:43:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857444#M16155</guid>
      <dc:creator>madhura_bhandig</dc:creator>
      <dc:date>2019-02-13T03:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857445#M16156</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Madhura,&lt;/P&gt;&lt;P&gt;Since I don't know the purpose of your filter, I can't suggest which one would be better. If you are using it for a control application, the exponential filter would likely be better (faster step-response and smoother transitions). If you are using it for numerical analysis, the boxcar filter may be better. If you need the filter for decimation, both would work, but I would lean toward the boxcar.&lt;/P&gt;&lt;P&gt;All of my code is written in assembler, for the DSP563xx of for the HC08/S08. But since the exponential filter is so simple to implement in C, here is some code (not verified for syntax):&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;PRE style="padding-left: 30px;"&gt;static float coefficient;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// 0 through 1 for how much filtering to do

/*
&amp;nbsp;&amp;nbsp; &amp;nbsp;This function implements an exponential filter
&amp;nbsp;&amp;nbsp; &amp;nbsp;on a sample, returning the filtered value.
*/
float filter( float sample )
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;static float result;
&amp;nbsp;&amp;nbsp; &amp;nbsp;
&amp;nbsp;&amp;nbsp; &amp;nbsp;result = coefficient*sample + (1-coefficient)*result
&amp;nbsp;&amp;nbsp; &amp;nbsp;return result;
}

/*
&amp;nbsp;&amp;nbsp; &amp;nbsp;This function sets the strength of the filter, and
&amp;nbsp;&amp;nbsp; &amp;nbsp;has a valid range of zero to one. A one will not filter
&amp;nbsp;&amp;nbsp; &amp;nbsp;at all, returning the sample unmodified, and a zero
&amp;nbsp;&amp;nbsp; &amp;nbsp;will freeze&amp;nbsp;&amp;nbsp; &amp;nbsp;the result at its current value.
&amp;nbsp;&amp;nbsp; &amp;nbsp;If the new coefficient is outside of the valid range,
&amp;nbsp;&amp;nbsp; &amp;nbsp;then it will remain unmodified.
*/
void setCoefficient ( float newCoefficient )
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;if ( newCoefficient &amp;gt;= 0&amp;nbsp; ) &amp;amp;&amp;amp; ( newCoefficient &amp;lt;= 1&amp;nbsp; )
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;coefficient = newCoefficient;
}&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you would like the filter to respond similarly to an average of eight samples, then you would want to set the coefficient to one-eighth (1/8, 0r 0.125). With the exponential filter, the filter coefficient can be changed on-the-fly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you need a true average, then that code would need a static array of the most recent samples, operated as a ring-buffer. You would also need the ring-buffer index to be static. Rather than adding all samples and the dividing by the number of samples, you should keep a static copy of the first result, and then simply subtract-out the oldest sample and add-in the newest sample with each new sample.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope that helps.&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Feb 2019 21:33:44 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857445#M16156</guid>
      <dc:creator>rocco</dc:creator>
      <dc:date>2019-02-14T21:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857446#M16157</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey Mark,&lt;/P&gt;&lt;P&gt;Yes, I am using it for numerical analysis. So, from the methods you have suggested will opt for Boxcar filter.&lt;/P&gt;&lt;P&gt;Well, Thank you so much for such a detailed answer.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Madhura&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Feb 2019 04:51:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857446#M16157</guid>
      <dc:creator>madhura_bhandig</dc:creator>
      <dc:date>2019-02-15T04:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: Moving Average for ADC</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857447#M16158</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;really fast idea to make moving average filter, probably, I hope, I am not wrong....not tested....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;unsigned int new_conversion_processing(unsigned int new_data)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; static unsigned int data[8] = { 0,0,0,0,0,0,0,0 }; // data set&lt;BR /&gt;&amp;nbsp; static unsigned char p_oldiest_data = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "pointer" to data to be changed&lt;BR /&gt;&amp;nbsp; static unsigned int&amp;nbsp; sum;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // sum of the data set&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; sum = sum-data[p_oldiest_data] + new_data;&amp;nbsp; // calculate a new sum by removing the oldest data from it&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // and add the newest one&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; data[p_oldiest_data] = new_data;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // replace the oldest value by the the newest one in the buffer&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; p_oldiest_data = (p_oldiest_data==7) ? 0 :(p_oldiest_data++);&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; return(sum&amp;gt;&amp;gt;3);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // return average value (=sum/8)&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Process flow description:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="pastedImage_3.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/73184i3C512AB89AB1E7FF/image-size/large?v=v2&amp;amp;px=999" role="button" title="pastedImage_3.png" alt="pastedImage_3.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For such calculations, it is good to be XGATE used to store data in continuous background process and you then only share the result to CPU for next processing. Of course, this is a little bit different story.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Ladislav&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2019 08:27:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Moving-Average-for-ADC/m-p/857447#M16158</guid>
      <dc:creator>lama</dc:creator>
      <dc:date>2019-02-19T08:27:41Z</dc:date>
    </item>
  </channel>
</rss>

