过滤功能的简单例子,具体递归选项 [英] simple examples of filter function, recursive option specifically
问题描述
中的
过滤器
函数寻找一些简单的(即 - 没有数学符号,长形式可重复的代码)的例子我想我有我的头绕卷积方法,但我坚持推广递归选项。我已经阅读并与各种文档作斗争,但对我的帮助只是有点不透明。 以下是我到目前为止所了解的例子:
#为滤波器组件设置一些值
f1 < - 1; f2 <-1; f3 <-1;
然后我们去:
<$ p $基本卷积滤波器
滤波器(1:5,f1,method =convolution)
[1] 1 2 3 4 5
#eqivalent to:
x [1] * f1
x [2] * f1
x [3] * f1
x [4] * f1
x [5] * f1
#滤波器中的2个系数的卷积
filter(1:5,c(f1,f2),method =convolution)
[1] 3 5 7 9 NA
#eqivalent to:
x [1] * f2 + x [2] * f1
x [2] * f2 + x [3] * f1
x [3] * f2 + x [4] * f1
x [4] * f2 + x [5] * f1
x [5] * f2 + x [6] * f1
# (1:5,c(f1,f2,f3),method =convolution)
[1] NA 6 9 12 NA
#等同于:
NA * f3 + x [1] * f2 + x [2] * f1 #x [0] =不存在/ 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
现在当我受伤的时候我可怜的小脑干。
我设法弄明白这个职位信息最基本的例子: https://stackoverflow.com/a/11552765 / 496803
$ b
过滤器(1:5,f1,method =recursive)
[1 ] 1 3 6 10 15
#eqivalent to:
x [1]
x [2] + f1 * x [1]
x [3] + f1 * x [2] + f1 ^ 2 * x [1]
x [4] + f1 * x [3] + f1 ^ 2 * x [2] + f1 ^ 3 * x [1]
x [5] + f1 * x [4] + f1 ^ 2 * x [3] + f1 ^ 3 * x [2] + f1 ^ 4 * x [1]
有人可以提供类似的代码,以上的递归版本的卷积实例具有 filter = c(f1,f2 )
和 filter = c(f1,f2,f3)?
(1:5,c(f1,f2),method =recursive)。
[1] 1 3 7 14 26
filter(1:5,c(f1,f2,f3),method =recursive)
[1] 1 3 7 15 30
编辑
使用@ agstudy的整齐答案:
>过滤器(1:5,f1,method =递归)
时间序列:
开始= 1
结束= 5
频率= 1
[1] 1 3 6 10 15
> y1 < - x [1]
> y2 < - x [2] + f1 * y1
> y3 < - x [3] + f1 * y2
> y4 < - x [4] + f1 * y3
> y5 < - x [5] + f1 * y4
> c(y1,y2,y3,y4,y5)
[1] 1 3 6 10 15
和...
> filter(1:5,c(f1,f2),method =recursive)
时间序列:
开始= 1
结束= 5
频率= 1
[1] 1 3 7 14 26
> y1 < - x [1]
> y2 < - x [2] + f1 * y1
> y3 < - x [3] + f1 * y2 + f2 * y1
> y4 < - x [4] + f1 * y3 + f2 * y2
> y5 < - x [5] + f1 * y4 + f2 * y3
> c(y1,y2,y3,y4,y5)
[1] 1 3 7 14 26
和...
> filter(1:5,c(f1,f2,f3),method =recursive)
时间序列:
开始= 1
结束= 5
频率= 1
[1] 1 3 7 15 30
> y1 < - x [1]
> y2 < - x [2] + f1 * y1
> y3 < - x [3] + f1 * y2 + f2 * y1
> y4 < - x [4] + f1 * y3 + f2 * y2 + f3 * y1
> y5 < - x [5] + f1 * y4 + f2 * y3 + f3 * y2
> c(y1,y2,y3,y4,y5)
[1] 1 3 7 15 30
在递归的情况下,我认为不需要用xi来扩展表达式。
递归的关键是用前面的y来表示右手表达式。
我更喜欢根据过滤器大小来思考。
filter size = 1
y1 < - x1
y2 < - x2 + f1 * y1
y3 < - x3 + f1 * y2
y4 < - x4 + f1 * y3
y5 < - x5 + f1 * y4
filter size = 2
y1 < - x1
/ pre>
y2 < - x2 + f1 * y1
y3 < - x3 + f1 * y2 + f2 * y1#将过滤器应用于过去值并添加当前输入
y4 < - x4 + f1 * y3 + f2 * y2
y5 < - x5 + f1 * y4 + f2 * y3
I am seeking some simple (i.e. - no maths notation, long-form reproducible code) examples for the
filter
function in R I think I have my head around the convolution method, but am stuck at generalising the recursive option. I have read and battled with various documentation, but the help is just a bit opaque to me.Here are the examples I have figured out so far:
# Set some values for filter components f1 <- 1; f2 <- 1; f3 <- 1;
And on we go:
# basic convolution filter filter(1:5,f1,method="convolution") [1] 1 2 3 4 5 #equivalent to: x[1] * f1 x[2] * f1 x[3] * f1 x[4] * f1 x[5] * f1 # convolution with 2 coefficients in filter filter(1:5,c(f1,f2),method="convolution") [1] 3 5 7 9 NA #equivalent to: x[1] * f2 + x[2] * f1 x[2] * f2 + x[3] * f1 x[3] * f2 + x[4] * f1 x[4] * f2 + x[5] * f1 x[5] * f2 + x[6] * f1 # 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
Now's when I am hurting my poor little brain stem. I managed to figure out the most basic example using info at this post: https://stackoverflow.com/a/11552765/496803
filter(1:5, f1, method="recursive") [1] 1 3 6 10 15 #equivalent to: x[1] x[2] + f1*x[1] x[3] + f1*x[2] + f1^2*x[1] x[4] + f1*x[3] + f1^2*x[2] + f1^3*x[1] x[5] + f1*x[4] + f1^2*x[3] + f1^3*x[2] + f1^4*x[1]
Can someone provide similar code to what I have above for the convolution examples for the recursive version with
filter = c(f1,f2)
andfilter = c(f1,f2,f3)
?Answers should match the results from the function:
filter(1:5, c(f1,f2), method="recursive") [1] 1 3 7 14 26 filter(1:5, c(f1,f2,f3), method="recursive") [1] 1 3 7 15 30
EDIT
To finalise using @agstudy's neat answer:
> filter(1:5, f1, method="recursive") Time Series: Start = 1 End = 5 Frequency = 1 [1] 1 3 6 10 15 > y1 <- x[1] > y2 <- x[2] + f1*y1 > y3 <- x[3] + f1*y2 > y4 <- x[4] + f1*y3 > y5 <- x[5] + f1*y4 > c(y1,y2,y3,y4,y5) [1] 1 3 6 10 15
and...
> filter(1:5, c(f1,f2), method="recursive") Time Series: Start = 1 End = 5 Frequency = 1 [1] 1 3 7 14 26 > y1 <- x[1] > y2 <- x[2] + f1*y1 > y3 <- x[3] + f1*y2 + f2*y1 > y4 <- x[4] + f1*y3 + f2*y2 > y5 <- x[5] + f1*y4 + f2*y3 > c(y1,y2,y3,y4,y5) [1] 1 3 7 14 26
and...
> filter(1:5, c(f1,f2,f3), method="recursive") Time Series: Start = 1 End = 5 Frequency = 1 [1] 1 3 7 15 30 > y1 <- x[1] > y2 <- x[2] + f1*y1 > y3 <- x[3] + f1*y2 + f2*y1 > y4 <- x[4] + f1*y3 + f2*y2 + f3*y1 > y5 <- x[5] + f1*y4 + f2*y3 + f3*y2 > c(y1,y2,y3,y4,y5) [1] 1 3 7 15 30
解决方案In the recursive case, I think no need to expand the expression in terms of xi. The key with "recursive" is to express the right hand expression in terms of previous y's.
I prefer thinking in terms of filter size.
filter size =1
y1 <- x1 y2 <- x2 + f1*y1 y3 <- x3 + f1*y2 y4 <- x4 + f1*y3 y5 <- x5 + f1*y4
filter size = 2
y1 <- x1 y2 <- x2 + f1*y1 y3 <- x3 + f1*y2 + f2*y1 # apply the filter for the past value and add current input y4 <- x4 + f1*y3 + f2*y2 y5 <- x5 + f1*y4 + f2*y3
这篇关于过滤功能的简单例子,具体递归选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!