Thursday, March 1, 2018

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


I am having a hard time trying to find documentation to implement band-pass or high-pass filter with python/scipy/numpy.


I can easily create and apply a low-pass filter, though, so I ask:



Would it be conceptually correct to low-pass-filter a signal, then subtract the result from the original signal, in order to get just the high-frequencies?


Also, if anyone has a simple example of a naive bandpass filter in Python (preferrably using the numpy and scipy libraries), I'd be very thankful.


What I look for is something like:


filtered_signal = band_pass(original_signal, rate, low=20, high=500)

Thanks for any help!


EDIT: with scipy, I'm using this as low-pass, with good results:


import numpy, scipy.signal

def firfilt(interval, freq, sampling_rate):

nfreq = freq/(0.5*sampling_rate)
taps = sampling_rate + 1
a = 1
b = scipy.signal.firwin(taps, cutoff=nfreq)
firstpass = scipy.signal.lfilter(b, a, interval)
## second pass to compensate phase delay
secondpass = scipy.signal.lfilter(b, a, firstpass[::-1])[::-1]
return secondpass

Answer



In theory you can do this, but in practice it is difficult to do because the time and phase alignment must be pretty good for it to work. If the alignment is good you will get the destructive interference that you are seeking. If they aren't, you will get some constructive interference. Even worse, whether they are destructively or constructively interfering will depend on the frequency- i.e. you can get both constructive and destructive interference at the same time. It can work, though, if you are only filtering out fairly low frequencies, since their timing requirements are the loosest because they change so slowly.



Short story- it's possible to do but is difficult enough that it generally makes sense to just do a high-pass filter.


A relatively straightforward way to create a bandpass filter is to create a low-pass filter, and then modulate it to the center frequency you want by multiplying it with a sinusoid of that frequency.


No comments:

Post a Comment

digital communications - Understanding the Matched Filter

I have a question about matched filtering. Does the matched filter maximise the SNR at the moment of decision only? As far as I understand, ...