title: Moving Average for ADC
body: <p>Hi All i am working on a project where I have to calculate the moving average of ADC readings. The data coming out from ADC represent an Sinusoidal wave. </p>
  
  <p>This is the code I am using to get moving average of a given signal.</p>
  
  <pre><code>longNew = (8 bit data from ADC);
  longNew = longNew &lt;&lt; 8;
  //Division
  longNew = longNew &gt;&gt; 8;    //255 Samples
  longTemp = avgALong &gt;&gt; 8;
  
  avgALong -= longTemp;// Old data 
  avgALong += longNew;// New Data
  
  avgA = avgALong &gt;&gt; 8;//256 Point Average
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pumG2.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pumG2.jpg" alt="enter image description here"></a></p>
  
  <p>Reference Image</p>
  
  <p><a href="https://i.stack.imgur.com/d1Hld.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d1Hld.jpg" alt="Reference Image"></a></p>
  
  <p>Please refer this image for upper limit and lower limit relative to reference (or avgA)</p>
  
  <p>Currently I am using a constant value to obtain the upper limit and lower limit of voltage for my application
  which I am calculating as follows</p>
  
  <p><strong>upper_limit  =  avgA + Delta(x);
  lower_limit  =  avgA - Delta(x);</strong></p>
  
  <blockquote>
    <p>In my case I am taking Delta(x) = 15.</p>
  </blockquote>
  
  <p>I want to calculate this constant expression or Delta(x) based on signal strength. 
  The maximum voltage level of signal is 255 or 5Volt. 
  The minimum voltage level of signal varies frequently because of that a constant value is not useful for my application which determines the lower and upper limit. </p>
  
  <p>Please help </p>
  
  <p>Thank you  </p>