2013-01-17 27 views
18

我正在尋求一些簡單的(即 - 沒有數學符號,長形式可重複的代碼)filter函數的例子R 我想我有我的頭圍繞卷積方法,但我堅持推廣遞歸選項。我已閱讀並與各種文檔作鬥爭,但對我的幫助只是有點不透明。簡單的例子,篩選功能,具體的遞歸選項

下面是到目前爲止,我已經找到了例子:

# 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 

當我傷害我的可憐的小腦幹現在是。 我設法找出使用信息在這個職位的最基本的例子: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] 

有人可以給上面我所爲遞歸版本filter = c(f1,f2)filter = c(f1,f2,f3)卷積例子提供類似的代碼?

答案應與從功能結果:

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的簡潔答案:

> 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 

an d ...

> 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 
+2

認爲'filter'是跨越你的原始矢量,在每一步應用權重和求和。遞歸濾波器就像卷積濾波器一樣,除了權重f1,...,fn自動變爲c(1,f1,...,fn),並且在每個步驟1被應用於當前值,而f1, ...,fn應用於正在創建的新校正矢量的最後n個值,而不是原始值。使用卷積(默認邊數= 2),權重跨越當前值,其中一邊是下一個n/2原始值,另一邊是前n/2個原始值。 –

回答

14

在遞歸的情況下,我認爲不需要用xi來擴展表達式。 「遞歸」的關鍵是用前面的y來表達右手錶達式。

我更喜歡用濾鏡尺寸來思考。

濾波器的尺寸= 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 
+2

+1這是迄今爲止最清晰的例子。 –

+0

說得好:帶遞歸的鍵(與「卷積」相反)是用y來表示RHS。 –

+0

感謝您的回答。你不知道你做出了一個看似複雜的概念有多簡單! – thelatemail

2

下面是我發現最有用的形象化什麼遞歸濾波是真正做例子:

(x <- rep(1, 10)) 
# [1] 1 1 1 1 1 1 1 1 1 1 

as.vector(filter(x, c(1), method="recursive")) ## Equivalent to cumsum() 
# [1] 1 2 3 4 5 6 7 8 9 10 
as.vector(filter(x, c(0,1), method="recursive")) 
# [1] 1 1 2 2 3 3 4 4 5 5 
as.vector(filter(x, c(0,0,1), method="recursive")) 
# [1] 1 1 1 2 2 2 3 3 3 4 
as.vector(filter(x, c(0,0,0,1), method="recursive")) 
# [1] 1 1 1 1 2 2 2 2 3 3 
as.vector(filter(x, c(0,0,0,0,1), method="recursive")) 
# [1] 1 1 1 1 1 2 2 2 2 2 
2

遞歸,你的「過濾器」的順序是添加劑先前的總和或輸出值的係數。用filter=c(1,1)表示「在我的序列x中取第i個組分,並將其添加到前一步結果的1倍和前一步結果的1倍」。下面是幾個例子來說明

我覺得滯後效應符號看起來像這樣:

## only one filter, so autoregressive cumsum only looks "one sequence behind" 
> filter(1:5, c(2), method='recursive') 
Time Series: 
Start = 1 
End = 5 
Frequency = 1 
[1] 1 4 11 26 57 

1 = 1 
2*1 + 2 = 4 
2*(2*1 + 2) + 3 = 11 
... 

## filter with lag in it, looks two sequences back 
> filter(1:5, c(0, 2), method='recursive') 
Time Series: 
Start = 1 
End = 5 
Frequency = 1 
[1] 1 2 5 8 15 

1= 1 
0*1 + 2 = 2 
2*1 + 0*(0*1 + 2) + 3 = 5 
2*(0*1 + 2) + 0 * (2*1 + 0*(0*1 + 2) + 3) + 4 = 8 
2*(2*1 + 0*(0*1 + 2) + 3) + 0*(2*(0*1 + 2) + 0 * (2*1 + 0*(0*1 + 2) + 3) + 4) + 5 = 15 

你看到有累計模式?換句話說。

1 = 1 
0*1 + 2 = 2 
2*1 + 0*2 + 3 = 5 
2*2 + 0*5 + 4 = 8 
2*5 + 0*8 + 5 = 15 
0

我在閱讀本花費一個小時以下是我的總結,通過比較用Matlab

NOTATION :命令在Matlab = R中的命令

filter([1,1,1], 1, data) = filter(data, [1,1,1], method = "convolution") ; but the difference is that the first 2 elements are NA 


filter(1, [1,-1,-1,-1], data) = filter(data, [1,1,1], method = "recursive") 

如果你知道一些來自DSP的話,那麼遞歸用於IIR,卷積是用於FIR的