title: DSP - Group Delay of an IIR filter
body: <p>I need to write a program in order to filter a signal for 0.8-3 Hz. Even though I have a working FIR filter this one takes too long and I've decided to change to an IIR filter. I've designed one myself using fdatool in Matlab and I've got the NUM and DEN. The improvement in time would be quite good (the FIR was 125 taps and this one's order is 12).
  The next step was to move to the C implementation and I've found this nice website <a href="http://iowahills.com/Example%20Code/IIRNthOrderImplementation.txt" rel="nofollow noreferrer">http://iowahills.com/Example%20Code/IIRNthOrderImplementation.txt</a> .</p>
  
  <p>The problem is that in their code there is a parameter I just don't understand and that is NumSigPts. </p>
  
  <pre><code>void RunIIRPoly( double *Signal, double *FilteredSignal, int NumSigPts)
  {
   int j, k, N;
   double y, Reg[100];
  
   for(j=0; j&lt;100; j++)Reg[j] = 0.0; // Init the delay registers.
  
   for(j=0; j&lt;NumSigPts; j++)
   {
    // Shift the delay register values.
    for(k=N; k&gt;0; k--)Reg[k] = Reg[k-1];
  
    // The denominator
    Reg[0] = Signal[j];
    for(k=1; k&lt;=N; k++)Reg[0] -= DenomCoeff[k] * Reg[k];
  
    // The numerator
    y = 0;
    for(k=0; k&lt;=N; k++)y += NumCoeff[k] * Reg[k];
    FilteredSignal[j] = y;
   }
  
  }
  </code></pre>
  
  <p>In the description they say </p>
  
  <blockquote>
    <p>This particular filter has a nominal group delay of 4 so we set
    NumSigPts to at least 1000 + 2*4</p>
  </blockquote>
  
  <p>How can I find the group delay of my filter. Does it have anything to do with the filter's order?
  The signal I filter is continuously provided so my exact question would be what is the minimum size of the signal in order to begin filtering?</p>
  
  <p>Later edit:</p>
  
  <p>So today I had some attempts with this IIR filter, but still haven't managed to get some good results.
  I took Nate's advice and tried Matlab's grpdelay function. The thing is I'm not quite sure how interpret the output.
  <img src="https://i.imgur.com/TVzECmO.png?1" alt="grpdelay"></p>
  
  <p>What I'm trying to do is to filter some images,frames, pixel by pixel. The way I'm doing this is to store the images in a array of images which is all_frames. To access each pixel I call all_frames[frame_number][pixel_number].</p>
  
  <p>The code I came up with following the website mention above is:</p>
  
  <pre><code>void ApplyIIR ( float **all_frames, float *num, float *den, int frame_number,  float *filter_Xs, int w, int h)
  {   
          float Reg[FILTER_ORDER];
  
      for (int i=0; i&lt; (width*height) ; i++) {  //go pixel by pixel
  
          for(int j=0; j&lt;FILTER_ORDER; j++) //init regs
                  Reg[j] = 0.0;
          float final_X=0;
  
              for(int l=0; l&lt; FILTER_ORDER+ DELAY ; l++) {  // not sure how to set DELAY
  
                      for(int k=FILTER_ORDER-1; k&gt;0;k--)
                          Reg[k] = Reg[k-1];
  
                      Reg[0] = all_frames[frame_number][i]; //get pixels one by one
                      for(int k=1; k&lt;FILTER_ORDER;k++)
                          Reg[0] -= den[k]* Reg[k];
  
  
                      for(int k=0;k&lt;FILTER_ORDER;k++)
                          final_X += num[k] * Reg[k];
  
                      if(frame_number == 0)             //go through all the frames
                          frame_number = FILTER_ORDER - 1;
                      else
                          frame_number--;
  
                  }
                      filter_Xs[i] = final_X;         
      }
  
  }
  </code></pre>
  
  <p>FILTER_ORDER is set to 13, since num and den have 0-12 values.</p>
  
  <p>Am I on a completely wrong path?</p>