2
$\begingroup$

Does the following code properly implement a phase-neutral band-pass filter using SciPy?

freq_fraction = FREQ / (SAMPLE_RATE/2)
sharpness = 0.1
filter_length=128
fir = sig.firwin(filter_length, [freq_fraction*(1-sharpness),freq_fraction*(1+sharpness)], pass_zero=False )
filtered = sig.lfilter( fir, 1, data_to_filter )
filtered = sig.lfilter( fir, 1,filtered[::-1] )[::-1]

The code is based on the answer to:

Is it correct to subtract a low-pass filtered signal from the original signal and use the result as a "high-pass"?

which doesn't give a final code listing for how to maintain phase neutrality. I'm assuming it's just a matter of running the filter forward and backwards but want to double-check.

Also, if there are better/different ways of doing this (other than the remez and firwin2 methods listed in the other question) I'd love to know.

$\endgroup$
  • $\begingroup$ What does "phase-neutral" mean? $\endgroup$ – endolith Jul 22 '13 at 16:41
  • 1
    $\begingroup$ @endolith: I assume he's looking for zero-phase filtering. The code above looks to implement forward-backward filtering, similar to MATLAB's filtfilt function. $\endgroup$ – Jason R Jul 22 '13 at 17:07
  • 1
    $\begingroup$ By "phase-neutral" I mean not affecting the phase of the signal. In other words, key points in the signal should not be moved forward or backward in time. $\endgroup$ – Dan Sandberg Jul 22 '13 at 20:42
  • $\begingroup$ I this question answered? If so, you may accept the answer to close the issue and show other's "this is solved". $\endgroup$ – ppasler Feb 20 '17 at 12:59
  • $\begingroup$ Doesn't seem answered -- no one ever said if the code will do the right thing or not. $\endgroup$ – Dan Sandberg Feb 20 '17 at 17:09
1
$\begingroup$

How about scipy.signal.filtfilt and scipy.signal.butter filter? See the example at the bottom of the documentation.

Here's another example with and without phase neutrality: Applying filter in scipy.signal: Use lfilter or filtfilt?

$\endgroup$
-1
$\begingroup$

Calculate the frequency response (https://en.wikipedia.org/wiki/Frequency_response) of the filter and see yourself.

$\endgroup$
  • 2
    $\begingroup$ While I tend to agree that people should try and do as much as they can on their own, generally they are here asking questions because they are struggling with some part of the process. Therefore simply telling them to 'see for themselves' doesn't really add much as an answer. $\endgroup$ – Sam Maloney Jul 22 '13 at 18:34
  • $\begingroup$ It's a reasonable suggestion. I've run the code already and it seems to work but wanted to hear if there were any flaws with the approach or better ways of doing it. $\endgroup$ – Dan Sandberg Jul 22 '13 at 20:44
  • $\begingroup$ Out of curiosity, how would the frequency response graph look if there is an induced phase offset vs. not? $\endgroup$ – Dan Sandberg Jul 22 '13 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.