title: How can I write the Matlab "filter"-function myself?
body: <p>I would like to use a Butterworth filter on a 1D-Signal. In Matlab the script would look like this:</p>
  
  <pre><code> f=100;
   f_cutoff = 20; 
   fnorm =f_cutoff/(f/2);
   [b,a] = butter(8,fnorm,'low');
   filteredData = filter(b,a,rawData); % I want to write this myself
  </code></pre>
  
  <p>Now I don't want to directly use the <em>filter</em>-function given in Matlab but write it myself.
  In the Matlab documentation it's described as follows:</p>
  
  <blockquote>
    <p>The filter function is implemented as a direct form II transposed structure,</p>
    
    <p>y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)
                        - a(2)*y(n-1) - ... - a(na+1)*y(n-na)</p>
    
    <p>where n-1 is the filter order, which handles both FIR and IIR filters [1], na is the feedback filter order, and nb is the feedforward filter order.</p>
  </blockquote>
  
  <p>So I've already tried to write the function like that:</p>
  
  <pre><code>f=100;
  f_cutoff = 20; 
  fnorm =f_cutoff/(f/2);
  [b,a] = butter(8,fnorm,'low');
  for n = 9:size(rawData,1)
      filteredData(n,1) = b(1)*n + b(2)*(n-1) + b(3)*(n-2) + b(4)*(n-3) + b(5)*(n-4) ...
                        - a(2)*rawData(n-1,1) - a(3)*rawData(n-2,1) - a(4)*rawData(n-3,1) - a(5)*accel(n-4,1);
  end
  </code></pre>
  
  <p>But that's not working. Can you please help me? What am I doing wrong?</p>
  
  <p>Sincerely,
  Cerdo</p>
  
  <p>PS: the filter documentation can be foud here: <a href="http://www.mathworks.de/de/help/matlab/ref/filter.html#f83-1015962" rel="nofollow">http://www.mathworks.de/de/help/matlab/ref/filter.html#f83-1015962</a> when expanding <em>More About -> Algorithms</em></p>