title: Design of a Notch filter in Python
body: <p>I'm trying to design an IIR Notch filter in python using numpy array and the scipy librairy to remove a sine tone from a imported wave file (I'm using the wave module to do so). My file was generated by Adobe audition : it is a pure sine @ 1.2 kHZ, sampled @  48, 96 or 192 kHz, in order to have a "pseudo-periodic" data for my circular fft (just ask if I'm not clear enough)</p>
  
  <p>Here is the code I used to implement the coefficient of my filter (I get the coefficient from the article <a href="http://www.atlantis-press.com/php/download_paper.php?id=5887" rel="nofollow noreferrer">"Second-order IIR Notch Filter Design and implementation of digital signal 
  processing system" by C. M. Wang &amp; W. C. Xiao</a>)</p>
  
  <pre><code>f_cut = 1200.0
  wn = f_cut/rate    
  r = 0.99
  B, A = np.zeros(3), np.zeros(3)
  A[0],A[1],A[2] = 1.0, -2.0*r*np.cos(2*np.pi*wn), r*r
  B[0],B[1],B[2] = 1.0, -2.0*np.cos(2*np.pi*wn), 1.0
  filtered = signal.lfilter(B, A, data_flt_R, axis=0)
  </code></pre>
  
  <p>Where <code>data_flt_R</code> is a numpy array containing my right channel in a float64 type, and <code>rate</code> is my sampling frequency. I plot the frequency response and the fft of my data using the matplotlib module to see if everything is ok. </p>
  
  <pre><code>N = len(data_flt_R)    
  
  w, h = signal.freqz(B,A, N)
  pyplot.subplot(2,1,1)
  pyplot.semilogx(w*rate/(2*np.pi), 20*np.log10(np.absolute(h)))    
  
  fft1 = fftpack.fft(data_flt_R, N)
  fft_abs1 = np.absolute(fft1)
  ref = np.nanmax(fft_abs1)
  dB_unfiltered = 20*np.log10(fft_abs1/ref)
  
  fft2 = fftpack.fft(filtered, N)
  fft_abs2 = np.absolute(fft2)
  dB_filtered = 20*np.log10(fft_abs2/ref)
  
  abs = fftpack.fftfreq(N,1.0/rate)
  
  pyplot.subplot(2,1,2)
  pyplot.semilogx(abs,dB_unfiltered,'r', label='unfiltered')
  pyplot.semilogx(abs,dB_filtered,'b', label='filtered')
  pyplot.grid(True)
  pyplot.legend()
  pyplot.ylabel('power spectrum (in dB)')
  pyplot.xlim(10,rate/2)
  pyplot.xlabel('frequencies (in Hz)')
  </code></pre>
  
  <p>And here is what I get : </p>
  
  <p><img src="https://i.stack.imgur.com/wYfD6.png" alt="enter image description here"></p>
  
  <p>I don't understand the results and values I get before and after my fc. Shouldn't I get a plot which looks like the red one but whithout the main peak ? Why do I have a slope in HF? Is this linked with windowing?</p>
  
  <p>Moreover, the result changes if I change my sampling frequency and/or the data length (16/24 or 32 bits). Can anyone enlighten me?</p>