过滤函数的简单例子,特别是递归选项

编程入门 行业动态 更新时间:2024-10-28 07:32:07
本文介绍了过滤函数的简单例子,特别是递归选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为 R 中的 filter 函数寻找一些简单的(即 - 没有数学符号,长格式的可重复代码)示例我想我对卷积方法有所了解,但我坚持概括递归选项.我已经阅读并处理了各种文档,但这些帮助对我来说有点不透明.

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;

然后我们继续:

# 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

现在我正在伤害我可怜的小脑干.我设法在这篇文章中使用信息找出了最基本的例子:stackoverflow/a/11552765/496803

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: stackoverflow/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]

有人可以为递归版本的卷积示例提供类似于我上面的代码,filter = c(f1,f2) 和 filter = c(f1,f2,f3)?

Can someone provide similar code to what I have above for the convolution examples for the recursive version with filter = c(f1,f2) and filter = 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

编辑

最后使用@agstudy 的简洁回答:

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

还有……

> 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

还有……

> 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

推荐答案

在递归的情况下,我认为没有必要在 xi 方面展开表达式.递归"的关键是用前面的y来表达右手表达式.

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.

过滤器尺寸 =1

y1 <- x1 y2 <- x2 + f1*y1 y3 <- x3 + f1*y2 y4 <- x4 + f1*y3 y5 <- x5 + f1*y4

过滤器大小 = 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

更多推荐

过滤函数的简单例子,特别是递归选项

本文发布于:2023-10-07 17:01:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   函数   选项   例子   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!