Matlab中滤波器和卷积有什么区别?
I am trying to calculate the output of a LTI system. I came across two different Matlab functions that are supposed to be appropriate for the job: filter
and conv
. What is the difference between the two of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
filter
可以处理 FIR 和 IIR 系统,而conv
接受两个输入并返回它们的卷积。因此,conv(h,x)
和filter(h,1,x)
会给出相同的结果。 filter中的1表示滤波器的递归系数就是[1]
。但如果您有 IIR 滤波器,则无法使用conv
。filter
还可以返回过滤器状态,以便可以在后续调用中使用它,而不会引起过滤器瞬态。请参阅转换和filter 文档了解详细信息。
filter
can handle FIR and IIR systems, whileconv
takes two inputs and returns their convolution. Soconv(h,x)
andfilter(h,1,x)
would give the same result. The 1 in filter indicates that the recursive coefficients of the filter are just[1]
. But if you have an IIR filter, you can't useconv
.filter
can also return the filter states, so that it can be used in subsequent calls without incurring filter transients.See the conv and filter documentation for details.
conv(x,b)
执行完整的卷积。结果的长度为length(x)+ length(b)-1
。filter(b,[1],x)
给出与x
相同长度的输出。它不会刷新滤波器的延迟线。假设 x 是行向量。使
x0 = [x zeros(1,length(b)-1)]
;现在filter(b,[1],x0)
与conv(x,b)
相同。这是因为额外的 0 用于刷新延迟线。哪一种更合理呢?这取决于你需要什么!
conv(x,b)
performs the complete convolution. The length of the result islength(x)+ length(b)-1
.filter(b,[1],x)
gives an output of the same length thanx
. It doesn’t flush the delay line of the filter.Assume
x
is a row vector. Makex0 = [x zeros(1,length(b)-1)]
; nowfilter(b,[1],x0)
is the same asconv(x,b)
. This is because the additional 0’s are used to flush the delay line.Which one is more reasonable? It depends of what you need!
一个相关的答案是Python中的情况。如上所述,对于 FIR 滤波器,函数 scipy.signal.lfilter 和 numpy.convolve 对边界效应执行相同的操作。
假设 len(x) >长度(h)。当使用 numpy.convolve(h,x,mode='same') 时,我们会得到一个 len(x) 向量,但对称地用零填充。
然而,当使用`scipy.signal.lfilter时,零填充是不对称的,而是单边的!
人们可以检查是否
给出所有“True”。
这个想法是,模式满填充两侧都最大程度地填充零,给出大小为 len(x) + len(h) - 1 的向量(请参阅 Numpy 文档),你需要做的是最后修剪掉多余的元素。
A related answer is the situation in Python. As mentioned above, for FIR filters the functions
scipy.signal.lfilter
andnumpy.convolve
do the same operation up to boundary effects.Assume
len(x) > len(h)
. When usingnumpy.convolve(h,x,mode='same')
one gets a vector oflen(x)
but padded with zeros symmetrically.However, when using `scipy.signal.lfilter the zero padding is not symmetrical but one-sided!
One can check that
gives all "True".
The idea is that the mode full pads maximally with zeros on both sides, giving a vector of size
len(x) + len(h) - 1
(see Numpy documentation) and what you need to do is trim the redundant elements in the end.如果应用过滤器,结果将与应用的过滤器具有相同的尺寸。在应用卷积时,会通过应用滤波器来减少输入的维度。
If filter is applied, result would have same dimensions with applied filter. While applying convolution, would decrease the dimensions of input with filter applied.