stats::filter 中的卷积是如何工作的

How does convolution work in stats::filter

我读了一些关于 stats:filter 的答案,他们已经部分清楚地说明了卷积的工作原理(与系数排列有关的部分),但还有其他内容。

示例:

# convolution with 3 coefficients in filter
filter(1:5,c(f1,f2,f3),method="convolution")
[1] NA  6  9 12 NA

#equivalent to:
 NA  * f3 + x[1] * f2 + x[2] * f1  #x[0] = doesn't exist/NA
x[1] * f3 + x[2] * f2 + x[3] * f1
x[2] * f3 + x[3] * f2 + x[4] * f1 
x[3] * f3 + x[4] * f2 + x[5] * f1 
x[4] * f3 + x[5] * f2 + x[6] * f1 #x[6] also doesn't exist

我不明白 X[0] 是从哪里来的;我以为我们会从 x[1] 开始。我怀疑这可能与 stats::filter.

的帮助文档中提到的偏移量有关

The convolution filter is

y[i] = f[1]*x[i+o] + … + f[p]*x[i+o-(p-1)]

where o is the offset: see sides for how it is determined.

sides: for convolution filters only. If sides = 1 the filter coefficients are for past values only; if sides = 2 they are centred around lag 0. In this case the length of the filter should be odd, but if it is even, more of the filter is forward in time than backward.

我仍然很困惑。我看不出各方如何解释偏移量,也不确定 x[0]' 是由于偏移量造成的。

卷积在 stats::filter 中的真正作用是什么?

也许你应该了解什么是卷积。从最简单的开始。即卷积两个信号。我只会描述细节。但是您可以在线搜索 WHY。

所以filter(1:5,c(f1,f2,f3),method="convolution"),使用默认的2边。那就是过滤器,过滤器将居中,如果不可能,则较大的一端将进入数据。计算从滤波器的中心与信号的第一个值重合的地方开始。回想一下,由于滤波器在信号上滚动,它必须小于或等于信号的长度。这是一个例子。

如果过滤器是 c(1,2,3),那么我们会将其居中于 2。

因此,为了将 c(1,2,3,4,5)c(1,2,3) 进行卷积,我们首先 reverse 过滤器然后 rollapply 它贯穿 signal:

step 1

    1  2  3  4  5
 3  2  1
  We see that 3 is not multiplied by anything! Thus `3*NA + 2*1+1*2 = NA`

We then move the filter one step and do the math again

     1  2  3  4  5
     3  2  1
   Here all the values are present: 3*1+2*2+1*3 = 10

The third step:

  1  2  3  4  5
     3  2  1
  3*2+2*3+1*4 = 16

我们重复此操作,直到中心值即 2 与最后一个值重合。

如果您选择 side = 1 则它不会居中,而是首先使过滤器的最后一个值与信号的第一个值重合,最后使过滤器的最后一个值与最后一个值重合信号。