# Help designing Butterworth filter

I am trying to bandpass filter a signal using a butterworth filter, but I am getting the following result (click to enlarge):

The original signal is the blue and the filtered one is the green.

Why does the first part of the filtered signal (from 0 to 50) is changing so much from the original? Can you help me understand why this is happening?

Here goes some details:

• Signal length: 256
• Frequency: 22Hz
• Cutoff frequencies: 0.7Hz and 3.0Hz
• Filter order: 6

Python code:

nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
y = lfilter(b, a, data)

• If you let us know your filter coefficients it's easier to see what's actually going on. Is your sampling frequency 22Hz? – Matt L. Apr 17 '13 at 13:58
• Yes, Sample frequency: 22Hz. The filter coefficients are: b = [3.9e-04, 0.0, -2.3e-03, 1.49e-18, 5.8e-03, -7.48e-19, -7.8e-03, 7.48159970e-19, 5.8e-03, -1.49e-18, -2.3e-03, 0.0, 3.9e-04] and a = [1.0e+00, -8.7e+00, 3.5e+01, -8.8e+01, 1.5e+02, -1.9e+02, 1.79e+02, -1.25e+02, 6.54e+01, -2.47e+01, 6.4e+00, -1.03e+00, 7.8e-02] – Ricardo Belchior Apr 18 '13 at 10:24

## 2 Answers

The first part of the response may be a result of the transient response of the Butterworth filter. Also note that the signal is phase shifted and the basic shape is altered. This looks like the result of phase distortion. You may want to try again using a Bessel filter which has better transient and phase characteristics. If you are using DSP, you can also try a true linear phase FIR approach. This will yield the best transient and phase characteristics.

• As BZ says, the initial transient is due to the filter start-up. You can change this by setting the initial conditions. Use lfiltic to set the initial conditions. – Peter K. Apr 17 '13 at 16:43
• Thanks for your answer. The Bessel slightly decreased that first part of the signal, although not enough (just 1,2 values in the Y axis). The FIR filter was however what I was looking for. Here's a nice article on that: mbed.org/cookbook/FIR-Filter – Ricardo Belchior Apr 18 '13 at 10:28
• @PeterK. I tried the lfiltic although it only increased the initial part of the signal, which is the opposite of what I needed! I tried: zi = lfiltic(b, a, data[0:order-1]) Do you recommend something else? – Ricardo Belchior Apr 18 '13 at 10:37
• Just using some of the other parameters on lfiltic, the required output (perhaps all zeros) as well as the input. The way you've called it would try to set the output of the filter to be the same as the input, which is not what you want. – Peter K. Apr 18 '13 at 11:28
• Hopefully someone can write an example for lfiltic in the Scipy documentation. – LWZ Apr 17 '14 at 19:05

Just in case you wonder why the filtered signal is shifted down: by bandpass filtering you remove the (relatively strong) DC component of the signal. So the filter signal has a DC value of 0.

• umm i had no clue on why that was happening so thanks. would give it an upvote if I could – Ricardo Belchior Apr 18 '13 at 10:40
• vote up requires 15 reputation – Ricardo Belchior Apr 18 '13 at 10:56
• This is not an answer as it's completely irrelevant to the question, but it gets my upvote. – LWZ Apr 16 '14 at 21:50