I have a signal and want to bandpass-filter it:
def butter_bandpass_prep(lowcut, highcut, fs, order=5):
"""Butterworth bandpass auxilliary function."""
nyq = 0.5 * fs # Minimal frequency (nyquist criterion)
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass(src, lowcut, highcut, fs, order=5):
"""Butterworth bandpass filter."""
b, a = butter_bandpass_prep(lowcut, highcut, fs, order=order)
dst = lfilter(b, a, src)
return dst
However, my signal does not start at zero which seems to cause problems as the filtered signal is shifted in x-direction. How can I compensate for this? Or maybe the butterworth filter is not the filter of choice?!
You can use filtfilt
, which will filter the signal twice, once forwards and once backwards. This will eliminate all phase shifts, but double the stopband attenuation:
dst = filtfilt(b, a, src)