0

I'm using this filter in python:

def bandpass_firwin(ntaps, lowcut, highcut, fs, window='hamming'):
    nyq = 0.5 * fs
    taps = firwin(ntaps, [lowcut, highcut], nyq=nyq, pass_zero=False,
                  window=window, scale=False)

where my ntaps=128; lowcut = 0.7 ; highcut = 4 ; fs = 61

I filter my signal which has 610 samples sampled at 61 Hz (so it is 10 sec long).

When I look at the spectrum of the signal which has been filtered by this bandpass filter, I see this:

enter image description here

The peek in this spectrum is at 0.61 Hz. Which is not in the range of 0.7 to 4 Hz.

How is this possible ? & How can I prevent it ?

Olivier_s_j
  • 5,490
  • 24
  • 80
  • 126
  • I can't reproduce your results. I get a very nice bandpass filter using your code. How are you generating that plot? – Henry Gomersall May 03 '13 at 09:21
  • Oh, it's your filtered data. – Henry Gomersall May 03 '13 at 09:23
  • yes it is already convolved. taps_hamming = bandpass_firwin(ntaps, 0.7, 4, fs=fs) Ynew3 = np.convolve(Ynew2, taps_hamming, "same") – Olivier_s_j May 03 '13 at 09:25
  • What does the spectrum look like *before* filtering? If you look at the graph in my answer to your other question (http://stackoverflow.com/questions/16301569/bandpass-filter-in-python/16306891#16306891), you'll see that the gain at 0.61 Hz is approximately 0.4. – Warren Weckesser May 03 '13 at 10:10
  • Perhaps with an image twice as long, you'll get twice as many answers. – tiago May 03 '13 at 14:42

1 Answers1

0

Your filter isn't magic - there are intrinsic limitations on the bandwidth. Try using more taps if you really need the tight cutoff.

The more taps you use though, the more you need to think about edge effects and how you handle them (as the edge assumptions encroach further and further into the data block). Perhaps you want a smooth roll off at the edge? Or a mirror and repeat of the data? Perhaps you can ignore it entirely...

Another technique to convolution is to filter directly in the frequency domain by simply multiplying by the desired spectrum. This imposes the edge assumption that your signal is repeated, though you can change this by extending your signal as you see fit. If you want to know the support of the equivalent FIR filter, take the IFFT of the window and you can see how much the beginning of the time block will smear into the beginning.

Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
  • I don't think any edge effect is important for me. I just need the peek frequency between 0.7 Hz and 4 Hz. – Olivier_s_j May 03 '13 at 09:47
  • Is the peak frequency your ultimate goal? Do you also need the filtered signal? If you don't really need the filtered signal, you could find the peak between 0.7 Hz and 4 Hz of the spectrum of the unfiltered signal. – Warren Weckesser May 03 '13 at 10:23
  • @Ojtwist so I've given you a solution... You *should* be aware of the impact of edge effects as they can be quite pronounced. Also, I echo Warren's comment. – Henry Gomersall May 03 '13 at 11:09