title: How should I deal with NaN being returned by freqz in R?
body: <p>I'm working on an optimization method to design digital filters and using <code>freqz</code>, from the <code>signal</code> package, to evaluate the candidates. However, I've been getting <code>NaN's</code> on the response given by <code>freqz</code>. This is a big problem to me because I need to calculate the mean squared error of this response.</p>
  
  <p>The filter bellow is an example of this. The very first point on the response is a <code>NaN</code>.</p>
  
  <p><strong>R Code:</strong></p>
  
  <pre><code>b &lt;- c(-100.00000, 62.92546, -73.64395, -82.57045, 35.11388, -100.00000)
  a &lt;- c(100, 100, 100, -100, -100, -100)
  r &lt;- freqz(b,a)
  r$h[1]
  </code></pre>
  
  <p>Running the equivalent code in Matlab I get <code>-Inf</code>, though. Which is just fine to my purpose.</p>
  
  <p><strong>Matlab Code:</strong></p>
  
  <pre><code>b = [-100.00000, 62.92546, -73.64395, -82.57045, 35.11388, -100.00000]
  a = [100, 100, 100, -100, -100, -100]
  [h, ~] = freqz(b,a)
  h(1)
  </code></pre>
  
  <p>My understanding is that, since <code>freqz</code> does its calculations based on the formula bellow, what must be happening is that we have a <em>value close to zero on the denominator</em> (note that the coefficients in <code>a</code> added together are <em>equal to zero</em>) but instead of <code>+/-Inf</code> (as Matlab does) it is mistakenly returning <code>NaN</code>.</p>
  
  <p><img src="https://latex.codecogs.com/gif.latex?H(z)=%5Cdfrac%7B%5Csum_%7Bk=0%7D%5E%7BM%7Db_kz%5E%7B-k%7D%7D%7B%5Csum_%7Bk=0%7D%5E%7BN%7Da_kz%5E%7B-k%7D%7D" title="H(z)=\dfrac{\sum_{k=0}^{M}b_kz^{-k}}{\sum_{k=0}^{N}a_kz^{-k}}" /></p>
  
  <p>The bottom line is that I think it should give the same response as Matlab.</p>
  
  <hr>
  
  <h2>Work Around (with buts)</h2>
  
  <p>So far, this is what I've done as a work around:</p>
  
  <pre><code>r &lt;- freqz(b,a)
  if(anyNA(hw$h)){
    r$h[is.nan(r$h)] &lt;- Inf
  }
  </code></pre>
  
  <p>It does solve my problem but I wonder if there's a better way of doing so or if I should consider it a bug and report it, I'd be happy to help.
  Maybe it has even been solved already (in case it is an actual bug).</p>
  
  <hr>
  
  <h2>Additional Information</h2>
  
  <p>In case it is relevant here's my dev environment information:</p>
  
  <pre><code>platform       x86_64-apple-darwin13.4.0   
  arch           x86_64                      
  os             darwin13.4.0                
  system         x86_64, darwin13.4.0        
  status                                     
  major          3                           
  minor          2.3                         
  year           2015                        
  month          12                          
  day            10                          
  svn rev        69752                       
  language       R                           
  version.string R version 3.2.3 (2015-12-10)
  </code></pre>