title: Filter design and frequency extraction in Python
body: <p>I'm working on a project to find the instantaneous frequency of a multicomponent audio signal in Python. I am currently using a Butterworth bandpass filter combined with <code>scipy.signal.lfilter</code> to extract around my desired frequency region. I then use the analytic signal (from <code>scipy.signal.hilbert</code>) to get the instantaneous phase, which can be unwrapped to give frequency.</p>
  
  <p>As a relative novice to signal processing, I have two main questions:</p>
  
  <ol>
  <li><p>I have read that in many applications, it is preferable to use <code>scipy.signal.filtfilt</code> over <code>scipy.signal.lfilter</code>. Certainly when I apply <code>filtfilt</code> to my data, I get a significantly smoother looking instantaneous frequency signal. I would like to know the major differences between the two, bearing in mind that I would like to get an output which is as close to the "true" instantaneous frequency as possible.</p></li>
  <li><p>The instantaneous frequency data is nonstationary, which means that in some instances I must use a wider bandpass filter to capture all my desired data. This appears to introduce additional noise, and occassional instabilities, into my signal. Are there ways to deal with these kinds of problems, for example with a better designed filter?</p></li>
  </ol>
  
  <p><strong>EDIT</strong></p>
  
  <p>In response to flebool, below are some images of the data I am looking at. First, a comparison of <code>filt</code> and <code>filtfilt</code>:
  <img src="https://i.stack.imgur.com/tfFcQ.png" alt="Comparison of &lt;code&gt;filt&lt;/code&gt; and &lt;code&gt;filtfilt&lt;/code&gt;">
  Both the above signals have had the same Butterworth filter applied (although the filter function is different), followed by extraction of instantaneous frequency (which is what is plotted, as a function of time). <code>filtfilt</code> seems to shift and smooth the data. Is one of these signals a better approximation of the "true" signal?</p>
  
  <p>Note that this graph shows just a subset of a particular signal.</p>
  
  <p>Second, the effect of increasing the Butterworth filter size:
  <img src="https://i.stack.imgur.com/IRmQx.png" alt="Widening the filter">
  This is for the same subset of data as figure 1. The legend shows the lower and upper bound for the filter, respectively (the red trace is the <code>filt</code> version of the data in figure 1).</p>
  
  <p>Although it may not be clear here why I would use a larger pass band, in some instances the data may be located at various points between, say, 600 and 800Hz. It is here that I would require a broader filter design. You can see that additional noise enters the trace as the filter gets wider; I'd like to know if there's a way to optimise/improve my filter design.</p>