title: bandpass FIR filter
body: <p>I need to make a simple bandpass audio filter. 
  Now I've used this simple C++ class: <a href="http://www.cardinalpeak.com/blog/a-c-class-to-implement-low-pass-high-pass-and-band-pass-filters" rel="nofollow">http://www.cardinalpeak.com/blog/a-c-class-to-implement-low-pass-high-pass-and-band-pass-filters</a></p>
  
  <p>It works well and cut off the desired bands. But when I try to change upper or lower limit with small steps, on some values of limit I hear the wrong result - attenuated or shifted in frequency (not corresponding to current limits) sound.</p>
  
  <p>Function for calculating impulse response:</p>
  
  <pre><code>void Filter::designBPF()
  {
      int n;
      float mm;
  
      for(n = 0; n &lt; m_num_taps; n++){
          mm = n - (m_num_taps - 1.0) / 2.0;
          if( mm == 0.0 ) m_taps[n] = (m_phi - m_lambda) / M_PI;
          else m_taps[n] = (   sin( mm * m_phi ) -
                               sin( mm * m_lambda )   ) / (mm * M_PI);
      }
  
      return;
  }
  </code></pre>
  
  <p>where </p>
  
  <pre><code>m_lambda = M_PI * Fl / (Fs/2);
  m_phi = M_PI * Fu / (Fs/2);
  </code></pre>
  
  <p>Fs - sample rate (44.100)
  Fl - lower limit
  Fu - upper limit</p>
  
  <p>And simple filtering function:</p>
  
  <pre><code>float Filter::do_sample(float data_sample)
  {
      int i;
      float result;
  
      if( m_error_flag != 0 ) return(0);
  
      for(i = m_num_taps - 1; i &gt;= 1; i--){
          m_sr[i] = m_sr[i-1];
      }   
      m_sr[0] = data_sample;
  
      result = 0;
      for(i = 0; i &lt; m_num_taps; i++) result += m_sr[i] * m_taps[i];
  
      return result;
  }
  </code></pre>
  
  <p>Do I need to use any window function (Blackman, etc.)? If yes, how do I do this?
  I have tried to multiply my impulse response to Blackman window:</p>
  
  <pre><code>m_taps[n] *= 0.42 - 0.5 * cos(2.0 * M_PI * n / double(N - 1)) +
                  0.08 * cos(4.0 * M_PI * n / double(N - 1));
  </code></pre>
  
  <p>but the result was wrong.
  And do I need to normalize taps?</p>