如何将过滤器应用于python中的信号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13740348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To apply a filter to a signal in python
提问by Navid Khairdast
is there any prepared function in python to apply a filter (for example Butterworth filter) to a given signal? I looking for such a function in 'scipy.signal' but I haven't find any useful functions more than filter design ones. actually I want this function to convolve a filter with the signal.
python中是否有任何准备好的函数可以将过滤器(例如巴特沃斯过滤器)应用于给定的信号?我在 'scipy.signal' 中寻找这样的函数,但我没有找到比过滤器设计更有用的函数。实际上我希望这个函数将滤波器与信号进行卷积。
采纳答案by cxrodgers
Yes! There are two:
是的!那里有两个:
scipy.signal.filtfilt
scipy.signal.lfilter
There are also methods for convolution (convolveand fftconvolve), but these are probably not appropriate for your application because it involves IIR filters.
也有卷积 (convolve和fftconvolve) 的方法,但这些方法可能不适合您的应用,因为它涉及 IIR 滤波器。
Full code sample:
完整代码示例:
b, a = scipy.signal.butter(N, Wn, 'low')
output_signal = scipy.signal.filtfilt(b, a, input_signal)
You can read more about the arguments and usage in the documentation. One gotcha is that Wnis a fraction of the Nyquist frequency (half the sampling frequency). So if the sampling rate is 1000Hz and you want a cutoff of 250Hz, you should use Wn=0.5.
您可以在文档中阅读有关参数和用法的更多信息。一个问题是这Wn是奈奎斯特频率的一小部分(采样频率的一半)。因此,如果采样率为 1000Hz 并且您希望截止频率为 250Hz,则应使用Wn=0.5.
By the way, I highly recommend the use of filtfiltover lfilter(which is called just filterin Matlab) for most applications. As the documentationstates:
顺便说一句,我强烈建议在大多数应用程序中使用filtfiltover lfilter(filter在 Matlab 中仅称为)。正如文档所述:
This function applies a linear filter twice, once forward and once backwards. The combined filter has linear phase.
此函数应用线性过滤器两次,一次向前,一次向后。组合滤波器具有线性相位。
What this means is that each value of the output is a function of both "past" and "future" points in the input equally. Therefore it will not lag the input.
这意味着输出的每个值都是输入中“过去”和“未来”点的函数。因此它不会滞后于输入。
In contrast, lfilteruses only "past" values of the input. This inevitably introduces a time lag, which will be frequency-dependent. There are of course a few applications for which this is desirable (notably real-time filtering), but most users are far better off with filtfilt.
相反,lfilter仅使用输入的“过去”值。这不可避免地引入了时间延迟,这将取决于频率。当然有一些应用程序需要这样做(特别是实时过滤),但大多数用户使用filtfilt.

