title: Signal smoothing algorithm (Matlab's moving average)
body: <p>I have written a simple code that performs a 3-point moving average smoothing algorithm. It is meant to follow the same basic algorithm as Matlab's smooth(...) function as described <a href="https://www.mathworks.com/help/curvefit/smoothing-data.html#bq_6ys3-1" rel="nofollow noreferrer">here</a>.</p>
  
  <p>However, the result of my code is very different from that of Matlab's. Matlab's 3-point filter appears to perform a much more aggressive smoothing.</p>
  
  <p>Here is a comparison of a noisy data smoothed using my code (red) and Matlab's function (blue):</p>
  
  <p><img src="https://i.imgur.com/L1chqVO.png" alt="Here is a comparison:"></p>
  
  <p>Here is my code written in the form of a function:</p>
  
  <pre><code>function [NewSignal] = smoothing(signal)
  NewSignal = signal;
  for i = 2 : length(signal)-1
      NewSignal(i,:) = (NewSignal(i,:)+NewSignal(i-1,:)+NewSignal(i+1,:))./3;
  end
  end
  </code></pre>
  
  <p>Matlab's function is used as follows:</p>
  
  <p><code>signal = smooth(time, signal, 3, 'moving');</code></p>
  
  <p>As far as I understand Matlab's function works the same way; it averages 3 adjacent bins to a single bin. So I expected both algorithms to produce the same results.</p>
  
  <p>So, what is the reason for the discrepancy? And how could I tweak my code to produce the same results?</p>
  
  <p>Edit:</p>
  
  <p>My sample data can be found <a href="http://www.filedropper.com/ds0009" rel="nofollow noreferrer">here</a>. It can be accessed using:</p>
  
  <pre><code>M = csvread('DS0009.csv');
  time = M(:,1);
  signal = M(:,2);
  </code></pre>
  
  <p>Here is the new result (red plot) using rinkert's correction:</p>
  
  <p><a href="https://i.stack.imgur.com/kUpjY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kUpjY.png" alt="enter image description here"></a></p>