Why is there a difference in magnitude response between scipy.filtfilt and scipy.lfilter?

时间:2015-06-10 01:11:36

标签: python scipy signal-processing

I was trying to filter a signal using the scipy module of python and I wanted to see which of lfilter or filtfilt is better. I tried to compare them and I got the following plot from my mwe

import numpy as np
import scipy.signal as sp
import matplotlib.pyplot as plt

frequency = 100.             #cycles/second
samplingFrequency = 2500.         #samples/second
amplitude = 16384   
signalDuration = 2.3
cycles = frequency*signalDuration

time = np.linspace(0, 2*np.pi*cycles, signalDuration*samplingFrequency)
freq = np.fft.fftfreq(time.shape[-1])
inputSine = amplitude*np.sin(time)
#Create IIR Filter
b, a = sp.iirfilter(1, 0.3, btype = 'lowpass')

#Apply filter to input
filteredSignal = sp.filtfilt(b, a, inputSine)
filteredSignalInFrequency = np.fft.fft(filteredSignal)
filteredSignal2 = sp.lfilter(b, a, inputSine)
filteredSignal2InFrequency = np.fft.fft(filteredSignal2)

plt.close('all')
plt.figure(1)
plt.title('Sine filtered with filtfilt')
plt.plot(freq, abs(filteredSignalInFrequency))
plt.subplot(122)
plt.title('Sine filtered with lfilter')
plt.plot(freq, abs(filteredSignal2InFrequency))

print max(abs(filteredSignalInFrequency))
print max(abs(filteredSignal2InFrequency))
plt.show()

Can someone please explain why there is a difference in the magnitude response?

Thanks a lot for your help.lfilt vs filtfilt

1 个答案:

答案 0 :(得分:1)

查看图表显示,使用filtfilt过滤的信号在频域中的峰值幅度为4.43x10 7 ,而4.56x10 7 lfilter过滤的信号。换句话说,使用filtfilt过滤的信号的峰值幅度为0.97,当使用

进行过滤时

现在我们应该注意scipy.signal.filtfilt应用过滤器两次,而scipy.signal.lfilter仅应用一次。结果,输入信号衰减两倍。为了确认这一点,我们可以查看您使用的Butterworth滤波器的频率响应(使用iirfilter获得)围绕输入音调的归一化频率100/2500 = 0.04

enter image description here

确实表明这个滤波器的应用确实导致频率为0.04时衰减为0.97。